chiark / gitweb /
Added rotation circle to make it easier to rotate models.
authordaid303 <daid303@gmail.com>
Wed, 5 Dec 2012 08:46:58 +0000 (09:46 +0100)
committerdaid303 <daid303@gmail.com>
Wed, 5 Dec 2012 08:46:58 +0000 (09:46 +0100)
Cura/gui/preview3d.py
Cura/util/mesh.py

index 2e62d41f3430ea73553f9927941edb699d4693c9..c830711cc282db66d6ea953b1c9530503177f629 100644 (file)
@@ -43,6 +43,7 @@ class previewPanel(wx.Panel):
                self.gcode = None\r
                self.objectsMinV = None\r
                self.objectsMaxV = None\r
+               self.objectsBounderyCircleSize = None\r
                self.loadThread = None\r
                self.machineSize = util3d.Vector3(profile.getPreferenceFloat('machine_width'), profile.getPreferenceFloat('machine_depth'), profile.getPreferenceFloat('machine_height'))\r
                self.machineCenter = util3d.Vector3(self.machineSize.x / 2, self.machineSize.y / 2, 0)\r
@@ -395,6 +396,7 @@ class previewPanel(wx.Panel):
                \r
                minV = self.objectList[0].mesh.getMinimum()\r
                maxV = self.objectList[0].mesh.getMaximum()\r
+               objectsBounderyCircleSize = self.objectList[0].mesh.bounderyCircleSize\r
                for obj in self.objectList:\r
                        if obj.mesh == None:\r
                                continue\r
@@ -402,9 +404,11 @@ class previewPanel(wx.Panel):
                        obj.mesh.getMinimumZ()\r
                        minV = numpy.minimum(minV, obj.mesh.getMinimum())\r
                        maxV = numpy.maximum(maxV, obj.mesh.getMaximum())\r
+                       objectsBounderyCircleSize = max(objectsBounderyCircleSize, obj.mesh.bounderyCircleSize)\r
 \r
                self.objectsMaxV = maxV\r
                self.objectsMinV = minV\r
+               self.objectsBounderyCircleSize = objectsBounderyCircleSize\r
                for obj in self.objectList:\r
                        if obj.mesh == None:\r
                                continue\r
@@ -468,16 +472,15 @@ class PreviewGLCanvas(glcanvas.GLCanvas):
 \r
        def OnMouseMotion(self,e):\r
                cursorXY = 100000\r
-               sizeXY = 0\r
+               radius = 0\r
                if self.parent.objectsMaxV != None:\r
-                       size = (self.parent.objectsMaxV - self.parent.objectsMinV)\r
-                       sizeXY = math.sqrt((size[0] * size[0]) + (size[1] * size[1]))\r
+                       radius = self.parent.objectsBounderyCircleSize * profile.getProfileSettingFloat('model_scale')\r
                        \r
                        p0 = numpy.array(gluUnProject(e.GetX(), self.viewport[1] + self.viewport[3] - e.GetY(), 0, self.modelMatrix, self.projMatrix, self.viewport))\r
                        p1 = numpy.array(gluUnProject(e.GetX(), self.viewport[1] + self.viewport[3] - e.GetY(), 1, self.modelMatrix, self.projMatrix, self.viewport))\r
                        cursorZ0 = p0 - (p1 - p0) * (p0[2] / (p1[2] - p0[2]))\r
                        cursorXY = math.sqrt((cursorZ0[0] * cursorZ0[0]) + (cursorZ0[1] * cursorZ0[1]))\r
-                       if cursorXY >= sizeXY * 0.7 and cursorXY <= sizeXY * 0.7 + 3 and False:\r
+                       if cursorXY >= radius * 1.1 and cursorXY <= radius * 1.3:\r
                                self.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))\r
                        else:\r
                                self.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))\r
@@ -485,7 +488,7 @@ class PreviewGLCanvas(glcanvas.GLCanvas):
                if e.Dragging() and e.LeftIsDown():\r
                        if self.dragType == '':\r
                                #Define the drag type depending on the cursor position.\r
-                               if cursorXY >= sizeXY * 0.7 and cursorXY <= sizeXY * 0.7 + 3 and False:\r
+                               if cursorXY >= radius * 1.1 and cursorXY <= radius * 1.3:\r
                                        self.dragType = 'modelRotate'\r
                                        self.dragStart = math.atan2(cursorZ0[0], cursorZ0[1])\r
                                else:\r
@@ -506,6 +509,8 @@ class PreviewGLCanvas(glcanvas.GLCanvas):
                                angle = math.atan2(cursorZ0[0], cursorZ0[1])\r
                                diff = self.dragStart - angle\r
                                self.tempRotate = diff * 180 / math.pi\r
+                               rot = profile.getProfileSettingFloat('model_rotate_base')\r
+                               self.tempRotate = round((self.tempRotate + rot) / 15) * 15 - rot\r
                        #Workaround for buggy ATI cards.\r
                        size = self.GetSizeTuple()\r
                        self.SetSize((size[0]+1, size[1]))\r
@@ -513,7 +518,13 @@ class PreviewGLCanvas(glcanvas.GLCanvas):
                        self.Refresh()\r
                else:\r
                        if self.tempRotate != 0:\r
-                               profile.putProfileSetting('model_rotate_base', profile.getProfileSettingFloat('model_rotate_base') + self.tempRotate)\r
+                               newRotation = profile.getProfileSettingFloat('model_rotate_base') + self.tempRotate\r
+                               while newRotation >= 360:\r
+                                       newRotation -= 360\r
+                               while newRotation < 0:\r
+                                       newRotation += 360\r
+                               profile.putProfileSetting('model_rotate_base', newRotation)\r
+                               self.parent.rotate.SetValue(newRotation)\r
                                self.parent.updateModelTransform()\r
                                self.tempRotate = 0\r
                                \r
@@ -754,29 +765,36 @@ class PreviewGLCanvas(glcanvas.GLCanvas):
                        glEnd()\r
                        glEnable(GL_DEPTH_TEST)\r
 \r
-               opengl.DrawMachine(machineSize)\r
-\r
                glPushMatrix()\r
                glTranslate(self.parent.machineCenter.x, self.parent.machineCenter.y, 0)\r
                \r
                #Draw the rotate circle\r
-               if self.parent.objectsMaxV != None and False:\r
+               if self.parent.objectsMaxV != None:\r
                        glDisable(GL_LIGHTING)\r
                        glDisable(GL_CULL_FACE)\r
                        glEnable(GL_BLEND)\r
+                       glRotate(self.tempRotate + profile.getProfileSettingFloat('model_rotate_base'), 0, 0, 1)\r
+                       radius = self.parent.objectsBounderyCircleSize * profile.getProfileSettingFloat('model_scale')\r
+                       glScalef(radius, radius, 1)\r
                        glBegin(GL_TRIANGLE_STRIP)\r
-                       size = (self.parent.objectsMaxV - self.parent.objectsMinV)\r
-                       sizeXY = math.sqrt((size[0] * size[0]) + (size[1] * size[1]))\r
                        for i in xrange(0, 64+1):\r
                                f = i if i < 64/2 else 64 - i\r
-                               glColor4ub(255,int(f*255/(64/2)),0,128)\r
-                               glVertex3f(sizeXY * 0.7 * math.cos(i/32.0*math.pi), sizeXY * 0.7 * math.sin(i/32.0*math.pi),0.1)\r
-                               glColor4ub(  0,128,0,128)\r
-                               glVertex3f((sizeXY * 0.7 + 3) * math.cos(i/32.0*math.pi), (sizeXY * 0.7 + 3) * math.sin(i/32.0*math.pi),0.1)\r
+                               glColor4ub(255,int(f*255/(64/2)),0,255)\r
+                               glVertex3f(1.1 * math.cos(i/32.0*math.pi), 1.1 * math.sin(i/32.0*math.pi),0.1)\r
+                               glColor4ub(  0,128,0,255)\r
+                               glVertex3f(1.3 * math.cos(i/32.0*math.pi), 1.3 * math.sin(i/32.0*math.pi),0.1)\r
+                       glEnd()\r
+                       glBegin(GL_TRIANGLES)\r
+                       glColor4ub(0,0,0,192)\r
+                       glVertex3f(1, 0.1,0.15)\r
+                       glVertex3f(1,-0.1,0.15)\r
+                       glVertex3f(1.4,0,0.15)\r
                        glEnd()\r
                        glEnable(GL_CULL_FACE)\r
                \r
                glPopMatrix()\r
+\r
+               opengl.DrawMachine(machineSize)\r
                \r
                glFlush()\r
        \r
index 04ba9a2ff40341ec6ce6a459a21621ef9d1c147e..89d96b76c3dd5562fd9a895e53678c4d683fb52f 100644 (file)
@@ -41,6 +41,7 @@ class mesh(object):
                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
@@ -62,7 +63,14 @@ class mesh(object):
                if swapYZ:
                        mat = numpy.array([mat[0],mat[2],mat[1]], numpy.float32)
                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)
+               
+               #Calculate the normals
                tris = self.vertexes.reshape(self.vertexCount / 3, 3, 3)
                normals = numpy.cross( tris[::,1 ] - tris[::,0]  , tris[::,2 ] - tris[::,0] )
                lens = numpy.sqrt( normals[:,0]**2 + normals[:,1]**2 + normals[:,2]**2 )
@@ -76,8 +84,6 @@ class mesh(object):
                n[:,6:9] = normals
                self.normal = n.reshape(self.vertexCount, 3)
                self.invNormal = -self.normal
-               
-               self.getMinimumZ()
 
        def splitToParts(self, callback = None):
                t0 = time.time()