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 objMatrix = self.parent.getObjectMatrix()
340 scaleX = numpy.linalg.norm(objMatrix[::,0].getA().flatten())
341 scaleY = numpy.linalg.norm(objMatrix[::,1].getA().flatten())
342 scaleZ = numpy.linalg.norm(objMatrix[::,2].getA().flatten())
343 if self.node == 1 or not wx.GetKeyState(wx.WXK_CONTROL):
344 matrixScale = (scaleX + scaleY + scaleZ) / 3
351 scale = (round((matrixScale * scale) * 10) / 10) / matrixScale
357 if self.node == 1 or not wx.GetKeyState(wx.WXK_CONTROL):
358 self.parent.tempMatrix = numpy.matrix([[scale,0,0], [0, scale, 0], [0, 0, scale]], numpy.float64)
360 self.parent.tempMatrix = numpy.matrix([[scale,0,0], [0, 1, 0], [0, 0, 1]], numpy.float64)
362 self.parent.tempMatrix = numpy.matrix([[1,0,0], [0, scale, 0], [0, 0, 1]], numpy.float64)
364 self.parent.tempMatrix = numpy.matrix([[1,0,0], [0, 1, 0], [0, 0, scale]], numpy.float64)
374 if self.node == 2 and self.scale is not None:
376 if self.node == 3 and self.scale is not None:
378 if self.node == 4 and self.scale is not None:
380 objMatrix = self.parent.getObjectMatrix()
381 scaleX = numpy.linalg.norm(objMatrix[::,0].getA().flatten())
382 scaleY = numpy.linalg.norm(objMatrix[::,1].getA().flatten())
383 scaleZ = numpy.linalg.norm(objMatrix[::,2].getA().flatten())
384 if self.scale is not None:
389 glDisable(GL_LIGHTING)
390 glDisable(GL_DEPTH_TEST)
392 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
395 size = self.parent.getObjectSize()
396 radius = self.parent.getObjectBoundaryCircle()
397 if self.scale is not None:
400 glTranslate(0,0,size[2]/2 + 5)
401 glRotate(-self.parent.yaw, 0,0,1)
402 if self.parent.pitch < 80:
403 glTranslate(0, radius + 5,0)
404 elif self.parent.pitch < 100:
405 glTranslate(0, (radius + 5) * (90 - self.parent.pitch) / 10,0)
407 glTranslate(0,-(radius + 5),0)
408 if self.parent.tempMatrix is not None:
409 size = (numpy.matrix([size]) * self.parent.tempMatrix).getA().flatten()
410 opengl.glDrawStringCenter("W, D, H: %0.1f, %0.1f, %0.1f mm" % (size[0], size[1], size[2]))
418 glColor3ub(128,128,0)
428 glColor3ub(255,255,255)
430 glColor3ub(192,192,192)
431 opengl.DrawBox([-s,-s,-s], [s,s,s])
434 opengl.glDrawStringCenter("%0.2f" % ((scaleX + scaleY + scaleZ) / 3.0))
437 glColor3ub(255,64,64)
442 opengl.DrawBox([-s,-s,-s], [s,s,s])
445 opengl.glDrawStringCenter("%0.2f" % (scaleX))
448 glColor3ub(255,255,0)
450 glColor3ub(128,128,0)
453 opengl.DrawBox([-s,-s,-s], [s,s,s])
456 opengl.glDrawStringCenter("%0.2f" % (scaleY))
459 glColor3ub(64,255,64)
464 opengl.DrawBox([-s,-s,-s], [s,s,s])
467 opengl.glDrawStringCenter("%0.2f" % (scaleZ))
470 glEnable(GL_DEPTH_TEST)
476 glVertex3f(size[0], size[1], size[2])
477 glVertex3f(size[0], size[1], size[2]/4*3)
478 glVertex3f(size[0], size[1], size[2])
479 glVertex3f(size[0], size[1]/4*3, size[2])
480 glVertex3f(size[0], size[1], size[2])
481 glVertex3f(size[0]/4*3, size[1], size[2])
483 glVertex3f(-size[0], size[1], size[2])
484 glVertex3f(-size[0], size[1], size[2]/4*3)
485 glVertex3f(-size[0], size[1], size[2])
486 glVertex3f(-size[0], size[1]/4*3, size[2])
487 glVertex3f(-size[0], size[1], size[2])
488 glVertex3f(-size[0]/4*3, size[1], size[2])
490 glVertex3f(size[0], -size[1], size[2])
491 glVertex3f(size[0], -size[1], size[2]/4*3)
492 glVertex3f(size[0], -size[1], size[2])
493 glVertex3f(size[0], -size[1]/4*3, size[2])
494 glVertex3f(size[0], -size[1], size[2])
495 glVertex3f(size[0]/4*3, -size[1], size[2])
497 glVertex3f(-size[0], -size[1], size[2])
498 glVertex3f(-size[0], -size[1], size[2]/4*3)
499 glVertex3f(-size[0], -size[1], size[2])
500 glVertex3f(-size[0], -size[1]/4*3, size[2])
501 glVertex3f(-size[0], -size[1], size[2])
502 glVertex3f(-size[0]/4*3, -size[1], size[2])
504 glVertex3f(size[0], size[1], -size[2])
505 glVertex3f(size[0], size[1], -size[2]/4*3)
506 glVertex3f(size[0], size[1], -size[2])
507 glVertex3f(size[0], size[1]/4*3, -size[2])
508 glVertex3f(size[0], size[1], -size[2])
509 glVertex3f(size[0]/4*3, size[1], -size[2])
511 glVertex3f(-size[0], size[1], -size[2])
512 glVertex3f(-size[0], size[1], -size[2]/4*3)
513 glVertex3f(-size[0], size[1], -size[2])
514 glVertex3f(-size[0], size[1]/4*3, -size[2])
515 glVertex3f(-size[0], size[1], -size[2])
516 glVertex3f(-size[0]/4*3, size[1], -size[2])
518 glVertex3f(size[0], -size[1], -size[2])
519 glVertex3f(size[0], -size[1], -size[2]/4*3)
520 glVertex3f(size[0], -size[1], -size[2])
521 glVertex3f(size[0], -size[1]/4*3, -size[2])
522 glVertex3f(size[0], -size[1], -size[2])
523 glVertex3f(size[0]/4*3, -size[1], -size[2])
525 glVertex3f(-size[0], -size[1], -size[2])
526 glVertex3f(-size[0], -size[1], -size[2]/4*3)
527 glVertex3f(-size[0], -size[1], -size[2])
528 glVertex3f(-size[0], -size[1]/4*3, -size[2])
529 glVertex3f(-size[0], -size[1], -size[2])
530 glVertex3f(-size[0]/4*3, -size[1], -size[2])
533 glEnable(GL_DEPTH_TEST)