1 from __future__ import absolute_import
8 OpenGL.ERROR_CHECKING = False
9 from OpenGL.GLU import *
10 from OpenGL.GL import *
12 from Cura.gui.util import opengl
14 class toolNone(object):
15 def __init__(self, parent):
18 def OnMouseMove(self, p0, p1):
21 def OnDragStart(self, p0, p1):
24 def OnDrag(self, p0, p1):
33 class toolInfo(object):
34 def __init__(self, parent):
37 def OnMouseMove(self, p0, p1):
40 def OnDragStart(self, p0, p1):
43 def OnDrag(self, p0, p1):
50 glDisable(GL_LIGHTING)
52 glDisable(GL_DEPTH_TEST)
53 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
55 size = self.parent.getObjectSize()
56 radius = self.parent.getObjectBoundaryCircle()
58 glTranslate(0,0,size[2]/2 + 5)
59 glRotate(-self.parent.yaw, 0,0,1)
60 if self.parent.pitch < 80:
61 glTranslate(0, radius + 5,0)
62 elif self.parent.pitch < 100:
63 glTranslate(0, (radius + 5) * (90 - self.parent.pitch) / 10,0)
65 glTranslate(0,-(radius + 5),0)
66 opengl.glDrawStringCenter("%dx%dx%d" % (size[0], size[1], size[2]))
73 glVertex3f(size[0], size[1], size[2])
74 glVertex3f(size[0], size[1], size[2]/4*3)
75 glVertex3f(size[0], size[1], size[2])
76 glVertex3f(size[0], size[1]/4*3, size[2])
77 glVertex3f(size[0], size[1], size[2])
78 glVertex3f(size[0]/4*3, size[1], size[2])
80 glVertex3f(-size[0], -size[1], size[2])
81 glVertex3f(-size[0], -size[1], size[2]/4*3)
82 glVertex3f(-size[0], -size[1], size[2])
83 glVertex3f(-size[0], -size[1]/4*3, size[2])
84 glVertex3f(-size[0], -size[1], size[2])
85 glVertex3f(-size[0]/4*3, -size[1], size[2])
87 glVertex3f(size[0], -size[1], -size[2])
88 glVertex3f(size[0], -size[1], -size[2]/4*3)
89 glVertex3f(size[0], -size[1], -size[2])
90 glVertex3f(size[0], -size[1]/4*3, -size[2])
91 glVertex3f(size[0], -size[1], -size[2])
92 glVertex3f(size[0]/4*3, -size[1], -size[2])
94 glVertex3f(-size[0], size[1], -size[2])
95 glVertex3f(-size[0], size[1], -size[2]/4*3)
96 glVertex3f(-size[0], size[1], -size[2])
97 glVertex3f(-size[0], size[1]/4*3, -size[2])
98 glVertex3f(-size[0], size[1], -size[2])
99 glVertex3f(-size[0]/4*3, size[1], -size[2])
102 class toolRotate(object):
103 def __init__(self, parent):
105 self.rotateRingDist = 1.5
106 self.rotateRingDistMin = 1.3
107 self.rotateRingDistMax = 1.7
108 self.dragPlane = None
109 self.dragStartAngle = None
110 self.dragEndAngle = None
112 def _ProjectToPlanes(self, p0, p1):
113 cursorX0 = p0 - (p1 - p0) * (p0[0] / (p1[0] - p0[0]))
114 cursorY0 = p0 - (p1 - p0) * (p0[1] / (p1[1] - p0[1]))
115 cursorZ0 = p0 - (p1 - p0) * (p0[2] / (p1[2] - p0[2]))
116 cursorYZ = math.sqrt((cursorX0[1] * cursorX0[1]) + (cursorX0[2] * cursorX0[2]))
117 cursorXZ = math.sqrt((cursorY0[0] * cursorY0[0]) + (cursorY0[2] * cursorY0[2]))
118 cursorXY = math.sqrt((cursorZ0[0] * cursorZ0[0]) + (cursorZ0[1] * cursorZ0[1]))
119 return cursorX0, cursorY0, cursorZ0, cursorYZ, cursorXZ, cursorXY
121 def OnMouseMove(self, p0, p1):
122 radius = self.parent.getObjectBoundaryCircle()
123 cursorX0, cursorY0, cursorZ0, cursorYZ, cursorXZ, cursorXY = self._ProjectToPlanes(p0, p1)
124 oldDragPlane = self.dragPlane
125 if radius * self.rotateRingDistMin <= cursorXY <= radius * self.rotateRingDistMax or radius * self.rotateRingDistMin <= cursorYZ <= radius * self.rotateRingDistMax or radius * self.rotateRingDistMin <= cursorXZ <= radius * self.rotateRingDistMax:
126 self.parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))
127 if self.dragStartAngle is None:
128 if radius * self.rotateRingDistMin <= cursorXY <= radius * self.rotateRingDistMax:
129 self.dragPlane = 'XY'
130 elif radius * self.rotateRingDistMin <= cursorXZ <= radius * self.rotateRingDistMax:
131 self.dragPlane = 'XZ'
133 self.dragPlane = 'YZ'
135 if self.dragStartAngle is None:
137 self.parent.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
139 def OnDragStart(self, p0, p1):
140 radius = self.parent.getObjectBoundaryCircle()
141 cursorX0, cursorY0, cursorZ0, cursorYZ, cursorXZ, cursorXY = self._ProjectToPlanes(p0, p1)
142 if radius * self.rotateRingDistMin <= cursorXY <= radius * self.rotateRingDistMax or radius * self.rotateRingDistMin <= cursorYZ <= radius * self.rotateRingDistMax or radius * self.rotateRingDistMin <= cursorXZ <= radius * self.rotateRingDistMax:
143 if radius * self.rotateRingDistMin <= cursorXY <= radius * self.rotateRingDistMax:
144 self.dragPlane = 'XY'
145 self.dragStartAngle = math.atan2(cursorZ0[1], cursorZ0[0]) * 180 / math.pi
146 elif radius * self.rotateRingDistMin <= cursorXZ <= radius * self.rotateRingDistMax:
147 self.dragPlane = 'XZ'
148 self.dragStartAngle = math.atan2(cursorY0[2], cursorY0[0]) * 180 / math.pi
150 self.dragPlane = 'YZ'
151 self.dragStartAngle = math.atan2(cursorX0[2], cursorX0[1]) * 180 / math.pi
155 def OnDrag(self, p0, p1):
156 cursorX0, cursorY0, cursorZ0, cursorYZ, cursorXZ, cursorXY = self._ProjectToPlanes(p0, p1)
157 if self.dragPlane == 'XY':
158 angle = math.atan2(cursorZ0[1], cursorZ0[0]) * 180 / math.pi
159 elif self.dragPlane == 'XZ':
160 angle = math.atan2(cursorY0[2], cursorY0[0]) * 180 / math.pi
162 angle = math.atan2(cursorX0[2], cursorX0[1]) * 180 / math.pi
163 diff = angle - self.dragStartAngle
164 if wx.GetKeyState(wx.WXK_SHIFT):
165 diff = round(diff / 1) * 1
167 diff = round(diff / 15) * 15
172 rad = diff / 180.0 * math.pi
173 self.dragEndAngle = self.dragStartAngle + diff
174 if self.dragPlane == 'XY':
175 self.parent.tempMatrix = numpy.matrix([[math.cos(rad), math.sin(rad), 0], [-math.sin(rad), math.cos(rad), 0], [0,0,1]], numpy.float64)
176 elif self.dragPlane == 'XZ':
177 self.parent.tempMatrix = numpy.matrix([[math.cos(rad), 0, math.sin(rad)], [0,1,0], [-math.sin(rad), 0, math.cos(rad)]], numpy.float64)
179 self.parent.tempMatrix = numpy.matrix([[1,0,0], [0, math.cos(rad), math.sin(rad)], [0, -math.sin(rad), math.cos(rad)]], numpy.float64)
182 self.dragStartAngle = None
185 glDisable(GL_LIGHTING)
187 glDisable(GL_DEPTH_TEST)
188 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
189 radius = self.parent.getObjectBoundaryCircle()
190 glScalef(self.rotateRingDist * radius, self.rotateRingDist * radius, self.rotateRingDist * radius)
191 if self.dragPlane == 'XY':
193 glColor4ub(255,64,64,255)
194 if self.dragStartAngle is not None:
196 glRotate(self.dragStartAngle, 0,0,1)
203 glRotate(self.dragEndAngle, 0,0,1)
208 glTranslatef(1.1,0,0)
209 glColor4ub(0,0,0,255)
210 opengl.glDrawStringCenter("%d" % (abs(self.dragEndAngle - self.dragStartAngle)))
211 glColor4ub(255,64,64,255)
215 glColor4ub(128,0,0,255)
216 glBegin(GL_LINE_LOOP)
217 for i in xrange(0, 64):
218 glVertex3f(math.cos(i/32.0*math.pi), math.sin(i/32.0*math.pi),0)
220 if self.dragPlane == 'YZ':
221 glColor4ub(64,255,64,255)
223 if self.dragStartAngle is not None:
225 glRotate(self.dragStartAngle, 1,0,0)
232 glRotate(self.dragEndAngle, 1,0,0)
237 glTranslatef(0,1.1,0)
238 glColor4ub(0,0,0,255)
239 opengl.glDrawStringCenter("%d" % (abs(self.dragEndAngle - self.dragStartAngle)))
240 glColor4ub(64,255,64,255)
243 glColor4ub(0,128,0,255)
245 glBegin(GL_LINE_LOOP)
246 for i in xrange(0, 64):
247 glVertex3f(0, math.cos(i/32.0*math.pi), math.sin(i/32.0*math.pi))
249 if self.dragPlane == 'XZ':
251 glColor4ub(255,255,0,255)
252 if self.dragStartAngle is not None:
254 glRotate(self.dragStartAngle, 0,-1,0)
261 glRotate(self.dragEndAngle, 0,-1,0)
266 glTranslatef(1.1,0,0)
267 glColor4ub(0,0,0,255)
268 opengl.glDrawStringCenter("%d" % (abs(self.dragEndAngle - self.dragStartAngle)))
269 glColor4ub(255,255,0,255)
272 glColor4ub(128,128,0,255)
274 glBegin(GL_LINE_LOOP)
275 for i in xrange(0, 64):
276 glVertex3f(math.cos(i/32.0*math.pi), 0, math.sin(i/32.0*math.pi))
278 glEnable(GL_DEPTH_TEST)
280 class toolScale(object):
281 def __init__(self, parent):
286 def _pointDist(self, p0, p1, p2):
287 return numpy.linalg.norm(numpy.cross((p0 - p1), (p0 - p2))) / numpy.linalg.norm(p2 - p1)
289 def _traceNodes(self, p0, p1):
291 if self._pointDist(numpy.array([0,0,0],numpy.float32), p0, p1) < s * 2:
293 if self._pointDist(numpy.array([s*15,0,0],numpy.float32), p0, p1) < s * 2:
295 if self._pointDist(numpy.array([0,s*15,0],numpy.float32), p0, p1) < s * 2:
297 if self._pointDist(numpy.array([0,0,s*15],numpy.float32), p0, p1) < s * 2:
301 def _lineLineCrossingDistOnLine(self, s0, e0, s1, e1):
304 a = numpy.dot(d0, d0)
305 b = numpy.dot(d0, d1)
306 e = numpy.dot(d1, d1)
318 return float(self.parent.zoom) / float(self.parent.GetSize().GetWidth()) * 6.0
320 def OnMouseMove(self, p0, p1):
321 self.node = self._traceNodes(p0, p1)
323 def OnDragStart(self, p0, p1):
324 if self.node is None:
328 def OnDrag(self, p0, p1):
337 scale = self._lineLineCrossingDistOnLine(p0, p1, numpy.array([0,0,0], numpy.float32), numpy.array(endPoint, numpy.float32)) / 15.0 / s
338 if not wx.GetKeyState(wx.WXK_SHIFT):
339 scale = round(scale * 10) / 10
345 if self.node == 1 or not wx.GetKeyState(wx.WXK_CONTROL):
346 self.parent.tempMatrix = numpy.matrix([[scale,0,0], [0, scale, 0], [0, 0, scale]], numpy.float64)
348 self.parent.tempMatrix = numpy.matrix([[scale,0,0], [0, 1, 0], [0, 0, 1]], numpy.float64)
350 self.parent.tempMatrix = numpy.matrix([[1,0,0], [0, scale, 0], [0, 0, 1]], numpy.float64)
352 self.parent.tempMatrix = numpy.matrix([[1,0,0], [0, 1, 0], [0, 0, scale]], numpy.float64)
362 if self.node == 2 and self.scale is not None:
364 if self.node == 3 and self.scale is not None:
366 if self.node == 4 and self.scale is not None:
368 objMatrix = self.parent.getObjectMatrix()
369 scaleX = numpy.linalg.norm(objMatrix[0].getA().flatten())
370 scaleY = numpy.linalg.norm(objMatrix[1].getA().flatten())
371 scaleZ = numpy.linalg.norm(objMatrix[2].getA().flatten())
372 if self.scale is not None:
377 glDisable(GL_LIGHTING)
378 glDisable(GL_DEPTH_TEST)
380 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
383 size = self.parent.getObjectSize()
384 radius = self.parent.getObjectBoundaryCircle() * max(scaleX, scaleY, scaleZ)
386 glTranslate(0,0,size[2]/2 + 5)
387 glRotate(-self.parent.yaw, 0,0,1)
388 if self.parent.pitch < 80:
389 glTranslate(0, radius + 5,0)
390 elif self.parent.pitch < 100:
391 glTranslate(0, (radius + 5) * (90 - self.parent.pitch) / 10,0)
393 glTranslate(0,-(radius + 5),0)
394 opengl.glDrawStringCenter("%dx%dx%d" % (size[0], size[1], size[2]))
402 glColor3ub(128,128,0)
412 glColor3ub(255,255,255)
414 glColor3ub(192,192,192)
415 opengl.DrawBox([-s,-s,-s], [s,s,s])
418 opengl.glDrawStringCenter("%0.2f" % ((scaleX + scaleY + scaleZ) / 3.0))
421 glColor3ub(255,64,64)
426 opengl.DrawBox([-s,-s,-s], [s,s,s])
429 opengl.glDrawStringCenter("%0.2f" % (scaleX))
432 glColor3ub(255,255,0)
434 glColor3ub(128,128,0)
437 opengl.DrawBox([-s,-s,-s], [s,s,s])
440 opengl.glDrawStringCenter("%0.2f" % (scaleY))
443 glColor3ub(64,255,64)
448 opengl.DrawBox([-s,-s,-s], [s,s,s])
451 opengl.glDrawStringCenter("%0.2f" % (scaleZ))
454 glEnable(GL_DEPTH_TEST)