def triangulate_cdt(self): vertices = list() #print "preparing constrained delaunay triangulation" for coord_pair in self.src_polygon.exterior.coords: vertices.append(coord_pair) border = [p2t.Point(x, y) for x, y in vertices[1:]] #print "initializing cdt" cdt = p2t.CDT(border) #print "adding holes" for interior_ring in self.src_polygon.interiors: hole = list() for coord_pair in interior_ring.coords: hole.append(coord_pair) else: cdt.add_hole([p2t.Point(x, y) for x, y in hole[1:]]) #print "performing cdt" triangles = cdt.triangulate() #print "done" self.triangles = list() for t in triangles: triangle = Polygon([(t.a.x, t.a.y), (t.b.x, t.b.y), (t.c.x, t.c.y)]) self.triangles.append(triangle) else: self.triangles = sorted(self.triangles, key=attrgetter('area'), reverse=True)
def to2D(self, point): """ Get a 2D point from a 3D point by simply omitting the less significant coordinate Return an instance of poly2tri point because that's what we'll need """ p = point[:self.longest] + point[self.longest + 1:] return p2t.Point(*p)
def triangulate(self): """" Returns triangles that form the same shape as the polygon :return list of Triangle """ p2t_points = [p2t.Point(p[0], p[1]) for p in self.points] cdt = p2t.CDT(p2t_points) p2t_triangles = cdt.triangulate() triangles = [Triangle([t.a.x, t.a.y], [t.b.x, t.b.y], [t.c.x, t.c.y]) for t in p2t_triangles] return triangles
def cd_triangulate_single_polygon(self, polygon): u""" Perform a Constrained Delaunay Triangulation on a single polygon. The given polygon needs to be continuous (no multi-polygon) but may contain holes. """ # creating a complete list of the polygon's vertices vertices = list() for coord_pair in polygon.exterior.coords: vertices.append(coord_pair) # creating the necessary data structure for the triangulation (list of # vertex points) excluding the duplicated start and end vertex border = [p2t.Point(x, y) for x, y in vertices[1:]] # initializing triangulation cdt = p2t.CDT(border) # adding holes to triangulation configuration for interior_ring in polygon.interiors: hole = list() for coord_pair in interior_ring.coords: hole.append(coord_pair) else: cdt.add_hole([p2t.Point(x, y) for x, y in hole[1:]]) # performing triangulation and returning result return cdt.triangulate()
def triangulatePolygon( verts, holes = None ): polyline = [] x, y = None, None for i in range( 0, len( verts ), 2 ): xx, yy = verts[i], verts[i+1] if xx == x and yy == y: continue polyline.append( p2t.Point( xx, yy ) ) x = xx y = yy cdt = p2t.CDT( polyline ) if holes: for hole in holes: cdt.add_hole( hole ) triangles = cdt.triangulate() result = [] for t in triangles: result.append( t.a.x ) result.append( t.a.y ) result.append( t.b.x ) result.append( t.b.y ) result.append( t.c.x ) result.append( t.c.y ) return result
def test_points_on_line(self, points, point, on): points = map(lambda x: p2t.Point(*x), points) line = polygons.Line(*points) assert line.is_on(p2t.Point(*point)) == on