org.openstreetmap.josm.tools
Class Geometry

java.lang.Object
  extended by org.openstreetmap.josm.tools.Geometry

public class Geometry
extends java.lang.Object

Some tools for geometry related tasks.


Nested Class Summary
static class Geometry.PolygonIntersection
           
 
Constructor Summary
Geometry()
           
 
Method Summary
static java.util.Set<Node> addIntersections(java.util.List<Way> ways, boolean test, java.util.List<Command> cmds)
          Will find all intersection and add nodes there for list of given ways.
static boolean angleIsClockwise(EastNorth commonNode, EastNorth firstNode, EastNorth secondNode)
          This method tests if secondNode is clockwise to first node.
static boolean angleIsClockwise(Node commonNode, Node firstNode, Node secondNode)
          This method tests if secondNode is clockwise to first node.
protected static double calcX(Node p1)
           
protected static double calcY(Node p1)
           
static double closedWayArea(Way way)
          Returns area of a closed way in square meters.
static EastNorth closestPointToLine(EastNorth lineP1, EastNorth lineP2, EastNorth point)
           
static EastNorth closestPointToSegment(EastNorth segmentP1, EastNorth segmentP2, EastNorth point)
          Calculates closest point to a line segment.
static EastNorth getCentroid(java.util.List<Node> nodes)
           
static double getCornerAngle(EastNorth p1, EastNorth p2, EastNorth p3)
          Returns angle of a corner defined with 3 point coordinates.
static EastNorth getLineLineIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4)
          Finds the intersection of two lines of infinite length.
private static BBox getNodesBounds(java.util.ArrayList<Node> nodes)
           
static EastNorth getSegmentAltituteIntersection(EastNorth sp1, EastNorth sp2, EastNorth ap)
          Returns the coordinate of intersection of segment sp1-sp2 and an altitude to it starting at point ap.
static double getSegmentAngle(EastNorth p1, EastNorth p2)
          Returns angle of a segment defined with 2 point coordinates.
static EastNorth getSegmentSegmentIntersection(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4)
          Finds the intersection of two line segments
static boolean isClockwise(Way w)
          Determines whether a way is oriented clockwise.
static boolean isToTheRightSideOfLine(Node lineP1, Node lineP2, Node lineP3, Node testPoint)
          Tests if given point is to the right side of path consisting of 3 points.
static boolean nodeInsidePolygon(Node point, java.util.List<Node> polygonNodes)
          Tests if point is inside a polygon.
static Geometry.PolygonIntersection polygonIntersection(java.util.List<Node> first, java.util.List<Node> second)
          Tests if two polygons intersect.
static boolean segmentsParallel(EastNorth p1, EastNorth p2, EastNorth p3, EastNorth p4)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Geometry

public Geometry()
Method Detail

addIntersections

public static java.util.Set<Node> addIntersections(java.util.List<Way> ways,
                                                   boolean test,
                                                   java.util.List<Command> cmds)
Will find all intersection and add nodes there for list of given ways. Handles self-intersections too. And makes commands to add the intersection points to ways. Prerequisite: no two nodes have the same coordinates.

Parameters:
ways - a list of ways to test
test - if false, do not build list of Commands, just return nodes
cmds - list of commands, typically empty when handed to this method. Will be filled with commands that add intersection nodes to the ways.
Returns:
list of new nodes

getNodesBounds

private static BBox getNodesBounds(java.util.ArrayList<Node> nodes)

isToTheRightSideOfLine

public static boolean isToTheRightSideOfLine(Node lineP1,
                                             Node lineP2,
                                             Node lineP3,
                                             Node testPoint)
Tests if given point is to the right side of path consisting of 3 points.

Parameters:
lineP1 - first point in path
lineP2 - second point in path
lineP3 - third point in path
testPoint -
Returns:
true if to the right side, false otherwise

angleIsClockwise

public static boolean angleIsClockwise(Node commonNode,
                                       Node firstNode,
                                       Node secondNode)
This method tests if secondNode is clockwise to first node.

Parameters:
commonNode - starting point for both vectors
firstNode - first vector end node
secondNode - second vector end node
Returns:
true if first vector is clockwise before second vector.

getSegmentSegmentIntersection

public static EastNorth getSegmentSegmentIntersection(EastNorth p1,
                                                      EastNorth p2,
                                                      EastNorth p3,
                                                      EastNorth p4)
Finds the intersection of two line segments

Returns:
EastNorth null if no intersection was found, the EastNorth coordinates of the intersection otherwise

getLineLineIntersection

public static EastNorth getLineLineIntersection(EastNorth p1,
                                                EastNorth p2,
                                                EastNorth p3,
                                                EastNorth p4)
Finds the intersection of two lines of infinite length.

Returns:
EastNorth null if no intersection was found, the coordinates of the intersection otherwise

segmentsParallel

public static boolean segmentsParallel(EastNorth p1,
                                       EastNorth p2,
                                       EastNorth p3,
                                       EastNorth p4)

closestPointToSegment

public static EastNorth closestPointToSegment(EastNorth segmentP1,
                                              EastNorth segmentP2,
                                              EastNorth point)
Calculates closest point to a line segment.

Parameters:
segmentP1 -
segmentP2 -
point -
Returns:
segmentP1 if it is the closest point, segmentP2 if it is the closest point, a new point if closest point is between segmentP1 and segmentP2.

closestPointToLine

public static EastNorth closestPointToLine(EastNorth lineP1,
                                           EastNorth lineP2,
                                           EastNorth point)

angleIsClockwise

public static boolean angleIsClockwise(EastNorth commonNode,
                                       EastNorth firstNode,
                                       EastNorth secondNode)
This method tests if secondNode is clockwise to first node.

Parameters:
commonNode - starting point for both vectors
firstNode - first vector end node
secondNode - second vector end node
Returns:
true if first vector is clockwise before second vector.

polygonIntersection

public static Geometry.PolygonIntersection polygonIntersection(java.util.List<Node> first,
                                                               java.util.List<Node> second)
Tests if two polygons intersect.

Parameters:
first -
second -
Returns:
intersection kind TODO: test segments, not only points TODO: is O(N*M), should use sweep for better performance.

nodeInsidePolygon

public static boolean nodeInsidePolygon(Node point,
                                        java.util.List<Node> polygonNodes)
Tests if point is inside a polygon. The polygon can be self-intersecting. In such case the contains function works in xor-like manner.

Parameters:
polygonNodes - list of nodes from polygon path.
point - the point to test
Returns:
true if the point is inside polygon.

closedWayArea

public static double closedWayArea(Way way)
Returns area of a closed way in square meters. (approximate(?), but should be OK for small areas) Relies on the current projection: Works correctly, when one unit in projected coordinates corresponds to one meter. This is true for most projections, but not for WGS84 and Mercator (EPSG:3857).

Parameters:
way - Way to measure, should be closed (first node is the same as last node)
Returns:
area of the closed way.

calcX

protected static double calcX(Node p1)

calcY

protected static double calcY(Node p1)

isClockwise

public static boolean isClockwise(Way w)
Determines whether a way is oriented clockwise. Internals: Assuming a closed non-looping way, compute twice the area of the polygon using the formula 2 * area = sum (X[n] * Y[n+1] - X[n+1] * Y[n]). If the area is negative the way is ordered in a clockwise direction. See http://paulbourke.net/geometry/polyarea/

Parameters:
w - the way to be checked.
Returns:
true if and only if way is oriented clockwise.
Throws:
java.lang.IllegalArgumentException - if way is not closed (see Way.isClosed()).

getSegmentAngle

public static double getSegmentAngle(EastNorth p1,
                                     EastNorth p2)
Returns angle of a segment defined with 2 point coordinates.

Parameters:
p1 -
p2 -
Returns:
Angle in radians (-pi, pi]

getCornerAngle

public static double getCornerAngle(EastNorth p1,
                                    EastNorth p2,
                                    EastNorth p3)
Returns angle of a corner defined with 3 point coordinates.

Parameters:
p1 -
p2 - Common endpoint
p3 -
Returns:
Angle in radians (-pi, pi]

getCentroid

public static EastNorth getCentroid(java.util.List<Node> nodes)

getSegmentAltituteIntersection

public static EastNorth getSegmentAltituteIntersection(EastNorth sp1,
                                                       EastNorth sp2,
                                                       EastNorth ap)
Returns the coordinate of intersection of segment sp1-sp2 and an altitude to it starting at point ap. If the line defined with sp1-sp2 intersects its altitude out of sp1-sp2, null is returned.

Parameters:
sp1 -
sp2 -
ap -
Returns:
Intersection coordinate or null


JOSM