/// <summary> /// Finally, build the triangles /// </summary> /// <param name="collector">the triangle collector</param> public void BuildTriangles(ITriangleCollector collector) { var splits = ScanSplitByTrapezoidation.BuildSplits(this.polygon); var polygonWithMonotones = Polygon.Split(this.polygon, splits, collector); foreach (var subPolygonId in polygonWithMonotones.SubPolygonIds) { var triangluator = new MonotonePolygonTriangulator(polygonWithMonotones, subPolygonId); triangluator.Build(collector); } }
/// <summary> /// Constructor /// </summary> /// <param name="polygon">the original polygon</param> /// <param name="splits">tuples of vertex ids, where to split</param> public PolygonSplitter(Polygon polygon, IEnumerable <Tuple <int, int> > splits, ITriangleCollector triangleCollector) { this.allSplits = splits.ToArray(); this.originalPolygon = polygon; this.polygonStartIndices = new List <int>(polygon.polygonStartIndices); this.chainFreeIndex = polygon.chain.Length; this.chain = new VertexChain[this.chainFreeIndex + this.allSplits.Length * 2]; Array.Copy(polygon.chain, this.chain, this.chainFreeIndex); this.triangleCollector = triangleCollector; }
/// <summary> /// traverse the polygon and add triangles to the collector /// </summary> /// <param name="collector">collector for resulting triangles</param> public void Build(ITriangleCollector collector) { var start = this.FindStartOfMonotonePolygon(); if (start >= 0) { this.TriangulateMonotonePolygon(start, collector); } else { var triangleVertices = this.polygon.SubPolygonVertices(this.subPolygonId).ToArray(); collector.AddTriangle(triangleVertices[0], triangleVertices[1], triangleVertices[2]); } }
/// <summary> /// Create triangles for a monotone polygon /// </summary> /// <param name="polygon">the monotone polygon</param> /// <param name="startPoint">the first point (clockwise) of the long edge.</param> /// <param name="result">the collector for resulting triangles</param> private void TriangulateMonotonePolygon(int startPoint, ITriangleCollector result) { this.PullFirstTriangle(startPoint); while (true) { if (this.IsConvexCorner()) { result.AddTriangle(this.current, this.third, this.second); if (!this.PopOrPullNextVertex()) { return; } } else { this.PushAndPullNextVertex(); } } }