/// <summary> /// triangulate polygon using ThirdParty.P2T library /// http://code.google.com/p/poly2tri/ /// </summary> /// <returns>index list of triangle points</returns> public List <int> Triangulate() { var p2tPoints = new List <PrimitivesPro.ThirdParty.P2T.PolygonPoint>(Points.Length); foreach (var point in Points) { p2tPoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(point.x, point.y)); } // create p2t polygon var p2tPolygon = new PrimitivesPro.ThirdParty.P2T.Polygon(p2tPoints); // add holes foreach (var polygonHole in holes) { var p2tHolePoints = new List <PrimitivesPro.ThirdParty.P2T.PolygonPoint>(polygonHole.Points.Length); foreach (var polygonPoint in polygonHole.Points) { p2tHolePoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(polygonPoint.x, polygonPoint.y)); } p2tPolygon.AddHole(new PrimitivesPro.ThirdParty.P2T.Polygon(p2tHolePoints)); } PrimitivesPro.ThirdParty.P2T.P2T.Triangulate(p2tPolygon); var triangles = p2tPolygon.Triangles.Count; var indices = new List <int>(triangles * 3); Points = new Vector2[triangles * 3]; var j = 0; for (var i = 0; i < triangles; i++) { indices.Add((j + 0)); indices.Add((j + 1)); indices.Add((j + 2)); Points[j + 2].x = (float)p2tPolygon.Triangles[i].Points._0.X; Points[j + 2].y = (float)p2tPolygon.Triangles[i].Points._0.Y; Points[j + 1].x = (float)p2tPolygon.Triangles[i].Points._1.X; Points[j + 1].y = (float)p2tPolygon.Triangles[i].Points._1.Y; Points[j + 0].x = (float)p2tPolygon.Triangles[i].Points._2.X; Points[j + 0].y = (float)p2tPolygon.Triangles[i].Points._2.Y; j += 3; } return(indices); }
/// <summary> /// triangulate polygon using ThirdParty.P2T library /// http://code.google.com/p/poly2tri/ /// </summary> /// <returns>index list of triangle points</returns> public List<int> Triangulate() { var p2tPoints = new List<PrimitivesPro.ThirdParty.P2T.PolygonPoint>(Points.Length); foreach (var point in Points) { p2tPoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(point.x, point.y)); } // create p2t polygon var p2tPolygon = new PrimitivesPro.ThirdParty.P2T.Polygon(p2tPoints); // add holes foreach (var polygonHole in holes) { var p2tHolePoints = new List<PrimitivesPro.ThirdParty.P2T.PolygonPoint>(polygonHole.Points.Length); foreach (var polygonPoint in polygonHole.Points) { p2tHolePoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(polygonPoint.x, polygonPoint.y)); } p2tPolygon.AddHole(new PrimitivesPro.ThirdParty.P2T.Polygon(p2tHolePoints)); } PrimitivesPro.ThirdParty.P2T.P2T.Triangulate(p2tPolygon); var triangles = p2tPolygon.Triangles.Count; var indices = new List<int>(triangles*3); Points = new Vector2[triangles*3]; var j = 0; for (int i = 0; i < triangles; i++) { indices.Add((j + 0)); indices.Add((j + 1)); indices.Add((j + 2)); Points[j + 2].x = (float)p2tPolygon.Triangles[i].Points._0.X; Points[j + 2].y = (float)p2tPolygon.Triangles[i].Points._0.Y; Points[j + 1].x = (float)p2tPolygon.Triangles[i].Points._1.X; Points[j + 1].y = (float)p2tPolygon.Triangles[i].Points._1.Y; Points[j + 0].x = (float)p2tPolygon.Triangles[i].Points._2.X; Points[j + 0].y = (float)p2tPolygon.Triangles[i].Points._2.Y; j += 3; } return indices; }
/// <summary> /// triangulate polygon, algorithm from wiki is fast enough /// http://wiki.unity3d.com/index.php?title=Triangulator /// </summary> /// <returns></returns> public bool Triangulate(Array <int> indicesArray) { // no holes supported if (holes.Count == 0) { indicesArray.Initialize(Points.Length * 3); int n = Points.Length; if (n < 3) { return(true); } var V = new int[n]; if (Area > 0) { for (int v = 0; v < n; v++) { V[v] = v; } } else { for (int v = 0; v < n; v++) { V[v] = (n - 1) - v; } } int nv = n; int count = 2 * nv; for (int m = 0, v = nv - 1; nv > 2;) { if ((count--) <= 0) { return(true); } int u = v; if (nv <= u) { u = 0; } v = u + 1; if (nv <= v) { v = 0; } int w = v + 1; if (nv <= w) { w = 0; } if (Snip(u, v, w, nv, V)) { int a, b, c, s, t; a = V[u]; b = V[v]; c = V[w]; indicesArray.Add(a); indicesArray.Add(b); indicesArray.Add(c); m++; for (s = v, t = v + 1; t < nv; s++, t++) { V[s] = V[t]; } nv--; count = 2 * nv; } } indicesArray.Reverse(); return(true); } else { // use poly2tri library to triangulate mesh with holes var p2tPoints = new List <PrimitivesPro.ThirdParty.P2T.PolygonPoint>(Points.Length); foreach (var point in Points) { p2tPoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(point.x, point.y)); } // create p2t polygon var p2tPolygon = new PrimitivesPro.ThirdParty.P2T.Polygon(p2tPoints); // add holes foreach (var polygonHole in holes) { var p2tHolePoints = new List <PrimitivesPro.ThirdParty.P2T.PolygonPoint>(polygonHole.Points.Length); foreach (var polygonPoint in polygonHole.Points) { p2tHolePoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(polygonPoint.x, polygonPoint.y)); } p2tPolygon.AddHole(new PrimitivesPro.ThirdParty.P2T.Polygon(p2tHolePoints)); } try { PrimitivesPro.ThirdParty.P2T.P2T.Triangulate(p2tPolygon); } catch (Exception ex) { ExploderUtils.Log("P2T Exception: " + ex); return(false); } var triangles = p2tPolygon.Triangles.Count; indicesArray.Initialize(triangles * 3); Points = new Vector2[triangles * 3]; var j = 0; // recalc min max Min.x = float.MaxValue; Min.y = float.MaxValue; Max.x = float.MinValue; Max.y = float.MinValue; for (int i = 0; i < triangles; i++) { indicesArray.Add((j + 0)); indicesArray.Add((j + 1)); indicesArray.Add((j + 2)); Points[j + 2].x = (float)p2tPolygon.Triangles[i].Points._0.X; Points[j + 2].y = (float)p2tPolygon.Triangles[i].Points._0.Y; Points[j + 1].x = (float)p2tPolygon.Triangles[i].Points._1.X; Points[j + 1].y = (float)p2tPolygon.Triangles[i].Points._1.Y; Points[j + 0].x = (float)p2tPolygon.Triangles[i].Points._2.X; Points[j + 0].y = (float)p2tPolygon.Triangles[i].Points._2.Y; // recalc min max for (int k = 0; k < 3; k++) { if (Points[j + k].x < Min.x) { Min.x = Points[j + k].x; } if (Points[j + k].y < Min.y) { Min.y = Points[j + k].y; } if (Points[j + k].x > Max.x) { Max.x = Points[j + k].x; } if (Points[j + k].y > Max.y) { Max.y = Points[j + k].y; } } j += 3; } return(true); } }
/// <summary> /// triangulate polygon, algorithm from wiki is fast enough /// http://wiki.unity3d.com/index.php?title=Triangulator /// </summary> /// <returns></returns> public List<int> Triangulate() { // no holes supported if (holes.Count == 0) { var indices = new List<int>(Points.Length); int n = Points.Length; if (n < 3) return indices; var V = new int[n]; if (Area > 0) { for (int v = 0; v < n; v++) V[v] = v; } else { for (int v = 0; v < n; v++) V[v] = (n - 1) - v; } int nv = n; int count = 2*nv; for (int m = 0, v = nv - 1; nv > 2;) { if ((count--) <= 0) return indices; int u = v; if (nv <= u) u = 0; v = u + 1; if (nv <= v) v = 0; int w = v + 1; if (nv <= w) w = 0; if (Snip(u, v, w, nv, V)) { int a, b, c, s, t; a = V[u]; b = V[v]; c = V[w]; indices.Add(a); indices.Add(b); indices.Add(c); m++; for (s = v, t = v + 1; t < nv; s++, t++) V[s] = V[t]; nv--; count = 2*nv; } } indices.Reverse(); return indices; } else { // use poly2tri library to triangulate mesh with holes var p2tPoints = new List<PrimitivesPro.ThirdParty.P2T.PolygonPoint>(Points.Length); foreach (var point in Points) { p2tPoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(point.x, point.y)); } // create p2t polygon var p2tPolygon = new PrimitivesPro.ThirdParty.P2T.Polygon(p2tPoints); // add holes foreach (var polygonHole in holes) { var p2tHolePoints = new List<PrimitivesPro.ThirdParty.P2T.PolygonPoint>(polygonHole.Points.Length); foreach (var polygonPoint in polygonHole.Points) { p2tHolePoints.Add(new PrimitivesPro.ThirdParty.P2T.PolygonPoint(polygonPoint.x, polygonPoint.y)); } p2tPolygon.AddHole(new PrimitivesPro.ThirdParty.P2T.Polygon(p2tHolePoints)); } try { PrimitivesPro.ThirdParty.P2T.P2T.Triangulate(p2tPolygon); } catch (Exception ex) { ExploderUtils.Log("P2T Exception: " + ex); return null; } var triangles = p2tPolygon.Triangles.Count; var indices = new List<int>(triangles*3); Points = new Vector2[triangles*3]; var j = 0; // recalc min max Min.x = float.MaxValue; Min.y = float.MaxValue; Max.x = float.MinValue; Max.y = float.MinValue; for (int i = 0; i < triangles; i++) { indices.Add((j + 0)); indices.Add((j + 1)); indices.Add((j + 2)); Points[j + 2].x = (float)p2tPolygon.Triangles[i].Points._0.X; Points[j + 2].y = (float)p2tPolygon.Triangles[i].Points._0.Y; Points[j + 1].x = (float)p2tPolygon.Triangles[i].Points._1.X; Points[j + 1].y = (float)p2tPolygon.Triangles[i].Points._1.Y; Points[j + 0].x = (float)p2tPolygon.Triangles[i].Points._2.X; Points[j + 0].y = (float)p2tPolygon.Triangles[i].Points._2.Y; // recalc min max for (int k = 0; k < 3; k++) { if (Points[j + k].x < Min.x) { Min.x = Points[j + k].x; } if (Points[j + k].y < Min.y) { Min.y = Points[j + k].y; } if (Points[j + k].x > Max.x) { Max.x = Points[j + k].x; } if (Points[j + k].y > Max.y) { Max.y = Points[j + k].y; } } j += 3; } return indices; } }