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 get_tris_repr(self): if self.__triangulation is None: cdt = p2t.CDT( [Point2(*i) for i in self.__geometry.exterior.coords[:-1]]) for interior in self.__geometry.interiors: cdt.add_hole([Point2(*i) for i in interior.coords[:-1]]) self.__triangulation = cdt.triangulate() return self.__triangulation
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 triangulate(poly): """ Takes a polygon and returns a shape representing it with triangular pieces For now just returns the delaunay triangulation for testing (until I get a real triangulation library) """ cdt = p2t.CDT(poly) triangles = cdt.triangulate() pieces = [] for t in triangles: pieces.append( Piece([ Point(t.a.x, t.a.y), Point(t.b.x, t.b.y), Point(t.c.x, t.c.y) ])) return Shape(pieces)
def triangulate(cls, polygon): """ Triangulate the given polygon """ epoints, ipoints = cls.epoints_ipoints(polygon) plane = Plane(epoints) epoints = cls.preprocess(map(plane.to2D, epoints)) if not epoints: return [] cdt = p2t.CDT(epoints) for hole in ipoints: hole = cls.preprocess(map(plane.to2D, hole)) if hole: cdt.add_hole(hole) triangles2d = cdt.triangulate() return [list(map(plane.to3D, [t.a, t.b, t.c])) for t in triangles2d]
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