Exemplo n.º 1
0
        static Poly2Tri.Polygon CreatePolygon(FlattenContour cnt)
        {
            using (Borrow(out List <Poly2Tri.TriangulationPoint> points))
            {
                //limitation: poly tri not accept duplicated points! ***
                double prevX = 0;
                double prevY = 0;
                int    j     = cnt.VertexCount;

                for (int i = 0; i < j; ++i)
                {
                    cnt.GetPoint(i, out float x, out float y);
                    if (x == prevX && y == prevY)
                    {
                        if (i > 0)
                        {
                            //skip duplicated point, not use
                            continue;
                        }
                    }
                    else
                    {
                        //var triPoint = new Poly2Tri.TriangulationPoint(prevX = x, prevY = y) { userData = p };
                        //points.Add(triPoint);
                        points.Add(new Poly2Tri.TriangulationPoint(prevX = x, prevY = y));
                    }
                }
                return(new Poly2Tri.Polygon(points.ToArray()));
            }
        }
Exemplo n.º 2
0
        static void SeparateToFlattenContourList(float[] polygonXYs, int[] contourEndIndices, List <FlattenContour> contours)
        {
            int contourCount = contourEndIndices.Length;
            int index        = 0;

            for (int c = 0; c < contourCount; ++c)
            {
                int endAt = contourEndIndices[c] + 1; //glyph convention***

                FlattenContour cnt = new FlattenContour(polygonXYs, index, endAt - index);
                //--
                //temp hack here!
                //ensure=> duplicated points,
                //most common => first point and last point
                cnt.GetPoint(0, out float p0x, out float p0y);
                cnt.GetPoint(cnt.VertexCount - 1, out float p_lastX, out float p_lastY);
                if (p0x == p_lastX && p0y == p_lastY)
                {
                    cnt._len -= 2;//
                }
                index = endAt;
                contours.Add(cnt);
            }
        }
Exemplo n.º 3
0
        public void PreparePolygons(float[] flattenPolygonXYs, int[] contourEndIndices, List <Poly2Tri.Polygon> outputPolygons)
        {
            _waitingHoles.Clear();
            _flattenContours.Clear();
            _otherPolygons.Clear();

            //
            SeparateToFlattenContourList(flattenPolygonXYs, contourEndIndices, _flattenContours);
            //--------------------------
            //TODO: review here, add hole or not
            // more than 1 contours, no hole => eg.  i, j, ;,  etc
            // more than 1 contours, with hole => eg.  a,e ,   etc

            //clockwise => not hole

            int cntCount = _flattenContours.Count;

            Poly2Tri.Polygon mainPolygon = null;
            //
            //this version if it is a hole=> we add it to main polygon
            //TODO: add to more proper polygon ***
            //eg i
            //--------------------------

            for (int n = 0; n < cntCount; ++n)
            {
                FlattenContour cnt = _flattenContours[n];

                bool cntIsMainPolygon = YAxisPointDown ? !cnt.IsClockwise : cnt.IsClockwise;

                if (cntIsMainPolygon)
                {
                    //main polygon
                    //not a hole
                    if (mainPolygon == null)
                    {
                        //if we don't have mainPolygon before
                        //this is main polygon
                        mainPolygon = CreatePolygon(cnt);

                        if (_waitingHoles.Count > 0)
                        {
                            //flush all waiting holes to the main polygon
                            int j = _waitingHoles.Count;
                            for (int i = 0; i < j; ++i)
                            {
                                mainPolygon.AddHole(_waitingHoles[i]);
                            }
                            _waitingHoles.Clear();
                        }
                    }
                    else
                    {
                        //if we already have a main polygon
                        //then this is another sub polygon
                        //IsHole is correct after we Analyze() the glyph contour
                        _otherPolygons.Add(CreatePolygon(cnt));
                    }
                }
                else
                {
                    //this is a hole
                    Poly2Tri.Polygon subPolygon = CreatePolygon(cnt);
                    if (mainPolygon == null)
                    {
                        //add to waiting polygon
                        _waitingHoles.Add(subPolygon);
                    }
                    else
                    {
                        //add to mainPolygon
                        mainPolygon.AddHole(subPolygon);
                    }
                }
            }
            if (_waitingHoles.Count > 0)
            {
                throw new NotSupportedException();
            }

            outputPolygons.Add(mainPolygon);
            if (_otherPolygons.Count > 0)
            {
                outputPolygons.AddRange(_otherPolygons);
            }


            _waitingHoles.Clear();
            _flattenContours.Clear();
            _otherPolygons.Clear();
        }