| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
package uk.me.parabola.util; |
| 14 |
|
| 15 |
import static org.junit.Assert.assertEquals; |
| 16 |
import static org.junit.Assert.assertNotEquals; |
| 17 |
import static org.junit.Assert.assertTrue; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.Collections; |
| 21 |
import java.util.List; |
| 22 |
|
| 23 |
import org.junit.Test; |
| 24 |
|
| 25 |
import uk.me.parabola.imgfmt.app.Area; |
| 26 |
import uk.me.parabola.imgfmt.app.Coord; |
| 27 |
import uk.me.parabola.mkgmap.filters.ShapeMergeFilter; |
| 28 |
import uk.me.parabola.mkgmap.general.MapShape; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class ShapeSplitterTest { |
| 37 |
|
| 38 |
@Test |
| 39 |
public void test1SimpleSplit() { |
| 40 |
|
| 41 |
int[][] os = { { 1, 1 }, { 5, 3 }, { 7, 7 }, { 3, 5 } }; |
| 42 |
SplitTester st = new SplitTester(os); |
| 43 |
|
| 44 |
st.cutPosn(3, false); |
| 45 |
int[][] f1 = { { 1, 1 }, { 3, 2 }, { 3, 5 } }; |
| 46 |
st.addExpected(f1); |
| 47 |
int[][] f2 = { { 3, 2 }, { 5, 3 }, { 7, 7 }, { 3, 5 } }; |
| 48 |
st.addExpected(f2); |
| 49 |
st.runSplitShape(); |
| 50 |
st.runClipToBounds(); |
| 51 |
st.runJava2D(); |
| 52 |
|
| 53 |
|
| 54 |
st.cutPosn(5, true); |
| 55 |
int[][] t1 = { { 1, 1 }, { 5, 3 }, { 6, 5 }, { 3, 5 } }; |
| 56 |
st.addExpected(t1); |
| 57 |
int[][] t2 = { { 6, 5 }, { 7, 7 }, { 3, 5 } }; |
| 58 |
st.addExpected(t2); |
| 59 |
st.runSplitShape(); |
| 60 |
st.runClipToBounds(); |
| 61 |
st.runJava2D(); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
@Test |
| 66 |
public void test2CutToHole() { |
| 67 |
|
| 68 |
int[][] os = { { 1, 1 }, { 3, 1 }, { 3, 3 }, { 2, 3 }, { 2, 4 }, { 4, 4 }, { 4, 3 }, { 3, 3 }, { 3, 1 }, |
| 69 |
{ 5, 1 }, { 5, 5 }, { 1, 5 } }; |
| 70 |
SplitTester st = new SplitTester(os); |
| 71 |
|
| 72 |
|
| 73 |
st.cutPosn(2, true); |
| 74 |
int[][] t1 = { { 1, 1 }, { 3, 1 }, { 3, 2 }, { 1, 2 } }; |
| 75 |
st.addExpected(t1); |
| 76 |
int[][] t2 = { { 3, 1 }, { 5, 1 }, { 5, 2 }, { 3, 2 } }; |
| 77 |
st.addExpected(t2); |
| 78 |
int[][] t3 = { { 1, 2 }, { 3, 2 }, { 3, 3 }, { 2, 3 }, { 2, 4 }, { 4, 4 }, { 4, 3 }, { 3, 3 }, { 3, 2 }, |
| 79 |
{ 5, 2 }, { 5, 5 }, { 1, 5 } }; |
| 80 |
st.addExpected(t3); |
| 81 |
st.runSplitShape(); |
| 82 |
st.runClipToBounds(); |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
st.cutPosn(3, false); |
| 88 |
int[][] f1 = { { 1, 1 }, { 3, 1 }, { 3, 3 }, { 2, 3 }, { 2, 4 }, { 3, 4 }, { 3, 5 }, { 1, 5 } }; |
| 89 |
st.addExpected(f1); |
| 90 |
int[][] f2 = { { 3, 1 }, { 5, 1 }, { 5, 5 }, { 3, 5 }, { 3, 4 }, { 4, 4 }, { 4, 3 }, { 3, 3 } }; |
| 91 |
st.addExpected(f2); |
| 92 |
st.runSplitShape(); |
| 93 |
st.runClipToBounds(); |
| 94 |
st.runJava2D(); |
| 95 |
|
| 96 |
} |
| 97 |
|
| 98 |
@Test |
| 99 |
public void test3CutSpiral() { |
| 100 |
|
| 101 |
int[][] os = { |
| 102 |
|
| 103 |
{ 7, 10 }, { 6, 10 }, { 6, 6 }, { 10, 6 }, { 10, 14 }, { 2, 14 }, { 2, 2 }, { 14, 2 }, |
| 104 |
|
| 105 |
{ 14, 14 }, { 13, 14 }, { 13, 3 }, |
| 106 |
|
| 107 |
{ 3, 3 }, { 3, 13 }, { 9, 13 }, { 9, 7 }, { 7, 7 }, |
| 108 |
|
| 109 |
{ 7, 8 }, { 8, 8 }, { 8, 12 }, { 4, 12 }, { 4, 4 }, { 12, 4 }, { 12, 15 }, |
| 110 |
|
| 111 |
{ 15, 15 }, { 15, 1 }, |
| 112 |
|
| 113 |
{ 1, 1 }, { 1, 15 }, { 11, 15 }, { 11, 5 }, { 5, 5 }, { 5, 11 }, { 7, 11 } }; |
| 114 |
SplitTester st = new SplitTester(os); |
| 115 |
|
| 116 |
st.cutPosn(9, true); |
| 117 |
|
| 118 |
int[][] l1 = { { 1, 9 }, { 1, 1 }, { 15, 1 }, { 15, 9 }, { 14, 9 }, { 14, 2 }, { 2, 2 }, { 2, 9 } }; |
| 119 |
st.addExpected(l1); |
| 120 |
int[][] l2 = { { 3, 9 }, { 3, 3 }, { 13, 3 }, { 13, 9 }, { 12, 9 }, { 12, 4 }, { 4, 4 }, { 4, 9 } }; |
| 121 |
st.addExpected(l2); |
| 122 |
int[][] l3 = { { 5, 9 }, { 5, 5 }, { 11, 5 }, { 11, 9 }, { 10, 9 }, { 10, 6 }, { 6, 6 }, { 6, 9 } }; |
| 123 |
st.addExpected(l3); |
| 124 |
int[][] l4 = { { 8, 9 }, { 8, 8 }, { 7, 8 }, { 7, 7 }, { 9, 7 }, { 9, 9 } }; |
| 125 |
st.addExpected(l4); |
| 126 |
|
| 127 |
int[][] r1 = { { 1, 9 }, { 1, 15 }, { 11, 15 }, { 11, 9 }, { 10, 9 }, { 10, 14 }, { 2, 14 }, { 2, 9 } }; |
| 128 |
st.addExpected(r1); |
| 129 |
int[][] r2 = { { 3, 9 }, { 3, 13 }, { 9, 13 }, { 9, 9 }, { 8, 9 }, { 8, 12 }, { 4, 12 }, { 4, 9 } }; |
| 130 |
st.addExpected(r2); |
| 131 |
int[][] r3 = { { 5, 9 }, { 5, 11 }, { 7, 11 }, { 7, 10 }, { 6, 10 }, { 6, 9 } }; |
| 132 |
st.addExpected(r3); |
| 133 |
int[][] r4 = { { 12, 9 }, { 12, 15 }, { 15, 15 }, { 15, 9 }, { 14, 9 }, { 14, 14 }, { 13, 14 }, { 13, 9 } }; |
| 134 |
st.addExpected(r4); |
| 135 |
st.runSplitShape(); |
| 136 |
st.runClipToBounds(); |
| 137 |
st.runJava2D(); |
| 138 |
|
| 139 |
} |
| 140 |
|
| 141 |
@Test |
| 142 |
public void test4CutFlash() { |
| 143 |
|
| 144 |
int[][] os = { { 20, 18 }, { 15, 18 }, { 6, 9 }, { 6, 10 }, { 4, 8 }, { 4, 18 }, { 1, 18 }, { 1, 1 }, { 20, 1 }, |
| 145 |
{ 20, 10 }, { 12, 2 }, { 12, 10 }, { 11, 9 }, { 11, 10 }, { 9, 8 }, { 9, 10 }, { 2, 3 }, { 2, 10 }, |
| 146 |
{ 3, 11 }, { 3, 5 }, { 13, 15 }, { 13, 7 }, { 16, 10 }, { 16, 8 }, { 18, 10 }, { 18, 9 }, { 20, 11 } }; |
| 147 |
SplitTester st = new SplitTester(os); |
| 148 |
|
| 149 |
st.cutPosn(9, true); |
| 150 |
|
| 151 |
int[][] l1 = { { 1, 9 }, { 1, 1 }, { 20, 1 }, { 20, 9 }, { 19, 9 }, { 12, 2 }, { 12, 9 }, { 10, 9 }, { 9, 8 }, |
| 152 |
{ 9, 9 }, { 8, 9 }, { 2, 3 }, { 2, 9 } }; |
| 153 |
st.addExpected(l1); |
| 154 |
int[][] l2 = { { 3, 9 }, { 3, 5 }, { 7, 9 }, { 5, 9 }, { 4, 8 }, { 4, 9 } }; |
| 155 |
st.addExpected(l2); |
| 156 |
int[][] l3 = { { 13, 9 }, { 13, 7 }, { 15, 9 } }; |
| 157 |
st.addExpected(l3); |
| 158 |
int[][] l4 = { { 16, 9 }, { 16, 8 }, { 17, 9 } }; |
| 159 |
st.addExpected(l4); |
| 160 |
|
| 161 |
int[][] r1 = { { 1, 9 }, { 1, 18 }, { 4, 18 }, { 4, 9 }, { 3, 9 }, { 3, 11 }, { 2, 10 }, { 2, 9 } }; |
| 162 |
st.addExpected(r1); |
| 163 |
int[][] r2 = { { 5, 9 }, { 6, 10 }, { 6, 9 } }; |
| 164 |
st.addExpected(r2); |
| 165 |
int[][] r3 = { { 6, 9 }, { 15, 18 }, { 20, 18 }, { 20, 11 }, { 18, 9 }, { 18, 10 }, { 17, 9 }, { 16, 9 }, |
| 166 |
{ 16, 10 }, { 15, 9 }, { 13, 9 }, { 13, 15 }, { 7, 9 } }; |
| 167 |
st.addExpected(r3); |
| 168 |
int[][] r4 = { { 8, 9 }, { 9, 10 }, { 9, 9 } }; |
| 169 |
st.addExpected(r4); |
| 170 |
int[][] r5 = { { 10, 9 }, { 11, 10 }, { 11, 9 } }; |
| 171 |
st.addExpected(r5); |
| 172 |
int[][] r6 = { { 11, 9 }, { 12, 10 }, { 12, 9 } }; |
| 173 |
st.addExpected(r6); |
| 174 |
int[][] r7 = { { 19, 9 }, { 20, 10 }, { 20, 9 } }; |
| 175 |
st.addExpected(r7); |
| 176 |
st.runSplitShape(); |
| 177 |
st.runClipToBounds(); |
| 178 |
st.runJava2D(); |
| 179 |
|
| 180 |
|
| 181 |
st.clipBounds(1, 1, 20, 18); |
| 182 |
st.addExpected(os); |
| 183 |
st.runClipToBounds(); |
| 184 |
st.runJava2D(); |
| 185 |
|
| 186 |
|
| 187 |
st.clipBounds(13, 10, 19, 16); |
| 188 |
int[][] s1 = { { 13, 10 }, { 13, 16 }, { 19, 16 }, { 19, 10 } }; |
| 189 |
st.addExpected(s1); |
| 190 |
st.runClipToBounds(); |
| 191 |
st.runJava2D(); |
| 192 |
|
| 193 |
|
| 194 |
st.clipBounds(9, 10, 13, 11); |
| 195 |
st.runClipToBounds(); |
| 196 |
st.runJava2D(); |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
@Test |
| 201 |
public void test4ClipTest() { |
| 202 |
|
| 203 |
int[][] os = { { 2, 1 }, { 10, 1 }, { 10, 10 }, { 1, 10 }, { 1, 2 }, { 2, 3 }, { 2, 5 }, { 4, 5 }, { 4, 6 }, |
| 204 |
{ 2, 6 }, { 2, 9 }, { 5, 9 }, { 5, 7 }, { 6, 7 }, { 6, 9 }, { 9, 9 }, { 9, 6 }, { 7, 6 }, { 7, 5 }, |
| 205 |
{ 9, 5 }, { 9, 2 }, { 6, 2 }, { 6, 4 }, { 5, 4 }, { 5, 2 }, { 3, 2 } }; |
| 206 |
SplitTester st = new SplitTester(os); |
| 207 |
st.clipBounds(3, 3, 8, 8); |
| 208 |
|
| 209 |
int[][] s1 = { { 6, 3 }, { 6, 4 }, { 5, 4 }, { 5, 3 } }; |
| 210 |
st.addExpected(s1); |
| 211 |
int[][] s2 = { { 7, 5 }, { 8, 5 }, { 8, 6 }, { 7, 6 } }; |
| 212 |
st.addExpected(s2); |
| 213 |
int[][] s3 = { { 5, 7 }, { 6, 7 }, { 6, 8 }, { 5, 8 } }; |
| 214 |
st.addExpected(s3); |
| 215 |
int[][] s4 = { { 3, 5 }, { 4, 5 }, { 4, 6 }, { 3, 6 } }; |
| 216 |
st.addExpected(s4); |
| 217 |
st.runClipToBounds(); |
| 218 |
st.runJava2D(); |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
private class SplitTester { |
| 223 |
|
| 224 |
List<Coord> origShape; |
| 225 |
long origArea; |
| 226 |
|
| 227 |
List<List<Coord>> expectedShapes, resultShapes; |
| 228 |
long totalArea; |
| 229 |
|
| 230 |
boolean dividingInTwo; |
| 231 |
int dividingLine; |
| 232 |
boolean isLongitude; |
| 233 |
Area fstBounds, lstBounds; |
| 234 |
|
| 235 |
String algorithm; |
| 236 |
|
| 237 |
List<Coord> makeShape(int[][] lowPrecPoints) { |
| 238 |
List<Coord> points = new ArrayList<>(); |
| 239 |
for (int[] intPair : lowPrecPoints) { |
| 240 |
points.add(new Coord(intPair[0], intPair[1])); |
| 241 |
} |
| 242 |
points.add(points.get(0)); |
| 243 |
return points; |
| 244 |
} |
| 245 |
|
| 246 |
SplitTester(int[][] lowPrecPoints) { |
| 247 |
origShape = makeShape(lowPrecPoints); |
| 248 |
origArea = ShapeMergeFilter.calcAreaSizeTestVal(origShape); |
| 249 |
} |
| 250 |
|
| 251 |
void cutPosn(int dividingLine, boolean isLongitude) { |
| 252 |
dividingInTwo = true; |
| 253 |
this.dividingLine = dividingLine; |
| 254 |
this.isLongitude = isLongitude; |
| 255 |
expectedShapes = new ArrayList<>(); |
| 256 |
totalArea = 0; |
| 257 |
|
| 258 |
MapShape tempShape = new MapShape(); |
| 259 |
tempShape.setPoints(origShape); |
| 260 |
Area bounds = tempShape.getBounds(); |
| 261 |
if (isLongitude) { |
| 262 |
fstBounds = new Area(bounds.getMinLat(), bounds.getMinLong(), bounds.getMaxLat(), dividingLine); |
| 263 |
lstBounds = new Area(bounds.getMinLat(), dividingLine, bounds.getMaxLat(), bounds.getMaxLong()); |
| 264 |
} else { |
| 265 |
fstBounds = new Area(bounds.getMinLat(), bounds.getMinLong(), dividingLine, bounds.getMaxLong()); |
| 266 |
lstBounds = new Area(dividingLine, bounds.getMinLong(), bounds.getMaxLat(), bounds.getMaxLong()); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
void clipBounds(int minLat, int minLong, int maxLat, int maxLong) { |
| 271 |
dividingInTwo = false; |
| 272 |
expectedShapes = new ArrayList<>(); |
| 273 |
totalArea = 0; |
| 274 |
fstBounds = new Area(minLat, minLong, maxLat, maxLong); |
| 275 |
} |
| 276 |
|
| 277 |
void addExpected(int[][] lowPrecPoints) { |
| 278 |
List<Coord> another = makeShape(lowPrecPoints); |
| 279 |
totalArea += Math.abs(ShapeMergeFilter.calcAreaSizeTestVal(another)); |
| 280 |
expectedShapes.add(another); |
| 281 |
} |
| 282 |
|
| 283 |
void preSplit(String algorithm) { |
| 284 |
this.algorithm = algorithm; |
| 285 |
if (dividingInTwo) { |
| 286 |
assertEquals(algorithm + " bits area", Math.abs(origArea), totalArea); |
| 287 |
} |
| 288 |
resultShapes = null; |
| 289 |
} |
| 290 |
|
| 291 |
void checkResults() { |
| 292 |
assertEquals(algorithm + " number of areas", expectedShapes.size(), resultShapes.size()); |
| 293 |
long resTotalArea = 0; |
| 294 |
for (List<Coord> resShape : resultShapes) { |
| 295 |
long resArea = ShapeMergeFilter.calcAreaSizeTestVal(resShape); |
| 296 |
resTotalArea += Math.abs(resArea); |
| 297 |
MapShape resTemp = new MapShape(); |
| 298 |
resTemp.setPoints(resShape); |
| 299 |
Area resBounds = resTemp.getBounds(); |
| 300 |
|
| 301 |
boolean foundIt = false; |
| 302 |
for (List<Coord> expShape : expectedShapes) { |
| 303 |
long expArea = ShapeMergeFilter.calcAreaSizeTestVal(expShape); |
| 304 |
MapShape expTemp = new MapShape(); |
| 305 |
expTemp.setPoints(expShape); |
| 306 |
Area expBounds = expTemp.getBounds(); |
| 307 |
if (Math.abs(expArea) == Math.abs(resArea) && expBounds.equals(resBounds)) { |
| 308 |
foundIt = true; |
| 309 |
|
| 310 |
|
| 311 |
List<Coord> resPoints = new ArrayList<>(resShape); |
| 312 |
resPoints.remove(resPoints.size() - 1); |
| 313 |
if (expArea != resArea) |
| 314 |
Collections.reverse(resPoints); |
| 315 |
|
| 316 |
Coord expFst = expShape.get(0); |
| 317 |
int inx; |
| 318 |
for (inx = 0; inx < resPoints.size(); ++inx) |
| 319 |
if (resPoints.get(inx).equals(expFst)) |
| 320 |
break; |
| 321 |
assertNotEquals("find first point failed", resPoints.size(), inx); |
| 322 |
if (inx > 0) |
| 323 |
Collections.rotate(resPoints, -inx); |
| 324 |
|
| 325 |
|
| 326 |
inx = 0; |
| 327 |
for (Coord resPoint : resPoints) { |
| 328 |
assertTrue(algorithm + " point " + inx, resPoint.equals(expShape.get(inx))); |
| 329 |
++inx; |
| 330 |
} |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
assertTrue(algorithm + " result shape not matched", foundIt); |
| 335 |
} |
| 336 |
assertEquals(algorithm + " result total area", totalArea, resTotalArea); |
| 337 |
} |
| 338 |
|
| 339 |
void runSplitShape() { |
| 340 |
preSplit("splitShape"); |
| 341 |
resultShapes = new ArrayList<>(); |
| 342 |
assertTrue(algorithm + " Not applicable to clip", dividingInTwo); |
| 343 |
ShapeSplitter.splitShape(origShape, dividingLine << Coord.DELTA_SHIFT, isLongitude, resultShapes, |
| 344 |
resultShapes, null); |
| 345 |
checkResults(); |
| 346 |
} |
| 347 |
|
| 348 |
void runClipToBounds() { |
| 349 |
preSplit("clipToBounds"); |
| 350 |
resultShapes = ShapeSplitter.clipToBounds(origShape, fstBounds, null); |
| 351 |
if (dividingInTwo) { |
| 352 |
List<List<Coord>> moreShapes = ShapeSplitter.clipToBounds(origShape, lstBounds, null); |
| 353 |
resultShapes.addAll(moreShapes); |
| 354 |
} |
| 355 |
checkResults(); |
| 356 |
} |
| 357 |
|
| 358 |
void runJava2D() { |
| 359 |
preSplit("java2D"); |
| 360 |
java.awt.geom.Area area = Java2DConverter.createArea(origShape); |
| 361 |
java.awt.geom.Area clipper = Java2DConverter.createBoundsArea(fstBounds); |
| 362 |
clipper.intersect(area); |
| 363 |
resultShapes = Java2DConverter.areaToShapes(clipper); |
| 364 |
if (dividingInTwo) { |
| 365 |
clipper = Java2DConverter.createBoundsArea(lstBounds); |
| 366 |
clipper.intersect(area); |
| 367 |
List<List<Coord>> moreShapes = Java2DConverter.areaToShapes(clipper); |
| 368 |
resultShapes.addAll(moreShapes); |
| 369 |
} |
| 370 |
checkResults(); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
} |
| 384 |
|
| 385 |
} |