public Point[] GetTranslations() { var nodes = _moveableRectanglesTree.GetAllLeaves(); Point[] translation = new Point[_moveableRectanglesTree.Count]; foreach (var n in nodes) { translation[n.rectId] = n.rect.Center - oldPositionsMoveable[n.rectId]; } return(translation); }
public void RTreeQuery_PointIntersectionTest() { for (int seed = 0; seed < 1; ++seed) { int n = 100000; var points = new Point[n]; var rand = new Random(seed); double scale = 1000; for (int i = 0; i < n; ++i) { points[i] = new Point(rand.NextDouble() * scale, rand.NextDouble() * scale); } var bsptree = new RTree<Point>( from p in points select new KeyValuePair<Rectangle, Point>(new Rectangle(p), p)); Assert.AreEqual(bsptree.GetAllLeaves().Count(), n); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(0, 0, scale, scale)).Count(), n); int intersecting = 0; for (int i = 0; i < 10000; ++i) { double s = scale / 100; var query = new Rectangle(rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s); if (bsptree.IsIntersecting(query)) { ++intersecting; } } System.Console.WriteLine(intersecting); } }
public void RTreeQuery_PointIntersectionTest() { for (int seed = 0; seed < 1; ++seed) { int n = 100000; var points = new Point[n]; var rand = new Random(seed); double scale = 1000; for (int i = 0; i < n; ++i) { points[i] = new Point(rand.NextDouble() * scale, rand.NextDouble() * scale); } var bsptree = new RTree <Point>( from p in points select new KeyValuePair <Rectangle, Point>(new Rectangle(p), p)); Assert.AreEqual(bsptree.GetAllLeaves().Count(), n); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(0, 0, scale, scale)).Count(), n); int intersecting = 0; for (int i = 0; i < 10000; ++i) { double s = scale / 100; var query = new Rectangle(rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s); if (bsptree.IsIntersecting(query)) { ++intersecting; } } System.Diagnostics.Debug.WriteLine(intersecting); } }
public void RTreeQuery_Rectangles() { const int Seeds = 100; const int RectCount = 1000; const int RegionSize = 1000; const int RectSize = 10; for (int seed = 0; seed < Seeds; ++seed) { var rects = new Rectangle[RectCount]; var rand = new Random(seed); for (int i = 0; i < RectCount; ++i) { rects[i] = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), new Point(RectSize, RectSize)); } var bsptree = new RTree <Rectangle>( from r in rects select new KeyValuePair <Rectangle, Rectangle>(r, r)); Assert.AreEqual(bsptree.GetAllLeaves().Count(), RectCount); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(0, 0, RegionSize + RectSize, RegionSize + RectSize)).Count(), RectCount); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0); var query = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize)); var checkList = (from r in rects where query.Intersects(r) select r).ToList(); var checkSet = new HashSet <Rectangle>(checkList); var result = bsptree.GetAllIntersecting(query).ToList(); Assert.AreEqual(result.Count, checkList.Count, "result and check are different sizes: seed={0}", seed); foreach (var r in result) { Assert.IsTrue(query.Intersects(r), "rect doesn't intersect query: seed={0}, rect={1}, query={2}", seed, r, query); Assert.IsTrue(checkSet.Contains(r), "check set does not contain rect: seed={0}", seed); } } }
void DrawFixedRectsSegments(RTree <Rectangle> fixedRectanglesTree, RTree <SymmetricSegment> fixedSegmentsTree) { foreach (var fr in fixedRectanglesTree.GetAllLeaves()) { DrawRectDilated(fr); } foreach (var seg in fixedSegmentsTree.GetAllLeaves()) { DrawLineSegDilated(seg.A, seg.B); } }
public void RTreeQuery_IncrementalRectangles() { const int RectsCount = 1000; const int RegionSize = 1000; const int RectSize = 10; for (int seed = 0; seed < 1; ++seed) { var rects = new Rectangle[RectsCount]; var rand = new Random(seed); for (int i = 0; i < RectsCount; ++i) { rects[i] = new Rectangle(new Point(rand.Next(RegionSize), rand.Next(RegionSize))); } // create rTree with just the first rectangle var l = new List <KeyValuePair <Rectangle, Rectangle> > { new KeyValuePair <Rectangle, Rectangle>(rects[0], rects[0]) }; var queryTree = new RTree <Rectangle>(l); // add remaining rectangles 10 at a time for (int a = 1, b = 10; b < RectsCount; a = b, b += 10) { for (int i = a; i < b; ++i) { queryTree.Add(rects[i], rects[i]); } Assert.AreEqual(queryTree.GetAllLeaves().Count(), b, "did we lose leaves?"); Assert.AreEqual(queryTree.GetAllIntersecting( new Rectangle(0, 0, RegionSize + RectSize, RegionSize + RectSize)).Count(), b, "are all leaves inside the max range?"); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0, "should be no leaves inside this rectangle!"); var query = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize)); var checkList = (from r in rects.Take(b) where query.Intersects(r) select r).ToList(); var checkSet = new HashSet <string>(checkList.Select(r => r.ToString())); var result = queryTree.GetAllIntersecting(query).ToList(); Assert.AreEqual(result.Count, checkList.Count, "result and check are different sizes: seed={0}", seed); foreach (var r in result) { Assert.IsTrue(query.Intersects(r), "rect doesn't intersect query: seed={0}, rect={1}, query={2}", seed, r, query); Assert.IsTrue(checkSet.Contains(r.ToString()), "check set does not contain rect: seed={0}", seed); } } } }
//public void CreateRailTree() { // RailTree = new RTree<Rail,Point>( // RailDictionary.Values.Select(rail => new KeyValuePair<Rectangle, Rail>(rail.BoundingBox, rail))); //} /// <summary> /// get endpoint tuples of all rails /// </summary> /// <returns></returns> public List <SymmetricSegment> GetAllRailsEndpoints() { var endpts = new List <SymmetricSegment>(); Point p0, p1; foreach (var rail in _railTree.GetAllLeaves()) { if (rail.GetStartEnd(out p0, out p1)) { endpts.Add(new SymmetricSegment(p0, p1)); } } return(endpts); }
public void RTreeQuery_RandomPoints() { var bspQueryTime = new Stopwatch(); var checkQueryTime = new Stopwatch(); const int Seeds = 5; const int Repeats = 5; for (int seed = 0; seed < Seeds; ++seed) { int n = 100000; var points = new Point[n]; var rand = new Random(seed); double scale = 1000; for (int i = 0; i < n; ++i) { points[i] = new Point(rand.NextDouble() * scale, rand.NextDouble() * scale); } var queryTree = new RTree <Point>( from p in points select new KeyValuePair <Rectangle, Point>(new Rectangle(p), p)); Assert.AreEqual(queryTree.GetAllLeaves().Count(), n); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(0, 0, scale, scale)).Count(), n); for (int i = 0; i < Repeats; ++i) { double s = scale / 100; var query = new Rectangle(rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s); bspQueryTime.Start(); var result = queryTree.GetAllIntersecting(query).ToList(); bspQueryTime.Stop(); checkQueryTime.Start(); var checkList = (from p in points where query.Contains(p) select p).ToList(); checkQueryTime.Stop(); var checkSet = new HashSet <Point>(checkList); Assert.AreEqual(result.Count, checkList.Count); foreach (var r in result) { Assert.IsTrue(query.Contains(r)); Assert.IsTrue(checkSet.Contains(r)); } } Assert.IsTrue(bspQueryTime.ElapsedMilliseconds < checkQueryTime.ElapsedMilliseconds); } }
internal IEnumerable <Point> Calculate() { if (BoundingBox.Width == 0) { FillBoundingBoxWithSites(); } Cdt cdt = new Cdt(cites, null, null); cdt.Run(); var triangles = cdt.GetTriangles(); foreach (var triangle in triangles) { AddVoronoiCite(triangle); } return(voronoiSiteTree.GetAllLeaves()); }
public void RTreeQuery_IncrementalRectangles() { const int RectsCount = 1000; const int RegionSize = 1000; const int RectSize = 10; var rects = new Rectangle[RectsCount]; var rand = new Random(0); for (int i = 0; i < RectsCount; ++i) { rects[i] = new Rectangle(new Point(rand.Next(RegionSize), rand.Next(RegionSize))); } // create rTree with just the first rectangle var rtree = new RTree <Rectangle, Point>(); rtree.Add(rects[0], rects[0]); // add remaining rectangles 10 at a time for (int a = 1, b = 10; b < RectsCount; a = b, b += 10) { for (int i = a; i < b; ++i) { rtree.Add(rects[i], rects[i]); } var allLeaves = rtree.GetAllLeaves().ToArray(); Assert.AreEqual(allLeaves.Length, b, "did we lose leaves?"); Assert.AreEqual(rtree.GetAllIntersecting( new Rectangle(0, 0, RegionSize + RectSize, RegionSize + RectSize)).Count(), b, "are all leaves inside the max range?"); Assert.AreEqual(rtree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0, "should be no leaves inside this rectangle!"); var rect = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize)); var checkList = (from r in rects.Take(b) where rect.Intersects(r) select r).ToList(); var checkSet = new HashSet <string>(checkList.Select(r => r.ToString())); var result = rtree.GetAllIntersecting(rect).ToList(); Assert.AreEqual(result.Count, checkList.Count, "result and check are different sizes"); foreach (var r in result) { Assert.IsTrue(rect.Intersects(r), "rect doesn't intersect query: rect={1}, query={2}", r, rect); Assert.IsTrue(checkSet.Contains(r.ToString()), "check set does not contain rect"); } } }
public void RTreeQuery_RandomPoints() { var bspQueryTime = new Stopwatch(); var checkQueryTime = new Stopwatch(); const int Seeds = 5; const int Repeats = 5; for (int seed = 0; seed < Seeds; ++seed) { int n = 100000; var points = new Point[n]; var rand = new Random(seed); double scale = 1000; for (int i = 0; i < n; ++i) { points[i] = new Point(rand.NextDouble() * scale, rand.NextDouble() * scale); } var queryTree = new RTree<Point>( from p in points select new KeyValuePair<Rectangle, Point>(new Rectangle(p), p)); Assert.AreEqual(queryTree.GetAllLeaves().Count(), n); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(0, 0, scale, scale)).Count(), n); for (int i = 0; i < Repeats; ++i) { double s = scale / 100; var query = new Rectangle(rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s, rand.NextDouble() * s); bspQueryTime.Start(); var result = queryTree.GetAllIntersecting(query).ToList(); bspQueryTime.Stop(); checkQueryTime.Start(); var checkList = (from p in points where query.Contains(p) select p).ToList(); checkQueryTime.Stop(); var checkSet = new HashSet<Point>(checkList); Assert.AreEqual(result.Count, checkList.Count); foreach (var r in result) { Assert.IsTrue(query.Contains(r)); Assert.IsTrue(checkSet.Contains(r)); } } Assert.IsTrue(bspQueryTime.ElapsedMilliseconds < checkQueryTime.ElapsedMilliseconds); } }
public void RTreeQuery_IncrementalRectangles() { const int RectsCount = 1000; const int RegionSize = 1000; const int RectSize = 10; for (int seed = 0; seed < 1; ++seed) { var rects = new Rectangle[RectsCount]; var rand = new Random(seed); for (int i = 0; i < RectsCount; ++i) { rects[i] = new Rectangle(new Point(rand.Next(RegionSize), rand.Next(RegionSize))); } // create rTree with just the first rectangle var l = new List<KeyValuePair<Rectangle, Rectangle>> { new KeyValuePair<Rectangle, Rectangle>(rects[0], rects[0]) }; var queryTree = new RTree<Rectangle>(l); // add remaining rectangles 10 at a time for (int a = 1, b = 10; b < RectsCount; a = b, b += 10) { for (int i = a; i < b; ++i) { queryTree.Add(rects[i], rects[i]); } Assert.AreEqual(queryTree.GetAllLeaves().Count(), b, "did we lose leaves?"); Assert.AreEqual(queryTree.GetAllIntersecting( new Rectangle(0, 0, RegionSize + RectSize, RegionSize + RectSize)).Count(), b, "are all leaves inside the max range?"); Assert.AreEqual(queryTree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0, "should be no leaves inside this rectangle!"); var query = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize)); var checkList = (from r in rects.Take(b) where query.Intersects(r) select r).ToList(); var checkSet = new HashSet<Rectangle>(checkList); var result = queryTree.GetAllIntersecting(query).ToList(); Assert.AreEqual(result.Count, checkList.Count, "result and check are different sizes: seed={0}", seed); foreach (var r in result) { Assert.IsTrue(query.Intersects(r), "rect doesn't intersect query: seed={0}, rect={1}, query={2}", seed, r, query); Assert.IsTrue(checkSet.Contains(r), "check set does not contain rect: seed={0}", seed); } } } }
public void RTreeQuery_Rectangles() { const int Seeds = 100; const int RectCount = 1000; const int RegionSize = 1000; const int RectSize = 10; for (int seed = 0; seed < Seeds; ++seed) { var rects = new Rectangle[RectCount]; var rand = new Random(seed); for (int i = 0; i < RectCount; ++i) { rects[i] = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), new Point(RectSize, RectSize)); } var bsptree = new RTree<Rectangle>( from r in rects select new KeyValuePair<Rectangle, Rectangle>(r, r)); Assert.AreEqual(bsptree.GetAllLeaves().Count(), RectCount); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(0, 0, RegionSize + RectSize, RegionSize + RectSize)).Count(), RectCount); Assert.AreEqual(bsptree.GetAllIntersecting(new Rectangle(-2, -2, -1, -1)).Count(), 0); var query = new Rectangle(rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize), rand.Next(RegionSize)); var checkList = (from r in rects where query.Intersects(r) select r).ToList(); var checkSet = new HashSet<Rectangle>(checkList); var result = bsptree.GetAllIntersecting(query).ToList(); Assert.AreEqual(result.Count, checkList.Count, "result and check are different sizes: seed={0}", seed); foreach (var r in result) { Assert.IsTrue(query.Intersects(r), "rect doesn't intersect query: seed={0}, rect={1}, query={2}", seed, r, query); Assert.IsTrue(checkSet.Contains(r), "check set does not contain rect: seed={0}", seed); } } }
void DrawFixedRectsSegments(RTree<Rectangle> fixedRectanglesTree, RTree<SymmetricSegment> fixedSegmentsTree) { foreach (var fr in fixedRectanglesTree.GetAllLeaves()) DrawRectDilated(fr); foreach (var seg in fixedSegmentsTree.GetAllLeaves()) { DrawLineSegDilated(seg.A, seg.B); } }