Ejemplo n.º 1
0
        public List <short> TriangulateAndReturnIndices()
        {
            EarClipping EarClipper = new EarClipping();

            EarClipper.SetPoints(Vertices, null, Normal);
            EarClipper.Triangulate();
            var Result = EarClipper.Result;

            List <short> Indices = new List <short>();

            for (short Index = 0; Index < Result.Count; Index++)
            {
                Vector3m Vertex = Result[Index];
                for (short IndexPoint = 0; IndexPoint < Vertices.Count; IndexPoint++)
                {
                    if (Vertex.Equals(Vertices[IndexPoint]))
                    {
                        Indices.Add(IndexPoint);
                    }
                }
            }
            return(Indices);
        }
Ejemplo n.º 2
0
        public void Triangulate()
        {
            if (Normal.Equals(Vector3m.Zero()))
            {
                throw new Exception("The input is not a valid polygon");
            }
            if (_holes != null && _holes.Count > 0)
            {
                ProcessHoles();
            }

            List <ConnectionEdge> nonConvexPoints = FindNonConvexPoints(_mainPointList);

            if (nonConvexPoints.Count == _mainPointList.PointCount)
            {
                throw new ArgumentException("The triangle input is not valid");
            }

            while (_mainPointList.PointCount > 2)
            {
                bool guard = false;
                foreach (var cur in _mainPointList.GetPolygonCirculator())
                {
                    if (!IsConvex(cur))
                    {
                        continue;
                    }

                    if (!IsPointInTriangle(cur.Prev.Origin, cur.Origin, cur.Next.Origin, nonConvexPoints))
                    {
                        // 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))
                        {
                            int index = nonConvexPoints.FindIndex(x => x == cur.Prev);
                            if (index >= 0)
                            {
                                nonConvexPoints.RemoveAt(index);
                            }
                        }
                        if (IsConvex(cur.Next))
                        {
                            int index = nonConvexPoints.FindIndex(x => x == cur.Next);
                            if (index >= 0)
                            {
                                nonConvexPoints.RemoveAt(index);
                            }
                        }
                        _mainPointList.Remove(cur);
                        break;
                    }
                }

                if (PointsOnLine(_mainPointList))
                {
                    break;
                }
                if (!guard)
                {
                    throw new Exception("No progression. The input must be wrong");
                }
            }
        }