voronoi.py revision 82d65f7901b1e62f55b5fa161ad7b5491405ef70
#############################################################################
#
# Voronoi diagram calculator/ Delaunay triangulator
# Translated to Python by Bill Simons
# September, 2005
#
# Calculate Delaunay triangulation or the Voronoi polygons for a set of
# 2D input points.
#
# Derived from code bearing the following notice:
#
# The author of this software is Steven Fortune. Copyright (c) 1994 by AT&T
# Bell Laboratories.
# Permission to use, copy, modify, and distribute this software for any
# purpose without fee is hereby granted, provided that this entire notice
# is included in all copies of any software which is or includes a copy
# or modification of this software and in all copies of the supporting
# documentation for such software.
# THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
# WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR AT&T MAKE ANY
# REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
# OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
#
# Comments were incorporated from Shane O'Sullivan's translation of the
# original code into C++ (http://mapviewer.skynet.ie/voronoi.html)
#
# Steve Fortune's homepage: http://netlib.bell-labs.com/cm/cs/who/sjf/index.html
#
#############################################################################
def usage():
print """
voronoi - compute Voronoi diagram or Delaunay triangulation
voronoi [-t -p -d] [filename]
Voronoi reads from filename (or standard input if no filename given) for a set
of points in the plane and writes either the Voronoi diagram or the Delaunay
triangulation to the standard output. Each input line should consist of two
real numbers, separated by white space.
If option -t is present, the Delaunay triangulation is produced.
Each output line is a triple i j k, which are the indices of the three points
in a Delaunay triangle. Points are numbered starting at 0.
If option -t is not present, the Voronoi diagram is produced.
There are four output record types.
s a b indicates that an input point at coordinates a b was seen.
l a b c indicates a line with equation ax + by = c.
v a b indicates a vertex at a b.
e l v1 v2 indicates a Voronoi segment which is a subsegment of line number l
with endpoints numbered v1 and v2. If v1 or v2 is -1, the line
extends to infinity.
Other options include:
d Print debugging info
p Produce output suitable for input to plot (1), rather than the forms
described above.
On unsorted data uniformly distributed in the unit square, voronoi uses about
20n+140 bytes of storage.
AUTHOR
Steve J. Fortune (1987) A Sweepline Algorithm for Voronoi Diagrams,
Algorithmica 2, 153-174.
"""
#############################################################################
#
# For programmatic use two functions are available:
#
# computeVoronoiDiagram(points)
#
# Takes a list of point objects (which must have x and y fields).
# Returns a 3-tuple of:
#
# (1) a list of 2-tuples, which are the x,y coordinates of the
# Voronoi diagram vertices
# (2) a list of 3-tuples (a,b,c) which are the equations of the
# lines in the Voronoi diagram: a*x + b*y = c
# (3) a list of 3-tuples, (l, v1, v2) representing edges of the
# Voronoi diagram. l is the index of the line, v1 and v2 are
# the indices of the vetices at the end of the edge. If
# v1 or v2 is -1, the line extends to infinity.
#
# computeDelaunayTriangulation(points):
#
# Takes a list of point objects (which must have x and y fields).
# Returns a list of 3-tuples: the indices of the points that form a
# Delaunay triangle.
#
#############################################################################
import math
import sys
import getopt
TOLERANCE = 1e-9
BIG_FLOAT = 1e38
#------------------------------------------------------------------
self.edges = [] # edge 3-tuple: (line index, vertex 1 index, vertex 2 index) if either vertex index is -1, the edge extends to infiinity
pass
pass
pass
print "site (%d) at %f %f" % (s.sitenum, s.x, s.y)
elif(self.triangulate):
pass
print "s %f %f" % (s.x, s.y)
print "vertex(%d) at %f %f" % (s.sitenum, s.x, s.y)
elif(self.triangulate):
pass
print "v %f %f" % (s.x,s.y)
print "line(%d) %gx+%gy=%g, bisecting %d %d" % (edge.edgenum, edge.a, edge.b, edge.c, edge.reg[0].sitenum, edge.reg[1].sitenum)
elif(self.triangulate):
sitenumL = -1
sitenumR = -1
if(not self.triangulate):
print " %d " % sitenumL,
print "%d" % sitenumR
#------------------------------------------------------------------
while True:
# newsite is smallest - this is a site event
# get first Halfedge to the LEFT and RIGHT of the new site
# if this halfedge has no edge, bot = bottom site (whatever that is)
# create a new edge that bisects
# create a new Halfedge, setting its pm field to 0 and insert
# this new bisector edge between the left and right vectors in
# a linked list
# if the new bisector intersects with the left edge, remove
# the left edge's vertex, and put in the new one
if p is not None:
# create a new Halfedge, setting its pm field to 1
# insert the new Halfedge to the right of the original bisector
# if this new bisector intersects with the right Halfedge
if p is not None:
# push the Halfedge into the ordered linked list of vertices
# intersection is smallest - this is a vector (circle) event
# pop the Halfedge with the lowest vector off the ordered list of
# vectors. Get the Halfedge to the left and right of the above HE
# and also the Halfedge to the right of the right HE
# get the Site to the left of the left HE and to the right of
# the right HE which it bisects
# output the triple of sites, stating that a circle goes through them
# get the vertex that caused this event and set the vertex number
# couldn't do this earlier since we didn't know when it would be processed
# set the endpoint of the left and right Halfedge to be this vector
# delete the lowest HE, remove all vertex events to do with the
# right HE and delete the right HE
# if the site to the left of the event is higher than the Site
# to the right of it, then swap them and set 'pm' to RIGHT
# Create an Edge (or line) that is between the two Sites. This
# creates the formula of the line, and assigns a line number to it
# create a HE from the edge
# insert the new bisector to the right of the left HE
# set one endpoint to the new edge to be the vector point 'v'
# If the site to the left of this bisector is higher than the right
# Site, then this endpoint is put in position 0; otherwise in pos 1
# if left HE and the new bisector don't intersect, then delete
# the left HE, and reinsert it
if p is not None:
# if right HE and the new bisector don't intersect, then reinsert it
if p is not None:
else:
break
#------------------------------------------------------------------
# is nearly equal to within the allowed relative error
#------------------------------------------------------------------
self.x = x
self.y = y
return -1
return 1
return -1
return 1
else:
return 0
#------------------------------------------------------------------
LE = 0
RE = 1
EDGE_NUM = 0
DELETED = {} # marker value
self.a = 0.0
self.b = 0.0
self.c = 0.0
return False
return True
# to begin with, there are no endpoints on the bisector - it goes to infinity
# ep[0] and ep[1] are None
# get the difference in x dist between the sites
# get the slope of the line
# set formula of line, with x fixed to 1
newedge.a = 1.0
else:
# set formula of line, with y fixed to 1
newedge.b = 1.0
if dy <= 0:
dy = 0.01
return newedge
#------------------------------------------------------------------
print "Halfedge--------------------------"
print "vertex: ",
else: print "None"
return 1
return -1
return 1
return -1
else:
return 0
return default
else:
return default
else:
# returns True if p is to right of halfedge self
return True
return False
if(e.a == 1.0):
fast = 0;
else:
if(e.b < 0.0):
if (not above):
fast = 1
if (not fast):
if(e.b < 0.0):
else: # e.b == 1.0
return above
else:
return not above
#--------------------------
# create a new site where the Halfedges el1 and el2 intersect
return None
# if the two edges bisect the same parent return None
return None
if isEqual(d,0.0):
return None
e = e1
else:
e = e2
return None
# create a new site at the point of intersection - this is a new
# vector event waiting to happen
#------------------------------------------------------------------
# Get entry from hash table, pruning any deleted nodes
return None
return he
# Hash table points to deleted half edge. Patch as necessary.
return None
# Use hash table to get close to desired halfedge
if(bucket < 0):
bucket =0;
if(he is None):
i = 1
while True:
if (he is not None): break;
if (he is not None): break;
i += 1
# Now search linear list of halfedges for the corect one
else:
# Update hash table and reference counts
return he
#------------------------------------------------------------------
class PriorityQueue(object):
return bucket
return Site(x,y)
def popMinHalfedge(self):
return curr
#------------------------------------------------------------------
try:
except StopIteration:
return None
#------------------------------------------------------------------
def computeVoronoiDiagram(points):
""" Takes a list of point objects (which must have x and y fields).
Returns a 3-tuple of:
(1) a list of 2-tuples, which are the x,y coordinates of the
Voronoi diagram vertices
(2) a list of 3-tuples (a,b,c) which are the equations of the
lines in the Voronoi diagram: a*x + b*y = c
(3) a list of 3-tuples, (l, v1, v2) representing edges of the
Voronoi diagram. l is the index of the line, v1 and v2 are
the indices of the vetices at the end of the edge. If
v1 or v2 is -1, the line extends to infinity.
"""
#------------------------------------------------------------------
""" Takes a list of point objects (which must have x and y fields).
Returns a list of 3-tuples: the indices of the points that form a
Delaunay triangle.
"""
#-----------------------------------------------------------------------------
if __name__=="__main__":
try:
except getopt.GetoptError:
usage()
doHelp = 0
c = Context()
c.doPrint = 1
if not doHelp:
pts = []
usage()