Class ParallelWays
java.lang.Object
org.openstreetmap.josm.actions.mapmode.ParallelWays
Helper for
ParallelWayAction.
Computes a one-sided offset ("parallel") of a branchless path made of one or more ways.
The algorithm is a proper one-sided buffer, which also works when the offset is much larger than the length of the segments of the source path (e.g. a maritime boundary 22 km off a coastline):
- Each segment is offset by the requested distance.
- At every vertex the two neighbouring offset segments are joined: on the inner side of a turn they are clipped at their intersection, on the outer side a mitre is used for gentle turns and a circular arc (approximated by chords) for sharp turns or when the mitre would overshoot too far.
- The resulting raw polyline is split at its self-intersections. Every piece is kept only if it lies at (at least) the offset distance from the source path. Pieces which are closer belong to inverted loops ("swallowtails") and are dropped. The remaining pieces are chained; the longest chain is the result.
Contrary to earlier versions the nodes and ways are only created (and added to the data set) by
commit(), since the number of nodes of the result depends on the offset. Use
getOffsetPoints() to draw a preview while the offset is changed.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final classA chain of connected valid piecesstatic final classThe point of the source path closest to a given point, seeclosestPoint(EastNorth).private static final classGrowable list of pieces (sub segments of the raw polyline)private static final classPoint table used while trimming: raw points first, then intersection and transition pointsprivate static final classThe raw (untrimmed) offset polyline. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final doubleprivate static final intmarker for raw segments of the end caps, which are only used to trim the result and are never part of itprivate final booleanprivate final booleanstatic final doubleDefault angular step used to approximate circular arcs, in degreesprivate final double[]unit direction of segment iprivate final double[]private doubleprivate int[][]private intprivate doubleprivate doubleprivate intprivate intprivate int[]private static final intprivate final intprivate final double[]private final double[]private booleanprivate int[]private int[]private final double[]private final double[]private final double[]private final double[]bounding boxes of the source segments, used to speed up distance computationsprivate final double[]private final int[]source way index for each source segmentthe source nodes, in path order (duplicates by coordinate removed, oriented like the reference way)private final boolean[]whether the source way runs in the same direction as sortedNodes (aligned with sourceWays) -
Constructor Summary
ConstructorsConstructorDescriptionParallelWays(Collection<Way> sourceWays, boolean copyTags, int refWayIndex) Constructs a newParallelWays.ParallelWays(Collection<Way> sourceWays, boolean copyTags, int refWayIndex, double arcStepDegrees) Constructs a newParallelWays. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddCap(ParallelWays.RawPolyline raw, int k, double startAngle, int direction, double d, boolean leading) Adds the chords of a half circle around nodek, starting at the given angle.private voidaddJoin(ParallelWays.RawPolyline raw, int k, int prev, int next, double d) Adds the offset points at vertexk, where segmentprevends and segmentnextstarts.private voidbuildGrid(double r) Builds a uniform grid over the source segments, with a cell size of (at least) r, so that all segments within distance r of a point are found in the 3x3 cells around it.private ParallelWays.RawPolylinebuildRawOffset(double d) Builds the (not yet added) ways from the last computed offset path.voidchangeOffset(double d) Offsets the way(s) d units.private voidclassify(ParallelWays.RawPolyline raw, double r, ParallelWays.Points points, ParallelWays.Pieces pieces, int i, int sId, int eId) Determines the validity of the piece of raw segmentibetween two points, and adds the resulting piece(s).Finds the point of the source path closest to the given point.voidcommit()Creates the nodes and ways of the offset path (as computed by the last call tochangeOffset(double)), and adds them to the edit data set by adding a new sequence command to the undo/redo queue.private doubledistanceToSource(double x, double y, double cutoff) Distance from a point to the source path.private static ParallelWays.ChainfindChainEndingAt(List<ParallelWays.Chain> chains, int id, ParallelWays.Points points, double tolerance) Finds the chain ending at (or within the tolerance of) the given point, preferring an exact match.private voidfindSelfIntersections(ParallelWays.RawPolyline raw, int rawSegCount, boolean[] skip, ParallelWays.Points points, List<List<double[]>> breaks) Finds all intersections between non adjacent segments of the raw polyline (sweep on x).private voidforEachCell(double minX, double minY, double maxX, double maxY, IntConsumer consumer) Returns the points of the offset path computed by the last call tochangeOffset(double).getWays()Returns the resulting parallel ways, available aftercommit().booleanDetermines if the nodes graph form a closed pathprivate static booleanChecks whether a way runs in the same direction as a node list.booleanDetermines if the result of the lastchangeOffset(double)call is a closed ring.private booleanisValid(double x, double y, double r, double tolerance, int arc) Checks whether a point of the raw polyline lies at (at least) distance r from the source path.private doublelocateTransition(double[] s, double[] e, double a, double b, double r, double tolerance, int arc) Locates (by bisection) the parameter betweenaandbwhere the validity changes.private NodemakeNode(int idx) private static voidreplaceIntactArcsByMitres(ParallelWays.RawPolyline raw, ParallelWays.Points points, List<Integer> ids, List<Integer> segs) Replaces arcs which are completely part of the result by a mitre (where the corner is gentle enough).private static booleansegmentIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] uv) Segment/segment intersection.private voidprivate voidtrim(ParallelWays.RawPolyline raw, double r)
-
Field Details
-
DEFAULT_ARC_STEP_DEGREES
Default angular step used to approximate circular arcs, in degrees- See Also:
-
NO_NODE
- See Also:
-
CAP
marker for raw segments of the end caps, which are only used to trim the result and are never part of it- See Also:
-
sourceWays
-
copyTags
-
arcStep
-
sortedNodes
the source nodes, in path order (duplicates by coordinate removed, oriented like the reference way) -
closed
-
nodeCount
-
px
-
py
-
dirX
unit direction of segment i -
dirY
-
segLen
-
segMinX
bounding boxes of the source segments, used to speed up distance computations -
segMinY
-
segMaxX
-
segMaxY
-
wayForward
whether the source way runs in the same direction as sortedNodes (aligned with sourceWays) -
segWay
source way index for each source segment -
gridCell
-
gridMinX
-
gridMinY
-
gridCols
-
gridRows
-
gridCells
-
gridStamp
-
gridQuery
-
resultPts
-
resultPieceSeg
-
resultPointNode
-
resultClosed
-
ways
-
-
Constructor Details
-
ParallelWays
Constructs a newParallelWays.- Parameters:
sourceWays- source wayscopyTags- whether tags should be copiedrefWayIndex- Need a reference way to determine the direction of the offset when we manage multiple ways- Throws:
IllegalArgumentException- if the ways do not form a branchless path
-
ParallelWays
public ParallelWays(Collection<Way> sourceWays, boolean copyTags, int refWayIndex, double arcStepDegrees) Constructs a newParallelWays.- Parameters:
sourceWays- source wayscopyTags- whether tags should be copiedrefWayIndex- Need a reference way to determine the direction of the offset when we manage multiple waysarcStepDegrees- angular step (in degrees) of the chords approximating arcs at convex corners- Throws:
IllegalArgumentException- if the ways do not form a branchless path- Since:
- 19624
-
-
Method Details
-
isForward
Checks whether a way runs in the same direction as a node list.- Parameters:
path- the node listway- the waycyclic- whether the node list is a ring (without repeated closing node)- Returns:
trueif the first segment of the way, found in the path, has the same orientation
-
isClosedPath
Determines if the nodes graph form a closed path- Returns:
trueif the nodes graph form a closed path
-
closestPoint
Finds the point of the source path closest to the given point. Used to relate the offset to the part of the path the mouse is currently next to (rather than to the segment where the drag started).- Parameters:
p- the point (projected coordinates)- Returns:
- the closest point, the segment it lies on and the signed distance
- Since:
- 19624
-
changeOffset
Offsets the way(s) d units. Positive d means to the left (relative to the reference way)- Parameters:
d- offset
-
buildGrid
Builds a uniform grid over the source segments, with a cell size of (at least) r, so that all segments within distance r of a point are found in the 3x3 cells around it.- Parameters:
r- the (absolute) offset
-
forEachCell
-
buildRawOffset
-
addCap
private void addCap(ParallelWays.RawPolyline raw, int k, double startAngle, int direction, double d, boolean leading) Adds the chords of a half circle around nodek, starting at the given angle. The chords are marked asCAP: they are only used to trim other pieces.- Parameters:
raw- the raw polylinek- source node indexstartAngle- angle of the first point of the half circledirection- rotation direction (+1 counter clockwise)d- the offsetleading-trueif the cap precedes the offset path (the end point of the half circle is then added by the caller),falseif it follows it (the start point has been added by the caller)
-
addJoin
Adds the offset points at vertexk, where segmentprevends and segmentnextstarts.- Parameters:
raw- the raw polylinek- source node indexprev- index of the segment ending at knext- index of the segment starting at kd- the offset
-
trim
-
setEmptyResult
-
classify
private void classify(ParallelWays.RawPolyline raw, double r, ParallelWays.Points points, ParallelWays.Pieces pieces, int i, int sId, int eId) Determines the validity of the piece of raw segmentibetween two points, and adds the resulting piece(s). The validity is sampled at both ends and in the middle; if it changes, the transition point is located and the piece is split there. -
locateTransition
private double locateTransition(double[] s, double[] e, double a, double b, double r, double tolerance, int arc) Locates (by bisection) the parameter betweenaandbwhere the validity changes.- Returns:
- the parameter of the transition
-
isValid
Checks whether a point of the raw polyline lies at (at least) distance r from the source path.- Parameters:
x- pointy- pointr- offsettolerance- allowed deficitarc- if the point lies on an arc chord: the center node of the arc; the point is then projected onto the arc before testing, elseNO_NODE- Returns:
trueif the point is valid
-
replaceIntactArcsByMitres
private static void replaceIntactArcsByMitres(ParallelWays.RawPolyline raw, ParallelWays.Points points, List<Integer> ids, List<Integer> segs) Replaces arcs which are completely part of the result by a mitre (where the corner is gentle enough). -
findChainEndingAt
private static ParallelWays.Chain findChainEndingAt(List<ParallelWays.Chain> chains, int id, ParallelWays.Points points, double tolerance) Finds the chain ending at (or within the tolerance of) the given point, preferring an exact match.- Returns:
- the chain, or
null
-
findSelfIntersections
private void findSelfIntersections(ParallelWays.RawPolyline raw, int rawSegCount, boolean[] skip, ParallelWays.Points points, List<List<double[]>> breaks) Finds all intersections between non adjacent segments of the raw polyline (sweep on x).- Parameters:
raw- the raw polylinerawSegCount- number of raw segmentsskip- raw segments to ignorepoints- point table, intersection points are appendedbreaks- per raw segment list of (param, pointId), filled
-
segmentIntersection
private static boolean segmentIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] uv) Segment/segment intersection.- Parameters:
uv- output: parameters along the first and the second segment- Returns:
- true if the segments intersect (touching end points count as intersection)
-
distanceToSource
Distance from a point to the source path.- Parameters:
x- pointy- pointcutoff- the search can stop as soon as a distance below this value is found- Returns:
- the distance (or any value below cutoff if such a distance exists)
-
getOffsetPoints
Returns the points of the offset path computed by the last call tochangeOffset(double). For a closed result the first point is not repeated at the end, seeisResultClosed().- Returns:
- the offset points (projected coordinates), empty if nothing has been computed or nothing remains
- Since:
- 19624
-
isResultClosed
Determines if the result of the lastchangeOffset(double)call is a closed ring.- Returns:
trueif the offset path is a closed ring- Since:
- 19624
-
commit
Creates the nodes and ways of the offset path (as computed by the last call tochangeOffset(double)), and adds them to the edit data set by adding a new sequence command to the undo/redo queue.Does nothing if there is no offset path.
-
makeAddWayAndNodesCommandList
-
buildWays
Builds the (not yet added) ways from the last computed offset path.- Returns:
- the ways, in the order of the source ways; ways swallowed by the offset are omitted
-
makeNode
-
getWays
Returns the resulting parallel ways, available aftercommit().- Returns:
- the resulting parallel ways (empty before commit)
-