Ejemplo n.º 1
0
        private static List <Vertices> Execute(Vertices subject, Vertices clip, PolyClipType clipType, out PolyClipError error)
        {
            Debug.Assert(subject.IsSimple() && clip.IsSimple(), "Non simple input!", "Input polygons must be simple (cannot intersect themselves).");
            Vertices vertices;
            Vertices vertices2;

            YuPengClipper.CalculateIntersections(subject, clip, out vertices, out vertices2);
            TSVector2 lowerBound  = subject.GetAABB().LowerBound;
            TSVector2 lowerBound2 = clip.GetAABB().LowerBound;
            TSVector2 tSVector;

            TSVector2.Min(ref lowerBound, ref lowerBound2, out tSVector);
            tSVector = TSVector2.one - tSVector;
            bool flag = tSVector != TSVector2.zero;

            if (flag)
            {
                vertices.Translate(ref tSVector);
                vertices2.Translate(ref tSVector);
            }
            vertices.ForceCounterClockWise();
            vertices2.ForceCounterClockWise();
            List <FP> poly1Coeff;
            List <YuPengClipper.Edge> poly1Simplicies;

            YuPengClipper.CalculateSimplicalChain(vertices, out poly1Coeff, out poly1Simplicies);
            List <FP> poly2Coeff;
            List <YuPengClipper.Edge> poly2Simplicies;

            YuPengClipper.CalculateSimplicalChain(vertices2, out poly2Coeff, out poly2Simplicies);
            List <YuPengClipper.Edge> simplicies;

            YuPengClipper.CalculateResultChain(poly1Coeff, poly1Simplicies, poly2Coeff, poly2Simplicies, clipType, out simplicies);
            List <Vertices> list;

            error     = YuPengClipper.BuildPolygonsFromChain(simplicies, out list);
            tSVector *= -1f;
            for (int i = 0; i < list.Count; i++)
            {
                list[i].Translate(ref tSVector);
                SimplifyTools.CollinearSimplify(list[i], FP.Zero);
            }
            return(list);
        }
        /// <summary>
        /// Implements "A new algorithm for Boolean operations on general polygons"
        /// available here: http://liama.ia.ac.cn/wiki/_media/user:dong:dong_cg_05.pdf
        /// Merges two polygons, a subject and a clip with the specified operation. Polygons may not be
        /// self-intersecting.
        ///
        /// Warning: May yield incorrect results or even crash if polygons contain collinear points.
        /// </summary>
        /// <param name="subject">The subject polygon.</param>
        /// <param name="clip">The clip polygon, which is added,
        /// substracted or intersected with the subject</param>
        /// <param name="clipType">The operation to be performed. Either
        /// Union, Difference or Intersection.</param>
        /// <param name="error">The error generated (if any)</param>
        /// <returns>A list of closed polygons, which make up the result of the clipping operation.
        /// Outer contours are ordered counter clockwise, holes are ordered clockwise.</returns>
        private static List <Vertices> Execute(Vertices subject, Vertices clip, PolyClipType clipType, out PolyClipError error)
        {
            Debug.Assert(subject.IsSimple() && clip.IsSimple(), "Non simple input!", "Input polygons must be simple (cannot intersect themselves).");

            // Copy polygons
            Vertices slicedSubject;
            Vertices slicedClip;

            // Calculate the intersection and touch points between
            // subject and clip and add them to both
            CalculateIntersections(subject, clip, out slicedSubject, out slicedClip);

            // Translate polygons into upper right quadrant
            // as the algorithm depends on it
            TSVector2 lbSubject = subject.GetAABB().LowerBound;
            TSVector2 lbClip    = clip.GetAABB().LowerBound;
            TSVector2 translate;

            TSVector2.Min(ref lbSubject, ref lbClip, out translate);
            translate = TSVector2.one - translate;
            if (translate != TSVector2.zero)
            {
                slicedSubject.Translate(ref translate);
                slicedClip.Translate(ref translate);
            }

            // Enforce counterclockwise contours
            slicedSubject.ForceCounterClockWise();
            slicedClip.ForceCounterClockWise();

            List <Edge> subjectSimplices;
            List <FP>   subjectCoeff;
            List <Edge> clipSimplices;
            List <FP>   clipCoeff;

            // Build simplical chains from the polygons and calculate the
            // the corresponding coefficients
            CalculateSimplicalChain(slicedSubject, out subjectCoeff, out subjectSimplices);
            CalculateSimplicalChain(slicedClip, out clipCoeff, out clipSimplices);

            List <Edge> resultSimplices;

            // Determine the characteristics function for all non-original edges
            // in subject and clip simplical chain and combine the edges contributing
            // to the result, depending on the clipType
            CalculateResultChain(subjectCoeff, subjectSimplices, clipCoeff, clipSimplices, clipType,
                                 out resultSimplices);

            List <Vertices> result;

            // Convert result chain back to polygon(s)
            error = BuildPolygonsFromChain(resultSimplices, out result);

            // Reverse the polygon translation from the beginning
            // and remove collinear points from output
            translate *= -1f;
            for (int i = 0; i < result.Count; ++i)
            {
                result[i].Translate(ref translate);
                SimplifyTools.CollinearSimplify(result[i], FP.Zero);
            }
            return(result);
        }