Changeset 24061 in osm for applications
- Timestamp:
- 2010-11-04T16:14:43+01:00 (14 years ago)
- Location:
- applications/editors/josm/plugins/pdfimport/src/pdfimport
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
r24056 r24061 53 53 54 54 public void filterByColor(Color color) { 55 56 int rgb = color.getRGB() & 0xffffff; 57 55 58 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 56 59 for(LayerContents l: this.layers) { … … 58 61 boolean good = false; 59 62 60 if (l.info.fill != null && l.info.fill.equals(color)) { 63 64 if (l.info.fill != null && (l.info.fill.getRGB() & 0xffffff) == rgb) { 61 65 good = true; 62 66 } 63 67 64 if (l.info.stroke != null && l.info.stroke.equals(color)) {68 if (l.info.stroke != null && (l.info.stroke.getRGB() & 0xffffff) == rgb) { 65 69 good = true; 66 70 } … … 277 281 */ 278 282 private void concatenatePaths(LayerContents layer) { 279 Map<Point2D, PdfPath> pathEndpoints = new HashMap<Point2D, PdfPath>();283 Map<Point2D, List<PdfPath>> pathEndpoints = new HashMap<Point2D, List<PdfPath>>(); 280 284 Set<PdfPath> mergedPaths = new HashSet<PdfPath>(); 281 285 List<PdfPath> newPaths = new ArrayList<PdfPath>(); 286 287 //fill pathEndpoints map 282 288 for(PdfPath pp: layer.paths){ 283 284 PdfPath path = pp; 289 if (pp.isClosed()) { 290 newPaths.add(pp); 291 continue; 292 } 293 294 List<PdfPath> paths = pathEndpoints.get(pp.firstPoint()); 295 if (paths == null){ 296 paths = new ArrayList<PdfPath>(2); 297 pathEndpoints.put(pp.firstPoint(), paths); 298 } 299 paths.add(pp); 300 301 paths = pathEndpoints.get(pp.lastPoint()); 302 if (paths == null){ 303 paths = new ArrayList<PdfPath>(2); 304 pathEndpoints.put(pp.lastPoint(), paths); 305 } 306 paths.add(pp); 307 } 308 309 List<PdfPath> pathChain = new ArrayList<PdfPath>(2); 310 Set<Point2D> pointsInPath = new HashSet<Point2D>(); 311 312 //join the paths 313 for(PdfPath pp: layer.paths) { 314 315 if (pp.isClosed() || mergedPaths.contains(pp)) { 316 continue; 317 } 318 285 319 boolean changed = true; 286 320 287 while (changed && !path.isClosed()) { 288 changed = false; 289 290 if (pathEndpoints.containsKey(path.firstPoint())) { 291 292 PdfPath p1 = pathEndpoints.get(path.firstPoint()); 293 294 if (this.isSubpathOf(p1, path)){ 295 continue; 321 PdfPath firstPath = pp; 322 PdfPath lastPath = pp; 323 Point2D firstPoint = pp.firstPoint(); 324 Point2D lastPoint = pp.lastPoint(); 325 326 327 pathChain.clear(); 328 pathChain.add(pp); 329 pointsInPath.clear(); 330 pointsInPath.add(firstPoint); 331 pointsInPath.add(lastPoint); 332 333 //process last point 334 while (changed && firstPoint != lastPoint) { 335 changed = false; 336 337 List<PdfPath> adjacentPaths = pathEndpoints.get(lastPoint); 338 PdfPath nextPath = findNextPath(adjacentPaths, lastPath); 339 340 if (nextPath != null) { 341 Point2D nextPoint = nextPath.getOtherEnd(lastPoint); 342 343 lastPoint = nextPoint; 344 lastPath = nextPath; 345 pathChain.add(lastPath); 346 347 if (!pointsInPath.contains(lastPoint)) { 348 pointsInPath.add(lastPoint); 349 changed = true; 296 350 } 297 298 pathEndpoints.remove(p1.firstPoint()); 299 pathEndpoints.remove(p1.lastPoint()); 300 301 List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points); 302 303 if (newNodes == null) 351 else 304 352 { 305 int a = 10; 306 a++; 353 //closed path found 354 //remove excess segments from start of chain 355 while (lastPoint != firstPoint) { 356 PdfPath pathToRemove = pathChain.get(0); 357 firstPoint = pathToRemove.getOtherEnd(firstPoint); 358 } 359 360 changed = false; 307 361 } 308 309 path.points = newNodes;310 mergedPaths.add(p1);311 changed = true;312 362 } 313 314 if (pathEndpoints.containsKey(path.lastPoint())) { 315 PdfPath p1 = pathEndpoints.get(path.lastPoint()); 316 317 if (this.isSubpathOf(p1, path)){ 318 continue; 363 } 364 365 366 //process first point 367 changed = true; 368 while (changed && firstPoint != lastPoint) { 369 changed = false; 370 371 List<PdfPath> adjacentPaths = pathEndpoints.get(firstPoint); 372 PdfPath nextPath = findNextPath(adjacentPaths, firstPath); 373 374 if (nextPath != null) { 375 Point2D nextPoint = nextPath.getOtherEnd(firstPoint); 376 377 firstPoint = nextPoint; 378 firstPath = nextPath; 379 pathChain.add(0, firstPath); 380 381 if (!pointsInPath.contains(nextPoint)) { 382 pointsInPath.add(firstPoint); 383 changed = true; 319 384 } 320 321 pathEndpoints.remove(p1.firstPoint()); 322 pathEndpoints.remove(p1.lastPoint()); 323 324 List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points); 325 path.points = newNodes; 326 mergedPaths.add(p1); 327 changed = true; 385 else 386 { 387 //closed path found 388 //remove excess segments from end of chain 389 while (lastPoint != firstPoint) { 390 PdfPath pathToRemove = pathChain.get(pathChain.size() - 1); 391 lastPoint = pathToRemove.getOtherEnd(lastPoint); 392 } 393 394 changed = false; 395 } 328 396 } 329 397 } 330 398 331 if (!path.isClosed()){ 332 pathEndpoints.put(path.firstPoint(), path); 333 pathEndpoints.put(path.lastPoint(), path); 334 } 335 } 336 337 List<PdfPath> resultPaths = new ArrayList<PdfPath>(); 338 339 for(PdfPath path: layer.paths) { 340 if (!mergedPaths.contains(path)){ 341 resultPaths.add(path); 342 } 343 } 344 345 layer.paths = resultPaths; 346 } 399 //construct path 400 401 //remove from map 402 for (PdfPath path: pathChain) { 403 pathEndpoints.get(path.firstPoint()).remove(path); 404 pathEndpoints.get(path.lastPoint()).remove(path); 405 } 406 407 PdfPath path = pathChain.get(0); 408 409 for (int pos = 1; pos < pathChain.size(); pos ++) { 410 path.points = tryMergeNodeLists(path.points, pathChain.get(pos).points); 411 412 if (path.points == null) { 413 throw new RuntimeException(); 414 } 415 416 mergedPaths.add(pathChain.get(pos)); 417 } 418 419 newPaths.add(path); 420 } 421 422 layer.paths = newPaths; 423 } 424 425 private PdfPath findNextPath(List<PdfPath> adjacentPaths, PdfPath firstPath) { 426 for (int pos = 0; pos < adjacentPaths.size(); pos ++) { 427 PdfPath p = adjacentPaths.get(pos); 428 if (p != firstPath && !isSubpathOf(firstPath, p)){ 429 return p; 430 } 431 } 432 433 return null; 434 } 435 347 436 348 437 /** -
applications/editors/josm/plugins/pdfimport/src/pdfimport/PdfPath.java
r23702 r24061 7 7 public List<Point2D> points; 8 8 public double length; 9 9 10 10 LayerContents layer; 11 11 public int nr; 12 13 12 13 14 14 public PdfPath(List<Point2D> nodes) { 15 points = nodes; 15 points = nodes; 16 16 } 17 17 18 public boolean isClosed() { 19 return points.size() > 1 && points.get(0) == points.get(points.size() - 1); 18 public boolean isClosed() { 19 return points.size() > 1 && points.get(0) == points.get(points.size() - 1); 20 20 } 21 21 22 22 public Point2D firstPoint() { 23 return points.get(0); 23 return points.get(0); 24 24 } 25 25 26 26 public Point2D lastPoint() { 27 return points.get(points.size() - 1); 27 return points.get(points.size() - 1); 28 28 } 29 29 30 30 public void calculateLength() { 31 31 double len = 0; 32 32 33 33 for(int pos =1; pos < points.size(); pos ++) { 34 34 len += points.get(pos).distance(points.get(pos -1)); 35 35 } 36 36 37 37 this.length = len; 38 38 } 39 40 public Point2D getOtherEnd(Point2D endPoint) { 41 if (this.firstPoint() == endPoint) { 42 return this.lastPoint(); 43 } 44 45 if (this.lastPoint() == endPoint) { 46 return this.firstPoint(); 47 } 48 49 throw new RuntimeException("Unexpected point"); 50 51 } 39 52 }
Note:
See TracChangeset
for help on using the changeset viewer.