printrun-src/printrun/gl/libtatlin/actors.py

Fri, 03 Jun 2016 09:16:07 +0200

author
mbayer
date
Fri, 03 Jun 2016 09:16:07 +0200
changeset 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added printrun sourcecode from
https://github.com/kliment/Printrun
03.06.2016 09:10

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 # -*- coding: utf-8 -*-
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 # Copyright (C) 2013 Guillaume Seguin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # Copyright (C) 2011 Denis Kobozev
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # This program 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 2 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 # This program 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 this program; if not, write to the Free Software Foundation,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 # Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import numpy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import array
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 import math
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 import threading
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 from ctypes import sizeof
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 from pyglet.gl import glPushMatrix, glPopMatrix, glTranslatef, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 glGenLists, glNewList, GL_COMPILE, glEndList, glCallList, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 GL_ELEMENT_ARRAY_BUFFER, GL_UNSIGNED_INT, GL_TRIANGLES, GL_LINE_LOOP, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 GL_ARRAY_BUFFER, GL_STATIC_DRAW, glColor4f, glVertex3f, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32 glBegin, glEnd, GL_LINES, glEnable, glDisable, glGetFloatv, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 GL_LINE_SMOOTH, glLineWidth, GL_LINE_WIDTH, GLfloat, GL_FLOAT, GLuint, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 glVertexPointer, glColorPointer, glDrawArrays, glDrawRangeElements, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 glEnableClientState, glDisableClientState, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 GL_FRONT_AND_BACK, GL_FRONT, glMaterialfv, GL_SPECULAR, GL_EMISSION, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 glColorMaterial, GL_AMBIENT_AND_DIFFUSE, glMaterialf, GL_SHININESS, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 GL_NORMAL_ARRAY, glNormalPointer, GL_LIGHTING, glColor3f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 from pyglet.graphics.vertexbuffer import create_buffer, VertexBufferObject
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 from printrun.utils import install_locale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 def vec(*args):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 return (GLfloat * len(args))(*args)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 def compile_display_list(func, *options):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 display_list = glGenLists(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 glNewList(display_list, GL_COMPILE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 func(*options)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 glEndList()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 return display_list
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 def numpy2vbo(nparray, target = GL_ARRAY_BUFFER, usage = GL_STATIC_DRAW, use_vbos = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 vbo = create_buffer(nparray.nbytes, target = target, usage = usage, vbo = use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 vbo.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 vbo.set_data(nparray.ctypes.data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 return vbo
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 def triangulate_rectangle(i1, i2, i3, i4):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 return [i1, i4, i3, i3, i2, i1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 def triangulate_box(i1, i2, i3, i4,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 j1, j2, j3, j4):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 return [i1, i2, j2, j2, j1, i1, i2, i3, j3, j3, j2, i2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 i3, i4, j4, j4, j3, i3, i4, i1, j1, j1, j4, i4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 class BoundingBox(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 A rectangular box (cuboid) enclosing a 3D model, defined by lower and upper corners.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 def __init__(self, upper_corner, lower_corner):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 self.upper_corner = upper_corner
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 self.lower_corner = lower_corner
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 def width(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 width = abs(self.upper_corner[0] - self.lower_corner[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 return round(width, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 def depth(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 depth = abs(self.upper_corner[1] - self.lower_corner[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 return round(depth, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 def height(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 height = abs(self.upper_corner[2] - self.lower_corner[2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 return round(height, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 class Platform(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 Platform on which models are placed.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 graduations_major = 10
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 def __init__(self, build_dimensions, light = False, circular = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 self.light = light
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 self.circular = circular
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 self.width = build_dimensions[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 self.depth = build_dimensions[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 self.height = build_dimensions[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 self.xoffset = build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 self.yoffset = build_dimensions[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 self.zoffset = build_dimensions[5]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 self.color_grads_minor = (0xaf / 255, 0xdf / 255, 0x5f / 255, 0.1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.color_grads_interm = (0xaf / 255, 0xdf / 255, 0x5f / 255, 0.2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110 self.color_grads_major = (0xaf / 255, 0xdf / 255, 0x5f / 255, 0.33)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 def init(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.display_list = compile_display_list(self.draw)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 def draw(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 glTranslatef(self.xoffset, self.yoffset, self.zoffset)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 def color(i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 if i % self.graduations_major == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 glColor4f(*self.color_grads_major)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 elif i % (self.graduations_major / 2) == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 glColor4f(*self.color_grads_interm)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 if self.light: return False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 glColor4f(*self.color_grads_minor)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 return True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 # draw the grid
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 glBegin(GL_LINES)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 if self.circular: # Draw a circular grid
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 for i in range(0, int(math.ceil(self.width + 1))):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 angle = math.asin(2 * float(i) / self.width - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 x = (math.cos(angle) + 1) * self.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 if color(i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 glVertex3f(float(i), self.depth - x, 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 glVertex3f(float(i), x, 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 for i in range(0, int(math.ceil(self.depth + 1))):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 angle = math.acos(2 * float(i) / self.depth - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 x = (math.sin(angle) + 1) * self.width / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 if color(i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 glVertex3f(self.width - x, float(i), 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 glVertex3f(x, float(i), 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 else: # Draw a rectangular grid
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 for i in range(0, int(math.ceil(self.width + 1))):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 if color(i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 glVertex3f(float(i), 0.0, 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 glVertex3f(float(i), self.depth, 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 for i in range(0, int(math.ceil(self.depth + 1))):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 if color(i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 glVertex3f(0, float(i), 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 glVertex3f(self.width, float(i), 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 if self.circular:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 glBegin(GL_LINE_LOOP)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 for i in range(0, 360):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 angle = math.radians(i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 glVertex3f((math.cos(angle) + 1) * self.width / 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 (math.sin(angle) + 1) * self.depth / 2, 0.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 def display(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 # FIXME: using the list sometimes results in graphical corruptions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 # glCallList(self.display_list)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 self.draw()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 class PrintHead(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 def __init__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 self.color = (43. / 255, 0., 175. / 255, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 self.scale = 5
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 self.height = 5
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 def init(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 self.display_list = compile_display_list(self.draw)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 def draw(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 glBegin(GL_LINES)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 glColor4f(*self.color)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 for di in [-1, 1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 for dj in [-1, 1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 glVertex3f(0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 glVertex3f(self.scale * di, self.scale * dj, self.height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 glEnd()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 def display(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 glEnable(GL_LINE_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 orig_linewidth = (GLfloat)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 glGetFloatv(GL_LINE_WIDTH, orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 glLineWidth(3.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 glCallList(self.display_list)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 glLineWidth(orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 glDisable(GL_LINE_SMOOTH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 class Model(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 Parent class for models that provides common functionality.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 AXIS_X = (1, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 AXIS_Y = (0, 1, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 AXIS_Z = (0, 0, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 letter_axis_map = {
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 'x': AXIS_X,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 'y': AXIS_Y,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 'z': AXIS_Z,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 }
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 axis_letter_map = dict([(v, k) for k, v in letter_axis_map.items()])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 lock = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 def __init__(self, offset_x=0, offset_y=0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 self.offset_x = offset_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 self.offset_y = offset_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 self.lock = threading.Lock()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 self.init_model_attributes()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 def init_model_attributes(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 Set/reset saved properties.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 self.invalidate_bounding_box()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 self.modified = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 def invalidate_bounding_box(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 self._bounding_box = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 def bounding_box(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 Get a bounding box for the model.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 if self._bounding_box is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 self._bounding_box = self._calculate_bounding_box()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 return self._bounding_box
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 def _calculate_bounding_box(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 Calculate an axis-aligned box enclosing the model.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 # swap rows and columns in our vertex arrays so that we can do max and
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 # min on axis 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 xyz_rows = self.vertices.reshape(-1, order='F').reshape(3, -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 lower_corner = xyz_rows.min(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 upper_corner = xyz_rows.max(1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 box = BoundingBox(upper_corner, lower_corner)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 return box
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 def width(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 return self.bounding_box.width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 def depth(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 return self.bounding_box.depth
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 @property
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 def height(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 return self.bounding_box.height
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 def movement_color(self, move):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 Return the color to use for particular type of movement.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 if move.extruding:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 if move.current_tool == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 return self.color_tool0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 elif move.current_tool == 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 return self.color_tool1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 elif move.current_tool == 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 return self.color_tool2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 elif move.current_tool == 3:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 return self.color_tool3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 return self.color_tool4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 return self.color_travel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 def movement_angle(src, dst, precision=0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 x = dst[0] - src[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 y = dst[1] - src[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 angle = math.degrees(math.atan2(y, -x)) # negate x for clockwise rotation angle
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 return round(angle, precision)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 def get_next_move(gcode, layer_idx, gline_idx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 gline_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 while layer_idx < len(gcode.all_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 layer = gcode.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 while gline_idx < len(layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 gline = layer[gline_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 if gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 return gline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 gline_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 gline_idx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 class GcodeModel(Model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 Model for displaying Gcode data.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 color_travel = (0.6, 0.6, 0.6, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 color_tool0 = (1.0, 0.0, 0.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 color_tool1 = (0.67, 0.05, 0.9, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 color_tool2 = (1.0, 0.8, 0., 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 color_tool3 = (1.0, 0., 0.62, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 color_tool4 = (0., 1.0, 0.58, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 color_printed = (0.2, 0.75, 0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 color_current = (0, 0.9, 1.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 color_current_printed = (0.1, 0.4, 0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 display_travels = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 buffers_created = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 use_vbos = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 fully_loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 path_halfwidth = 0.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 path_halfheight = 0.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 def set_path_size(self, path_halfwidth, path_halfheight):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 self.path_halfwidth = path_halfwidth
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 self.path_halfheight = path_halfheight
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 def load_data(self, model_data, callback=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 t_start = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 self.gcode = model_data
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 self.count_travel_indices = count_travel_indices = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 self.count_print_indices = count_print_indices = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 self.count_print_vertices = count_print_vertices = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 # Some trivial computations, but that's mostly for documentation :)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 # Not like 10 multiplications are going to cost much time vs what's
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 # about to happen :)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 # Max number of values which can be generated per gline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 # to store coordinates/colors/normals.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 # Nicely enough we have 3 per kind of thing for all kinds.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 coordspervertex = 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 verticesperline = 8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 coordsperline = coordspervertex * verticesperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 coords_count = lambda nlines: nlines * coordsperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 travelverticesperline = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 travelcoordsperline = coordspervertex * travelverticesperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 travel_coords_count = lambda nlines: nlines * travelcoordsperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 trianglesperface = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 facesperbox = 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 trianglesperbox = trianglesperface * facesperbox
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 verticespertriangle = 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 indicesperbox = verticespertriangle * trianglesperbox
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 boxperline = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 indicesperline = indicesperbox * boxperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 indices_count = lambda nlines: nlines * indicesperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 ntravelcoords = travel_coords_count(nlines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 ncoords = coords_count(nlines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 nindices = indices_count(nlines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 travel_vertices = self.travels = numpy.zeros(ntravelcoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 travel_vertex_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 vertices = self.vertices = numpy.zeros(ncoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 vertex_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 colors = self.colors = numpy.zeros(ncoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 color_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 normals = self.normals = numpy.zeros(ncoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 normal_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 indices = self.indices = numpy.zeros(nindices, dtype = GLuint)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 index_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 self.layer_idxs_map = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 self.layer_stops = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 prev_is_extruding = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 prev_move_normal_x = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 prev_move_normal_y = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 prev_move_angle = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 prev_pos = (0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 layer_idx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 self.printed_until = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 self.only_current = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 twopi = 2 * math.pi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 processed_lines = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 while layer_idx < len(model_data.all_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 remaining_lines = nlines - processed_lines
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 # Only reallocate memory which might be needed, not memory
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 # for everything
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 ntravelcoords = coords_count(remaining_lines) + travel_vertex_k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 ncoords = coords_count(remaining_lines) + vertex_k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 nindices = indices_count(remaining_lines) + index_k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 if ncoords > vertices.size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 self.travels.resize(ntravelcoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 self.vertices.resize(ncoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 self.colors.resize(ncoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 self.normals.resize(ncoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 self.indices.resize(nindices, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 layer = model_data.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 has_movement = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 for gline_idx, gline in enumerate(layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 if not gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 if gline.x is None and gline.y is None and gline.z is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 has_movement = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 current_pos = (gline.current_x, gline.current_y, gline.current_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 if not gline.extruding:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 travel_vertices[travel_vertex_k] = prev_pos[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 travel_vertices[travel_vertex_k + 1] = prev_pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 travel_vertices[travel_vertex_k + 2] = prev_pos[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 travel_vertices[travel_vertex_k + 3] = current_pos[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 travel_vertices[travel_vertex_k + 4] = current_pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 travel_vertices[travel_vertex_k + 5] = current_pos[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 travel_vertex_k += 6
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 prev_is_extruding = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 gline_color = self.movement_color(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 next_move = get_next_move(model_data, layer_idx, gline_idx)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 next_is_extruding = (next_move.extruding
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 if next_move is not None else False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 delta_x = current_pos[0] - prev_pos[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 delta_y = current_pos[1] - prev_pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 norm = delta_x * delta_x + delta_y * delta_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 if norm == 0: # Don't draw anything if this move is Z+E only
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 norm = math.sqrt(norm)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 move_normal_x = - delta_y / norm
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 move_normal_y = delta_x / norm
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 move_angle = math.atan2(delta_y, delta_x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 # FIXME: compute these dynamically
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 path_halfwidth = self.path_halfwidth * 1.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 path_halfheight = self.path_halfheight * 1.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 new_indices = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 new_vertices = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 new_normals = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 if prev_is_extruding:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 # Store previous vertices indices
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 prev_id = vertex_k / 3 - 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 avg_move_normal_x = (prev_move_normal_x + move_normal_x) / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 avg_move_normal_y = (prev_move_normal_y + move_normal_y) / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 norm = avg_move_normal_x * avg_move_normal_x + avg_move_normal_y * avg_move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478 if norm == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 avg_move_normal_x = move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 avg_move_normal_y = move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 norm = math.sqrt(norm)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 avg_move_normal_x /= norm
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 avg_move_normal_y /= norm
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 delta_angle = move_angle - prev_move_angle
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486 delta_angle = (delta_angle + twopi) % twopi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 fact = abs(math.cos(delta_angle / 2))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488 # If move is turning too much, avoid creating a big peak
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 # by adding an intermediate box
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 if fact < 0.5:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 # FIXME: It looks like there's some heavy code duplication here...
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 hw = path_halfwidth
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 p1x = prev_pos[0] - hw * prev_move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 p2x = prev_pos[0] + hw * prev_move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 p1y = prev_pos[1] - hw * prev_move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 p2y = prev_pos[1] + hw * prev_move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 new_vertices.extend((p1x, p1y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 new_vertices.extend((p2x, p2y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 new_normals.extend((0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 new_normals.extend((-prev_move_normal_x, -prev_move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 new_normals.extend((0, 0, -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 new_normals.extend((prev_move_normal_x, prev_move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 first = vertex_k / 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 # Link to previous
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 new_indices += triangulate_box(prev_id, prev_id + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 prev_id + 2, prev_id + 3,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 first, first + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 first + 2, first + 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 p1x = prev_pos[0] - hw * move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 p2x = prev_pos[0] + hw * move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 p1y = prev_pos[1] - hw * move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 p2y = prev_pos[1] + hw * move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 new_vertices.extend((p1x, p1y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 new_vertices.extend((p2x, p2y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519 new_normals.extend((0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 new_normals.extend((-move_normal_x, -move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 new_normals.extend((0, 0, -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 new_normals.extend((move_normal_x, move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523 prev_id += 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 first += 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
525 # Link to previous
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
526 new_indices += triangulate_box(prev_id, prev_id + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
527 prev_id + 2, prev_id + 3,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
528 first, first + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
529 first + 2, first + 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
530 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
531 hw = path_halfwidth / fact
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
532 # Compute vertices
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
533 p1x = prev_pos[0] - hw * avg_move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
534 p2x = prev_pos[0] + hw * avg_move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
535 p1y = prev_pos[1] - hw * avg_move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
536 p2y = prev_pos[1] + hw * avg_move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
537 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
538 new_vertices.extend((p1x, p1y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
539 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
540 new_vertices.extend((p2x, p2y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
541 new_normals.extend((0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
542 new_normals.extend((-avg_move_normal_x, -avg_move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
543 new_normals.extend((0, 0, -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
544 new_normals.extend((avg_move_normal_x, avg_move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
545 first = vertex_k / 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
546 # Link to previous
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
547 new_indices += triangulate_box(prev_id, prev_id + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
548 prev_id + 2, prev_id + 3,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
549 first, first + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
550 first + 2, first + 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
551 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
552 # Compute vertices normal to the current move and cap it
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
553 p1x = prev_pos[0] - path_halfwidth * move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
554 p2x = prev_pos[0] + path_halfwidth * move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
555 p1y = prev_pos[1] - path_halfwidth * move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
556 p2y = prev_pos[1] + path_halfwidth * move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
557 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
558 new_vertices.extend((p1x, p1y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
559 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
560 new_vertices.extend((p2x, p2y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
561 new_normals.extend((0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
562 new_normals.extend((-move_normal_x, -move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
563 new_normals.extend((0, 0, -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
564 new_normals.extend((move_normal_x, move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
565 first = vertex_k / 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
566 new_indices = triangulate_rectangle(first, first + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
567 first + 2, first + 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
568
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
569 if not next_is_extruding:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
570 # Compute caps and link everything
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
571 p1x = current_pos[0] - path_halfwidth * move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
572 p2x = current_pos[0] + path_halfwidth * move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
573 p1y = current_pos[1] - path_halfwidth * move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
574 p2y = current_pos[1] + path_halfwidth * move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
575 new_vertices.extend((current_pos[0], current_pos[1], current_pos[2] + path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
576 new_vertices.extend((p1x, p1y, current_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
577 new_vertices.extend((current_pos[0], current_pos[1], current_pos[2] - path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
578 new_vertices.extend((p2x, p2y, current_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
579 new_normals.extend((0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
580 new_normals.extend((-move_normal_x, -move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
581 new_normals.extend((0, 0, -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
582 new_normals.extend((move_normal_x, move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
583 end_first = vertex_k / 3 + len(new_vertices) / 3 - 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
584 new_indices += triangulate_rectangle(end_first + 3, end_first + 2,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
585 end_first + 1, end_first)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
586 new_indices += triangulate_box(first, first + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
587 first + 2, first + 3,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
588 end_first, end_first + 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
589 end_first + 2, end_first + 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
590
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
591 for new_i, item in enumerate(new_indices):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
592 indices[index_k + new_i] = item
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
593 index_k += len(new_indices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
594 for new_i, item in enumerate(new_vertices):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
595 vertices[vertex_k + new_i] = item
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
596 vertex_k += len(new_vertices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
597 for new_i, item in enumerate(new_normals):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
598 normals[normal_k + new_i] = item
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
599 normal_k += len(new_normals)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
600 new_colors = list(gline_color)[:-1] * (len(new_vertices) / 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
601 for new_i, item in enumerate(new_colors):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
602 colors[color_k + new_i] = item
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
603 color_k += len(new_colors)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
604
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
605 prev_is_extruding = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
606 prev_move_normal_x = move_normal_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
607 prev_move_normal_y = move_normal_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
608 prev_move_angle = move_angle
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
609
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
610 prev_pos = current_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
611 count_travel_indices.append(travel_vertex_k / 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
612 count_print_indices.append(index_k)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
613 count_print_vertices.append(vertex_k / 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
614 gline.gcview_end_vertex = len(count_print_indices) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
615
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
616 if has_movement:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
617 self.layer_stops.append(len(count_print_indices) - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
618 self.layer_idxs_map[layer_idx] = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
619 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
620 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
621 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
622 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
623
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
624 processed_lines += len(layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
625
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
626 if callback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
627 callback(layer_idx + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
628
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
629 yield layer_idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
630 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
631
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
632 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
633 self.dims = ((model_data.xmin, model_data.xmax, model_data.width),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
634 (model_data.ymin, model_data.ymax, model_data.depth),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
635 (model_data.zmin, model_data.zmax, model_data.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
636
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
637 self.travels.resize(travel_vertex_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
638 self.vertices.resize(vertex_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
639 self.colors.resize(color_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
640 self.normals.resize(normal_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
641 self.indices.resize(index_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
642
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
643 self.layer_stops = array.array('L', self.layer_stops)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
644 self.count_travel_indices = array.array('L', count_travel_indices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
645 self.count_print_indices = array.array('L', count_print_indices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
646 self.count_print_vertices = array.array('L', count_print_vertices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
647
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
648 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
649 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
650 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
651 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
652 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
653 self.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
654
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
655 t_end = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
656
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
657 logging.debug(_('Initialized 3D visualization in %.2f seconds') % (t_end - t_start))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
658 logging.debug(_('Vertex count: %d') % ((len(self.vertices) + len(self.travels)) / 3))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
659 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
660
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
661 def copy(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
662 copy = GcodeModel()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
663 for var in ["vertices", "colors", "travels", "indices", "normals",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
664 "max_layers", "num_layers_to_draw", "printed_until",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
665 "layer_stops", "dims", "only_current",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
666 "layer_idxs_map", "count_travel_indices",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
667 "count_print_indices", "count_print_vertices",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
668 "path_halfwidth", "path_halfheight",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
669 "gcode"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
670 setattr(copy, var, getattr(self, var))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
671 copy.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
672 copy.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
673 copy.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
674 return copy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
675
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
676 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
677 # DRAWING
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
678 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
679
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
680 def init(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
681 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
682 self.layers_loaded = self.max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
683 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
684 if self.buffers_created:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
685 self.travel_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
686 self.index_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
687 self.vertex_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
688 self.vertex_color_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
689 self.vertex_normal_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
690 self.travel_buffer = numpy2vbo(self.travels, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
691 self.index_buffer = numpy2vbo(self.indices, use_vbos = self.use_vbos,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
692 target = GL_ELEMENT_ARRAY_BUFFER)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
693 self.vertex_buffer = numpy2vbo(self.vertices, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
694 self.vertex_color_buffer = numpy2vbo(self.colors, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
695 self.vertex_normal_buffer = numpy2vbo(self.normals, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
696 if self.fully_loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
697 # Delete numpy arrays after creating VBOs after full load
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
698 self.travels = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
699 self.indices = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
700 self.vertices = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
701 self.colors = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
702 self.normals = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
703 self.buffers_created = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
704
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
705 def display(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
706 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
707 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
708 glTranslatef(self.offset_x, self.offset_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
709 glEnableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
710
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
711 has_vbo = isinstance(self.vertex_buffer, VertexBufferObject)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
712 if self.display_travels:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
713 self._display_travels(has_vbo)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
714
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
715 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
716 glEnableClientState(GL_NORMAL_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
717 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
718 glMaterialfv(GL_FRONT, GL_SPECULAR, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
719 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
720 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
721
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
722 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
723 self._display_movements(has_vbo)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
724
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
725 glDisable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
726
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
727 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
728 glDisableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
729 glDisableClientState(GL_NORMAL_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
730
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
731 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
732
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
733 def _display_travels(self, has_vbo):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
734 self.travel_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
735 glVertexPointer(3, GL_FLOAT, 0, self.travel_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
736
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
737 # Prevent race condition by using the number of currently loaded layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
738 max_layers = self.layers_loaded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
739 # TODO: show current layer travels in a different color
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
740 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
741 end_index = self.count_travel_indices[end]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
742 glColor4f(*self.color_travel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
743 if self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
744 if self.num_layers_to_draw < max_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
745 end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
746 start_index = self.count_travel_indices[end_prev_layer + 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
747 glDrawArrays(GL_LINES, start_index, end_index - start_index + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
748 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
749 glDrawArrays(GL_LINES, 0, end_index)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
750
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
751 self.travel_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
752
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
753 def _draw_elements(self, start, end, draw_type = GL_TRIANGLES):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
754 # Don't attempt printing empty layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
755 if self.count_print_indices[end] == self.count_print_indices[start - 1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
756 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
757 glDrawRangeElements(draw_type,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
758 self.count_print_vertices[start - 1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
759 self.count_print_vertices[end] - 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
760 self.count_print_indices[end] - self.count_print_indices[start - 1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
761 GL_UNSIGNED_INT,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
762 sizeof(GLuint) * self.count_print_indices[start - 1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
763
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
764 def _display_movements(self, has_vbo):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
765 self.vertex_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
766 glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
767
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
768 self.vertex_color_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
769 glColorPointer(3, GL_FLOAT, 0, self.vertex_color_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
770
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
771 self.vertex_normal_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
772 glNormalPointer(GL_FLOAT, 0, self.vertex_normal_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
773
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
774 self.index_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
775
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
776 # Prevent race condition by using the number of currently loaded layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
777 max_layers = self.layers_loaded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
778
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
779 start = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
780 layer_selected = self.num_layers_to_draw <= max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
781 if layer_selected:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
782 end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
783 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
784 end_prev_layer = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
785 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
786
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
787 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
788
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
789 glColor3f(*self.color_printed[:-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
790
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
791 # Draw printed stuff until end or end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
792 cur_end = min(self.printed_until, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
793 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
794 if 1 <= end_prev_layer <= cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
795 self._draw_elements(1, end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
796 elif cur_end >= 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
797 self._draw_elements(1, cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
798
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
799 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
800
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
801 # Draw nonprinted stuff until end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
802 start = max(cur_end, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
803 if end_prev_layer >= start:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
804 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
805 self._draw_elements(start, end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
806 cur_end = end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
807
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
808 # Draw current layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
809 if layer_selected:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
810 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
811
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
812 glColor3f(*self.color_current_printed[:-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
813
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
814 if cur_end > end_prev_layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
815 self._draw_elements(end_prev_layer + 1, cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
816
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
817 glColor3f(*self.color_current[:-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
818
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
819 if end > cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
820 self._draw_elements(cur_end + 1, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
821
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
822 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
823
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
824 # Draw non printed stuff until end (if not ending at a given layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
825 start = max(self.printed_until, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
826 if not layer_selected and end >= start:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
827 self._draw_elements(start, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
828
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
829 self.index_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
830 self.vertex_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
831 self.vertex_color_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
832 self.vertex_normal_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
833
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
834 class GcodeModelLight(Model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
835 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
836 Model for displaying Gcode data.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
837 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
838
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
839 color_travel = (0.6, 0.6, 0.6, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
840 color_tool0 = (1.0, 0.0, 0.0, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
841 color_tool1 = (0.67, 0.05, 0.9, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
842 color_tool2 = (1.0, 0.8, 0., 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
843 color_tool3 = (1.0, 0., 0.62, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
844 color_tool4 = (0., 1.0, 0.58, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
845 color_printed = (0.2, 0.75, 0, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
846 color_current = (0, 0.9, 1.0, 0.8)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
847 color_current_printed = (0.1, 0.4, 0, 0.8)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
848
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
849 buffers_created = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
850 use_vbos = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
851 loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
852 fully_loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
853
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
854 gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
855
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
856 def load_data(self, model_data, callback=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
857 t_start = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
858 self.gcode = model_data
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
859
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
860 self.layer_idxs_map = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
861 self.layer_stops = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
862
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
863 prev_pos = (0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
864 layer_idx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
865 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
866 vertices = self.vertices = numpy.zeros(nlines * 6, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
867 vertex_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
868 colors = self.colors = numpy.zeros(nlines * 8, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
869 color_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
870 self.printed_until = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
871 self.only_current = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
872 while layer_idx < len(model_data.all_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
873 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
874 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
875 if nlines * 6 != vertices.size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
876 self.vertices.resize(nlines * 6, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
877 self.colors.resize(nlines * 8, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
878 layer = model_data.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
879 has_movement = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
880 for gline in layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
881 if not gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
882 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
883 if gline.x is None and gline.y is None and gline.z is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
884 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
885 has_movement = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
886 vertices[vertex_k] = prev_pos[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
887 vertices[vertex_k + 1] = prev_pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
888 vertices[vertex_k + 2] = prev_pos[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
889 current_pos = (gline.current_x, gline.current_y, gline.current_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
890 vertices[vertex_k + 3] = current_pos[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
891 vertices[vertex_k + 4] = current_pos[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
892 vertices[vertex_k + 5] = current_pos[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
893 vertex_k += 6
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
894
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
895 vertex_color = self.movement_color(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
896 colors[color_k] = vertex_color[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
897 colors[color_k + 1] = vertex_color[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
898 colors[color_k + 2] = vertex_color[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
899 colors[color_k + 3] = vertex_color[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
900 colors[color_k + 4] = vertex_color[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
901 colors[color_k + 5] = vertex_color[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
902 colors[color_k + 6] = vertex_color[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
903 colors[color_k + 7] = vertex_color[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
904 color_k += 8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
905
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
906 prev_pos = current_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
907 gline.gcview_end_vertex = vertex_k / 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
908
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
909 if has_movement:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
910 self.layer_stops.append(vertex_k / 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
911 self.layer_idxs_map[layer_idx] = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
912 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
913 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
914 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
915 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
916
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
917 if callback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
918 callback(layer_idx + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
919
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
920 yield layer_idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
921 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
922
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
923 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
924 self.dims = ((model_data.xmin, model_data.xmax, model_data.width),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
925 (model_data.ymin, model_data.ymax, model_data.depth),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
926 (model_data.zmin, model_data.zmax, model_data.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
927
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
928 self.vertices.resize(vertex_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
929 self.colors.resize(color_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
930 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
931 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
932 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
933 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
934 self.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
935
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
936 t_end = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
937
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
938 logging.debug(_('Initialized 3D visualization in %.2f seconds') % (t_end - t_start))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
939 logging.debug(_('Vertex count: %d') % (len(self.vertices) / 3))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
940 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
941
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
942 def copy(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
943 copy = GcodeModelLight()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
944 for var in ["vertices", "colors", "max_layers",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
945 "num_layers_to_draw", "printed_until",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
946 "layer_stops", "dims", "only_current",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
947 "layer_idxs_map", "gcode"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
948 setattr(copy, var, getattr(self, var))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
949 copy.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
950 copy.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
951 copy.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
952 return copy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
953
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
954 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
955 # DRAWING
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
956 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
957
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
958 def init(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
959 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
960 self.layers_loaded = self.max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
961 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
962 if self.buffers_created:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
963 self.vertex_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
964 self.vertex_color_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
965 self.vertex_buffer = numpy2vbo(self.vertices, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
966 self.vertex_color_buffer = numpy2vbo(self.colors, use_vbos = self.use_vbos) # each pair of vertices shares the color
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
967 if self.fully_loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
968 # Delete numpy arrays after creating VBOs after full load
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
969 self.vertices = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
970 self.colors = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
971 self.buffers_created = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
972
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
973 def display(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
974 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
975 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
976 glTranslatef(self.offset_x, self.offset_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
977 glEnableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
978 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
979
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
980 self._display_movements(mode_2d)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
981
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
982 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
983 glDisableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
984 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
985
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
986 def _display_movements(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
987 self.vertex_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
988 has_vbo = isinstance(self.vertex_buffer, VertexBufferObject)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
989 if has_vbo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
990 glVertexPointer(3, GL_FLOAT, 0, None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
991 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
992 glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
993
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
994 self.vertex_color_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
995 if has_vbo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
996 glColorPointer(4, GL_FLOAT, 0, None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
997 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
998 glColorPointer(4, GL_FLOAT, 0, self.vertex_color_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
999
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1000 # Prevent race condition by using the number of currently loaded layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1001 max_layers = self.layers_loaded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1002
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1003 start = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1004 if self.num_layers_to_draw <= max_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1005 end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1006 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1007 end_prev_layer = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1008 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1009
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1010 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1011
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1012 glColor4f(*self.color_printed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1013
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1014 # Draw printed stuff until end or end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1015 cur_end = min(self.printed_until, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1016 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1017 if 0 <= end_prev_layer <= cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1018 glDrawArrays(GL_LINES, start, end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1019 elif cur_end >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1020 glDrawArrays(GL_LINES, start, cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1021
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1022 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1023
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1024 # Draw nonprinted stuff until end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1025 start = max(cur_end, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1026 if end_prev_layer >= start:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1027 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1028 glDrawArrays(GL_LINES, start, end_prev_layer - start)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1029 cur_end = end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1030
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1031 # Draw current layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1032 if end_prev_layer >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1033 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1034
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1035 # Backup & increase line width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1036 orig_linewidth = (GLfloat)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1037 glGetFloatv(GL_LINE_WIDTH, orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1038 glLineWidth(2.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1039
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1040 glColor4f(*self.color_current_printed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1041
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1042 if cur_end > end_prev_layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1043 glDrawArrays(GL_LINES, end_prev_layer, cur_end - end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1044
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1045 glColor4f(*self.color_current)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1046
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1047 if end > cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1048 glDrawArrays(GL_LINES, cur_end, end - cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1049
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1050 # Restore line width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1051 glLineWidth(orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1052
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1053 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1054
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1055 # Draw non printed stuff until end (if not ending at a given layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1056 start = max(self.printed_until, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1057 end = end - start
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1058 if end_prev_layer < 0 and end > 0 and not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1059 glDrawArrays(GL_LINES, start, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1060
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1061 self.vertex_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1062 self.vertex_color_buffer.unbind()

mercurial