Changeset 23746 in osm for applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
- Timestamp:
- 2010-10-22T15:51:17+02:00 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java
r23702 r23746 10 10 11 11 public class PathOptimizer { 12 12 13 13 public Map<Point2D, Point2D> uniquePointMap; 14 private Map<LayerInfo, LayerContents> layerMap; 14 private final Map<LayerInfo, LayerContents> layerMap; 15 15 private List<LayerContents> layers; 16 16 17 17 public PathOptimizer() 18 18 { … … 30 30 this.uniquePointMap.put(point, point); 31 31 return point; 32 } 33 } 34 32 } 33 } 34 35 35 public void addPath(LayerInfo info, PdfPath path) 36 36 { … … 38 38 layer.paths.add(path); 39 39 } 40 40 41 41 public void addMultiPath(LayerInfo info, List<PdfPath> paths) { 42 42 LayerContents layer = this.getLayer(info); 43 43 PdfMultiPath p = new PdfMultiPath(paths); 44 layer.multiPaths.add(p); 45 } 46 44 layer.multiPaths.add(p); 45 } 46 47 47 private LayerContents getLayer(LayerInfo info) { 48 48 LayerContents layer; 49 49 50 50 if (this.layerMap.containsKey(info)) 51 51 { … … 63 63 return layer; 64 64 } 65 65 66 66 public void optimize() 67 67 { 68 68 for(LayerContents layer: this.layers) { 69 this. optimizeLayer(layer);70 } 71 69 this.concatenataPaths(layer); 70 } 71 72 72 List<LayerContents> newLayers = new ArrayList<LayerContents>(); 73 73 int nr = 0; 74 74 75 75 for(LayerContents l: this.layers) { 76 76 List<LayerContents> splitResult = splitBySegmentKind(l); 77 77 78 78 for(LayerContents ll: splitResult) { 79 79 ll.info.nr = nr; … … 82 82 } 83 83 } 84 84 85 85 this.layers = newLayers; 86 86 87 87 for(LayerContents layer: this.layers) { 88 88 finalizeLayer(layer); … … 93 93 Set<Point2D> points = new HashSet<Point2D>(); 94 94 layer.points = new ArrayList<Point2D>(); 95 95 96 96 for(PdfPath pp: layer.paths){ 97 97 pp.layer = layer; 98 98 99 99 for(Point2D point: pp.points){ 100 100 if (!points.contains(point)) { … … 102 102 points.add(point); 103 103 } 104 } 105 } 106 104 } 105 } 106 107 107 for (PdfMultiPath multipath: layer.multiPaths) { 108 108 multipath.layer = layer; 109 109 for(PdfPath pp: multipath.paths){ 110 110 pp.layer = layer; 111 111 112 112 for(Point2D point: pp.points){ 113 113 if (!points.contains(point)) { … … 119 119 } 120 120 } 121 121 122 122 /** 123 * This method merges together paths with common end nodes. 123 * This method merges together paths with common end nodes. 124 124 * @param layer the layer to process. 125 125 */ 126 private void optimizeLayer(LayerContents layer) {126 private void concatenataPaths(LayerContents layer) { 127 127 Map<Point2D, PdfPath> pathEndpoints = new HashMap<Point2D, PdfPath>(); 128 Set<PdfPath> mergedPaths = new HashSet<PdfPath>(); 129 130 /* 131 //sort paths, longest first 132 List<PdfPath> sortedPaths = new ArrayList<PdfPath>(); 133 134 for(PdfPath path: layer.paths) { 135 path.calculateLength(); 136 sortedPaths.add(path); 137 } 138 139 Collections.sort(sortedPaths, new Comparator<PdfPath>(){ 140 public int compare(PdfPath o1, PdfPath o2) { 141 142 if (o1.length > o2.length) { 143 return -1; 144 } else if (o1.length < o2.length) { 145 return 1; 146 } else { 147 return 0; 148 } 149 } 150 151 }); 152 */ 153 128 Set<PdfPath> mergedPaths = new HashSet<PdfPath>(); 129 154 130 for(PdfPath pp: layer.paths){ 155 131 156 132 PdfPath path = pp; 157 133 boolean changed = true; 158 134 159 135 while (changed && !path.isClosed()) { 160 136 changed = false; 161 162 if (pathEndpoints.containsKey(path.firstPoint())) {137 138 if (pathEndpoints.containsKey(path.firstPoint())) { 163 139 PdfPath p1 = pathEndpoints.get(path.firstPoint()); 164 140 pathEndpoints.remove(p1.firstPoint()); 165 141 pathEndpoints.remove(p1.lastPoint()); 166 142 167 143 List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points); 168 path.points = newNodes; 144 path.points = newNodes; 169 145 mergedPaths.add(p1); 170 146 changed = true; 171 147 } 172 173 if (pathEndpoints.containsKey(path.lastPoint())) {148 149 if (pathEndpoints.containsKey(path.lastPoint())) { 174 150 PdfPath p1 = pathEndpoints.get(path.lastPoint()); 175 151 pathEndpoints.remove(p1.firstPoint()); 176 152 pathEndpoints.remove(p1.lastPoint()); 177 153 178 154 List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points); 179 path.points = newNodes; 155 path.points = newNodes; 180 156 mergedPaths.add(p1); 181 157 changed = true; 182 } 183 } 184 158 } 159 } 160 185 161 if (!path.isClosed()){ 186 162 pathEndpoints.put(path.firstPoint(), path); … … 188 164 } 189 165 } 190 166 191 167 List<PdfPath> resultPaths = new ArrayList<PdfPath>(); 192 168 193 169 for(PdfPath path: layer.paths) { 194 170 if (!mergedPaths.contains(path)){ … … 196 172 } 197 173 } 198 174 199 175 layer.paths = resultPaths; 200 176 } 201 177 202 178 private List<LayerContents> splitBySegmentKind(LayerContents layer) 203 179 { … … 205 181 List<PdfPath> multiSegmentPaths = new ArrayList<PdfPath>(); 206 182 List<PdfPath> closedPaths = new ArrayList<PdfPath>(); 207 183 208 184 for(PdfPath path: layer.paths) { 209 185 if (path.points.size() <= 3) { … … 217 193 } 218 194 } 219 195 220 196 List<LayerContents> layers = new ArrayList<LayerContents>(); 221 197 222 198 if (multiSegmentPaths.size() > 0) { 223 199 LayerContents l = new LayerContents(); 224 200 l.paths = multiSegmentPaths; 225 201 l.info = layer.info.copy(); 226 202 227 203 layers.add(l); 228 204 } 229 205 230 206 if (singleSegmentPaths.size() > 0) { 231 207 LayerContents l = new LayerContents(); … … 242 218 layers.add(l); 243 219 } 244 220 245 221 return layers; 246 222 } 247 248 223 224 249 225 250 226 private List<Point2D> tryMergeNodeLists(List<Point2D> nodes1, List<Point2D> nodes2) { 251 227 252 228 boolean nodes1Closed = (nodes1.get(0) == nodes1.get(nodes1.size() - 1)); 253 229 boolean nodes2Closed = (nodes2.get(0) == nodes2.get(nodes2.size() - 1)); 254 230 255 231 if (nodes1Closed || nodes2Closed) { 256 232 return null; 257 233 } 258 234 259 235 if (nodes1.get(nodes1.size() - 1) == nodes2.get(0)) { 260 236 nodes1.remove(nodes1.size() -1); … … 267 243 nodes1.add(nodes2.get(pos)); 268 244 } 269 270 return nodes1; 271 } 245 246 return nodes1; 247 } 272 248 else if (nodes1.get(0) == nodes2.get(nodes2.size() - 1)) { 273 249 nodes1.remove(0); 274 250 nodes1.addAll(0, nodes2); 275 251 return nodes1; 276 } 252 } 277 253 else if (nodes1.get(0) == nodes2.get(0)) { 278 nodes1.remove(0); 254 nodes1.remove(0); 279 255 for (int pos = 0; pos < nodes2.size(); pos ++) { 280 256 nodes1.add(0, nodes2.get(pos)); 281 257 } 282 258 283 259 return nodes1; 284 260 } else {
Note:
See TracChangeset
for help on using the changeset viewer.