def makeMeshSlow(self): if self.Xs.shape[2] == 0: return X = self.Xs[:, :, 0] Y = self.Ys[:, :, 0] Z = self.Zs[:, :, 0] mesh = PolyMesh() Vs = [[None] * self.Mask.shape[1] for i in range(self.Mask.shape[0])] #Add vertices for i in range(self.Mask.shape[0]): for j in range(self.Mask.shape[1]): if self.Mask[i, j]: Vs[i][j] = mesh.addVertex( np.array([X[i, j], Y[i, j], -Z[i, j]])) #Add triangles on grid for i in range(self.Mask.shape[0] - 1): print i for j in range(self.Mask.shape[1] - 1): if self.Mask[i, j] and self.Mask[i + 1, j] and self.Mask[i, j + 1]: mesh.addFace([Vs[i][j], Vs[i + 1][j], Vs[i][j + 1]]) if self.Mask[i + 1][j] and self.Mask[i + 1][ j + 1] and self.Mask[i][j + 1]: mesh.addFace( [Vs[i + 1][j], Vs[i + 1][j + 1], Vs[i][j + 1]]) mesh.updateTris() self.mesh = mesh
def OnLoadMesh(self, evt): dlg = wx.FileDialog(self, "Choose a file", ".", "", "OFF files (*.off)|*.off|TOFF files (*.toff)|*.toff|OBJ files (*.obj)|*.obj", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() filepath = os.path.join(dirname, filename) print dirname self.glcanvas.mesh = PolyMesh() print "Loading mesh %s..."%filename self.glcanvas.mesh.loadFile(filepath) self.glcanvas.meshCentroid = self.glcanvas.mesh.getCentroid() self.glcanvas.meshPrincipalAxes = self.glcanvas.mesh.getPrincipalAxes() print "Finished loading mesh" print self.glcanvas.mesh self.glcanvas.initMeshBBox() self.glcanvas.Refresh() dlg.Destroy() return
def makeMesh(self): if self.Xs.shape[2] == 0: return X = self.Xs[:, :, 0] Y = self.Ys[:, :, 0] Z = self.Zs[:, :, 0] mesh = PolyMesh() #Come up with vertex indices in the mask Mask = np.array(self.Mask, dtype=np.int32) nV = np.sum(Mask) Mask[self.Mask > 0] = np.arange(nV) + 1 Mask = Mask - 1 VPos = np.zeros((nV, 3)) VPos[:, 0] = X[self.Mask > 0] VPos[:, 1] = Y[self.Mask > 0] VPos[:, 2] = -Z[self.Mask > 0] #Add lower right triangle v1 = Mask[0:-1, 0:-1].flatten() v2 = Mask[1:, 0:-1].flatten() v3 = Mask[1:, 1:].flatten() N = v1.size ITris1 = np.concatenate((np.reshape(v1, [N, 1]), np.reshape( v2, [N, 1]), np.reshape(v3, [N, 1])), 1) #Add upper right triangle v1 = Mask[0:-1, 0:-1].flatten() v2 = Mask[1:, 1:].flatten() v3 = Mask[0:-1, 1:].flatten() N = v1.size ITris2 = np.concatenate((np.reshape(v1, [N, 1]), np.reshape( v2, [N, 1]), np.reshape(v3, [N, 1])), 1) ITris = np.concatenate((ITris1, ITris2), 0) #Only retain triangles which have all three points ITris = ITris[np.sum(ITris == -1, 1) == 0, :] mesh.VPos = VPos mesh.ITris = ITris mesh.VColors = 0.5 * np.ones(mesh.VPos.shape) mesh.updateNormalBuffer() mesh.VPosVBO = vbo.VBO(np.array(mesh.VPos, dtype=np.float32)) mesh.VNormalsVBO = vbo.VBO(np.array(mesh.VNormals, dtype=np.float32)) mesh.VColorsVBO = vbo.VBO(np.array(mesh.VColors, dtype=np.float32)) mesh.IndexVBO = vbo.VBO(mesh.ITris, target=GL_ELEMENT_ARRAY_BUFFER) mesh.needsDisplayUpdate = False self.mesh = mesh
def __init__(self): #GLUT State variables self.GLUTwindow_height = 800 self.GLUTwindow_width = 800 self.GLUTmouse = [0, 0] self.GLUTButton = [0, 0, 0] self.GLUTModifiers = 0 self.keys = {} #Camera state variables #self.camera = Key6DOFCamera(Point3D(-0.230399, -0.0110676, -1.68559), Vector3D(0.135425, 0.0065054, 0.990766), Vector3D(-0.0479816, 0.998848, 0)) self.camera = Key6DOFCamera(Point3D(-1, 0, 0), Vector3D(1, 0, 0), Vector3D(0, 1, 0)) #Speeds of the UI controller self.dx = 0.1 self.dTheta = 0.1 self.mesh = PolyMesh() self.mesh.loadOffFile("meshes/homer.off") print "NVertices = %i, NEdges = %i, NFaces = %i"%(len(self.mesh.vertices), len(self.mesh.edges), len(self.mesh.faces)) self.initGL()
def OnLoadFace(self, evt): dlg = wx.FileDialog(self, "Choose a file", ".", "", "OBJ files (*.obj)|*.obj|OFF files (*.off)|*.off", wx.OPEN) if dlg.ShowModal() == wx.ID_OK: filename = dlg.GetFilename() dirname = dlg.GetDirectory() filepath = os.path.join(dirname, filename) print dirname self.glcanvas.faceMesh = PolyMesh() print "Loading face %s..." % filename self.glcanvas.faceMesh.loadFile(filepath) print "Finished loading face\n %s" % self.glcanvas.faceMesh #print "Deleting all but largest connected component..." #self.glcanvas.faceMesh.deleteAllButLargestConnectedComponent() print self.glcanvas.faceMesh self.glcanvas.bbox = self.glcanvas.faceMesh.getBBox() print "Face BBox: %s\n" % self.glcanvas.bbox self.glcanvas.camera.centerOnBBox(self.glcanvas.bbox, theta=-math.pi / 2, phi=math.pi / 2) self.glcanvas.Refresh() dlg.Destroy() return
# Adapted from Chris Tralie's manipulateGeometry.py import sys sys.path.append("S3DGLPy") from PolyMesh import * import numpy as np if __name__ == '__main__': if len(sys.argv) < 2: sys.exit(1) meshin = sys.argv[1] meshout = sys.argv[2] m = PolyMesh() (VPos, VColors, ITris) = loadOffFileExternal(meshin) #Note: Points come into the program as a matrix in rows, so we need #to transpose to get them into our column format VPos = VPos.T #Make a random rotation matrix R = np.eye(3, 3) R[2, 2] = -1 print(R) #Apply rotation VPos = R.dot(VPos) #Save the results VPos = VPos.T #Need to transpose back before saving saveOffFileExternal(meshout, VPos, VColors * 255, ITris)
def ReadRecurse(self, filename='', EMMaterials={}, OpticalMaterials={}, RadiosityMaterials={}, EMParentNode=None, XMLNode=None): if (len(filename) > 0): tree = ET.parse(filename) XMLNode = tree.getroot() #Load in materials if XMLNode.find('OpticalMaterials') != None: node = XMLNode.find('OpticalMaterials') for mat in node.getchildren(): name = mat.get("name") args = ["ka", "kd", "ks", "kt", "emission"] for arg in args: if mat.get(arg) == None: print "Error: No %s defined in optical material %s" % ( arg, name) return ka = [float(i) for i in mat.get("ka").split()] kd = [float(i) for i in mat.get("ks").split()] ks = [float(i) for i in mat.get("ka").split()] kt = [float(i) for i in mat.get("kt").split()] emission = [float(i) for i in mat.get("emission").split()] OpticalMaterials[name] = OpticalMaterial( RGB3D(ka[0], ka[1], ka[2]), RGB3D(kd[0], kd[1], kd[2]), RGB3D(ks[0], ks[1], ks[2]), RGB3D(kt[0], kt[1], kt[2]), RGB3D(emission[0], emission[1], emission[2])) if XMLNode.find('EMMaterials') != None: node = XMLNode.find('EMMaterials') for mat in node.getchildren(): name = mat.get("name") args = ["R", "T"] for arg in args: if mat.get(arg) == None: print "Error: No %s defined in EM material %s" % ( arg, name) return R = float(mat.get("R")) T = float(mat.get("T")) EMMaterials[name] = EMMaterial(R, T) if XMLNode.find('RadiosityMaterials') != None: node = XMLNode.find('RadiosityMaterials') for mat in node.getchildren(): name = mat.get("name") #arguments are emission and reflectance args = ["em", "p"] for arg in args: if mat.get(arg) == None: print "Error: No %s defined in Radiosity material %s" % ( arg, name) return [R, G, B] = [float(i) for i in mat.get("em").split()] em = RGB3D(R, G, B) [R, G, B] = [float(i) for i in mat.get("p").split()] p = RGB3D(R, G, B) RadiosityMaterials[name] = RadiosityMaterial(em, p) for currNode in XMLNode.getchildren(): EMMat = EMMaterial() OpticalMat = OpticalMaterial() RadiosityMat = RadiosityMaterial() #Get materials (if specified...otherwise use defaults) if currNode.tag in ["tri", "box", "rect", "mesh"]: if currNode.get("em") != None: EMMat = EMMaterials[currNode.get("em")] if currNode.get("om") != None: OpticalMat = OpticalMaterials[currNode.get("om")] if currNode.get("rm") != None: RadiosityMat = RadiosityMaterials[currNode.get("rm")] #Get transformation matrix (if it exists...otherwise use identity matrix) matrix = Matrix4() if currNode.tag in ["tri", "box", "mesh", "node", "include"]: if currNode.text != None: m = [float(i) for i in currNode.text.split()] if len(m) == 16: matrix = Matrix4(m) elif len(m) != 0: print "Invalid transformation matrix specification %s" % ( currNode.text) return #Now process tag-specific elements if currNode.tag == "tri": args = ["P0", "P1", "P2"] for arg in args: if currNode.get(arg) == None: print "Error: No %s defined in triangle" % (arg) return P0args = [float(i) for i in currNode.get("P0").split()] P1args = [float(i) for i in currNode.get("P1").split()] P2args = [float(i) for i in currNode.get("P2").split()] P0 = Point3D(P0args[0], P0args[1], P0args[2]) P1 = Point3D(P1args[0], P1args[1], P1args[2]) P2 = Point3D(P2args[0], P2args[1], P2args[2]) mesh = PolyMesh() [V0, V1, V2] = [ mesh.addVertex(P0), mesh.addVertex(P1), mesh.addVertex(P2) ] mesh.addFace([V0, V1, V2]) sceneNode = EMNode(EMParentNode, mesh, matrix, EMMat, OpticalMat, RadiosityMat) EMParentNode.children.append(sceneNode) elif currNode.tag == "box": args = ["length", "width", "height"] for arg in args: if currNode.get(arg) == None: print "Error: No %s defined in box" % (arg) return L = float(currNode.get("length")) #Length in z W = float(currNode.get("width")) #Width in x H = float(currNode.get("height")) #Height in y C = Point3D(0, 0, 0) stepSize = -1 #Center is an optional argument if currNode.get("center") != None: CPoints = [ float(i) for i in currNode.get("center").split() ] #Center of box C = Point3D(CPoints[0], CPoints[1], CPoints[2]) if currNode.get("stepSize") != None: stepSize = float(currNode.get("stepSize")) mesh = getBoxMesh(L, W, H, C, stepSize) if currNode.get("flipNormals") != None: if int(currNode.get("flipNormals")) == 1: mesh.flipNormals() sceneNode = EMNode(EMParentNode, mesh, matrix, EMMat, OpticalMat, RadiosityMat) EMParentNode.children.append(sceneNode) elif currNode.tag == "rect": args = ["P0", "P1", "P2", "P3"] for arg in args: if currNode.get(arg) == None: print "Error: No %s defined in rect" % (arg) return P0args = [float(i) for i in currNode.get("P0").split()] P1args = [float(i) for i in currNode.get("P1").split()] P2args = [float(i) for i in currNode.get("P2").split()] P3args = [float(i) for i in currNode.get("P3").split()] P0 = Point3D(P0args[0], P0args[1], P0args[2]) P1 = Point3D(P1args[0], P1args[1], P1args[2]) P2 = Point3D(P2args[0], P2args[1], P2args[2]) P3 = Point3D(P3args[0], P3args[1], P3args[2]) stepSize = -1 if currNode.get("stepSize") != None: stepSize = float(currNode.get("stepSize")) mesh = getRectMesh(P0, P1, P2, P3, stepSize) sceneNode = EMNode(EMParentNode, mesh, matrix, EMMat, OpticalMat, RadiosityMat) EMParentNode.children.append(sceneNode) elif currNode.tag == "mesh": args = ["filename"] for arg in args: if currNode.get(arg) == None: print "Error: No %s defined in mesh" % (arg) return meshfilename = currNode.get("filename") mesh = PolyMesh() mesh.loadFile(meshfilename) if currNode.get("flipNormals") != None: if int(currNode.get("flipNormals")) == 1: mesh.flipNormals() sceneNode = EMNode(EMParentNode, mesh, matrix, EMMat, OpticalMat, RadiosityMat) EMParentNode.children.append(sceneNode) elif currNode.tag == "node": #This is a transformation node in the graph sceneNode = EMNode(parent=EMParentNode, transformation=matrix) EMParentNode.children.append(sceneNode) #Now recursively add the branch in this node self.ReadRecurse('', EMMaterials, OpticalMaterials, RadiosityMaterials, sceneNode, currNode) elif currNode.tag == "include": #Recursively include another scene file as a branch in this tree if currNode.get("filename") == None: print "Error: No filename defined for included scene" return nextfilename = currNode.get("filename") sceneNode = EMNode(parent=EMParentNode, transformation=matrix) EMParentNode.children.append(sceneNode) self.ReadRecurse(nextfilename, {}, {}, {}, sceneNode, None) elif currNode.tag == "Source": if currNode.get("pos") == None: print "Error: No position defined for EM Source" return coords = [float(i) for i in currNode.get("pos").split()] self.Source = Point3D(coords[0], coords[1], coords[2]) elif currNode.tag == "Receiver": if currNode.get("pos") == None: print "Error: No position defined for EM Receiver" return coords = [float(i) for i in currNode.get("pos").split()] self.Receiver = Point3D(coords[0], coords[1], coords[2]) else: if not (currNode.tag in [ "EMMaterials", "OpticalMaterials", "RadiosityMaterials" ]): #Checking to make sure it's a string handles the case #where it's a comment if type(currNode.tag) is str: print "Unrecognized tag %s" % currNode.tag
def __init__(self, parent): attribs = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24) glcanvas.GLCanvas.__init__(self, parent, -1, attribList=attribs) self.context = glcanvas.GLContext(self) self.setText = False self.parent = parent #Camera state variables self.size = self.GetClientSize() #self.camera = MouseSphericalCamera(self.size.x, self.size.y) self.camera = MousePolarCamera(self.size.width, self.size.height) #Main state variables self.MousePos = [0, 0] self.initiallyResized = False random.seed() #GUI State Variables self.GUIState = STATE_INTRO self.GUISubstate = -1 #Head Mesh self.headMesh = PolyMesh() self.headMesh.loadFile('NotreDameMedium.off') self.headMeshLowres = PolyMesh() self.headMeshLowres.loadFile('NotreDameLowres.off') self.styrofoamHead = PolyMesh() self.styrofoamHead.loadFile('StyrofoamHead.off') self.rotAngle = 0 self.zoom = 0 self.rotCount = 0 self.timelineCount = 0 #User's face self.userMesh = None self.colorUserMesh = None #ICP state variables self.ICPTransformation = np.zeros(0) self.ICPMutex = Lock() self.ICPThread = None self.bbox = self.headMesh.getBBox() self.camera.centerOnBBox(self.bbox, theta=-math.pi / 2, phi=math.pi / 2) self.zCenter = (self.bbox.zmax + self.bbox.zmin) / 2.0 self.GLinitialized = False #GL-related events wx.EVT_ERASE_BACKGROUND(self, self.processEraseBackgroundEvent) wx.EVT_SIZE(self, self.processSizeEvent) wx.EVT_PAINT(self, self.processPaintEvent) #Mouse Events wx.EVT_LEFT_DOWN(self, self.MouseDown) wx.EVT_LEFT_UP(self, self.MouseUp) wx.EVT_RIGHT_DOWN(self, self.MouseDown) wx.EVT_RIGHT_UP(self, self.MouseUp) wx.EVT_MIDDLE_DOWN(self, self.MouseDown) wx.EVT_MIDDLE_UP(self, self.MouseUp) wx.EVT_MOTION(self, self.MouseMotion) self.initGL()
glutAddSubMenu("ICP Step By Step", stepByStepMenu) glutAddSubMenu("ICP Algorithm Full", icpMenu) glutAttachMenu(GLUT_RIGHT_BUTTON) glutMainLoop() if __name__ == '__main__': if len(sys.argv) < 3: print "Usage: python ICPViewerGLUT.py <mesh to align file> <target mesh file> [Maximum Number of Iterations] [Output File Prefix]" sys.exit(0) (xmeshfile, ymeshfile) = (sys.argv[1], sys.argv[2]) MaxIters = 200 if len(argv) > 3: MaxIters = int(argv[3]) outputPrefix = "" if len(argv) > 4: outputPrefix = argv[4] xmesh = PolyMesh() print "Loading %s..." % xmeshfile (xmesh.VPos, xmesh.VColors, xmesh.ITris) = loadOffFileExternal(xmeshfile) xmesh.performDisplayUpdate(True) ymesh = PolyMesh() print "Loading %s..." % ymeshfile (ymesh.VPos, ymesh.VColors, ymesh.ITris) = loadOffFileExternal(ymeshfile) ymesh.performDisplayUpdate(True) viewer = ICPViewerCanvas(xmesh, ymesh, MaxIters, outputPrefix) viewer.initGL()
print "Updating %s to %g " % (v3.pos, v3.FMMDist) Q.put((v3.FMMDist, v3)) if e.f2: v3 = updateFMM(v1, v2, e.f2) if v3: print "Updating %s to %g " % (v3.pos, v3.FMMDist) Q.put((v3.FMMDist, v3)) v1.FMMType = FMM_BLACK_TYPE #Now copy over the distances to the matrix for k in range(0, N): D[i, k] = V[i].FMMDist return D if __name__ == '__main__': mesh = PolyMesh() v1 = mesh.addVertex(Point3D(0, 0, 0)) v2 = mesh.addVertex(Point3D(1, 0, 0)) v3 = mesh.addVertex(Point3D(1, 1, 0)) v4 = mesh.addVertex(Point3D(0, 1, 0)) v5 = mesh.addVertex(Point3D(1, 2, 0)) v6 = mesh.addVertex(Point3D(0, 2, 0)) mesh.addFace([v1, v2, v3]) mesh.addFace([v1, v3, v4]) mesh.addFace([v3, v5, v6]) mesh.addFace([v4, v3, v6]) mesh = getOctahedronMesh() getGeodesicDistancesFMM(mesh) mesh.saveOffFile("out.off")
import numpy as np from PolyMesh import * if __name__ == '__main__': m = PolyMesh() m.loadFile("homer.off") m.performDisplayUpdate() m2 = PolyMesh() (m2.VPos, m2.VColors, m2.ITris) = loadOffFileExternal("homer.off") m2.performDisplayUpdate(True) print m.EdgeLines print "\n\n\n" print m2.EdgeLines