Skip to content

Commit

Permalink
Added documentation for the half edge.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatoom committed Apr 5, 2021
1 parent 9858ff8 commit a5b21ef
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 8 deletions.
89 changes: 81 additions & 8 deletions voronoi/graph/half_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,49 @@

class HalfEdge:
def __init__(self, incident_point, twin=None, origin=None):
"""
Edges are normally treated as undirected and shared between faces. However, for some tasks (such as simplifying
or cleaning geometry) it is useful to view faces as each having their own edges.
You can think of this as splitting each shared undirected edge along its length into two half edges.
(Boundary edges of course will only have one "half-edge".)
Each half-edge is directed (it has a start vertex and an end vertex).
The half-edge properties let you quickly find a half-edge’s source and destination vertex, the next half-edge,
get the other half-edge from the same edge, find all half-edges sharing a given point, and other manipulations.
Examples
--------
Get the half-edge's source
>>> edge.origin
Get the half-edge's destination
>>> edge.target # or edge.twin.origin
Get the previous and next half-edge
>>> edge.prev
>>> edge.next
Get the other half-edge from the same edge
>>> edge.twin
Find all half-edges sharing a given point
>>> edge.origin.connected_edges
Parameters
----------
incident_point: Point
The cell point of which this edge is the border
twin: HalfEdge
The other half-edge from the same edge
origin: Breakpoint or Vertex
The origin of the half edge. Can be a Breakpoint or a Vertex during construction, and only Vertex when
the diagram is finished.
"""

# Pointer to the origin. Can be breakpoint or vertex.
self.origin = origin
Expand All @@ -25,18 +68,35 @@ def __repr__(self):
return f"{self.incident_point}/{self.twin.incident_point or '-'}"

def set_next(self, next):
"""
Update the `next`-property for this edge and set the `prev`-property on the `next`-edge to the current edge.
Parameters
----------
next: HalfEdge
The next edge
"""
if next:
next.prev_edge = self
next.prev = self
self.next = next

def get_origin(self, y=None, max_y=None):
"""
Get the point of origin.
:param y: Sweep line (only used when the Voronoi diagram is under construction and we need to calculate
where it currently is)
:param max_y: Bounding box top for clipping infinite breakpoints
:return: The point of origin, or None
Get the coordinates of the edge's origin.
During construction of the Voronoi diagram, the origin can be a vertex, which has a fixed location, or a
breakpoint, which is a breakpoint between two moving arcs. In the latter case, we need to calculate the
position based on the `y`-coordinate of the sweep line.
Parameters
----------
y: Decimal
The y-coordinate of the sweep line.
max_y:
Bounding box top for clipping infinitely highly positioned breakpoints.
Returns
-------
origin: Coordinate
"""
if isinstance(self.origin, Vertex):
if self.origin.xd is None or self.origin.yd is None:
Expand All @@ -50,18 +110,31 @@ def get_origin(self, y=None, max_y=None):

@property
def twin(self):
"""
Get the other half-edge from the same edge
Returns
-------
twin: HalfEdge
"""
return self._twin

@twin.setter
def twin(self, twin):

if twin is not None:
twin._twin = self

self._twin = twin

@property
def target(self):
"""
The twin's origin.
Returns
-------
vertex: Vertex
"""
if self.twin is None:
return None
return self.twin.origin
Expand Down
24 changes: 24 additions & 0 deletions voronoi/visualization/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ def __init__(self, voronoi, canvas_offset=1, figsize=(8, 8)):
"""
A visualizer for your voronoi diagram.
Examples
--------
Quickly plot individual components of the graph.
>>> vis = Visualizer(voronoi, canvas_offset=1)
>>> vis.plot_sites(show_labels=True)
>>> vis.plot_edges(show_labels=False)
>>> vis.plot_vertices()
>>> vis.plot_border_to_site()
>>> vis.show()
Chaining commands
>>> Visualizer(voronoi, 1).plot_sites().plot_edges().plot_vertices().show()
Plot all components that are useful to visualize during construction of the diagram
>>> from voronoi.visualization import Presets
>>> Visualizer(voronoi, 1).plot_all(**Presets.construction)
Plot all components that are useful to visualize when the diagram is constructed
>>> Visualizer(voronoi, 1).plot_all()
Parameters
----------
voronoi: Voronoi
Expand Down

0 comments on commit a5b21ef

Please sign in to comment.