Class HalfEdge
- java.lang.Object
-
- org.locationtech.jts.edgegraph.HalfEdge
-
- Direct Known Subclasses:
MarkHalfEdge
public class HalfEdge extends java.lang.Object
Represents a directed component of an edge in anEdgeGraph
. HalfEdges link vertices whose locations are defined byCoordinate
s. HalfEdges start at an origin vertex, and terminate at a destination vertex. HalfEdges always occur in symmetric pairs, with thesym()
method giving access to the oppositely-oriented component. HalfEdges and the methods on them form an edge algebra, which can be used to traverse and query the topology of the graph formed by the edges.To support graphs where the edges are sequences of coordinates each edge may also have a direction point supplied. This is used to determine the ordering of the edges around the origin. HalfEdges with the same origin are ordered so that the ring of edges formed by them is oriented CCW.
By design HalfEdges carry minimal information about the actual usage of the graph they represent. They can be subclassed to carry more information if required.
HalfEdges form a complete and consistent data structure by themselves, but an
EdgeGraph
is useful to allow retrieving edges by vertex and edge location, as well as ensuring edges are created and linked appropriately.- Author:
- Martin Davis
-
-
Constructor Summary
Constructors Constructor Description HalfEdge(Coordinate orig)
Creates a half-edge originating from a given coordinate.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
compareAngularDirection(HalfEdge e)
Implements the total order relation:int
compareTo(java.lang.Object obj)
Compares edges which originate at the same vertex based on the angle they make at their origin vertex with the positive X-axis.static HalfEdge
create(Coordinate p0, Coordinate p1)
Creates a HalfEdge pair representing an edge between two vertices located at coordinates p0 and p1.int
degree()
Computes the degree of the origin vertex.Coordinate
dest()
Gets the destination coordinate of this edge.boolean
equals(Coordinate p0, Coordinate p1)
Tests whether this edge has the given orig and dest vertices.HalfEdge
find(Coordinate dest)
Finds the edge starting at the origin of this edge with the given dest vertex, if any.void
insert(HalfEdge eAdd)
Inserts an edge into the ring of edges around the origin vertex of this edge, ensuring that the edges remain ordered CCW.boolean
isEdgesSorted()
Tests whether the edges around the origin are sorted correctly.void
link(HalfEdge sym)
Links this edge with its sym (opposite) edge.HalfEdge
next()
Gets the next edge CCW around the destination vertex of this edge, originating at that vertex.HalfEdge
oNext()
Gets the next edge CCW around the origin of this edge, with the same origin.Coordinate
orig()
Gets the origin coordinate of this edge.HalfEdge
prev()
Gets the previous edge CW around the origin vertex of this edge, with that vertex being its destination.HalfEdge
prevNode()
Finds the first node previous to this edge, if any.HalfEdge
sym()
Gets the symmetric pair edge of this edge.java.lang.String
toString()
Provides a string representation of a HalfEdge.java.lang.String
toStringNode()
Provides a string representation of the edges around the origin node of this edge.
-
-
-
Constructor Detail
-
HalfEdge
public HalfEdge(Coordinate orig)
Creates a half-edge originating from a given coordinate.- Parameters:
orig
- the origin coordinate
-
-
Method Detail
-
create
public static HalfEdge create(Coordinate p0, Coordinate p1)
Creates a HalfEdge pair representing an edge between two vertices located at coordinates p0 and p1.- Parameters:
p0
- a vertex coordinatep1
- a vertex coordinate- Returns:
- the HalfEdge with origin at p0
-
link
public void link(HalfEdge sym)
Links this edge with its sym (opposite) edge. This must be done for each pair of edges created.- Parameters:
sym
- the sym edge to link.
-
orig
public Coordinate orig()
Gets the origin coordinate of this edge.- Returns:
- the origin coordinate
-
dest
public Coordinate dest()
Gets the destination coordinate of this edge.- Returns:
- the destination coordinate
-
sym
public HalfEdge sym()
Gets the symmetric pair edge of this edge.- Returns:
- the symmetric pair edge
-
next
public HalfEdge next()
Gets the next edge CCW around the destination vertex of this edge, originating at that vertex. If the destination vertex has degree 1 then this is the sym edge.- Returns:
- the next outgoing edge CCW around the destination vertex
-
prev
public HalfEdge prev()
Gets the previous edge CW around the origin vertex of this edge, with that vertex being its destination.It is always true that
e.next().prev() == e
Note that this requires a scan of the origin edges, so may not be efficient for some uses.
- Returns:
- the previous edge CW around the origin vertex
-
oNext
public HalfEdge oNext()
Gets the next edge CCW around the origin of this edge, with the same origin. If the origin vertex has degree 1 then this is the edge itself.e.oNext()
is equal toe.sym().next()
- Returns:
- the next edge around the origin
-
find
public HalfEdge find(Coordinate dest)
Finds the edge starting at the origin of this edge with the given dest vertex, if any.- Parameters:
dest
- the dest vertex to search for- Returns:
- the edge with the required dest vertex, if it exists, or null
-
equals
public boolean equals(Coordinate p0, Coordinate p1)
Tests whether this edge has the given orig and dest vertices.- Parameters:
p0
- the origin vertex to testp1
- the destination vertex to test- Returns:
- true if the vertices are equal to the ones of this edge
-
insert
public void insert(HalfEdge eAdd)
Inserts an edge into the ring of edges around the origin vertex of this edge, ensuring that the edges remain ordered CCW. The inserted edge must have the same origin as this edge.- Parameters:
eAdd
- the edge to insert
-
isEdgesSorted
public boolean isEdgesSorted()
Tests whether the edges around the origin are sorted correctly. Note that edges must be strictly increasing, which implies no two edges can have the same direction point.- Returns:
- true if the origin edges are sorted correctly
-
compareTo
public int compareTo(java.lang.Object obj)
Compares edges which originate at the same vertex based on the angle they make at their origin vertex with the positive X-axis. This allows sorting edges around their origin vertex in CCW order.
-
compareAngularDirection
public int compareAngularDirection(HalfEdge e)
Implements the total order relation:The angle of edge a is greater than the angle of edge b, where the angle of an edge is the angle made by the first segment of the edge with the positive x-axis
When applied to a list of edges originating at the same point, this produces a CCW ordering of the edges around the point.
Using the obvious algorithm of computing the angle is not robust, since the angle calculation is susceptible to roundoff error. A robust algorithm is:
- First, compare the quadrants the edge vectors lie in. If the quadrants are different, it is trivial to determine which edge has a greater angle.
- if the vectors lie in the same quadrant, the
Orientation.index(Coordinate, Coordinate, Coordinate)
function can be used to determine the relative orientation of the vectors.
-
toString
public java.lang.String toString()
Provides a string representation of a HalfEdge.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string representation
-
toStringNode
public java.lang.String toStringNode()
Provides a string representation of the edges around the origin node of this edge. Uses the subclass representation for each edge.- Returns:
- a string showing the edges around the origin
-
degree
public int degree()
Computes the degree of the origin vertex. The degree is the number of edges originating from the vertex.- Returns:
- the degree of the origin vertex
-
prevNode
public HalfEdge prevNode()
Finds the first node previous to this edge, if any. A node has degree<> 2
. If no such node exists (i.e. the edge is part of a ring) then null is returned.- Returns:
- an edge originating at the node prior to this edge, if any, or null if no node exists
-
-