internal static com.epl.geometry.Envelope2DIntersectorImpl GetEnvelope2DIntersectorForParts(com.epl.geometry.MultiPathImpl multipathImplA, com.epl.geometry.MultiPathImpl multipathImplB, double tolerance, bool bExteriorOnlyA, bool bExteriorOnlyB)
        {
            int type_a = multipathImplA.GetType().Value();
            int type_b = multipathImplB.GetType().Value();

            com.epl.geometry.Envelope2D env_a = new com.epl.geometry.Envelope2D();
            com.epl.geometry.Envelope2D env_b = new com.epl.geometry.Envelope2D();
            multipathImplA.QueryLooseEnvelope2D(env_a);
            multipathImplB.QueryLooseEnvelope2D(env_b);
            env_a.Inflate(tolerance, tolerance);
            env_b.Inflate(tolerance, tolerance);
            com.epl.geometry.Envelope2D envInter = new com.epl.geometry.Envelope2D();
            envInter.SetCoords(env_a);
            envInter.Intersect(env_b);
            com.epl.geometry.Envelope2DIntersectorImpl intersector = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector.SetTolerance(tolerance);
            bool b_found_red = false;

            intersector.StartRedConstruction();
            for (int ipath_a = 0, npaths = multipathImplA.GetPathCount(); ipath_a < npaths; ipath_a++)
            {
                if (bExteriorOnlyA && type_a == com.epl.geometry.Geometry.GeometryType.Polygon && !multipathImplA.IsExteriorRing(ipath_a))
                {
                    continue;
                }
                multipathImplA.QueryPathEnvelope2D(ipath_a, env_a);
                if (!env_a.IsIntersecting(envInter))
                {
                    continue;
                }
                b_found_red = true;
                intersector.AddRedEnvelope(ipath_a, env_a);
            }
            intersector.EndRedConstruction();
            if (!b_found_red)
            {
                return(null);
            }
            bool b_found_blue = false;

            intersector.StartBlueConstruction();
            for (int ipath_b = 0, npaths = multipathImplB.GetPathCount(); ipath_b < npaths; ipath_b++)
            {
                if (bExteriorOnlyB && type_b == com.epl.geometry.Geometry.GeometryType.Polygon && !multipathImplB.IsExteriorRing(ipath_b))
                {
                    continue;
                }
                multipathImplB.QueryPathEnvelope2D(ipath_b, env_b);
                if (!env_b.IsIntersecting(envInter))
                {
                    continue;
                }
                b_found_blue = true;
                intersector.AddBlueEnvelope(ipath_b, env_b);
            }
            intersector.EndBlueConstruction();
            if (!b_found_blue)
            {
                return(null);
            }
            return(intersector);
        }
        public static void TestEnvelope2Dintersector()
        {
            System.Collections.Generic.List <com.epl.geometry.Envelope2D> envelopes = new System.Collections.Generic.List <com.epl.geometry.Envelope2D>(0);
            com.epl.geometry.Envelope2D env0  = new com.epl.geometry.Envelope2D(2, 3, 4, 4);
            com.epl.geometry.Envelope2D env1  = new com.epl.geometry.Envelope2D(5, 13, 9, 15);
            com.epl.geometry.Envelope2D env2  = new com.epl.geometry.Envelope2D(6, 9, 11, 12);
            com.epl.geometry.Envelope2D env3  = new com.epl.geometry.Envelope2D(8, 10, 9, 17);
            com.epl.geometry.Envelope2D env4  = new com.epl.geometry.Envelope2D(11.001, 12, 14, 14);
            com.epl.geometry.Envelope2D env5  = new com.epl.geometry.Envelope2D(1, 3, 3, 4);
            com.epl.geometry.Envelope2D env6  = new com.epl.geometry.Envelope2D(0, 2, 5, 10);
            com.epl.geometry.Envelope2D env7  = new com.epl.geometry.Envelope2D(4, 7, 5, 10);
            com.epl.geometry.Envelope2D env8  = new com.epl.geometry.Envelope2D(3, 15, 15, 15);
            com.epl.geometry.Envelope2D env9  = new com.epl.geometry.Envelope2D(0, 9, 14, 9);
            com.epl.geometry.Envelope2D env10 = new com.epl.geometry.Envelope2D(0, 8.999, 14, 8.999);
            envelopes.Add(env0);
            envelopes.Add(env1);
            envelopes.Add(env2);
            envelopes.Add(env3);
            envelopes.Add(env4);
            envelopes.Add(env5);
            envelopes.Add(env6);
            envelopes.Add(env7);
            envelopes.Add(env8);
            envelopes.Add(env9);
            envelopes.Add(env10);
            com.epl.geometry.Envelope2DIntersectorImpl intersector = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector.SetTolerance(0.001);
            intersector.StartConstruction();
            for (int i = 0; i < envelopes.Count; i++)
            {
                intersector.AddEnvelope(i, envelopes[i]);
            }
            intersector.EndConstruction();
            int count = 0;

            while (intersector.Next())
            {
                int env_a = intersector.GetHandleA();
                int env_b = intersector.GetHandleB();
                count++;
                com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                env.SetCoords(envelopes[env_a]);
                env.Inflate(0.001, 0.001);
                NUnit.Framework.Assert.IsTrue(env.IsIntersecting(envelopes[env_b]));
            }
            System.Diagnostics.Debug.Assert((count == 16));
            com.epl.geometry.Envelope2DIntersectorImpl intersector2 = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector2.SetTolerance(0.0);
            intersector2.StartConstruction();
            for (int i_1 = 0; i_1 < envelopes.Count; i_1++)
            {
                intersector2.AddEnvelope(i_1, envelopes[i_1]);
            }
            intersector2.EndConstruction();
            count = 0;
            while (intersector2.Next())
            {
                int env_a = intersector2.GetHandleA();
                int env_b = intersector2.GetHandleB();
                count++;
                com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                env.SetCoords(envelopes[env_a]);
                NUnit.Framework.Assert.IsTrue(env.IsIntersecting(envelopes[env_b]));
            }
            System.Diagnostics.Debug.Assert((count == 13));
            env0 = new com.epl.geometry.Envelope2D(0, 0, 0, 10);
            env1 = new com.epl.geometry.Envelope2D(0, 10, 10, 10);
            env2 = new com.epl.geometry.Envelope2D(10, 0, 10, 10);
            env3 = new com.epl.geometry.Envelope2D(0, 0, 10, 0);
            envelopes.Clear();
            envelopes.Add(env0);
            envelopes.Add(env1);
            envelopes.Add(env2);
            envelopes.Add(env3);
            com.epl.geometry.Envelope2DIntersectorImpl intersector3 = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector3.SetTolerance(0.001);
            intersector3.StartConstruction();
            for (int i_2 = 0; i_2 < envelopes.Count; i_2++)
            {
                intersector3.AddEnvelope(i_2, envelopes[i_2]);
            }
            intersector3.EndConstruction();
            count = 0;
            while (intersector3.Next())
            {
                int env_a = intersector3.GetHandleA();
                int env_b = intersector3.GetHandleB();
                count++;
                com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                env.SetCoords(envelopes[env_a]);
                NUnit.Framework.Assert.IsTrue(env.IsIntersecting(envelopes[env_b]));
            }
            NUnit.Framework.Assert.IsTrue(count == 4);
            env0 = new com.epl.geometry.Envelope2D(0, 0, 0, 10);
            envelopes.Clear();
            envelopes.Add(env0);
            envelopes.Add(env0);
            envelopes.Add(env0);
            envelopes.Add(env0);
            com.epl.geometry.Envelope2DIntersectorImpl intersector4 = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector4.SetTolerance(0.001);
            intersector4.StartConstruction();
            for (int i_3 = 0; i_3 < envelopes.Count; i_3++)
            {
                intersector4.AddEnvelope(i_3, envelopes[i_3]);
            }
            intersector4.EndConstruction();
            count = 0;
            while (intersector4.Next())
            {
                int env_a = intersector4.GetHandleA();
                int env_b = intersector4.GetHandleB();
                count++;
                com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                env.SetCoords(envelopes[env_a]);
                NUnit.Framework.Assert.IsTrue(env.IsIntersecting(envelopes[env_b]));
            }
            System.Diagnostics.Debug.Assert((count == 6));
            env0 = new com.epl.geometry.Envelope2D(0, 10, 10, 10);
            envelopes.Clear();
            envelopes.Add(env0);
            envelopes.Add(env0);
            envelopes.Add(env0);
            envelopes.Add(env0);
            com.epl.geometry.Envelope2DIntersectorImpl intersector5 = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector5.SetTolerance(0.001);
            intersector5.StartConstruction();
            for (int i_4 = 0; i_4 < envelopes.Count; i_4++)
            {
                intersector5.AddEnvelope(i_4, envelopes[i_4]);
            }
            intersector5.EndConstruction();
            count = 0;
            while (intersector5.Next())
            {
                int env_a = intersector5.GetHandleA();
                int env_b = intersector5.GetHandleB();
                count++;
                com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                env.SetCoords(envelopes[env_a]);
                NUnit.Framework.Assert.IsTrue(env.IsIntersecting(envelopes[env_b]));
            }
            NUnit.Framework.Assert.IsTrue(count == 6);
        }
        internal static com.epl.geometry.Envelope2DIntersectorImpl GetEnvelope2DIntersector(com.epl.geometry.MultiPathImpl multipathImplA, com.epl.geometry.MultiPathImpl multipathImplB, double tolerance)
        {
            com.epl.geometry.Envelope2D env_a = new com.epl.geometry.Envelope2D();
            com.epl.geometry.Envelope2D env_b = new com.epl.geometry.Envelope2D();
            multipathImplA.QueryLooseEnvelope2D(env_a);
            multipathImplB.QueryLooseEnvelope2D(env_b);
            env_a.Inflate(tolerance, tolerance);
            env_b.Inflate(tolerance, tolerance);
            com.epl.geometry.Envelope2D envInter = new com.epl.geometry.Envelope2D();
            envInter.SetCoords(env_a);
            envInter.Intersect(env_b);
            com.epl.geometry.SegmentIteratorImpl       segIterA    = multipathImplA.QuerySegmentIterator();
            com.epl.geometry.SegmentIteratorImpl       segIterB    = multipathImplB.QuerySegmentIterator();
            com.epl.geometry.Envelope2DIntersectorImpl intersector = new com.epl.geometry.Envelope2DIntersectorImpl();
            intersector.SetTolerance(tolerance);
            bool b_found_red = false;

            intersector.StartRedConstruction();
            while (segIterA.NextPath())
            {
                while (segIterA.HasNextSegment())
                {
                    com.epl.geometry.Segment segmentA = segIterA.NextSegment();
                    segmentA.QueryEnvelope2D(env_a);
                    if (!env_a.IsIntersecting(envInter))
                    {
                        continue;
                    }
                    b_found_red = true;
                    com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                    env.SetCoords(env_a);
                    intersector.AddRedEnvelope(segIterA.GetStartPointIndex(), env);
                }
            }
            intersector.EndRedConstruction();
            if (!b_found_red)
            {
                return(null);
            }
            bool b_found_blue = false;

            intersector.StartBlueConstruction();
            while (segIterB.NextPath())
            {
                while (segIterB.HasNextSegment())
                {
                    com.epl.geometry.Segment segmentB = segIterB.NextSegment();
                    segmentB.QueryEnvelope2D(env_b);
                    if (!env_b.IsIntersecting(envInter))
                    {
                        continue;
                    }
                    b_found_blue = true;
                    com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                    env.SetCoords(env_b);
                    intersector.AddBlueEnvelope(segIterB.GetStartPointIndex(), env);
                }
            }
            intersector.EndBlueConstruction();
            if (!b_found_blue)
            {
                return(null);
            }
            return(intersector);
        }
        public static void TestRandom()
        {
            int passcount   = 10;
            int figureSize  = 100;
            int figureSize2 = 100;

            com.epl.geometry.Envelope extent1 = new com.epl.geometry.Envelope();
            com.epl.geometry.Envelope extent2 = new com.epl.geometry.Envelope();
            com.epl.geometry.Envelope extent3 = new com.epl.geometry.Envelope();
            com.epl.geometry.Envelope extent4 = new com.epl.geometry.Envelope();
            extent1.SetCoords(-10000, 5000, 10000, 25000);
            // red
            extent2.SetCoords(-10000, 2000, 10000, 8000);
            // blue
            extent3.SetCoords(-10000, -8000, 10000, -2000);
            // blue
            extent4.SetCoords(-10000, -25000, 10000, -5000);
            // red
            com.epl.geometry.RandomCoordinateGenerator generator1 = new com.epl.geometry.RandomCoordinateGenerator(System.Math.Max(figureSize, 10000), extent1, 0.001);
            com.epl.geometry.RandomCoordinateGenerator generator2 = new com.epl.geometry.RandomCoordinateGenerator(System.Math.Max(figureSize, 10000), extent2, 0.001);
            com.epl.geometry.RandomCoordinateGenerator generator3 = new com.epl.geometry.RandomCoordinateGenerator(System.Math.Max(figureSize, 10000), extent3, 0.001);
            com.epl.geometry.RandomCoordinateGenerator generator4 = new com.epl.geometry.RandomCoordinateGenerator(System.Math.Max(figureSize, 10000), extent4, 0.001);
            System.Random random   = new System.Random(1982);
            int           rand_max = 511;
            int           qCount   = 0;
            int           eCount   = 0;
            int           bCount   = 0;

            for (int c = 0; c < passcount; c++)
            {
                com.epl.geometry.Polygon polyRed  = new com.epl.geometry.Polygon();
                com.epl.geometry.Polygon polyBlue = new com.epl.geometry.Polygon();
                int r = figureSize;
                if (r < 3)
                {
                    continue;
                }
                com.epl.geometry.Point pt;
                for (int j = 0; j < r; j++)
                {
                    int  rand       = random.Next(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator1.GetRandomCoord();
                    if (j == 0 || bRandomNew)
                    {
                        polyRed.StartPath(pt);
                    }
                    else
                    {
                        polyRed.LineTo(pt);
                    }
                }
                for (int j_1 = 0; j_1 < r; j_1++)
                {
                    int  rand       = random.Next(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator4.GetRandomCoord();
                    if (j_1 == 0 || bRandomNew)
                    {
                        polyRed.StartPath(pt);
                    }
                    else
                    {
                        polyRed.LineTo(pt);
                    }
                }
                r = figureSize2;
                if (r < 3)
                {
                    continue;
                }
                for (int j_2 = 0; j_2 < r; j_2++)
                {
                    int  rand       = random.Next(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator2.GetRandomCoord();
                    if (j_2 == 0 || bRandomNew)
                    {
                        polyBlue.StartPath(pt);
                    }
                    else
                    {
                        polyBlue.LineTo(pt);
                    }
                }
                for (int j_3 = 0; j_3 < r; j_3++)
                {
                    int  rand       = random.Next(rand_max);
                    bool bRandomNew = (r > 10) && ((1.0 * rand) / rand_max > 0.95);
                    pt = generator3.GetRandomCoord();
                    if (j_3 == 0 || bRandomNew)
                    {
                        polyBlue.StartPath(pt);
                    }
                    else
                    {
                        polyBlue.LineTo(pt);
                    }
                }
                com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D();
                // Quad_tree
                com.epl.geometry.QuadTree quadTree = BuildQuadTree(polyBlue);
                com.epl.geometry.QuadTree.QuadTreeIterator iterator    = quadTree.GetIterator();
                com.epl.geometry.SegmentIteratorImpl       _segIterRed = ((com.epl.geometry.MultiPathImpl)polyRed._getImpl()).QuerySegmentIterator();
                while (_segIterRed.NextPath())
                {
                    while (_segIterRed.HasNextSegment())
                    {
                        com.epl.geometry.Segment segmentRed = _segIterRed.NextSegment();
                        segmentRed.QueryEnvelope2D(env);
                        iterator.ResetIterator(env, 0.001);
                        while (iterator.Next() != -1)
                        {
                            qCount++;
                        }
                    }
                }
                // Envelope_2D_intersector
                System.Collections.Generic.List <com.epl.geometry.Envelope2D> envelopes_red  = new System.Collections.Generic.List <com.epl.geometry.Envelope2D>();
                System.Collections.Generic.List <com.epl.geometry.Envelope2D> envelopes_blue = new System.Collections.Generic.List <com.epl.geometry.Envelope2D>();
                com.epl.geometry.SegmentIterator segIterRed = polyRed.QuerySegmentIterator();
                while (segIterRed.NextPath())
                {
                    while (segIterRed.HasNextSegment())
                    {
                        com.epl.geometry.Segment segment = segIterRed.NextSegment();
                        env = new com.epl.geometry.Envelope2D();
                        segment.QueryEnvelope2D(env);
                        envelopes_red.Add(env);
                    }
                }
                com.epl.geometry.SegmentIterator segIterBlue = polyBlue.QuerySegmentIterator();
                while (segIterBlue.NextPath())
                {
                    while (segIterBlue.HasNextSegment())
                    {
                        com.epl.geometry.Segment segment = segIterBlue.NextSegment();
                        env = new com.epl.geometry.Envelope2D();
                        segment.QueryEnvelope2D(env);
                        envelopes_blue.Add(env);
                    }
                }
                com.epl.geometry.Envelope2DIntersectorImpl intersector = new com.epl.geometry.Envelope2DIntersectorImpl();
                intersector.SetTolerance(0.001);
                intersector.StartRedConstruction();
                for (int i = 0; i < envelopes_red.Count; i++)
                {
                    intersector.AddRedEnvelope(i, envelopes_red[i]);
                }
                intersector.EndRedConstruction();
                intersector.StartBlueConstruction();
                for (int i_1 = 0; i_1 < envelopes_blue.Count; i_1++)
                {
                    intersector.AddBlueEnvelope(i_1, envelopes_blue[i_1]);
                }
                intersector.EndBlueConstruction();
                while (intersector.Next())
                {
                    eCount++;
                }
                NUnit.Framework.Assert.IsTrue(qCount == eCount);
            }
        }