Changeset 12505 in josm


Ignore:
Timestamp:
2017-07-24T19:24:44+02:00 (2 years ago)
Author:
michael2402
Message:

Add a method that allows traversing an offset version of a MapViewPath

Location:
trunk/src/org/openstreetmap/josm
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/paint/OffsetIterator.java

    r11992 r12505  
    2323public class OffsetIterator implements Iterator<MapViewPoint> {
    2424    private final MapViewState mapState;
    25     private final List<Node> nodes;
     25    private final List<MapViewPoint> nodes;
    2626    private final double offset;
    2727    private int idx;
     
    3737    /**
    3838     * Creates a new offset iterator
     39     * @param nodes The nodes of the original line
     40     * @param offset The offset of the line.
     41     */
     42    public OffsetIterator(List<MapViewPoint> nodes, double offset) {
     43        if (nodes.size() < 2) {
     44            throw new IllegalArgumentException("There must be at least 2 nodes.");
     45        }
     46        this.mapState = nodes.get(0).getMapViewState();
     47        this.nodes = nodes;
     48        this.offset = offset;
     49    }
     50
     51    /**
     52     * Creates a new offset iterator
    3953     * @param mapState The map view state this iterator is for.
    4054     * @param nodes The nodes of the original line
     
    4357    public OffsetIterator(MapViewState mapState, List<Node> nodes, double offset) {
    4458        this.mapState = mapState;
    45         this.nodes = nodes.stream().filter(Node::isLatLonKnown).collect(Collectors.toList());
     59        this.nodes = nodes.stream().filter(Node::isLatLonKnown).map(mapState::getPointFor).collect(Collectors.toList());
    4660        this.offset = offset;
    47         idx = 0;
    4861    }
    4962
     
    160173
    161174    private MapViewPoint getForIndex(int i) {
    162         return mapState.getPointFor(nodes.get(i));
     175        return nodes.get(i);
    163176    }
    164177
  • trunk/src/org/openstreetmap/josm/gui/MapViewState.java

    r12213 r12505  
    419419     */
    420420    public abstract class MapViewPoint {
     421        /**
     422         * Gets the map view state this path is used for.
     423         * @return The state.
     424         * @since 12505
     425         */
     426        public MapViewState getMapViewState() {
     427            return MapViewState.this;
     428        }
    421429
    422430        /**
  • trunk/src/org/openstreetmap/josm/gui/draw/MapViewPath.java

    r12455 r12505  
    77import java.awt.geom.Path2D;
    88import java.awt.geom.PathIterator;
     9import java.util.ArrayList;
    910
    1011import org.openstreetmap.josm.data.coor.EastNorth;
    1112import org.openstreetmap.josm.data.coor.ILatLon;
     13import org.openstreetmap.josm.data.osm.visitor.paint.OffsetIterator;
    1214import org.openstreetmap.josm.gui.MapView;
    1315import org.openstreetmap.josm.gui.MapViewState;
     
    280282    public double getLength() {
    281283        return visitLine((inLineOffset, start, end, startIsOldEnd) -> { });
     284    }
     285
     286    /**
     287     * Create a new {@link MapViewPath} that is the same as the current one except that it is offset in the view.
     288     * @param viewOffset The offset in view pixels
     289     * @return The new path
     290     * @since 12505
     291     */
     292    public MapViewPath offset(double viewOffset) {
     293        OffsetPathVisitor visitor = new OffsetPathVisitor(state, viewOffset);
     294        visitor.visit(this);
     295        return visitor.getPath();
    282296    }
    283297
     
    447461    }
    448462
     463    private class OffsetPathVisitor extends AbstractMapPathVisitor {
     464        private final MapViewPath collector;
     465        private final ArrayList<MapViewPoint> points = new ArrayList<>();
     466        private final double offset;
     467
     468        OffsetPathVisitor(MapViewState state, double offset) {
     469            this.collector = new MapViewPath(state);
     470            this.offset = offset;
     471        }
     472
     473        @Override
     474        void visitMoveTo(MapViewPoint p) {
     475            finishLineSegment();
     476            points.add(p);
     477        }
     478
     479        @Override
     480        void visitLineTo(MapViewPoint p) {
     481            points.add(p);
     482        }
     483
     484        MapViewPath getPath() {
     485            finishLineSegment();
     486            return collector;
     487        }
     488
     489        private void finishLineSegment() {
     490            if (points.size() > 2) {
     491                OffsetIterator iterator = new OffsetIterator(points, offset);
     492                collector.moveTo(iterator.next());
     493                while (iterator.hasNext()) {
     494                    collector.lineTo(iterator.next());
     495                }
     496                points.clear();
     497            }
     498        }
     499    }
    449500}
Note: See TracChangeset for help on using the changeset viewer.