printrun-src/printrun/stlview.py

Wed, 20 Jan 2021 11:37:03 +0100

author
mdd
date
Wed, 20 Jan 2021 11:37:03 +0100
changeset 48
3c27b4ee6fec
parent 46
cce0af6351f0
permissions
-rwxr-xr-x

reimplemented lasercutter changes

46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
1 #!/usr/bin/env python3
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import numpy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import pyglet
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 pyglet.options['debug_gl'] = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 from pyglet.gl import GL_AMBIENT_AND_DIFFUSE, glBegin, glClearColor, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 glColor3f, GL_CULL_FACE, GL_DEPTH_TEST, GL_DIFFUSE, GL_EMISSION, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 glEnable, glEnd, GL_FILL, GLfloat, GL_FRONT_AND_BACK, GL_LIGHT0, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 GL_LIGHT1, glLightfv, GL_LIGHTING, GL_LINE, glMaterialf, glMaterialfv, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 glMultMatrixd, glNormal3f, glPolygonMode, glPopMatrix, GL_POSITION, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 glPushMatrix, glRotatef, glScalef, glShadeModel, GL_SHININESS, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 GL_SMOOTH, GL_SPECULAR, glTranslatef, GL_TRIANGLES, glVertex3f, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 glGetDoublev, GL_MODELVIEW_MATRIX, GLdouble, glClearDepth, glDepthFunc, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 GL_LEQUAL, GL_BLEND, glBlendFunc, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 GL_LINE_LOOP, glGetFloatv, GL_LINE_WIDTH, glLineWidth, glDisable, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 GL_LINE_SMOOTH
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 from pyglet import gl
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 from .gl.panel import wxGLPanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 from .gl.trackball import build_rotmatrix
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 from .gl.libtatlin import actors
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 def vec(*args):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 return (GLfloat * len(args))(*args)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
45 class stlview:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 def __init__(self, facets, batch):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 # Create the vertex and normal arrays.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 vertices = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 normals = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 for i in facets:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 for j in i[1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 vertices.extend(j)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 normals.extend(i[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 # Create a list of triangle indices.
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
57 indices = list(range(3 * len(facets))) # [[3*i, 3*i+1, 3*i+2] for i in xrange(len(facets))]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 self.vertex_list = batch.add_indexed(len(vertices) // 3,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 GL_TRIANGLES,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 None, # group,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 indices,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 ('v3f/static', vertices),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 ('n3f/static', normals))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 def delete(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 self.vertex_list.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 class StlViewPanel(wxGLPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 do_lights = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
72 def __init__(self, parent, size,
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 build_dimensions = None, circular = False,
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
74 antialias_samples = 0,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
75 grid = (1, 10)):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
76 super().__init__(parent, wx.DefaultPosition, size, 0,
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 self.batches = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 self.rot = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 self.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.move)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.wheel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 self.canvas.Bind(wx.EVT_LEFT_DCLICK, self.double_click)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 self.parent = parent
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 if build_dimensions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.build_dimensions = build_dimensions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.build_dimensions = [200, 200, 100, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 self.platform = actors.Platform(self.build_dimensions,
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
91 circular = circular,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
92 grid = grid)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 self.dist = max(self.build_dimensions[0], self.build_dimensions[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 self.basequat = [0, 0, 0, 1]
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
95 wx.CallAfter(self.forceresize) #why needed
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 self.mousepos = (0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 def OnReshape(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 self.mview_initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 super(StlViewPanel, self).OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 # GLFrame OpenGL Event Handlers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 def OnInitGL(self, call_reshape = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 '''Initialize OpenGL for use in the window.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 if self.GLinitialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.GLinitialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 # create a pyglet context for this panel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 self.pygletcontext = gl.Context(gl.current_context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 self.pygletcontext.canvas = self
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.pygletcontext.set_current()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 # normal gl init
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 glClearColor(0, 0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 glColor3f(1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 glEnable(GL_DEPTH_TEST)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 glClearDepth(1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 glDepthFunc(GL_LEQUAL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 glEnable(GL_BLEND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 # Uncomment this line for a wireframe view
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 # glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 # Simple light setup. On Windows GL_LIGHT0 is enabled by default,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 # but this is not the case on Linux or Mac, so remember to always
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 # include it.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 glEnable(GL_LIGHT0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 glEnable(GL_LIGHT1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 glLightfv(GL_LIGHT0, GL_POSITION, vec(.5, .5, 1, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 glLightfv(GL_LIGHT0, GL_SPECULAR, vec(.5, .5, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 glLightfv(GL_LIGHT0, GL_DIFFUSE, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 glLightfv(GL_LIGHT1, GL_POSITION, vec(1, 0, .5, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 glLightfv(GL_LIGHT1, GL_DIFFUSE, vec(.5, .5, .5, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 glLightfv(GL_LIGHT1, GL_SPECULAR, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 glShadeModel(GL_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.5, 0, 0.3, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0.1, 0, 0.9))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 if call_reshape:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 self.OnReshape()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 if hasattr(self.parent, "filenames") and self.parent.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 for filename in self.parent.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 self.parent.load_file(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 self.parent.autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 if hasattr(self.parent, "loadcb"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 self.parent.loadcb()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 self.parent.filenames = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 def double_click(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 if hasattr(self.parent, "clickcb") and self.parent.clickcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 self.parent.clickcb(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 def forceresize(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
160 #print('forceresize')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
161 x, y = self.GetClientSize()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
162 #TODO: probably not needed
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
163 self.SetClientSize((x, y+1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
164 self.SetClientSize((x, y))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 def move(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 """react to mouse actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 no mouse: show red mousedrop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 LMB: move active object,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 with shift rotate viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 RMB: nothing
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 with shift move viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 """
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
175 self.mousepos = event.GetPosition()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
176 if event.Dragging():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
177 if event.LeftIsDown():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
178 self.handle_rotation(event)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
179 elif event.RightIsDown():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
180 self.handle_translation(event)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
181 self.Refresh(False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
182 elif event.ButtonUp(wx.MOUSE_BTN_LEFT) or \
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
183 event.ButtonUp(wx.MOUSE_BTN_RIGHT):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
184 self.initpos = None
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 def handle_wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 delta = event.GetWheelRotation()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 factor = 1.05
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
190 x, y = event.GetPosition()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 x, y, _ = self.mouse_to_3d(x, y, local_transform = True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 if delta > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 self.zoom(1 / factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 def wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 """react to mouse wheel actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 rotate object
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 with shift zoom viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 self.handle_wheel(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 def keypress(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 """gets keypress events and moves/rotates acive shape"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 keycode = event.GetKeyCode()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 step = 5
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 angle = 18
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 if event.ControlDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 step = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 angle = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 # h
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 if keycode == 72:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 self.parent.move_shape((-step, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 # l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 if keycode == 76:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 self.parent.move_shape((step, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 # j
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 if keycode == 75:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self.parent.move_shape((0, step))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 # k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 if keycode == 74:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 self.parent.move_shape((0, -step))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 # [
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 if keycode == 91:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 self.parent.rotate_shape(-angle)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 # ]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 if keycode == 93:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 self.parent.rotate_shape(angle)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 def anim(self, obj):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 g = 50 * 9.8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 v = 20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 dt = 0.05
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 basepos = obj.offsets[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 obj.offsets[2] += obj.animoffset
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 while obj.offsets[2] > -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 time.sleep(dt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 obj.offsets[2] -= v * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 v += g * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 if obj.offsets[2] < 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 obj.scale[2] *= 1 - 3 * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 # return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 v = v / 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 while obj.offsets[2] < basepos:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 time.sleep(dt)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 obj.offsets[2] += v * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 v -= g * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 obj.scale[2] *= 1 + 5 * dt
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 obj.scale[2] = 1.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 def create_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 '''create opengl objects when opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 if not self.platform.initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.platform.init()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 self.initialized = 1
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
260 #TODO: this probably creates constant redraw
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
261 # create_objects is called during OnDraw, remove
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 def prepare_model(self, m, scale):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 batch = pyglet.graphics.Batch()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 stlview(m.facets, batch = batch)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 m.batch = batch
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 # m.animoffset = 300
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 # threading.Thread(target = self.anim, args = (m, )).start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 def update_object_resize(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 '''called when the window recieves only if opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 def draw_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 '''called in the middle of ondraw after the buffer has been cleared'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 self.create_objects()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 glTranslatef(0, 0, -self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 glMultMatrixd(build_rotmatrix(self.basequat)) # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.2, 0.2, 0.2, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 glTranslatef(- self.build_dimensions[3] - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 - self.build_dimensions[4] - self.platform.depth / 2, 0) # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 # Draw platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 glDisable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 self.platform.draw()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 # Draw mouse
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 plane_normal = (0, 0, 1), plane_offset = 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 local_transform = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 if inter is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 glTranslatef(inter[0], inter[1], inter[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 glBegin(GL_TRIANGLES)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(1, 0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 glNormal3f(0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 glVertex3f(2, 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 glVertex3f(-2, 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 glVertex3f(-2, -2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 glVertex3f(2, -2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 glVertex3f(2, 2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 glVertex3f(-2, -2, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 # Draw objects
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 glDisable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0.3, 0.7, 0.5, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 for i in self.parent.models:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 model = self.parent.models[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 glTranslatef(*(model.offsets))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 glRotatef(model.rot, 0.0, 0.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 glTranslatef(*(model.centeroffset))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 glScalef(*model.scale)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 model.batch.draw()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 # Draw cutting plane
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 if self.parent.cutting:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 # FIXME: make this a proper Actor
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 axis = self.parent.cutting_axis
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 fixed_dist = self.parent.cutting_dist
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 dist, plane_width, plane_height = self.get_cutting_plane(axis, fixed_dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 if dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 if axis == "x":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 glRotatef(90, 0, 1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 glRotatef(90, 0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 glTranslatef(0, 0, dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 elif axis == "y":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 glRotatef(90, 1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 glTranslatef(0, 0, -dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 elif axis == "z":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 glTranslatef(0, 0, dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 glDisable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 glBegin(GL_TRIANGLES)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0, 0.9, 0.15, 0.3))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 glNormal3f(0, 0, self.parent.cutting_direction)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 glVertex3f(plane_width, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 glVertex3f(0, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 glVertex3f(plane_width, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 glVertex3f(plane_width, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 glEnable(GL_CULL_FACE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 glEnable(GL_LINE_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 orig_linewidth = (GLfloat)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 glGetFloatv(GL_LINE_WIDTH, orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 glLineWidth(4.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 glBegin(GL_LINE_LOOP)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, vec(0, 0.8, 0.15, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 glVertex3f(0, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 glVertex3f(plane_width, plane_height, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 glVertex3f(plane_width, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 glLineWidth(orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 glDisable(GL_LINE_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 # Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 def get_modelview_mat(self, local_transform):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 if local_transform:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 glTranslatef(0, 0, -self.dist)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 glMultMatrixd(build_rotmatrix(self.basequat)) # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 glTranslatef(- self.build_dimensions[3] - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 - self.build_dimensions[4] - self.platform.depth / 2, 0) # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 return mvmat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 def get_cutting_plane(self, cutting_axis, fixed_dist, local_transform = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 cutting_plane_sizes = {"x": (self.platform.depth, self.platform.height),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 "y": (self.platform.width, self.platform.height),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 "z": (self.platform.width, self.platform.depth)}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 plane_width, plane_height = cutting_plane_sizes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 if fixed_dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 return fixed_dist, plane_width, plane_height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 ref_sizes = {"x": self.platform.width,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 "y": self.platform.depth,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 "z": self.platform.height,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 ref_planes = {"x": (0, 0, 1),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 "y": (0, 0, 1),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 "z": (0, 1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 ref_offsets = {"x": 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 "y": 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 "z": - self.platform.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 translate_axis = {"x": 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 "y": 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 "z": 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 fallback_ref_planes = {"x": (0, 1, 0),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 "y": (1, 0, 0),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 "z": (1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 fallback_ref_offsets = {"x": - self.platform.height / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 "y": - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 "z": - self.platform.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 ref_size = ref_sizes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 ref_plane = ref_planes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 ref_offset = ref_offsets[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 plane_normal = ref_plane,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 plane_offset = ref_offset,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 local_transform = local_transform)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 max_size = max((self.platform.width,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 self.platform.depth,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 self.platform.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 dist = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 if inter is not None and numpy.fabs(inter).max() + max_size / 2 < 2 * max_size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 dist = inter[translate_axis[cutting_axis]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 if dist is None or dist < -0.5 * ref_size or dist > 1.5 * ref_size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 ref_plane = fallback_ref_planes[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 ref_offset = fallback_ref_offsets[cutting_axis]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 inter = self.mouse_to_plane(self.mousepos[0], self.mousepos[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 plane_normal = ref_plane,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 plane_offset = ref_offset,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 local_transform = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 if inter is not None and numpy.fabs(inter).max() + max_size / 2 < 2 * max_size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 dist = inter[translate_axis[cutting_axis]]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 if dist is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 dist = min(1.5 * ref_size, max(-0.5 * ref_size, dist))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 return dist, plane_width, plane_height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 def main():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 app = wx.App(redirect = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 frame = wx.Frame(None, -1, "GL Window", size = (400, 400))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 StlViewPanel(frame)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 frame.Show(True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 app.MainLoop()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 app.Destroy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 if __name__ == "__main__":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 main()

mercurial