public static void TestIntervalTree_RandomConstruction() { int pointcount = 0; int passcount = 1000; int figureSize = 50; com.epl.geometry.Envelope env = new com.epl.geometry.Envelope(); env.SetCoords(-10000, -10000, 10000, 10000); com.epl.geometry.RandomCoordinateGenerator generator = new com.epl.geometry.RandomCoordinateGenerator(System.Math.Max(figureSize, 10000), env, 0.001); System.Random random = new System.Random(2013); int rand_max = 98765; System.Collections.Generic.List <com.epl.geometry.Envelope1D> intervals = new System.Collections.Generic.List <com.epl.geometry.Envelope1D>(); com.epl.geometry.AttributeStreamOfInt8 intervalsFound = new com.epl.geometry.AttributeStreamOfInt8(0); for (int i = 0; i < passcount; i++) { int r = figureSize; if (r < 3) { continue; } com.epl.geometry.Polygon poly = new com.epl.geometry.Polygon(); 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 = generator.GetRandomCoord(); if (j == 0 || bRandomNew) { poly.StartPath(pt); } else { poly.LineTo(pt); } } { intervals.Clear(); com.epl.geometry.SegmentIterator seg_iter = poly.QuerySegmentIterator(); com.epl.geometry.Envelope1D interval; com.epl.geometry.Envelope1D range = poly.QueryInterval(com.epl.geometry.VertexDescription.Semantics.POSITION, 0); range.vmin -= 0.01; range.vmax += 0.01; while (seg_iter.NextPath()) { while (seg_iter.HasNextSegment()) { com.epl.geometry.Segment segment = seg_iter.NextSegment(); interval = segment.QueryInterval(com.epl.geometry.VertexDescription.Semantics.POSITION, 0); intervals.Add(interval); } } intervalsFound.Resize(intervals.Count, 0); // Just test construction for assertions com.epl.geometry.IntervalTreeImpl intervalTree = new com.epl.geometry.IntervalTreeImpl(true); Construct(intervalTree, intervals); for (int j_1 = 0; j_1 < intervals.Count; j_1++) { intervalTree.Insert(j_1); } com.epl.geometry.IntervalTreeImpl.IntervalTreeIteratorImpl iterator = intervalTree.GetIterator(range, 0.0); int count = 0; int handle; while ((handle = iterator.Next()) != -1) { count++; intervalsFound.Write(handle, unchecked ((byte)1)); } NUnit.Framework.Assert.IsTrue(count == intervals.Count); for (int j_2 = 0; j_2 < intervalsFound.Size(); j_2++) { interval = intervals[j_2]; NUnit.Framework.Assert.IsTrue(intervalsFound.Read(j_2) == 1); } for (int j_3 = 0; j_3 < intervals.Count >> 1; j_3++) { intervalTree.Remove(j_3); } iterator.ResetIterator(range, 0.0); count = 0; while ((handle = iterator.Next()) != -1) { count++; intervalsFound.Write(handle, unchecked ((byte)1)); } NUnit.Framework.Assert.IsTrue(count == intervals.Count - (intervals.Count >> 1)); for (int j_4 = (intervals.Count >> 1); j_4 < intervals.Count; j_4++) { intervalTree.Remove(j_4); } } } }
/// <exception cref="System.Exception"/> private static com.epl.geometry.Geometry ImportFromJsonMultiPath(bool b_polygon, com.epl.geometry.JsonReader parser, com.epl.geometry.AttributeStreamOfDbl @as, com.epl.geometry.AttributeStreamOfDbl bs) { if (parser.CurrentToken() != com.epl.geometry.JsonReader.Token.START_ARRAY) { throw new com.epl.geometry.GeometryException("failed to parse multipath: array of array of vertices is expected"); } com.epl.geometry.MultiPath multipath; if (b_polygon) { multipath = new com.epl.geometry.Polygon(); } else { multipath = new com.epl.geometry.Polyline(); } com.epl.geometry.AttributeStreamOfInt32 parts = (com.epl.geometry.AttributeStreamOfInt32)com.epl.geometry.AttributeStreamBase.CreateIndexStream(0); com.epl.geometry.AttributeStreamOfDbl position = (com.epl.geometry.AttributeStreamOfDbl)com.epl.geometry.AttributeStreamBase.CreateDoubleStream(2, 0); com.epl.geometry.AttributeStreamOfInt8 pathFlags = (com.epl.geometry.AttributeStreamOfInt8)com.epl.geometry.AttributeStreamBase.CreateByteStream(0); // set up min max variables double[] buf = new double[4]; double[] start = new double[4]; int point_count = 0; int path_count = 0; byte pathFlag = b_polygon ? unchecked ((byte)com.epl.geometry.PathFlags.enumClosed) : 0; int requiredSize = b_polygon ? 3 : 2; // At start of rings while (parser.NextToken() != com.epl.geometry.JsonReader.Token.END_ARRAY) { if (parser.CurrentToken() != com.epl.geometry.JsonReader.Token.START_ARRAY) { throw new com.epl.geometry.GeometryException("failed to parse multipath: ring/path array is expected"); } int pathPointCount = 0; bool b_first = true; int sz = 0; int szstart = 0; parser.NextToken(); while (parser.CurrentToken() != com.epl.geometry.JsonReader.Token.END_ARRAY) { if (parser.CurrentToken() != com.epl.geometry.JsonReader.Token.START_ARRAY) { throw new com.epl.geometry.GeometryException("failed to parse multipath: array is expected, rings/paths vertices consist of arrays of cooridinates"); } sz = 0; while (parser.NextToken() != com.epl.geometry.JsonReader.Token.END_ARRAY) { buf[sz++] = ReadDouble(parser); } if (sz < 2) { throw new com.epl.geometry.GeometryException("failed to parse multipath: each vertex array has to have at least 2 elements"); } parser.NextToken(); do { if (position.Size() == point_count * 2) { int c = point_count * 3; if (c % 2 != 0) { c++; } // have to be even if (c < 8) { c = 8; } else { if (c < 32) { c = 32; } } position.Resize(c); } position.Write(2 * point_count, buf[0]); position.Write(2 * point_count + 1, buf[1]); if (@as.Size() == point_count) { int c = (point_count * 3) / 2; // have to be even if (c < 4) { c = 4; } else { if (c < 16) { c = 16; } } @as.Resize(c); } if (sz > 2) { @as.Write(point_count, buf[2]); } else { @as.Write(point_count, com.epl.geometry.NumberUtils.NaN()); } if (bs.Size() == point_count) { int c = (point_count * 3) / 2; // have to be even if (c < 4) { c = 4; } else { if (c < 16) { c = 16; } } bs.Resize(c); } if (sz > 3) { bs.Write(point_count, buf[3]); } else { bs.Write(point_count, com.epl.geometry.NumberUtils.NaN()); } if (b_first) { path_count++; parts.Add(point_count); pathFlags.Add(pathFlag); b_first = false; szstart = sz; start[0] = buf[0]; start[1] = buf[1]; start[2] = buf[2]; start[3] = buf[3]; } point_count++; pathPointCount++; }while (pathPointCount < requiredSize && parser.CurrentToken() == com.epl.geometry.JsonReader.Token.END_ARRAY); } if (b_polygon && pathPointCount > requiredSize && sz == szstart && start[0] == buf[0] && start[1] == buf[1] && start[2] == buf[2] && start[3] == buf[3]) { // remove the end point that is equal to the start point. point_count--; pathPointCount--; } if (pathPointCount == 0) { continue; } } // skip empty paths if (point_count != 0) { parts.Resize(path_count); pathFlags.Resize(path_count); if (point_count > 0) { parts.Add(point_count); pathFlags.Add(unchecked ((byte)0)); } com.epl.geometry.MultiPathImpl mp_impl = (com.epl.geometry.MultiPathImpl)multipath._getImpl(); mp_impl.SetAttributeStreamRef(com.epl.geometry.VertexDescription.Semantics.POSITION, position); mp_impl.SetPathFlagsStreamRef(pathFlags); mp_impl.SetPathStreamRef(parts); } return(multipath); }