private static Boolean PointsOnLine(Polygon pointList, Vector3 Normal) { foreach (var connectionEdge in pointList.GetPolygonCirculator()) { if (GetOrientation(connectionEdge.Prev.Origin, connectionEdge.Origin, connectionEdge.Next.Origin, Normal) != 0) { return(false); } } return(true); }
private static List <ConnectionEdge> FindNonConvexPoints(Polygon p, Vector3 Normal) { List <ConnectionEdge> resultList = new List <ConnectionEdge>(); foreach (var connectionEdge in p.GetPolygonCirculator()) { if (GetOrientation(connectionEdge.Prev.Origin, connectionEdge.Origin, connectionEdge.Next.Origin, Normal) != 1) { resultList.Add(connectionEdge); } } return(resultList); }
public static List <Vector3> Triangulate(List <Vector3> points, Vector3 Normal) { if (points == null || points.Count < 3) { throw new ArgumentException("No list or an empty list passed"); } var incidentEdges = new Dictionary <Vector3, List <ConnectionEdge> >(); var _mainPointList = new Polygon(); LinkAndAddToList(_mainPointList, points, incidentEdges); ////////////////////////////////////////////// var Result = new List <Vector3>(); if (Normal.Equals(Vector3.Zero)) { throw new Exception("The input is not a valid polygon"); } //if (_holes != null && _holes.Count > 0) //{ // ProcessHoles(); //} List <ConnectionEdge> nonConvexPoints = FindNonConvexPoints(_mainPointList, Normal); if (nonConvexPoints.Count == _mainPointList.PointCount) { throw new ArgumentException("The triangle input is not valid"); } while (_mainPointList.PointCount > 2) { Boolean guard = false; foreach (var cur in _mainPointList.GetPolygonCirculator()) { if (!IsConvex(cur, Normal)) { continue; } if (!IsPointInTriangle(cur.Prev.Origin, cur.Origin, cur.Next.Origin, nonConvexPoints, Normal)) { // cut off ear guard = true; Result.Add(cur.Prev.Origin); Result.Add(cur.Origin); Result.Add(cur.Next.Origin); // Check if prev and next are still nonconvex. If not, then remove from non convex list if (IsConvex(cur.Prev, Normal)) { Int32 index = nonConvexPoints.FindIndex(x => x == cur.Prev); if (index >= 0) { nonConvexPoints.RemoveAt(index); } } if (IsConvex(cur.Next, Normal)) { Int32 index = nonConvexPoints.FindIndex(x => x == cur.Next); if (index >= 0) { nonConvexPoints.RemoveAt(index); } } _mainPointList.Remove(cur); break; } } if (PointsOnLine(_mainPointList, Normal)) { break; } if (!guard) { return(new List <Vector3>()); } } return(Result); }