public virtual void TestEnvelope2D_corners() { com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D(0, 1, 2, 3); NUnit.Framework.Assert.IsFalse(env.Equals(null)); NUnit.Framework.Assert.IsTrue(env.Equals((object)new com.epl.geometry.Envelope2D(0, 1, 2, 3))); com.epl.geometry.Point2D pt2D = env.GetLowerLeft(); NUnit.Framework.Assert.IsTrue(pt2D.Equals(com.epl.geometry.Point2D.Construct(0, 1))); pt2D = env.GetUpperLeft(); NUnit.Framework.Assert.IsTrue(pt2D.Equals(com.epl.geometry.Point2D.Construct(0, 3))); pt2D = env.GetUpperRight(); NUnit.Framework.Assert.IsTrue(pt2D.Equals(com.epl.geometry.Point2D.Construct(2, 3))); pt2D = env.GetLowerRight(); NUnit.Framework.Assert.IsTrue(pt2D.Equals(com.epl.geometry.Point2D.Construct(2, 1))); { com.epl.geometry.Point2D[] corners = new com.epl.geometry.Point2D[4]; env.QueryCorners(corners); NUnit.Framework.Assert.IsTrue(corners[0].Equals(com.epl.geometry.Point2D.Construct(0, 1))); NUnit.Framework.Assert.IsTrue(corners[1].Equals(com.epl.geometry.Point2D.Construct(0, 3))); NUnit.Framework.Assert.IsTrue(corners[2].Equals(com.epl.geometry.Point2D.Construct(2, 3))); NUnit.Framework.Assert.IsTrue(corners[3].Equals(com.epl.geometry.Point2D.Construct(2, 1))); env.QueryCorners(corners); NUnit.Framework.Assert.IsTrue(corners[0].Equals(env.QueryCorner(0))); NUnit.Framework.Assert.IsTrue(corners[1].Equals(env.QueryCorner(1))); NUnit.Framework.Assert.IsTrue(corners[2].Equals(env.QueryCorner(2))); NUnit.Framework.Assert.IsTrue(corners[3].Equals(env.QueryCorner(3))); } { com.epl.geometry.Point2D[] corners = new com.epl.geometry.Point2D[4]; env.QueryCornersReversed(corners); NUnit.Framework.Assert.IsTrue(corners[0].Equals(com.epl.geometry.Point2D.Construct(0, 1))); NUnit.Framework.Assert.IsTrue(corners[1].Equals(com.epl.geometry.Point2D.Construct(2, 1))); NUnit.Framework.Assert.IsTrue(corners[2].Equals(com.epl.geometry.Point2D.Construct(2, 3))); NUnit.Framework.Assert.IsTrue(corners[3].Equals(com.epl.geometry.Point2D.Construct(0, 3))); env.QueryCornersReversed(corners); NUnit.Framework.Assert.IsTrue(corners[0].Equals(env.QueryCorner(0))); NUnit.Framework.Assert.IsTrue(corners[1].Equals(env.QueryCorner(3))); NUnit.Framework.Assert.IsTrue(corners[2].Equals(env.QueryCorner(2))); NUnit.Framework.Assert.IsTrue(corners[3].Equals(env.QueryCorner(1))); } NUnit.Framework.Assert.IsTrue(env.GetCenter().Equals(com.epl.geometry.Point2D.Construct(1, 2))); NUnit.Framework.Assert.IsFalse(env.ContainsExclusive(env.GetUpperLeft())); NUnit.Framework.Assert.IsTrue(env.Contains(env.GetUpperLeft())); NUnit.Framework.Assert.IsTrue(env.ContainsExclusive(env.GetCenter())); }
public virtual void TestSerializeEnvelope2D() { try { java.io.ByteArrayOutputStream streamOut = new java.io.ByteArrayOutputStream(); java.io.ObjectOutputStream oo = new java.io.ObjectOutputStream(streamOut); com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D(1.213948734, 2.213948734, 11.213948734, 12.213948734); oo.WriteObject(env); System.IO.BinaryWriter streamIn = new System.IO.BinaryWriter(streamOut.ToByteArray()); java.io.ObjectInputStream ii = new java.io.ObjectInputStream(streamIn); com.epl.geometry.Envelope2D envRes = (com.epl.geometry.Envelope2D)ii.ReadObject(); NUnit.Framework.Assert.IsTrue(envRes.Equals(env)); } catch (System.Exception) { Fail("Envelope2D serialization failure"); } // try // { // FileOutputStream streamOut = new FileOutputStream( // "c:/temp/savedEnvelope2D.txt"); // ObjectOutputStream oo = new ObjectOutputStream(streamOut); // Envelope2D e = new Envelope2D(177.123, 188.234, 999.122, 888.999); // oo.writeObject(e); // } // catch(Exception ex) // { // fail("Envelope2D serialization failure"); // } try { java.io.InputStream s = typeof(com.epl.geometry.TestSerialization).GetResourceAsStream("savedEnvelope2D.txt"); java.io.ObjectInputStream ii = new java.io.ObjectInputStream(s); com.epl.geometry.Envelope2D e = (com.epl.geometry.Envelope2D)ii.ReadObject(); NUnit.Framework.Assert.IsTrue(e != null); NUnit.Framework.Assert.IsTrue(e.Equals(new com.epl.geometry.Envelope2D(177.123, 188.234, 999.122, 888.999))); } catch (System.Exception) { Fail("Envelope2D serialization failure"); } }
public static void TestQuadTreeWithDuplicates() { int pass_count = 10; int figure_size = 400; int figure_size2 = 100; com.epl.geometry.Envelope extent1 = new com.epl.geometry.Envelope(); extent1.SetCoords(-100000, -100000, 100000, 100000); com.epl.geometry.RandomCoordinateGenerator generator1 = new com.epl.geometry.RandomCoordinateGenerator(System.Math.Max(figure_size, 10000), extent1, 0.001); System.Random random = new System.Random(2013); int rand_max = 32; com.epl.geometry.Polygon poly_red = new com.epl.geometry.Polygon(); com.epl.geometry.Polygon poly_blue = new com.epl.geometry.Polygon(); int r = figure_size; for (int c = 0; c < pass_count; c++) { com.epl.geometry.Point pt; for (int j = 0; j < r; j++) { int rand = random.Next(rand_max); bool b_random_new = r > 10 && ((1.0 * rand) / rand_max > 0.95); pt = generator1.GetRandomCoord(); if (j == 0 || b_random_new) { poly_blue.StartPath(pt); } else { poly_blue.LineTo(pt); } } com.epl.geometry.Envelope2D env = new com.epl.geometry.Envelope2D(); com.epl.geometry.QuadTree quad_tree_blue = BuildQuadTree_((com.epl.geometry.MultiPathImpl)poly_blue._getImpl(), false); com.epl.geometry.QuadTree quad_tree_blue_duplicates = BuildQuadTree_((com.epl.geometry.MultiPathImpl)poly_blue._getImpl(), true); com.epl.geometry.Envelope2D e1 = quad_tree_blue.GetDataExtent(); com.epl.geometry.Envelope2D e2 = quad_tree_blue_duplicates.GetDataExtent(); NUnit.Framework.Assert.IsTrue(e1.Equals(e2)); NUnit.Framework.Assert.IsTrue(quad_tree_blue.GetElementCount() == poly_blue.GetSegmentCount()); com.epl.geometry.SegmentIterator seg_iter_blue = poly_blue.QuerySegmentIterator(); poly_red.SetEmpty(); r = figure_size2; if (r < 3) { continue; } for (int j_1 = 0; j_1 < r; j_1++) { int rand = random.Next(rand_max); bool b_random_new = r > 10 && ((1.0 * rand) / rand_max > 0.95); pt = generator1.GetRandomCoord(); if (j_1 == 0 || b_random_new) { poly_red.StartPath(pt); } else { poly_red.LineTo(pt); } } com.epl.geometry.QuadTree.QuadTreeIterator iterator = quad_tree_blue.GetIterator(); com.epl.geometry.SegmentIteratorImpl seg_iter_red = ((com.epl.geometry.MultiPathImpl)poly_red._getImpl()).QuerySegmentIterator(); System.Collections.Generic.Dictionary <int, bool> map1 = new System.Collections.Generic.Dictionary <int, bool>(0); int count = 0; int intersections_per_query = 0; while (seg_iter_red.NextPath()) { while (seg_iter_red.HasNextSegment()) { com.epl.geometry.Segment segment_red = seg_iter_red.NextSegment(); segment_red.QueryEnvelope2D(env); iterator.ResetIterator(env, 0.0); int count_upper = 0; int element_handle; while ((element_handle = iterator.Next()) != -1) { count_upper++; int index = quad_tree_blue.GetElement(element_handle); bool iter = map1.ContainsKey(index); if (!iter) { count++; map1[index] = true; } intersections_per_query++; } int intersection_count = quad_tree_blue.GetIntersectionCount(env, 0.0, -1); NUnit.Framework.Assert.IsTrue(intersection_count == count_upper); } } seg_iter_red.ResetToFirstPath(); System.Collections.Generic.Dictionary <int, bool> map2 = new System.Collections.Generic.Dictionary <int, bool>(0); com.epl.geometry.QuadTree.QuadTreeIterator iterator_duplicates = quad_tree_blue_duplicates.GetIterator(); int count_duplicates = 0; int intersections_per_query_duplicates = 0; while (seg_iter_red.NextPath()) { while (seg_iter_red.HasNextSegment()) { com.epl.geometry.Segment segment_red = seg_iter_red.NextSegment(); segment_red.QueryEnvelope2D(env); iterator_duplicates.ResetIterator(env, 0.0); int count_lower = 0; System.Collections.Generic.Dictionary <int, bool> map_per_query = new System.Collections.Generic.Dictionary <int, bool>(0); int count_upper = 0; int element_handle; while ((element_handle = iterator_duplicates.Next()) != -1) { count_upper++; int index = quad_tree_blue_duplicates.GetElement(element_handle); bool iter = map2.ContainsKey(index); if (!iter) { count_duplicates++; map2[index] = true; } bool iter_lower = map_per_query.ContainsKey(index); if (!iter_lower) { count_lower++; intersections_per_query_duplicates++; map_per_query[index] = true; } int q = quad_tree_blue_duplicates.GetQuad(element_handle); NUnit.Framework.Assert.IsTrue(quad_tree_blue_duplicates.GetSubTreeElementCount(q) >= quad_tree_blue_duplicates.GetContainedSubTreeElementCount(q)); } int intersection_count = quad_tree_blue_duplicates.GetIntersectionCount(env, 0.0, -1); bool b_has_data = quad_tree_blue_duplicates.HasData(env, 0.0); NUnit.Framework.Assert.IsTrue(b_has_data || intersection_count == 0); NUnit.Framework.Assert.IsTrue(count_lower <= intersection_count && intersection_count <= count_upper); NUnit.Framework.Assert.IsTrue(count_upper <= 4 * count_lower); } } NUnit.Framework.Assert.IsTrue(count == count_duplicates); NUnit.Framework.Assert.IsTrue(intersections_per_query == intersections_per_query_duplicates); } }