def offset(mesh,offset=1,doclose=True): newMesh=Mesh() # calculate vertex normals for vertex in mesh.vertices: vertex.vertex = Vertex(0,0,0) vertex.nfaces = 0 for face in mesh.faces: normal = faceUtils.normal(face) for vertex in face.vertices: vertex.vertex.add(normal) vertex.nfaces += 1 for vertex in mesh.vertices: vertex.vertex.scale(offset / vertex.nfaces) vertex.vertex.add(vertex) # create faces for face in mesh.faces: offsetVertices = [] for vertex in face.vertices: offsetVertices.append(vertex.vertex) offsetVertices.reverse() newFace = Face(offsetVertices) newMesh.faces.append(newFace) newMesh.faces.append(face) # create sides if doclose: for edge in mesh.edges: if edge.face1 == None or edge.face2 == None: offsetVertices = [edge.v1, edge.v2, edge.v2.vertex, edge.v1.vertex] if edge.face2 == None: offsetVertices.reverse() newFace = Face(offsetVertices) newMesh.faces.append(newFace) newMesh.updateAdjacencies() return newMesh
def extrudeTapered(face, height=0.0, fraction=0.5,doCap=True): """ Extrudes the face tapered like a window by creating an offset face and quads between every original edge and the corresponding new edge. Arguments: ---------- face : mola.core.Face The face to be extruded height : float The distance of the new face to the original face, default 0 fraction : float The relative offset distance, 0: original vertex, 1: center point default 0.5 (halfway) """ center_vertex = faceUtils.center(face) normal = faceUtils.normal(face) scaled_normal = vec.scale(normal, height) # calculate new vertex positions new_vertices = [] for i in range(len(face.vertices)): n1 = face.vertices[i] betw = vec.subtract(center_vertex, n1) betw = vec.scale(betw, fraction) nn = vec.add(n1, betw) nn = vec.add(nn, scaled_normal) new_vertices.append(nn) new_faces = [] # create the quads along the edges num = len(face.vertices) for i in range(num): n1 = face.vertices[i] n2 = face.vertices[(i + 1) % num] n3 = new_vertices[(i + 1) % num] n4 = new_vertices[i] new_face = Face([n1,n2,n3,n4]) new_faces.append(new_face) # create the closing cap face if doCap: cap_face = Face(new_vertices) new_faces.append(cap_face) for new_face in new_faces: faceUtils.copyProperties(face,new_face) return new_faces
def extrudeToPointCenter(face, height=0.0): """ Extrudes the face to the center point moved by height normal to the face and creating a triangular face from each edge to the point. Arguments: ---------- face : mola.core.Face The face to be extruded height : float The distance of the new point to the face center, default 0 """ normal = faceUtils.normal(face) normal = vec.scale(normal,height) center = faceUtils.center(face) center = vec.add(center,normal) return extrudeToPoint(face,center)
def splitRoof(face, height): """ Extrudes a pitched roof Arguments: ---------- face : mola.core.Face The face to be extruded height : mola.core.Vertex Th height of the roof """ faces = [] normal = faceUtils.normal(face) normal = vec.scale(normal,height) if len(face.vertices) == 4: ev1 = vec.center(face.vertices[0], face.vertices[1]) ev1 = vec.add(ev1, normal) ev2 = vec.center(face.vertices[2], face.vertices[3]) ev2 = vec.add(ev2, normal) faces.append(Face([face.vertices[0], face.vertices[1], ev1])) faces.append(Face([face.vertices[1], face.vertices[2], ev2, ev1])) faces.append(Face([face.vertices[2], face.vertices[3], ev2])) faces.append(Face([face.vertices[3], face.vertices[0], ev1, ev2])) for f in faces: faceUtils.copyProperties(face,f) return faces elif len(face.vertices) == 3: ev1 = vec.center(face.vertices[0], face.vertices[1]) ev1 = vec.add(ev1, normal) ev2 = vec.center(face.vertices[1], face.vertices[2]) ev2 = vec.add(ev2, normal) faces.append(Face([face.vertices[0], face.vertices[1], ev1])) faces.append(Face([face.vertices[1], ev2, ev1])) faces.append(Face([face.vertices[1], face.vertices[2], ev2])) faces.append(Face([face.vertices[2], face.vertices[0], ev1, ev2])) for f in faces: faceUtils.copyProperties(face, f) return faces return [face]
def extrude(face, height=0.0, capBottom=False, capTop=True): """ Extrudes the face straight by distance height. Arguments: ---------- face : mola.core.Face The face to be extruded height : float The extrusion distance, default 0 capBottom : bool Toggle if bottom face (original face) should be created, default False capTop : bool Toggle if top face (extrusion face) should be created, default True """ normal=faceUtils.normal(face) normal=vec.scale(normal,height) # calculate vertices new_vertices=[] for i in range(len(face.vertices)): new_vertices.append(vec.add(face.vertices[i], normal)) # faces new_faces=[] if capBottom: new_faces.append(face) for i in range(len(face.vertices)): i2=i+1 if i2>=len(face.vertices): i2=0 v0=face.vertices[i] v1=face.vertices[i2] v2=new_vertices[i2] v3=new_vertices[i] new_faces.append(Face([v0,v1,v2,v3])) if capTop: new_faces.append(Face(new_vertices)) for new_face in new_faces: faceUtils.copyProperties(face,new_face) return new_faces
def _translateFaceVertices(mesh,values): for face,value in zip(mesh.faces, values): normal=faceUtils.normal(face) normal.scale(value) face.vertex.add(normal)