|
26 | 26 | * <p>Construction takes {@code O(n log n)} time for sorting and tree construction. |
27 | 27 | */ |
28 | 28 | final class ComponentTree implements Component2D { |
| 29 | + |
| 30 | + private static final Comparator<Component2D> XComparator = |
| 31 | + Comparator.comparingDouble(Component2D::getMinX).thenComparingDouble(Component2D::getMaxX); |
| 32 | + |
| 33 | + private static final Comparator<Component2D> YComparator = |
| 34 | + Comparator.comparingDouble(Component2D::getMinY).thenComparingDouble(Component2D::getMaxY); |
| 35 | + |
29 | 36 | /** minimum Y of this geometry's bounding box area */ |
30 | 37 | private double minY; |
31 | 38 |
|
@@ -94,9 +101,7 @@ public boolean contains(double x, double y) { |
94 | 101 | if (right != null |
95 | 102 | && ((splitX == false && y >= this.component.getMinY()) |
96 | 103 | || (splitX && x >= this.component.getMinX()))) { |
97 | | - if (right.contains(x, y)) { |
98 | | - return true; |
99 | | - } |
| 104 | + return right.contains(x, y); |
100 | 105 | } |
101 | 106 | } |
102 | 107 | return false; |
@@ -124,9 +129,7 @@ public boolean intersectsLine( |
124 | 129 | if (right != null |
125 | 130 | && ((splitX == false && maxY >= this.component.getMinY()) |
126 | 131 | || (splitX && maxX >= this.component.getMinX()))) { |
127 | | - if (right.intersectsLine(minX, maxX, minY, maxY, aX, aY, bX, bY)) { |
128 | | - return true; |
129 | | - } |
| 132 | + return right.intersectsLine(minX, maxX, minY, maxY, aX, aY, bX, bY); |
130 | 133 | } |
131 | 134 | } |
132 | 135 | return false; |
@@ -156,9 +159,7 @@ public boolean intersectsTriangle( |
156 | 159 | if (right != null |
157 | 160 | && ((splitX == false && maxY >= this.component.getMinY()) |
158 | 161 | || (splitX && maxX >= this.component.getMinX()))) { |
159 | | - if (right.intersectsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY)) { |
160 | | - return true; |
161 | | - } |
| 162 | + return right.intersectsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY); |
162 | 163 | } |
163 | 164 | } |
164 | 165 | return false; |
@@ -186,9 +187,7 @@ public boolean containsLine( |
186 | 187 | if (right != null |
187 | 188 | && ((splitX == false && maxY >= this.component.getMinY()) |
188 | 189 | || (splitX && maxX >= this.component.getMinX()))) { |
189 | | - if (right.containsLine(minX, maxX, minY, maxY, aX, aY, bX, bY)) { |
190 | | - return true; |
191 | | - } |
| 190 | + return right.containsLine(minX, maxX, minY, maxY, aX, aY, bX, bY); |
192 | 191 | } |
193 | 192 | } |
194 | 193 | return false; |
@@ -218,9 +217,7 @@ public boolean containsTriangle( |
218 | 217 | if (right != null |
219 | 218 | && ((splitX == false && maxY >= this.component.getMinY()) |
220 | 219 | || (splitX && maxX >= this.component.getMinX()))) { |
221 | | - if (right.containsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY)) { |
222 | | - return true; |
223 | | - } |
| 220 | + return right.containsTriangle(minX, maxX, minY, maxY, aX, aY, bX, bY, cX, cY); |
224 | 221 | } |
225 | 222 | } |
226 | 223 | return false; |
@@ -292,9 +289,7 @@ public Relation relate(double minX, double maxX, double minY, double maxY) { |
292 | 289 | && ((splitX == false && maxY >= this.component.getMinY()) |
293 | 290 | || (splitX && maxX >= this.component.getMinX()))) { |
294 | 291 | relation = right.relate(minX, maxX, minY, maxY); |
295 | | - if (relation != Relation.CELL_OUTSIDE_QUERY) { |
296 | | - return relation; |
297 | | - } |
| 292 | + return relation; |
298 | 293 | } |
299 | 294 | } |
300 | 295 | return Relation.CELL_OUTSIDE_QUERY; |
@@ -322,27 +317,11 @@ private static ComponentTree createTree( |
322 | 317 | } |
323 | 318 | final int mid = (low + high) >>> 1; |
324 | 319 | if (low < high) { |
325 | | - Comparator<Component2D> comparator; |
326 | 320 | if (splitX) { |
327 | | - comparator = |
328 | | - (left, right) -> { |
329 | | - int ret = Double.compare(left.getMinX(), right.getMinX()); |
330 | | - if (ret == 0) { |
331 | | - ret = Double.compare(left.getMaxX(), right.getMaxX()); |
332 | | - } |
333 | | - return ret; |
334 | | - }; |
| 321 | + ArrayUtil.select(components, low, high + 1, mid, XComparator); |
335 | 322 | } else { |
336 | | - comparator = |
337 | | - (left, right) -> { |
338 | | - int ret = Double.compare(left.getMinY(), right.getMinY()); |
339 | | - if (ret == 0) { |
340 | | - ret = Double.compare(left.getMaxY(), right.getMaxY()); |
341 | | - } |
342 | | - return ret; |
343 | | - }; |
| 323 | + ArrayUtil.select(components, low, high + 1, mid, YComparator); |
344 | 324 | } |
345 | | - ArrayUtil.select(components, low, high + 1, mid, comparator); |
346 | 325 | } |
347 | 326 | ComponentTree newNode = new ComponentTree(components[mid], splitX); |
348 | 327 | // find children |
|
0 commit comments