def triangulate(loops: List[List[Vec]]) -> List[List[Vec]]: tessalator = glu.gluNewTess() vertices = [] def new_vertex(pos): vertices.append(pos[:2]) glu.gluTessProperty(tessalator, glu.GLU_TESS_WINDING_RULE, glu.GLU_TESS_WINDING_ODD) glu.gluTessCallback(tessalator, glu.GLU_TESS_EDGE_FLAG_DATA, lambda *args: None) glu.gluTessCallback(tessalator, glu.GLU_TESS_BEGIN, lambda *args: None) glu.gluTessCallback(tessalator, glu.GLU_TESS_VERTEX, new_vertex) glu.gluTessCallback(tessalator, glu.GLU_TESS_COMBINE, lambda *args: args[0]) glu.gluTessCallback(tessalator, glu.GLU_TESS_END, lambda: None) glu.gluTessBeginPolygon(tessalator, 0) for loop in loops: glu.gluTessBeginContour(tessalator) for point in loop: point = point[0],point[1], 0 glu.gluTessVertex(tessalator, point, point) glu.gluTessEndContour(tessalator) glu.gluTessEndPolygon(tessalator) glu.gluDeleteTess(tessalator) return [vertices[i:i+3] for i in range(0,len(vertices),3)]
def triangulate(self, polygon): """ Triangulates polygon :param polygon: shapely.geometry.polygon Polygon to tessellate :return: list, list Array of triangle vertex indices [t0i0, t0i1, t0i2, t1i0, t1i1, ... ] Array of polygon points [(x0, y0), (x1, y1), ... ] """ # Create tessellation object tess = GLU.gluNewTess() # Setup callbacks GLU.gluTessCallback(tess, GLU.GLU_TESS_BEGIN, self._on_begin_primitive) GLU.gluTessCallback(tess, GLU.GLU_TESS_VERTEX, self._on_new_vertex) GLU.gluTessCallback(tess, GLU.GLU_TESS_EDGE_FLAG, self._on_edge_flag) GLU.gluTessCallback(tess, GLU.GLU_TESS_COMBINE, self._on_combine) GLU.gluTessCallback(tess, GLU.GLU_TESS_ERROR, self._on_error) GLU.gluTessCallback(tess, GLU.GLU_TESS_END, self._on_end_primitive) # Reset data del self.tris[:] del self.pts[:] self.vertex_index = 0 # Define polygon GLU.gluTessBeginPolygon(tess, None) def define_contour(contour): vertices = list(contour.coords) # Get vertices coordinates if vertices[0] == vertices[-1]: # Open ring vertices = vertices[:-1] self.pts += vertices GLU.gluTessBeginContour(tess) # Start contour # Set vertices for vertex in vertices: point = (vertex[0], vertex[1], 0) GLU.gluTessVertex(tess, point, self.vertex_index) self.vertex_index += 1 GLU.gluTessEndContour(tess) # End contour # Polygon exterior define_contour(polygon.exterior) # Interiors for interior in polygon.interiors: define_contour(interior) # Start tessellation GLU.gluTessEndPolygon(tess) # Free resources GLU.gluDeleteTess(tess) return self.tris, self.pts
def __del__( self ): if hasattr( self,'__tess' ): # checking this might be speared, but shit happens ? GLU.gluDeleteTess( self.__tess )