1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.routing.spatial;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Random;
18 import org.simantics.g2d.routing.algorithm1.Rectangle;
21 * 2-dimensional R-tree
25 public static final int MAX_ENTRIES = 7;
26 public static final int MIN_ENTRIES = 3;
27 public static final int MAX_ENTRIES_AFTER_SPLIT = MAX_ENTRIES - MIN_ENTRIES + 1;
29 static interface INode {
30 void search(Rectangle rect, IRectangleProcedure proc);
31 boolean intersects(Rectangle rect);
32 Split add(Rectangle rect, Object value);
34 double[] getRectangles();
35 Object[] getChildren();
36 void setCount(int count);
38 Rectangle findBounds();
42 double oldX0, oldY0, oldX1, oldY1;
43 double newX0, newY0, newX1, newY1;
46 public Split(double oldX0, double oldY0, double oldX1, double oldY1,
47 double newX0, double newY0, double newX1, double newY1,
57 this.newNode = newNode;
62 class Node implements INode {
63 double[] rectangles = new double[MAX_ENTRIES*4];
64 INode[] children = new INode[MAX_ENTRIES];
67 public void search(Rectangle rect, IRectangleProcedure proc) {
72 for(int i=0,ad=0;i<count;++i,ad+=4)
73 if(x1 >= rectangles[ad] && x0 <= rectangles[ad+2] &&
74 y1 >= rectangles[ad+1] && y0 <= rectangles[ad+3])
75 children[i].search(rect, proc);
78 public boolean intersects(Rectangle rect) {
83 for(int i=0,ad=0;i<count;++i,ad+=4)
84 if(x1 >= rectangles[ad] && x0 <= rectangles[ad+2] &&
85 y1 >= rectangles[ad+1] && y0 <= rectangles[ad+3])
86 if(children[i].intersects(rect))
92 public Split add(Rectangle rect, Object value) {
99 double bestArea = 0.0;
100 double bestAddition = Double.POSITIVE_INFINITY;
101 for(int i=0,ad=0;i<count;++i) {
102 double x0b = rectangles[ad++];
103 double y0b = rectangles[ad++];
104 double x1b = rectangles[ad++];
105 double y1b = rectangles[ad++];
106 double area = (x1b-x0b)*(y1b-y0b);
107 if(x0 < x0b) x0b = x0;
108 if(y0 < y0b) y0b = y0;
109 if(x1 > x1b) x1b = x1;
110 if(y1 > y1b) y1b = y1;
111 double addition = (x1b-x0b)*(y1b-y0b) - area;
112 if(addition < bestAddition || (addition==bestAddition && area < bestArea)) {
115 bestAddition = addition;
118 Split split = children[bestId].add(rect, value);
120 if(bestAddition > 0.0) {
122 rectangles[ad] = Math.min(rectangles[ad], x0); ++ad;
123 rectangles[ad] = Math.min(rectangles[ad], y0); ++ad;
124 rectangles[ad] = Math.max(rectangles[ad], x1); ++ad;
125 rectangles[ad] = Math.max(rectangles[ad], y1);
130 rectangles[ad] = split.oldX0; ++ad;
131 rectangles[ad] = split.oldY0; ++ad;
132 rectangles[ad] = split.oldX1; ++ad;
133 rectangles[ad] = split.oldY1;
134 if(count < children.length) {
136 rectangles[ad] = split.newX0; ++ad;
137 rectangles[ad] = split.newY0; ++ad;
138 rectangles[ad] = split.newX1; ++ad;
139 rectangles[ad] = split.newY1;
140 children[count++] = split.newNode;
143 Node newNode = new Node();
144 findNodeSeeds(rectangles, children,
145 new Rectangle(split.newX0, split.newY0, split.newX1, split.newY1),
146 split.newNode, newNode.rectangles, newNode.children);
147 return splitNode(this, newNode);
155 public Object[] getChildren() {
160 public double[] getRectangles() {
165 public void setCount(int count) {
170 public Rectangle findBounds() {
171 double x0 = Double.POSITIVE_INFINITY;
172 double y0 = Double.POSITIVE_INFINITY;
173 double x1 = Double.NEGATIVE_INFINITY;
174 double y1 = Double.NEGATIVE_INFINITY;
175 for(int i=0;i<count;++i) {
177 double x0a = rectangles[ad++];
178 double y0a = rectangles[ad++];
179 double x1a = rectangles[ad++];
180 double y1a = rectangles[ad++];
181 Rectangle rect = children[i].findBounds();
183 throw new RuntimeException("x0");
185 throw new RuntimeException("y0");
187 throw new RuntimeException("x1");
189 throw new RuntimeException("y1");
190 x0 = Math.min(x0, x0a);
191 y0 = Math.min(y0, y0a);
192 x1 = Math.max(x1, x1a);
193 y1 = Math.max(y1, y1a);
195 return new Rectangle(x0, y0, x1, y1);
199 class Leaf implements INode {
200 double[] rectangles = new double[MAX_ENTRIES*4];
201 Object[] children = new Object[MAX_ENTRIES];
204 public void search(Rectangle rect, IRectangleProcedure proc) {
209 for(int i=0,ad=0;i<count;++i,ad+=4)
210 if(x1 >= rectangles[ad] && x0 <= rectangles[ad+2] &&
211 y1 >= rectangles[ad+1] && y0 <= rectangles[ad+3])
213 rectangles[ad], rectangles[ad+1],
214 rectangles[ad+2], rectangles[ad+3],
218 public boolean intersects(Rectangle rect) {
223 for(int i=0,ad=0;i<count;++i,ad+=4)
224 if(x1 >= rectangles[ad] && x0 <= rectangles[ad+2] &&
225 y1 >= rectangles[ad+1] && y0 <= rectangles[ad+3])
231 public Split add(Rectangle rect, Object value) {
232 if(count < children.length) {
234 rectangles[ad++] = rect.x0;
235 rectangles[ad++] = rect.y0;
236 rectangles[ad++] = rect.x1;
237 rectangles[ad] = rect.y1;
238 children[count++] = value;
242 Leaf newLeaf = new Leaf();
243 findNodeSeeds(rectangles, children, rect, value, newLeaf.rectangles, newLeaf.children);
244 return splitNode(this, newLeaf);
249 public Object[] getChildren() {
254 public double[] getRectangles() {
259 public void setCount(int count) {
264 public Rectangle findBounds() {
265 double x0 = Double.POSITIVE_INFINITY;
266 double y0 = Double.POSITIVE_INFINITY;
267 double x1 = Double.NEGATIVE_INFINITY;
268 double y1 = Double.NEGATIVE_INFINITY;
269 for(int i=0;i<count;++i) {
271 double x0a = rectangles[ad++];
272 double y0a = rectangles[ad++];
273 double x1a = rectangles[ad++];
274 double y1a = rectangles[ad++];
275 x0 = Math.min(x0, x0a);
276 y0 = Math.min(y0, y0a);
277 x1 = Math.max(x1, x1a);
278 y1 = Math.max(y1, y1a);
280 return new Rectangle(x0, y0, x1, y1);
284 static Split splitNode(INode a, INode b) {
285 double[] aRectangles = a.getRectangles();
286 Object[] aValues = a.getChildren();
287 double[] bRectangles = b.getRectangles();
288 Object[] bValues = b.getChildren();
289 double x0a = aRectangles[0];
290 double y0a = aRectangles[1];
291 double x1a = aRectangles[2];
292 double y1a = aRectangles[3];
293 double areaA = (x1a-x0a) * (y1a-y0a);
294 double x0b = bRectangles[0];
295 double y0b = bRectangles[1];
296 double x1b = bRectangles[2];
297 double y1b = bRectangles[3];
298 double areaB = (x1b-x0b) * (y1b-y0b);
299 int aCount = 1, bCount = 1;
301 int firstUnassigned = 1;
303 while(firstUnassigned < aValues.length) {
305 double bestDiff = Double.NEGATIVE_INFINITY;
306 boolean bestToB = false;
307 for(int i=firstUnassigned,ad=firstUnassigned*4;i<aValues.length;++i) {
308 double x0 = aRectangles[ad++];
309 double y0 = aRectangles[ad++];
310 double x1 = aRectangles[ad++];
311 double y1 = aRectangles[ad++];
313 (Math.max(x1a, x1) - Math.min(x0a, x0)) * (Math.max(y1a, y1) - Math.min(y0a, y0)) - areaA;
315 (Math.max(x1b, x1) - Math.min(x0b, x0)) * (Math.max(y1b, y1) - Math.min(y0b, y0)) - areaB;
316 double diff = Math.abs(da-db);
317 if(diff > bestDiff) {
320 bestToB = db < da || (db==da && bCount < aCount);
325 rotate2(bRectangles, bValues, bCount, aRectangles, aValues, firstUnassigned, bestI);
326 x0b = Math.min(x0b, bRectangles[4*bCount]);
327 y0b = Math.min(y0b, bRectangles[4*bCount+1]);
328 x1b = Math.max(x1b, bRectangles[4*bCount+2]);
329 y1b = Math.max(y1b, bRectangles[4*bCount+3]);
332 if(bCount == MAX_ENTRIES_AFTER_SPLIT) {
333 while(firstUnassigned < aValues.length) {
334 move(aRectangles, aValues, aCount, firstUnassigned);
335 x0a = Math.min(x0a, aRectangles[4*aCount]);
336 y0a = Math.min(y0a, aRectangles[4*aCount+1]);
337 x1a = Math.max(x1a, aRectangles[4*aCount+2]);
338 y1a = Math.max(y1a, aRectangles[4*aCount+3]);
346 //System.out.println(aRectangles.length + " " + aValues.length + " " + aCount + " " + bCount + " " + firstUnassigned + " " + bestI);
347 rotate(aRectangles, aValues, aCount, firstUnassigned, bestI);
348 x0a = Math.min(x0a, aRectangles[4*aCount]);
349 y0a = Math.min(y0a, aRectangles[4*aCount+1]);
350 x1a = Math.max(x1a, aRectangles[4*aCount+2]);
351 y1a = Math.max(y1a, aRectangles[4*aCount+3]);
354 if(aCount == MAX_ENTRIES_AFTER_SPLIT) {
355 while(firstUnassigned < aValues.length) {
356 move2(bRectangles, bValues, bCount, aRectangles, aValues, firstUnassigned);
357 x0b = Math.min(x0b, bRectangles[4*bCount]);
358 y0b = Math.min(y0b, bRectangles[4*bCount+1]);
359 x1b = Math.max(x1b, bRectangles[4*bCount+2]);
360 y1b = Math.max(y1b, bRectangles[4*bCount+3]);
371 return new Split(x0a, y0a, x1a, y1a, x0b, y0b, x1b, y1b, b);
374 static void move(double[] array, Object[] oArray, int a, int b) {
376 oArray[a] = oArray[b];
381 array[a++] = array[b++];
384 static void move2(double[] array, Object[] oArray, int a, double[] array2, Object[] oArray2, int b) {
385 oArray[a] = oArray2[b];
390 array[a++] = array2[b++];
393 static void rotate(double[] array, Object[] oArray, int a, int b, int c) {
398 Object temp = oArray[a];
399 oArray[a] = oArray[c];
405 for(int i=0;i<4;++i) {
406 double temp = array[a];
415 Object temp = oArray[a];
416 oArray[a] = oArray[c];
422 for(int i=0;i<4;++i) {
423 double temp = array[a];
431 Object temp = oArray[b];
432 oArray[b] = oArray[a];
433 oArray[a] = oArray[c];
440 for(int i=0;i<4;++i) {
441 double temp = array[b];
450 static void rotate2(double[] array, Object[] oArray, int a, double[] array2, Object[] oArray2, int b, int c) {
452 oArray[a] = oArray2[c];
453 oArray2[c] = oArray2[b];
460 for(int i=0;i<4;++i) {
461 array[a] = array2[c];
462 array2[c] = array2[b];
468 static void findNodeSeeds(double[] rectangles, Object[] values, Rectangle rect, Object value,
469 double[] newRectangles, Object[] newValues) {
472 double bestInefficiency = Double.NEGATIVE_INFINITY;
473 for(int j=0;j<=rectangles.length;j+=4) {
474 double x0b, y0b, x1b, y1b;
475 if(j < rectangles.length) {
477 y0b = rectangles[j+1];
478 x1b = rectangles[j+2];
479 y1b = rectangles[j+3];
487 double areaB = (x1b-x0b) * (y1b-y0b);
489 double x0a = rectangles[i++];
490 double y0a = rectangles[i++];
491 double x1a = rectangles[i++];
492 double y1a = rectangles[i++];
493 double inefficiency =
494 (Math.max(x1a, x1b) - Math.min(x0a, x0b)) * (Math.max(y1a, y1b) - Math.min(y0a, y0b))
495 - (x1a-x0a) * (y1a-y0a) - areaB;
496 if(inefficiency > bestInefficiency) {
499 bestInefficiency = inefficiency;
503 if(bestJ==rectangles.length) {
504 newRectangles[0] = rect.x0;
505 newRectangles[1] = rect.y0;
506 newRectangles[2] = rect.x1;
507 newRectangles[3] = rect.y1;
508 newValues[0] = value;
511 newValues[0] = values[bestJ/4];
512 values[bestJ/4] = value;
514 newRectangles[0] = rectangles[bestJ++];
515 newRectangles[1] = rectangles[bestJ++];
516 newRectangles[2] = rectangles[bestJ++];
517 newRectangles[3] = rectangles[bestJ];
520 rectangles[bestJ++] = rect.x0;
521 rectangles[bestJ++] = rect.y0;
522 rectangles[bestJ++] = rect.x1;
523 rectangles[bestJ] = rect.y1;
528 Object temp2 = values[0];
529 values[0] = values[bestI/4];
530 values[bestI/4] = temp2;
533 temp = rectangles[0];
534 rectangles[0] = rectangles[bestI];
535 rectangles[bestI++] = temp;
536 temp = rectangles[1];
537 rectangles[1] = rectangles[bestI];
538 rectangles[bestI++] = temp;
539 temp = rectangles[2];
540 rectangles[2] = rectangles[bestI];
541 rectangles[bestI++] = temp;
542 temp = rectangles[3];
543 rectangles[3] = rectangles[bestI];
544 rectangles[bestI++] = temp;
548 INode root = new Leaf();
550 public void search(Rectangle rect, IRectangleProcedure proc) {
551 root.search(rect, proc);
554 public boolean intersects(Rectangle rect) {
555 return root.intersects(rect);
558 public void add(Rectangle rect, Object obj) {
559 Split split = root.add(rect, obj);
561 Node node = new Node();
562 node.rectangles[0] = split.oldX0;
563 node.rectangles[1] = split.oldY0;
564 node.rectangles[2] = split.oldX1;
565 node.rectangles[3] = split.oldY1;
566 node.rectangles[4] = split.newX0;
567 node.rectangles[5] = split.newY0;
568 node.rectangles[6] = split.newX1;
569 node.rectangles[7] = split.newY1;
570 node.children[0] = root;
571 node.children[1] = split.newNode;
578 static Random random = new Random();
580 public static Rectangle randomRectangle() {
581 double x = random.nextDouble();
582 double y = random.nextDouble();
583 double w = random.nextDouble()*0.1;
584 double h = random.nextDouble()*0.1;
585 return new Rectangle(x, y, x+w, y+h);
588 public static void main(String[] args) {
589 Collection<Rectangle> rects = new ArrayList<Rectangle>();
591 for(int i=0;i<1000;++i)
592 rects.add(randomRectangle());
594 RTree tree = new RTree();
595 for(Rectangle rect : rects) {
596 tree.add(rect, rect);
597 tree.root.findBounds();
602 for(int i=0;i<100;++i) {
603 final Rectangle q = randomRectangle();
606 for(Rectangle rect : rects)
607 if(q.intersects(rect))
610 final int[] iCount2 = new int[] { 0 };
611 tree.search(q, new IRectangleProcedure() {
614 public void call(double x0, double y0, double x1, double y1,
617 Rectangle r = (Rectangle)value;
619 System.out.println("Not really intersecting!");
624 System.out.println("Value and key differ!" +
625 "(" + x0 + " " + y0 + " " + x1 + " " + y1 + ") " +
626 "(" + r.x0 + " " + r.y0 + " " + r.x1 + " " + r.y1 + ")");
630 if(iCount != iCount2[0])
631 System.out.println("Different iCount: " + iCount + " " + iCount2[0]);