Example #1
0
        public virtual SpatialRelation Relate(IRectangle rectangle)
        {
            SpatialRelation bboxR = bbox.Relate(rectangle);

            if (bboxR == SpatialRelation.WITHIN || bboxR == SpatialRelation.DISJOINT)
            {
                return(bboxR);
            }
            // FYI, the right answer could still be DISJOINT or WITHIN, but we don't know yet.
            return(Relate(ctx.GetGeometryFrom(rectangle)));
        }
Example #2
0
        public virtual SpatialRelation Relate(IRectangle r)
        {
            //Check BBox for disjoint & within.
            SpatialRelation bboxR = bbox.Relate(r);

            if (bboxR == SpatialRelation.DISJOINT || bboxR == SpatialRelation.WITHIN)
            {
                return(bboxR);
            }
            //Either CONTAINS, INTERSECTS, or DISJOINT

            IPoint          scratch = new Point(0, 0, null);
            IPoint          prC     = r.Center;
            SpatialRelation result  = linePrimary.Relate(r, prC, scratch);

            if (result == SpatialRelation.DISJOINT)
            {
                return(SpatialRelation.DISJOINT);
            }
            SpatialRelation resultOpp = linePerp.Relate(r, prC, scratch);

            if (resultOpp == SpatialRelation.DISJOINT)
            {
                return(SpatialRelation.DISJOINT);
            }
            if (result == resultOpp)//either CONTAINS or INTERSECTS
            {
                return(result);
            }
            return(SpatialRelation.INTERSECTS);
        }
Example #3
0
        protected virtual IPoint RandomPointIn(IRectangle r)
        {
            double x = r.MinX + random.NextDouble() * r.Width;
            double y = r.MinY + random.NextDouble() * r.Height;

            x = NormX(x);
            y = NormY(y);
            IPoint p = ctx.MakePoint(x, y);

            Assert.Equal(SpatialRelation.CONTAINS, r.Relate(p));
            return(p);
        }
Example #4
0
        protected virtual IPoint RandomPointIn(IShape shape)
        {
            if (!shape.HasArea)// or try the center?
            {
                throw new InvalidOperationException("Need area to define shape!");
            }
            IRectangle bbox = shape.BoundingBox;
            IPoint     p;

            do
            {
                p = RandomPointIn(bbox);
            } while (!bbox.Relate(p).Intersects());
            return(p);
        }
Example #5
0
            public override bool Evaluate(IShape indexedShape, IShape queryShape)
            {
                // LUCENENET specific - added guard clauses
                if (indexedShape is null)
                {
                    throw new ArgumentNullException(nameof(indexedShape));
                }
                if (queryShape is null)
                {
                    throw new ArgumentNullException(nameof(queryShape));
                }

                IRectangle bbox = indexedShape.BoundingBox;

                return(bbox.Relate(queryShape) == SpatialRelation.Within || bbox.Equals(queryShape));
            }
Example #6
0
        public virtual SpatialRelation Relate(IRectangle r)
        {
            //Note: Surprisingly complicated!

            //--We start by leveraging the fact we have a calculated bbox that is "cheaper" than use of DistanceCalculator.
            SpatialRelation bboxSect = enclosingBox.Relate(r);

            if (bboxSect == SpatialRelation.DISJOINT || bboxSect == SpatialRelation.WITHIN)
            {
                return(bboxSect);
            }
            if (bboxSect == SpatialRelation.CONTAINS && enclosingBox.Equals(r)) //nasty identity edge-case
            {
                return(SpatialRelation.WITHIN);
            }
            //bboxSect is INTERSECTS or CONTAINS
            //The result can be DISJOINT, CONTAINS, or INTERSECTS (not WITHIN)

            return(RelateRectanglePhase2(r, bboxSect));
        }
Example #7
0
        private IRectangle IntersectRects(IRectangle r1, IRectangle r2)
        {
            Debug.Assert(r1.Relate(r2).Intersects());
            double minX, maxX;

            if (r1.RelateXRange(r2.MinX, r2.MinX).Intersects())
            {
                minX = r2.MinX;
            }
            else
            {
                minX = r1.MinX;
            }
            if (r1.RelateXRange(r2.MaxX, r2.MaxX).Intersects())
            {
                maxX = r2.MaxX;
            }
            else
            {
                maxX = r1.MaxX;
            }
            double minY, maxY;

            if (r1.RelateYRange(r2.MinY, r2.MinY).Intersects())
            {
                minY = r2.MinY;
            }
            else
            {
                minY = r1.MinY;
            }
            if (r1.RelateYRange(r2.MaxY, r2.MaxY).Intersects())
            {
                maxY = r2.MaxY;
            }
            else
            {
                maxY = r1.MaxY;
            }
            return(ctx.MakeRectangle(minX, maxX, minY, maxY));
        }
Example #8
0
        public virtual void TestCalcBoxByDistFromPt()
        {
            //first test regression
            {
                double d    = 6894.1 * DistanceUtils.KM_TO_DEG;
                IPoint pCtr = PLL(-20, 84);
                IPoint pTgt = PLL(-42, 15);
                Assert.True(Dc().Distance(pCtr, pTgt) < d);
                //since the pairwise distance is less than d, a bounding box from ctr with d should contain pTgt.
                IRectangle r = Dc().CalcBoxByDistFromPt(pCtr, d, ctx, null);
                Assert.Equal(SpatialRelation.CONTAINS, r.Relate(pTgt));
                CheckBBox(pCtr, d);
            }

            CustomAssert.EqualWithDelta(/*"0 dist, horiz line",*/
                -45, Dc().CalcBoxByDistFromPt_yHorizAxisDEG(ctx.MakePoint(-180, -45), 0, ctx), 0);

            double MAXDIST = (double)180 * DistanceUtils.DEG_TO_KM;

            CheckBBox(ctx.MakePoint(0, 0), MAXDIST);
            CheckBBox(ctx.MakePoint(0, 0), MAXDIST * 0.999999);
            CheckBBox(ctx.MakePoint(0, 0), 0);
            CheckBBox(ctx.MakePoint(0, 0), 0.000001);
            CheckBBox(ctx.MakePoint(0, 90), 0.000001);
            CheckBBox(ctx.MakePoint(-32.7, -5.42), 9829);
            CheckBBox(ctx.MakePoint(0, 90 - 20), (double)20 * DistanceUtils.DEG_TO_KM);
            {
                double d = 0.010;//10m
                CheckBBox(ctx.MakePoint(0, 90 - (d + 0.001) * DistanceUtils.KM_TO_DEG), d);
            }

            for (int T = 0; T < 100; T++)
            {
                double lat  = -90 + random.NextDouble() * 180;
                double lon  = -180 + random.NextDouble() * 360;
                IPoint ctr  = ctx.MakePoint(lon, lat);
                double dist = MAXDIST * random.NextDouble();
                CheckBBox(ctr, dist);
            }
        }
Example #9
0
        public virtual SpatialRelation Relate(IShape other)
        {
            SpatialRelation bboxSect = m_bbox.Relate(other);

            if (bboxSect == SpatialRelation.DISJOINT || bboxSect == SpatialRelation.WITHIN)
            {
                return(bboxSect);
            }

            bool containsWillShortCircuit = (other is IPoint) ||
                                            RelateContainsShortCircuits();
            SpatialRelation?sect = null;

            foreach (Shapes.IShape shape in m_shapes)
            {
                SpatialRelation nextSect = shape.Relate(other);

                if (sect == null)
                {//first pass
                    sect = nextSect;
                }
                else
                {
                    // TODO: What is the logic supposed to be if sect is null?
                    sect = sect.Value.Combine(nextSect);
                }

                if (sect == SpatialRelation.INTERSECTS)
                {
                    return(SpatialRelation.INTERSECTS);
                }

                if (sect == SpatialRelation.CONTAINS && containsWillShortCircuit)
                {
                    return(SpatialRelation.CONTAINS);
                }
            }
            return(sect.GetValueOrDefault()); // TODO: What to return if null??
        }
Example #10
0
            public override bool Evaluate(IShape indexedShape, IShape queryShape)
            {
                IRectangle bbox = indexedShape.BoundingBox;

                return(bbox.Relate(queryShape) == SpatialRelation.WITHIN || bbox.Equals(queryShape));
            }