简体   繁体   English

将边缘集合转换为三角形集合的算法

[英]Algorithm to convert a collection of edges into collection of triangles

I have implemented a polygon triangulation algorithm into my program. 我已经在程序中实现了多边形三角剖分算法。 The algorithm first takes a simple polygon , described as 2D points/vertices, and splits it into y-monotone pieces . 该算法首先获取一个简单的多边形 (描述为2D点/顶点),然后将其分割为y个单调的片段 After this is done, the algorithm then takes each monotone polygon piece and splits it into triangular pieces. 完成此操作后,该算法将获取每个单调多边形片段并将其拆分为三角形片段。

The input to the algorithm I need help with is an array of vertices outlining a y-monotonic polygon in clockwise or counter-clockwise order. 我需要帮助的算法的输入是一组顶点,这些顶点按顺时针或逆时针顺序概述了y单调多边形。 The output is a collection of all edges, both from the original polygon but also the new ones added by the triangulation algorithm in order to split this y-monotonic piece into triangular pieces. 输出是所有边的集合,这些边既来自原始多边形,也来自三角剖分算法添加的新边,以将y单调块分割成三角形块。 This works great if I only want to paint the result as it's possible to just draw each edge. 如果我只想绘制结果,则效果很好,因为可以只绘制每个边缘。

However, there is no particular order to this collection of edges, and I need the output to be an array of type triangle strip or an array where each triangle is simply described by it's 3 vertices (Example: [a1,a2,a3,b1,b2,b3] as we have triangle a and triangle b ). 但是,此边缘集合没有特定的顺序,我需要输出为三角形带状数组每个三角形均由其3个顶点简单描述的数组(示例: [a1,a2,a3,b1 ,b2,b3],因为我们有三角形a和三角形b )。

Is there any conventional algorithm or any other way which can help me solve this? 是否有任何常规算法或其他方法可以帮助我解决此问题? Speed is not of extreme importance but I'd prefer a fast solution. 速度不是很重要,但我希望有一个快速的解决方案。

Here is a pseudocode example of the program used in order to give a better understanding of my question: 这是为了更好地理解我的问题而使用的程序的伪代码示例:

class Vertex {
 var point;
 var index;
 init(x,y,index){
   point = (x,y)
   self.index = index
 }
}

class Edge {
 var from; // of type Vertex
 var to; // of type Vertex
 init(from, to){
  self.from = from
  self.to = to
 }
}

A call to a function getMonotonePiecesOfPolygon(polygon) could generate one of many monotonic polygon pieces that would have these vertices and edges: 调用函数getMonotonePiecesOfPolygon(polygon)可能会生成许多具有这些顶点和边的单调多边形块之一:

var vertex0 = Vertex(x0,y0,0)
var vertex1 = Vertex(x1,y1,1)
var vertex2 = Vertex(x2,y2,2)
var vertex3 = Vertex(x3,y3,3)

var edge0 = Edge(vertex0, vertex1)
var edge1 = Edge(vertex1, vertex2)
var edge2 = Edge(vertex2, vertex3)
var edge3 = Edge(vertex3, vertex4)

This could correspond to a rectangle like this: 这可能对应于这样的矩形:

         edge0
       0------1
       |      |
 edge3 |      | edge1
       |      |
       3------2 
         edge2

Then, we can supply these edges in an array and divide the polygon into triangles with the a triangulation method 'makeMonotonePolygonIntoTrianglePieces()' 然后,我们可以在数组中提供这些边缘,并使用三角测量方法“ makeMonotonePolygonIntoTrianglePieces()”将多边形划分为三角形。

var edgesArray = [edge0,edge1,edge2,edge3]  
var edgesArray = makePolygonIntoTrianglePieces(edgesArray)

Now the edgesArray could look like this: 现在, edgesArray可能如下所示:

edgesArray == [edge0,edge1,edge2,edge3,newEdge1]

where 哪里

newEdge1.from == vertex0
newEdge1.to == vertex2

which would correspond to a polygon like this if we were to draw each edge: 如果我们要绘制每条边,则对应于这样的多边形:

         edge0
       0------1
       |\     |
 edge3 |  \   | edge1
       |    \ |
       3------2 
         edge2

Now here is the part of what my question is seeking to solve. 现在这是我的问题要解决的部分。 I need an array which would look like this: 我需要一个看起来像这样的数组:

var triangles = [
                 vertex0,vertex3,vertex2, 
                 vertex0,vertex2,vertex1
                ]

or using triangle strips (each triangle described by one new vertex and two vertices from the previous triangle): 或使用三角形条纹(每个三角形由一个新顶点和前一个三角形的两个顶点描述):

var triangles = [
                 vertex3, vertex2, vertex0,
                 vertex1
                ]  

In Chapter 2 of the book you refer to in one of the comments, they present a data structure called Doubly-Connected Edge List (DCEL). 在本书的第2章中,您在其中的一条注释中引用了它们,它们提出了一种称为双连接边缘列表(DCEL)的数据结构。 DCEL:s are used to store planar subdivisions such as triangulations. DCEL:s用于存储平面细分,例如三角剖分。 From what you write, I think it could be what you are looking for. 从您写的内容来看,我认为这可能是您想要的。

https://en.wikipedia.org/wiki/Doubly_connected_edge_list https://en.wikipedia.org/wiki/Doubly_connected_edge_list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM