Ignore:
Timestamp:
2010-10-22T15:51:17+02:00 (14 years ago)
Author:
extropy
Message:

fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/pdfimport/src/pdfimport/PathOptimizer.java

    r23702 r23746  
    1010
    1111public class PathOptimizer {
    12        
     12
    1313        public Map<Point2D, Point2D> uniquePointMap;
    14         private Map<LayerInfo, LayerContents> layerMap;
     14        private final Map<LayerInfo, LayerContents> layerMap;
    1515        private List<LayerContents> layers;
    16        
     16
    1717        public PathOptimizer()
    1818        {
     
    3030                        this.uniquePointMap.put(point, point);
    3131                        return point;
    32                 }               
    33         }
    34        
     32                }
     33        }
     34
    3535        public void addPath(LayerInfo info, PdfPath path)
    3636        {
     
    3838                layer.paths.add(path);
    3939        }
    40                
     40
    4141        public void addMultiPath(LayerInfo info, List<PdfPath> paths) {
    4242                LayerContents layer = this.getLayer(info);
    4343                PdfMultiPath p = new PdfMultiPath(paths);
    44                 layer.multiPaths.add(p);                                       
    45         }       
    46        
     44                layer.multiPaths.add(p);
     45        }
     46
    4747        private LayerContents getLayer(LayerInfo info) {
    4848                LayerContents layer;
    49                
     49
    5050                if (this.layerMap.containsKey(info))
    5151                {
     
    6363                return layer;
    6464        }
    65        
     65
    6666        public void optimize()
    6767        {
    6868                for(LayerContents layer: this.layers) {
    69                         this.optimizeLayer(layer);                     
    70                 }
    71                
     69                        this.concatenataPaths(layer);
     70                }
     71
    7272                List<LayerContents> newLayers = new ArrayList<LayerContents>();
    7373                int nr = 0;
    74                
     74
    7575                for(LayerContents l: this.layers) {
    7676                        List<LayerContents> splitResult = splitBySegmentKind(l);
    77                        
     77
    7878                        for(LayerContents ll: splitResult) {
    7979                                ll.info.nr = nr;
     
    8282                        }
    8383                }
    84                
     84
    8585                this.layers = newLayers;
    86                
     86
    8787                for(LayerContents layer: this.layers) {
    8888                        finalizeLayer(layer);
     
    9393                Set<Point2D> points = new HashSet<Point2D>();
    9494                layer.points = new ArrayList<Point2D>();
    95                
     95
    9696                for(PdfPath pp: layer.paths){
    9797                        pp.layer = layer;
    98                        
     98
    9999                        for(Point2D point: pp.points){
    100100                                if (!points.contains(point)) {
     
    102102                                        points.add(point);
    103103                                }
    104                         }       
    105                 }
    106                
     104                        }
     105                }
     106
    107107                for (PdfMultiPath multipath: layer.multiPaths) {
    108108                        multipath.layer = layer;
    109109                        for(PdfPath pp: multipath.paths){
    110110                                pp.layer = layer;
    111                                
     111
    112112                                for(Point2D point: pp.points){
    113113                                        if (!points.contains(point)) {
     
    119119                }
    120120        }
    121        
     121
    122122        /**
    123          * This method merges together paths with common end nodes. 
     123         * This method merges together paths with common end nodes.
    124124         * @param layer the layer to process.
    125125         */
    126         private void optimizeLayer(LayerContents layer) {
     126        private void concatenataPaths(LayerContents layer) {
    127127                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
    154130                for(PdfPath pp: layer.paths){
    155                                                
     131
    156132                        PdfPath path = pp;
    157133                        boolean changed = true;
    158                        
     134
    159135                        while (changed && !path.isClosed()) {
    160136                                changed  = false;
    161                                
    162                                 if (pathEndpoints.containsKey(path.firstPoint())){                                     
     137
     138                                if (pathEndpoints.containsKey(path.firstPoint())) {
    163139                                        PdfPath p1 = pathEndpoints.get(path.firstPoint());
    164140                                        pathEndpoints.remove(p1.firstPoint());
    165141                                        pathEndpoints.remove(p1.lastPoint());
    166                                                                        
     142
    167143                                        List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points);
    168                                         path.points = newNodes;                                                                 
     144                                        path.points = newNodes;
    169145                                        mergedPaths.add(p1);
    170146                                        changed = true;
    171147                                }
    172                                
    173                                 if (pathEndpoints.containsKey(path.lastPoint())){                                       
     148
     149                                if (pathEndpoints.containsKey(path.lastPoint())) {
    174150                                        PdfPath p1 = pathEndpoints.get(path.lastPoint());
    175151                                        pathEndpoints.remove(p1.firstPoint());
    176152                                        pathEndpoints.remove(p1.lastPoint());
    177                                                                        
     153
    178154                                        List<Point2D> newNodes = tryMergeNodeLists(path.points, p1.points);
    179                                         path.points = newNodes;                                                                 
     155                                        path.points = newNodes;
    180156                                        mergedPaths.add(p1);
    181157                                        changed = true;
    182                                 }                               
    183                         }       
    184                        
     158                                }
     159                        }
     160
    185161                        if (!path.isClosed()){
    186162                                pathEndpoints.put(path.firstPoint(), path);
     
    188164                        }
    189165                }
    190                
     166
    191167                List<PdfPath> resultPaths = new ArrayList<PdfPath>();
    192                
     168
    193169                for(PdfPath path: layer.paths) {
    194170                        if (!mergedPaths.contains(path)){
     
    196172                        }
    197173                }
    198                                
     174
    199175                layer.paths = resultPaths;
    200176        }
    201        
     177
    202178        private List<LayerContents> splitBySegmentKind(LayerContents layer)
    203179        {
     
    205181                List<PdfPath> multiSegmentPaths = new ArrayList<PdfPath>();
    206182                List<PdfPath> closedPaths = new ArrayList<PdfPath>();
    207                
     183
    208184                for(PdfPath path: layer.paths) {
    209185                        if (path.points.size() <= 3) {
     
    217193                        }
    218194                }
    219                
     195
    220196                List<LayerContents> layers = new ArrayList<LayerContents>();
    221                
     197
    222198                if (multiSegmentPaths.size() > 0) {
    223199                        LayerContents l = new LayerContents();
    224200                        l.paths = multiSegmentPaths;
    225201                        l.info = layer.info.copy();
    226                        
     202
    227203                        layers.add(l);
    228204                }
    229                
     205
    230206                if (singleSegmentPaths.size() > 0) {
    231207                        LayerContents l = new LayerContents();
     
    242218                        layers.add(l);
    243219                }
    244                
     220
    245221                return layers;
    246222        }
    247        
    248        
     223
     224
    249225
    250226        private List<Point2D> tryMergeNodeLists(List<Point2D> nodes1, List<Point2D> nodes2) {
    251                
     227
    252228                boolean nodes1Closed = (nodes1.get(0) == nodes1.get(nodes1.size() - 1));
    253229                boolean nodes2Closed = (nodes2.get(0) == nodes2.get(nodes2.size() - 1));
    254                
     230
    255231                if (nodes1Closed || nodes2Closed) {
    256232                        return null;
    257233                }
    258                
     234
    259235                if (nodes1.get(nodes1.size() - 1) == nodes2.get(0)) {
    260236                        nodes1.remove(nodes1.size() -1);
     
    267243                                nodes1.add(nodes2.get(pos));
    268244                        }
    269                        
    270                         return nodes1;
    271                 } 
     245
     246                        return nodes1;
     247                }
    272248                else if (nodes1.get(0) == nodes2.get(nodes2.size() - 1)) {
    273249                        nodes1.remove(0);
    274250                        nodes1.addAll(0, nodes2);
    275251                        return nodes1;
    276                 } 
     252                }
    277253                else if (nodes1.get(0) == nodes2.get(0)) {
    278                         nodes1.remove(0);                       
     254                        nodes1.remove(0);
    279255                        for (int pos = 0; pos < nodes2.size(); pos ++) {
    280256                                nodes1.add(0, nodes2.get(pos));
    281257                        }
    282                        
     258
    283259                        return nodes1;
    284260                } else {
Note: See TracChangeset for help on using the changeset viewer.