Beispiel #1
0
 def quad_mesh(self, functionIn, functionOut):
     faces = []
     for x in range(self.nx):
         for y in range(self.ny):
             for z in range(self.nz):
                 index = self.get_index(x, y, z)
                 if functionIn(self.values[index]):
                     # (x,y) (x1,y) (x1,y1) (x,y1)
                     if x == self.nx - 1 or functionOut(
                             self.get_value_at_xyz(x + 1, y, z)):
                         v1 = Vertex(x + 1, y, z)
                         v2 = Vertex(x + 1, y + 1, z)
                         v3 = Vertex(x + 1, y + 1, z + 1)
                         v4 = Vertex(x + 1, y, z + 1)
                         faces.append(Face([v1, v2, v3, v4]))
                     if x == 0 or functionOut(
                             self.get_value_at_xyz(x - 1, y, z)):
                         v1 = Vertex(x, y + 1, z)
                         v2 = Vertex(x, y, z)
                         v3 = Vertex(x, y, z + 1)
                         v4 = Vertex(x, y + 1, z + 1)
                         faces.append(Face([v1, v2, v3, v4]))
                     if y == self.ny - 1 or functionOut(
                             self.get_value_at_xyz(x, y + 1, z)):
                         v1 = Vertex(x + 1, y + 1, z)
                         v2 = Vertex(x, y + 1, z)
                         v3 = Vertex(x, y + 1, z + 1)
                         v4 = Vertex(x + 1, y + 1, z + 1)
                         faces.append(Face([v1, v2, v3, v4]))
                     if y == 0 or functionOut(
                             self.get_value_at_xyz(x, y - 1, z)):
                         v1 = Vertex(x, y, z)
                         v2 = Vertex(x + 1, y, z)
                         v3 = Vertex(x + 1, y, z + 1)
                         v4 = Vertex(x, y, z + 1)
                         faces.append(Face([v1, v2, v3, v4]))
                     if z == self.nz - 1 or functionOut(
                             self.get_value_at_xyz(x, y, z + 1)):
                         v1 = Vertex(x, y, z + 1)
                         v2 = Vertex(x + 1, y, z + 1)
                         v3 = Vertex(x + 1, y + 1, z + 1)
                         v4 = Vertex(x, y + 1, z + 1)
                         faces.append(Face([v1, v2, v3, v4]))
                     if z == 0 or functionOut(
                             self.get_value_at_xyz(x, y, z - 1)):
                         v1 = Vertex(x, y + 1, z)
                         v2 = Vertex(x + 1, y + 1, z)
                         v3 = Vertex(x + 1, y, z)
                         v4 = Vertex(x, y, z)
                         faces.append(Face([v1, v2, v3, v4]))
     mesh = Mesh()
     mesh.faces = faces
     mesh.update_topology()
     if (self.scale_to_canvas):
         mesh.translate(-self.nx / 2.0, -self.ny / 2.0, -self.nz / 2.0)
         sc = 20.0 / max(self.nx, self.ny)
         mesh.scale(sc, sc, sc)
     return mesh
def marching_cubes(nX,nY,nZ,values,iso, scale_to_canvas=False):
    mesh =  Mesh()
    nYZ = nY * nZ
    index = 0
    n =[0]*8
    switcher = {
        0:lambda: Vertex(x + _v(n[0], n[1], iso), y + 1, z),
        1:lambda: Vertex(x + 1, y + _v(n[2], n[1], iso), z),
        2:lambda: Vertex(x + _v(n[3], n[2], iso), y, z),
        3:lambda: Vertex(x, y + _v(n[3], n[0], iso), z),
        4:lambda: Vertex(x + _v(n[4], n[5], iso), y + 1, z + 1),
        5:lambda: Vertex(x + 1, y + _v(n[6], n[5], iso), z + 1),
        6:lambda: Vertex(x + _v(n[7], n[6], iso), y, z + 1),
        7:lambda: Vertex(x, y + _v(n[7], n[4], iso), z + 1),
        8:lambda: Vertex(x, y + 1, z + _v(n[0], n[4], iso)),
        9:lambda: Vertex(x + 1, y + 1, z + _v(n[1], n[5], iso)),
        10:lambda: Vertex(x, y, z + _v(n[3], n[7], iso)),
        11:lambda: Vertex(x + 1, y, z + _v(n[2], n[6], iso))
    }
    for x in range(nX - 1):
        for y in range(nY - 1):
            for z in range(nZ - 1):
                caseNumber = 0
                index = z + y * nZ + x * nYZ
                # collecting the values
                n[0] = values[index + nZ]# 0,1,0
                n[1] = values[index + nYZ + nZ]#1,1,0
                n[2] = values[index + nYZ]# 1,0,0
                n[3] = values[index]# 0,0,0
                n[4] = values[index + nZ + 1]# 0,1,1
                n[5] = values[index + nYZ + nZ + 1]# 1,1,1
                n[6] = values[index + nYZ + 1]# 1,0,1
                n[7] = values[index + 1]# 0,0,1
                for i in range(7,-1,-1):
                    if n[i] > iso:
                        caseNumber+=1
                    if i > 0:
                        caseNumber = caseNumber << 1
                # collecting the faces
                offset = caseNumber * 15
                for i in range(offset,offset + 15,3):
                    if _faces[i] > -1:
                        vs=[]
                        for j in range(i,i+3):
                            v = switcher[_faces[j]]()
                            mesh.vertices.append(v)
                            vs.append(v)
                            if len(vs) == 3:
                                mesh.faces.append(Face(vs))

    mesh.update_topology()
    if(scale_to_canvas):
        mesh.translate(-nX/2.0,-nY/2.0,-nZ/2.0)
        sc = 20.0/max(nX,nY)
        mesh.scale(sc,sc,sc)

    return mesh