chiark / gitweb /
Update on rotation tools, this breaks a lot of functionality, but is really needed.
[cura.git] / Cura / util / mesh.py
index 857d62011da477d743cf2b5bb781335d033101ba..d013d5973f09757c0a59946c7443057cdd4eae46 100644 (file)
@@ -11,70 +11,44 @@ from Cura.util import util3d
 class mesh(object):
        def __init__(self):
                self.vertexes = None
-               self.origonalVertexes = None
+               self.matrix = numpy.matrix([[1,0,0], [0,1,0], [0,0,1]], numpy.float32);
                self.vertexCount = 0
 
        def addVertex(self, x, y, z):
                n = self.vertexCount
-               self.origonalVertexes[n][0] = x
-               self.origonalVertexes[n][1] = y
-               self.origonalVertexes[n][2] = z
+               self.vertexes[n][0] = x
+               self.vertexes[n][1] = y
+               self.vertexes[n][2] = z
                self.vertexCount += 1
        
        def _prepareVertexCount(self, vertexNumber):
                #Set the amount of faces before loading data in them. This way we can create the numpy arrays before we fill them.
-               self.origonalVertexes = numpy.zeros((vertexNumber, 3), numpy.float32)
+               self.vertexes = numpy.zeros((vertexNumber, 3), numpy.float32)
                self.normal = numpy.zeros((vertexNumber, 3), numpy.float32)
                self.vertexCount = 0
 
        def _postProcessAfterLoad(self):
-               self.vertexes = self.origonalVertexes.copy()
-               self.getMinimumZ()
-
-       def getMinimumZ(self):
-               self.min = self.vertexes.min(0)
-               self.max = self.vertexes.max(0)
-               self.size = self.max - self.min
-               return self.min[2]
-       
+               self.processMatrix()
+               self._calculateNormals()
+
+       def processMatrix(self):
+               transformedVertexes = (numpy.matrix(self.vertexes, copy = False) * self.matrix).getA()
+               self.transformedMin = transformedVertexes.min(0)
+               self.transformedMax = transformedVertexes.max(0)
+               self.transformedSize = self.transformedMax - self.transformedMin
+
+               #Calculate the boundery circle
+               center = self.transformedMin + self.transformedSize / 2.0
+               self.bounderyCircleSize = round(math.sqrt(numpy.max(((transformedVertexes[::,0] - center[0]) * (transformedVertexes[::,0] - center[0])) + ((transformedVertexes[::,1] - center[1]) * (transformedVertexes[::,1] - center[1])))), 3)
+
        def getMaximum(self):
-               return self.max
+               return self.transformedMax
        def getMinimum(self):
-               return self.min
+               return self.transformedMin
        def getSize(self):
-               return self.size
-
-       def setRotateMirror(self, rotate, mirrorX, mirrorY, mirrorZ, swapXZ, swapYZ):
-               #Modify the vertexes with the rotation/mirror
-               rotate = rotate / 180.0 * math.pi
-               scaleX = 1.0
-               scaleY = 1.0
-               scaleZ = 1.0
-               if mirrorX:
-                       scaleX = -scaleX
-               if mirrorY:
-                       scaleY = -scaleY
-               if mirrorZ:
-                       scaleZ = -scaleZ
-               mat00 = math.cos(rotate) * scaleX
-               mat01 =-math.sin(rotate) * scaleY
-               mat10 = math.sin(rotate) * scaleX
-               mat11 = math.cos(rotate) * scaleY
-               
-               mat = numpy.array([[mat00,mat10,0],[mat01,mat11,0],[0,0,scaleZ]], numpy.float32)
-               if swapXZ:
-                       mat = numpy.array([mat[2],mat[1],mat[0]], numpy.float32)
-               if swapYZ:
-                       mat = numpy.array([mat[0],mat[2],mat[1]], numpy.float32)
-               self.matrix = mat
-               self.vertexes = (numpy.matrix(self.origonalVertexes, copy = False) * numpy.matrix(mat)).getA()
-               
-               #Calculate the boundery box of the object
-               self.getMinimumZ()
-               #Calculate the boundery circle
-               center = (self.max + self.min) / 2.0
-               self.bounderyCircleSize = round(math.sqrt(numpy.max(((self.vertexes[::,0] - center[0]) * (self.vertexes[::,0] - center[0])) + ((self.vertexes[::,1] - center[1]) * (self.vertexes[::,1] - center[1])))), 3)
-               
+               return self.transformedSize
+
+       def _calculateNormals(self):
                #Calculate the normals
                tris = self.vertexes.reshape(self.vertexCount / 3, 3, 3)
                normals = numpy.cross( tris[::,1 ] - tris[::,0]  , tris[::,2 ] - tris[::,0] )