private void AssertNtsConsistentRelate(IShape shape) { IntersectionMatrix expectedM = POLY_SHAPE.Geometry.Relate(((NtsSpatialContext)ctx).GetGeometryFrom(shape)); SpatialRelation expectedSR = NtsGeometry.IntersectionMatrixToSpatialRelation(expectedM); //NTS considers a point on a boundary INTERSECTS, not CONTAINS if (expectedSR == SpatialRelation.INTERSECTS && shape is Core.Shapes.IPoint) { expectedSR = SpatialRelation.CONTAINS; } AssertRelation(null, expectedSR, POLY_SHAPE, shape); if (ctx.IsGeo) { //shift shape, set to shape2 IShape shape2; if (shape is IRectangle) { IRectangle r = (IRectangle)shape; shape2 = MakeNormRect(r.MinX + DL_SHIFT, r.MaxX + DL_SHIFT, r.MinY, r.MaxY); } else if (shape is Core.Shapes.IPoint) { Core.Shapes.IPoint p = (Core.Shapes.IPoint)shape; shape2 = ctx.MakePoint(base.NormX(p.X + DL_SHIFT), p.Y); } else { throw new Exception("" + shape); } AssertRelation(null, expectedSR, POLY_SHAPE_DL, shape2); } }
#pragma warning disable xUnit1013 public virtual void TestRelationsImpl(bool prepare) #pragma warning restore xUnit1013 { Debug.Assert(!((NtsWktShapeParser)ctx.WktShapeParser).IsAutoIndex); //base polygon NtsGeometry @base = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((0 0, 10 0, 5 5, 0 0))"); //shares only "10 0" with base NtsGeometry polyI = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((10 0, 20 0, 15 5, 10 0))"); //within base: differs from base by one point is within NtsGeometry polyW = (NtsGeometry)ctx.ReadShapeFromWkt("POLYGON((0 0, 9 0, 5 5, 0 0))"); //a boundary point of base Core.Shapes.IPoint pointB = ctx.MakePoint(0, 0); //a shared boundary line of base NtsGeometry lineB = (NtsGeometry)ctx.ReadShapeFromWkt("LINESTRING(0 0, 10 0)"); //a line sharing only one point with base NtsGeometry lineI = (NtsGeometry)ctx.ReadShapeFromWkt("LINESTRING(10 0, 20 0)"); if (prepare) { @base.Index(); } AssertRelation(SpatialRelation.CONTAINS, @base, @base);//preferred result as there is no EQUALS AssertRelation(SpatialRelation.INTERSECTS, @base, polyI); AssertRelation(SpatialRelation.CONTAINS, @base, polyW); AssertRelation(SpatialRelation.CONTAINS, @base, pointB); AssertRelation(SpatialRelation.CONTAINS, @base, lineB); AssertRelation(SpatialRelation.INTERSECTS, @base, lineI); if (prepare) { lineB.Index(); } AssertRelation(SpatialRelation.CONTAINS, lineB, lineB);//line contains itself AssertRelation(SpatialRelation.CONTAINS, lineB, pointB); }
protected override IShape GenerateRandomShape(Core.Shapes.IPoint nearP) { IRectangle nearR = RandomRectangle(nearP); int numPoints = 2 + random.Next(3 + 1);//2-5 points List <Core.Shapes.IPoint> points = new List <Core.Shapes.IPoint>(numPoints); while (points.Count < numPoints) { points.Add(RandomPointIn(nearR)); } double maxBuf = Math.Max(nearR.Width, nearR.Height); double buf = Math.Abs(RandomGaussian()) * maxBuf / 4; buf = random.Next((int)Divisible(buf)); return(new BufferedLineString(points, buf, ctx)); }
public void TestRelateWithRectangle() { //counters for the different intersection cases int i_C = 0, i_I = 0, i_W = 0, i_D = 0, i_bboxD = 0; int laps = 0; int MINLAPSPERCASE = AtLeast(20); while (i_C < MINLAPSPERCASE || i_I < MINLAPSPERCASE || i_W < MINLAPSPERCASE || i_D < MINLAPSPERCASE || i_bboxD < MINLAPSPERCASE) { laps++; //TestLog.Clear(); Core.Shapes.IPoint nearP = RandomPointIn(ctx.WorldBounds); IShape s = GenerateRandomShape(nearP); IRectangle r = RandomRectangle(s.BoundingBox.Center); SpatialRelation ic = s.Relate(r); //TestLog.Log("S-R Rel: {}, Shape {}, Rectangle {}", ic, s, r); try { switch (ic) { case SpatialRelation.CONTAINS: i_C++; for (int j = 0; j < AtLeast(10); j++) { Core.Shapes.IPoint p = RandomPointIn(r); AssertRelation(null, SpatialRelation.CONTAINS, s, p); } break; case SpatialRelation.WITHIN: i_W++; for (int j = 0; j < AtLeast(10); j++) { Core.Shapes.IPoint p = RandomPointIn(s); AssertRelation(null, SpatialRelation.CONTAINS, r, p); } break; case SpatialRelation.DISJOINT: if (!s.BoundingBox.Relate(r).Intersects()) { //bboxes are disjoint i_bboxD++; if (i_bboxD > MINLAPSPERCASE) { break; } } else { i_D++; } for (int j = 0; j < AtLeast(10); j++) { Core.Shapes.IPoint p = RandomPointIn(r); AssertRelation(null, SpatialRelation.DISJOINT, s, p); } break; case SpatialRelation.INTERSECTS: i_I++; SpatialRelation?pointR = null;//set once IRectangle randomPointSpace = null; int MAX_TRIES = 1000; for (int j = 0; j < MAX_TRIES; j++) { Core.Shapes.IPoint p; if (j < 4) { p = new Core.Shapes.Impl.Point(0, 0, ctx); InfBufLine.CornerByQuadrant(r, j + 1, p); } else { if (randomPointSpace == null) { if (pointR == SpatialRelation.DISJOINT) { randomPointSpace = IntersectRects(r, s.BoundingBox); } else { //CONTAINS randomPointSpace = r; } } p = RandomPointIn(randomPointSpace); } SpatialRelation pointRNew = s.Relate(p); if (pointR == null) { pointR = pointRNew; } else if (pointR != pointRNew) { break; } else if (j >= MAX_TRIES) { //TODO consider logging instead of failing Assert.True(false, "Tried intersection brute-force too many times without success"); } } break; default: Assert.True(false, "" + ic); break; } } catch (/*AssertionError*/ Exception e) // TODO: Is there an exception to catch here?? { OnAssertFail(e, s, r, ic); } if (laps > MINLAPSPERCASE * 1000) { Assert.True(false, "Did not find enough intersection cases in a reasonable number" + " of random attempts. CWIDbD: " + i_C + "," + i_W + "," + i_I + "," + i_D + "," + i_bboxD + " Laps exceeded " + MINLAPSPERCASE * 1000); } } Console.WriteLine("Laps: " + laps + " CWIDbD: " + i_C + "," + i_W + "," + i_I + "," + i_D + "," + i_bboxD); }
protected abstract IShape GenerateRandomShape(Core.Shapes.IPoint nearP);