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

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rw-r--r--

updated and added new files for printrun

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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
68 class BoundingBox:
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
92 class Platform:
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
97 def __init__(self, build_dimensions, light = False, circular = False, grid = (1, 10)):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 self.light = light
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 self.circular = circular
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 self.width = build_dimensions[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 self.depth = build_dimensions[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 self.height = build_dimensions[2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 self.xoffset = build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 self.yoffset = build_dimensions[4]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 self.zoffset = build_dimensions[5]
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
106 self.grid = grid
15
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):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
125 if i % self.grid[1] == 0:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 glColor4f(*self.color_grads_major)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
127 elif i % (self.grid[1] // 2) == 0:
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
137 for i in numpy.arange(0, int(math.ceil(self.width + 1)), self.grid[0]):
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
144 for i in numpy.arange(0, int(math.ceil(self.depth + 1)), self.grid[0]):
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
151 for i in numpy.arange(0, int(math.ceil(self.width + 1)), self.grid[0]):
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
156 for i in numpy.arange(0, int(math.ceil(self.depth + 1)), self.grid[0]):
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
177 class PrintHead:
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
212 class Model:
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
318 def interpolate_arcs(gline, prev_gline):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
319 if gline.command == "G2" or gline.command == "G3":
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
320 rx = gline.i if gline.i is not None else 0
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
321 ry = gline.j if gline.j is not None else 0
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
322 r = math.sqrt(rx*rx + ry*ry)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
323
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
324 cx = prev_gline.current_x + rx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
325 cy = prev_gline.current_y + ry
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
326
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
327 a_start = math.atan2(-ry, -rx)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
328 dx = gline.current_x - cx
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
329 dy = gline.current_y - cy
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
330 a_end = math.atan2(dy, dx)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
331 a_delta = a_end - a_start
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
332
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
333 if gline.command == "G3" and a_delta <= 0:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
334 a_delta += math.pi * 2
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
335 elif gline.command == "G2" and a_delta >= 0:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
336 a_delta -= math.pi * 2
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
337
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
338 z0 = prev_gline.current_z
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
339 dz = gline.current_z - z0
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
340
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
341 # max segment size: 0.5mm, max num of segments: 100
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
342 segments = math.ceil(abs(a_delta) * r * 2 / 0.5)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
343 if segments > 100:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
344 segments = 100
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
345
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
346 for t in range(segments):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
347 a = t / segments * a_delta + a_start
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
348
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
349 mid = (
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
350 cx + math.cos(a) * r,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
351 cy + math.sin(a) * r,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
352 z0 + t / segments * dz
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
353 )
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
354 yield mid
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
355
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
356 yield (gline.current_x, gline.current_y, gline.current_z)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
357
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
358
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 class GcodeModel(Model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 Model for displaying Gcode data.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 color_travel = (0.6, 0.6, 0.6, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 color_tool0 = (1.0, 0.0, 0.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 color_tool1 = (0.67, 0.05, 0.9, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 color_tool2 = (1.0, 0.8, 0., 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 color_tool3 = (1.0, 0., 0.62, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 color_tool4 = (0., 1.0, 0.58, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 color_printed = (0.2, 0.75, 0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 color_current = (0, 0.9, 1.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 color_current_printed = (0.1, 0.4, 0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 display_travels = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 buffers_created = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 use_vbos = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 fully_loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 path_halfwidth = 0.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 path_halfheight = 0.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 def set_path_size(self, path_halfwidth, path_halfheight):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 self.path_halfwidth = path_halfwidth
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 self.path_halfheight = path_halfheight
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 def load_data(self, model_data, callback=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 t_start = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 self.gcode = model_data
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 self.count_travel_indices = count_travel_indices = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 self.count_print_indices = count_print_indices = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 self.count_print_vertices = count_print_vertices = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 # Some trivial computations, but that's mostly for documentation :)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 # Not like 10 multiplications are going to cost much time vs what's
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 # about to happen :)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 # Max number of values which can be generated per gline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 # to store coordinates/colors/normals.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 # Nicely enough we have 3 per kind of thing for all kinds.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 coordspervertex = 3
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
407 buffered_color_len = 3 # 4th color component (alpha) is ignored
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 verticesperline = 8
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 coordsperline = coordspervertex * verticesperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 coords_count = lambda nlines: nlines * coordsperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 travelverticesperline = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 travelcoordsperline = coordspervertex * travelverticesperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 travel_coords_count = lambda nlines: nlines * travelcoordsperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 trianglesperface = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 facesperbox = 4
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 trianglesperbox = trianglesperface * facesperbox
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 verticespertriangle = 3
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 indicesperbox = verticespertriangle * trianglesperbox
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 boxperline = 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 indicesperline = indicesperbox * boxperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 indices_count = lambda nlines: nlines * indicesperline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 ntravelcoords = travel_coords_count(nlines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 ncoords = coords_count(nlines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 nindices = indices_count(nlines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 travel_vertices = self.travels = numpy.zeros(ntravelcoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 travel_vertex_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 vertices = self.vertices = numpy.zeros(ncoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 vertex_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 colors = self.colors = numpy.zeros(ncoords, dtype = GLfloat)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
434
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 color_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 normals = self.normals = numpy.zeros(ncoords, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 indices = self.indices = numpy.zeros(nindices, dtype = GLuint)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 index_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 self.layer_idxs_map = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 self.layer_stops = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 prev_move_normal_x = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 prev_move_normal_y = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 prev_move_angle = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 prev_pos = (0, 0, 0)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
446 prev_gline = None
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 layer_idx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 self.printed_until = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 self.only_current = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 twopi = 2 * math.pi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 processed_lines = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 while layer_idx < len(model_data.all_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 remaining_lines = nlines - processed_lines
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 # Only reallocate memory which might be needed, not memory
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 # for everything
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 ntravelcoords = coords_count(remaining_lines) + travel_vertex_k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 ncoords = coords_count(remaining_lines) + vertex_k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 nindices = indices_count(remaining_lines) + index_k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 if ncoords > vertices.size:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 self.travels.resize(ntravelcoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 self.vertices.resize(ncoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468 self.colors.resize(ncoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 self.normals.resize(ncoords, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 self.indices.resize(nindices, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 layer = model_data.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 has_movement = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 for gline_idx, gline in enumerate(layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 if not gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 if gline.x is None and gline.y is None and gline.z is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478 has_movement = True
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
479 for current_pos in interpolate_arcs(gline, prev_gline):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
480 if not gline.extruding:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
481 if self.travels.size < (travel_vertex_k + 100 * 6):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
482 # arc interpolation extra points allocation
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
483 # if not enough room for another 100 points now,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
484 # allocate enough and 50% extra to minimize separate allocations
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
485 ratio = (travel_vertex_k + 100 * 6) / self.travels.size * 1.5
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
486 # print(f"gl realloc travel {self.travels.size} -> {int(self.travels.size * ratio)}")
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
487 self.travels.resize(int(self.travels.size * ratio), refcheck = False)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
489 travel_vertices[travel_vertex_k:travel_vertex_k+3] = prev_pos
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
490 travel_vertices[travel_vertex_k + 3:travel_vertex_k + 6] = current_pos
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
491 travel_vertex_k += 6
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
492 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
493 delta_x = current_pos[0] - prev_pos[0]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
494 delta_y = current_pos[1] - prev_pos[1]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
495 norm = delta_x * delta_x + delta_y * delta_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
496 if norm == 0: # Don't draw anything if this move is Z+E only
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
497 continue
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
498 norm = math.sqrt(norm)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
499 move_normal_x = - delta_y / norm
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
500 move_normal_y = delta_x / norm
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
501 move_angle = math.atan2(delta_y, delta_x)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
503 # FIXME: compute these dynamically
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
504 path_halfwidth = self.path_halfwidth * 1.2
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
505 path_halfheight = self.path_halfheight * 1.2
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
507 new_indices = []
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
508 new_vertices = []
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
509 new_normals = []
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
510 if prev_gline and prev_gline.extruding:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
511 # Store previous vertices indices
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
512 prev_id = vertex_k // 3 - 4
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
513 avg_move_normal_x = (prev_move_normal_x + move_normal_x) / 2
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
514 avg_move_normal_y = (prev_move_normal_y + move_normal_y) / 2
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
515 norm = avg_move_normal_x * avg_move_normal_x + avg_move_normal_y * avg_move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
516 if norm == 0:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
517 avg_move_normal_x = move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
518 avg_move_normal_y = move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
519 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
520 norm = math.sqrt(norm)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
521 avg_move_normal_x /= norm
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
522 avg_move_normal_y /= norm
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
523 delta_angle = move_angle - prev_move_angle
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
524 delta_angle = (delta_angle + twopi) % twopi
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
525 fact = abs(math.cos(delta_angle / 2))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
526 # If move is turning too much, avoid creating a big peak
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
527 # by adding an intermediate box
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
528 if fact < 0.5:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
529 # FIXME: It looks like there's some heavy code duplication here...
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
530 hw = path_halfwidth
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
531 p1x = prev_pos[0] - hw * prev_move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
532 p2x = prev_pos[0] + hw * prev_move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
533 p1y = prev_pos[1] - hw * prev_move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
534 p2y = prev_pos[1] + hw * prev_move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
535 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
536 new_vertices.extend((p1x, p1y, prev_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
537 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
538 new_vertices.extend((p2x, p2y, prev_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
539 new_normals.extend((0, 0, 1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
540 new_normals.extend((-prev_move_normal_x, -prev_move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
541 new_normals.extend((0, 0, -1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
542 new_normals.extend((prev_move_normal_x, prev_move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
543 first = vertex_k // 3
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
544 # Link to previous
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
545 new_indices += triangulate_box(prev_id, prev_id + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
546 prev_id + 2, prev_id + 3,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
547 first, first + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
548 first + 2, first + 3)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
549 p1x = prev_pos[0] - hw * move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
550 p2x = prev_pos[0] + hw * move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
551 p1y = prev_pos[1] - hw * move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
552 p2y = prev_pos[1] + hw * move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
553 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
554 new_vertices.extend((p1x, p1y, prev_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
555 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
556 new_vertices.extend((p2x, p2y, prev_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
557 new_normals.extend((0, 0, 1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
558 new_normals.extend((-move_normal_x, -move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
559 new_normals.extend((0, 0, -1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
560 new_normals.extend((move_normal_x, move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
561 prev_id += 4
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
562 first += 4
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
563 # Link to previous
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
564 new_indices += triangulate_box(prev_id, prev_id + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
565 prev_id + 2, prev_id + 3,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
566 first, first + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
567 first + 2, first + 3)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
568 else:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
569 hw = path_halfwidth / fact
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
570 # Compute vertices
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
571 p1x = prev_pos[0] - hw * avg_move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
572 p2x = prev_pos[0] + hw * avg_move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
573 p1y = prev_pos[1] - hw * avg_move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
574 p2y = prev_pos[1] + hw * avg_move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
575 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
576 new_vertices.extend((p1x, p1y, prev_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
577 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
578 new_vertices.extend((p2x, p2y, prev_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
579 new_normals.extend((0, 0, 1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
580 new_normals.extend((-avg_move_normal_x, -avg_move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
581 new_normals.extend((0, 0, -1))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
582 new_normals.extend((avg_move_normal_x, avg_move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
583 first = vertex_k // 3
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
584 # Link to previous
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
585 new_indices += triangulate_box(prev_id, prev_id + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
586 prev_id + 2, prev_id + 3,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
587 first, first + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
588 first + 2, first + 3)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
589 else:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
590 # Compute vertices normal to the current move and cap it
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
591 p1x = prev_pos[0] - path_halfwidth * move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
592 p2x = prev_pos[0] + path_halfwidth * move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
593 p1y = prev_pos[1] - path_halfwidth * move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
594 p2y = prev_pos[1] + path_halfwidth * move_normal_y
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
595 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] + path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
596 new_vertices.extend((p1x, p1y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
597 new_vertices.extend((prev_pos[0], prev_pos[1], prev_pos[2] - path_halfheight))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
598 new_vertices.extend((p2x, p2y, prev_pos[2]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
599 new_normals.extend((0, 0, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
600 new_normals.extend((-move_normal_x, -move_normal_y, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
601 new_normals.extend((0, 0, -1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
602 new_normals.extend((move_normal_x, move_normal_y, 0))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
603 first = vertex_k // 3
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
604 new_indices = triangulate_rectangle(first, first + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
605 first + 2, first + 3)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
606
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
607 next_move = get_next_move(model_data, layer_idx, gline_idx)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
608 next_is_extruding = next_move and next_move.extruding
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
609 if not next_is_extruding:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
610 # Compute caps and link everything
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
611 p1x = current_pos[0] - path_halfwidth * move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
612 p2x = current_pos[0] + path_halfwidth * move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
613 p1y = current_pos[1] - path_halfwidth * move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
614 p2y = current_pos[1] + path_halfwidth * move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
615 new_vertices.extend((current_pos[0], current_pos[1], current_pos[2] + path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
616 new_vertices.extend((p1x, p1y, current_pos[2]))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
617 new_vertices.extend((current_pos[0], current_pos[1], current_pos[2] - path_halfheight))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
618 new_vertices.extend((p2x, p2y, current_pos[2]))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
619 new_normals.extend((0, 0, 1))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
620 new_normals.extend((-move_normal_x, -move_normal_y, 0))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
621 new_normals.extend((0, 0, -1))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
622 new_normals.extend((move_normal_x, move_normal_y, 0))
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
623 end_first = vertex_k // 3 + len(new_vertices) // 3 - 4
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
624 new_indices += triangulate_rectangle(end_first + 3, end_first + 2,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
625 end_first + 1, end_first)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
626 new_indices += triangulate_box(first, first + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
627 first + 2, first + 3,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
628 end_first, end_first + 1,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
629 end_first + 2, end_first + 3)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
630
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
631 if self.indices.size < (index_k + len(new_indices) + 100 * indicesperline):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
632 # arc interpolation extra points allocation
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
633 ratio = (index_k + len(new_indices) + 100 * indicesperline) / self.indices.size * 1.5
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
634 # print(f"gl realloc print {self.vertices.size} -> {int(self.vertices.size * ratio)}")
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
635 self.vertices.resize(int(self.vertices.size * ratio), refcheck = False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
636 self.colors.resize(int(self.colors.size * ratio), refcheck = False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
637 self.normals.resize(int(self.normals.size * ratio), refcheck = False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
638 self.indices.resize(int(self.indices.size * ratio), refcheck = False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
639
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
640 for new_i, item in enumerate(new_indices):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
641 indices[index_k + new_i] = item
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
642 index_k += len(new_indices)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
643
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
644 new_vertices_len = len(new_vertices)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
645 vertices[vertex_k:vertex_k+new_vertices_len] = new_vertices
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
646 normals[vertex_k:vertex_k+new_vertices_len] = new_normals
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
647 vertex_k += new_vertices_len
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
648
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
649 new_vertices_count = new_vertices_len//coordspervertex
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
650 # settings support alpha (transperancy), but it is ignored here
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
651 gline_color = self.movement_color(gline)[:buffered_color_len]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
652 for vi in range(new_vertices_count):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
653 colors[color_k:color_k+buffered_color_len] = gline_color
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
654 color_k += buffered_color_len
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
655
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
656 prev_move_normal_x = move_normal_x
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
657 prev_move_normal_y = move_normal_y
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
658 prev_move_angle = move_angle
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
659
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
660 prev_pos = current_pos
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
661 prev_gline = gline
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
662 count_travel_indices.append(travel_vertex_k // 3)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
663 count_print_indices.append(index_k)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
664 count_print_vertices.append(vertex_k // 3)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
665 gline.gcview_end_vertex = len(count_print_indices) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
666
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
667 if has_movement:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
668 self.layer_stops.append(len(count_print_indices) - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
669 self.layer_idxs_map[layer_idx] = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
670 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
671 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
672 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
673 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
674
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
675 processed_lines += len(layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
676
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
677 if callback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
678 callback(layer_idx + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
679
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
680 yield layer_idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
681 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
682
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
683 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
684 self.dims = ((model_data.xmin, model_data.xmax, model_data.width),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
685 (model_data.ymin, model_data.ymax, model_data.depth),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
686 (model_data.zmin, model_data.zmax, model_data.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
687
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
688 self.travels.resize(travel_vertex_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
689 self.vertices.resize(vertex_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
690 self.colors.resize(color_k, refcheck = False)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
691 self.normals.resize(vertex_k, refcheck = False)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
692 self.indices.resize(index_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
693
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
694 self.layer_stops = array.array('L', self.layer_stops)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
695 self.count_travel_indices = array.array('L', count_travel_indices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
696 self.count_print_indices = array.array('L', count_print_indices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
697 self.count_print_vertices = array.array('L', count_print_vertices)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
698
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
699 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
700 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
701 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
702 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
703 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
704 self.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
705
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
706 t_end = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
707
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
708 logging.debug(_('Initialized 3D visualization in %.2f seconds') % (t_end - t_start))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
709 logging.debug(_('Vertex count: %d') % ((len(self.vertices) + len(self.travels)) // 3))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
710 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
711
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
712 def copy(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
713 copy = GcodeModel()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
714 for var in ["vertices", "colors", "travels", "indices", "normals",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
715 "max_layers", "num_layers_to_draw", "printed_until",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
716 "layer_stops", "dims", "only_current",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
717 "layer_idxs_map", "count_travel_indices",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
718 "count_print_indices", "count_print_vertices",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
719 "path_halfwidth", "path_halfheight",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
720 "gcode"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
721 setattr(copy, var, getattr(self, var))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
722 copy.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
723 copy.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
724 copy.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
725 return copy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
726
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
727 def update_colors(self):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
728 """Rebuild gl color buffer without loading. Used after color settings edit"""
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
729 ncoords = self.count_print_vertices[-1]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
730 colors = numpy.empty(ncoords*3, dtype = GLfloat)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
731 cur_vertex = 0
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
732 gline_i = 1
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
733 for gline in self.gcode.lines:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
734 if gline.gcview_end_vertex:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
735 gline_color = self.movement_color(gline)[:3]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
736 last_vertex = self.count_print_vertices[gline_i]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
737 gline_i += 1
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
738 while cur_vertex < last_vertex:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
739 colors[cur_vertex*3:cur_vertex*3+3] = gline_color
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
740 cur_vertex += 1
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
741 if self.vertex_color_buffer:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
742 self.vertex_color_buffer.delete()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
743 self.vertex_color_buffer = numpy2vbo(colors, use_vbos = self.use_vbos)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
744
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
745 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
746 # DRAWING
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
747 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
748
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
749 def init(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
750 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
751 self.layers_loaded = self.max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
752 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
753 if self.buffers_created:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
754 self.travel_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
755 self.index_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
756 self.vertex_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
757 self.vertex_color_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
758 self.vertex_normal_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
759 self.travel_buffer = numpy2vbo(self.travels, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
760 self.index_buffer = numpy2vbo(self.indices, use_vbos = self.use_vbos,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
761 target = GL_ELEMENT_ARRAY_BUFFER)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
762 self.vertex_buffer = numpy2vbo(self.vertices, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
763 self.vertex_color_buffer = numpy2vbo(self.colors, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
764 self.vertex_normal_buffer = numpy2vbo(self.normals, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
765 if self.fully_loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
766 # Delete numpy arrays after creating VBOs after full load
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
767 self.travels = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
768 self.indices = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
769 self.vertices = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
770 self.colors = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
771 self.normals = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
772 self.buffers_created = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
773
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
774 def display(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
775 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
776 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
777 glTranslatef(self.offset_x, self.offset_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
778 glEnableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
779
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
780 has_vbo = isinstance(self.vertex_buffer, VertexBufferObject)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
781 if self.display_travels:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
782 self._display_travels(has_vbo)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
783
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
784 glEnable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
785 glEnableClientState(GL_NORMAL_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
786 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
787 glMaterialfv(GL_FRONT, GL_SPECULAR, vec(1, 1, 1, 1))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
788 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, vec(0, 0, 0, 0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
789 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
790
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
791 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
792 self._display_movements(has_vbo)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
793
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
794 glDisable(GL_LIGHTING)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
795
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
796 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
797 glDisableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
798 glDisableClientState(GL_NORMAL_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
799
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
800 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
801
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
802 def _display_travels(self, has_vbo):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
803 self.travel_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
804 glVertexPointer(3, GL_FLOAT, 0, self.travel_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
805
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
806 # Prevent race condition by using the number of currently loaded layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
807 max_layers = self.layers_loaded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
808 # TODO: show current layer travels in a different color
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
809 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
810 end_index = self.count_travel_indices[end]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
811 glColor4f(*self.color_travel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
812 if self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
813 if self.num_layers_to_draw < max_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
814 end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
815 start_index = self.count_travel_indices[end_prev_layer + 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
816 glDrawArrays(GL_LINES, start_index, end_index - start_index + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
817 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
818 glDrawArrays(GL_LINES, 0, end_index)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
819
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
820 self.travel_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
821
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
822 def _draw_elements(self, start, end, draw_type = GL_TRIANGLES):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
823 # Don't attempt printing empty layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
824 if self.count_print_indices[end] == self.count_print_indices[start - 1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
825 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
826 glDrawRangeElements(draw_type,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
827 self.count_print_vertices[start - 1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
828 self.count_print_vertices[end] - 1,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
829 self.count_print_indices[end] - self.count_print_indices[start - 1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
830 GL_UNSIGNED_INT,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
831 sizeof(GLuint) * self.count_print_indices[start - 1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
832
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
833 def _display_movements(self, has_vbo):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
834 self.vertex_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
835 glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
836
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
837 self.vertex_color_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
838 glColorPointer(3, GL_FLOAT, 0, self.vertex_color_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
839
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
840 self.vertex_normal_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
841 glNormalPointer(GL_FLOAT, 0, self.vertex_normal_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
842
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
843 self.index_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
844
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
845 # Prevent race condition by using the number of currently loaded layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
846 max_layers = self.layers_loaded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
847
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
848 start = 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
849 layer_selected = self.num_layers_to_draw <= max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
850 if layer_selected:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
851 end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
852 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
853 end_prev_layer = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
854 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
855
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
856 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
857
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
858 glColor3f(*self.color_printed[:-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
859
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
860 # Draw printed stuff until end or end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
861 cur_end = min(self.printed_until, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
862 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
863 if 1 <= end_prev_layer <= cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
864 self._draw_elements(1, end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
865 elif cur_end >= 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
866 self._draw_elements(1, cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
867
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
868 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
869
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
870 # Draw nonprinted stuff until end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
871 start = max(cur_end, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
872 if end_prev_layer >= start:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
873 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
874 self._draw_elements(start, end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
875 cur_end = end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
876
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
877 # Draw current layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
878 if layer_selected:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
879 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
880
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
881 glColor3f(*self.color_current_printed[:-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
882
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
883 if cur_end > end_prev_layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
884 self._draw_elements(end_prev_layer + 1, cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
885
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
886 glColor3f(*self.color_current[:-1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
887
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
888 if end > cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
889 self._draw_elements(cur_end + 1, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
890
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
891 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
892
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
893 # Draw non printed stuff until end (if not ending at a given layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
894 start = max(self.printed_until, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
895 if not layer_selected and end >= start:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
896 self._draw_elements(start, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
897
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
898 self.index_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
899 self.vertex_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
900 self.vertex_color_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
901 self.vertex_normal_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
902
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
903 class GcodeModelLight(Model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
904 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
905 Model for displaying Gcode data.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
906 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
907
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
908 color_travel = (0.6, 0.6, 0.6, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
909 color_tool0 = (1.0, 0.0, 0.0, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
910 color_tool1 = (0.67, 0.05, 0.9, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
911 color_tool2 = (1.0, 0.8, 0., 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
912 color_tool3 = (1.0, 0., 0.62, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
913 color_tool4 = (0., 1.0, 0.58, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
914 color_printed = (0.2, 0.75, 0, 0.6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
915 color_current = (0, 0.9, 1.0, 0.8)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
916 color_current_printed = (0.1, 0.4, 0, 0.8)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
917
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
918 buffers_created = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
919 use_vbos = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
920 loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
921 fully_loaded = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
922
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
923 gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
924
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
925 def load_data(self, model_data, callback=None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
926 t_start = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
927 self.gcode = model_data
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
928
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
929 self.layer_idxs_map = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
930 self.layer_stops = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
931
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
932 prev_pos = (0, 0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
933 layer_idx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
934 nlines = len(model_data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
935 vertices = self.vertices = numpy.zeros(nlines * 6, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
936 vertex_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
937 colors = self.colors = numpy.zeros(nlines * 8, dtype = GLfloat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
938 color_k = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
939 self.printed_until = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
940 self.only_current = False
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
941 prev_gline = None
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
942 while layer_idx < len(model_data.all_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
943 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
944 nlines = len(model_data)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
945 if nlines * 6 > vertices.size:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
946 self.vertices.resize(nlines * 6, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
947 self.colors.resize(nlines * 8, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
948 layer = model_data.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
949 has_movement = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
950 for gline in layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
951 if not gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
952 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
953 if gline.x is None and gline.y is None and gline.z is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
954 continue
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
955
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
956 has_movement = True
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
957 for current_pos in interpolate_arcs(gline, prev_gline):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
958
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
959 if self.vertices.size < (vertex_k + 100 * 6):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
960 # arc interpolation extra points allocation
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
961 ratio = (vertex_k + 100 * 6) / self.vertices.size * 1.5
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
962 # print(f"gl realloc lite {self.vertices.size} -> {int(self.vertices.size * ratio)}")
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
963 self.vertices.resize(int(self.vertices.size * ratio), refcheck = False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
964 self.colors.resize(int(self.colors.size * ratio), refcheck = False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
965
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
966
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
967 vertices[vertex_k] = prev_pos[0]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
968 vertices[vertex_k + 1] = prev_pos[1]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
969 vertices[vertex_k + 2] = prev_pos[2]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
970 vertices[vertex_k + 3] = current_pos[0]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
971 vertices[vertex_k + 4] = current_pos[1]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
972 vertices[vertex_k + 5] = current_pos[2]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
973 vertex_k += 6
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
974
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
975 vertex_color = self.movement_color(gline)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
976 colors[color_k] = vertex_color[0]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
977 colors[color_k + 1] = vertex_color[1]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
978 colors[color_k + 2] = vertex_color[2]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
979 colors[color_k + 3] = vertex_color[3]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
980 colors[color_k + 4] = vertex_color[0]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
981 colors[color_k + 5] = vertex_color[1]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
982 colors[color_k + 6] = vertex_color[2]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
983 colors[color_k + 7] = vertex_color[3]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
984 color_k += 8
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
985
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
986 prev_pos = current_pos
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
987 prev_gline = gline
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
988 gline.gcview_end_vertex = vertex_k // 3
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
989
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
990 if has_movement:
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
991 self.layer_stops.append(vertex_k // 3)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
992 self.layer_idxs_map[layer_idx] = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
993 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
994 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
995 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
996 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
997
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
998 if callback:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
999 callback(layer_idx + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1000
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1001 yield layer_idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1002 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1003
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1004 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1005 self.dims = ((model_data.xmin, model_data.xmax, model_data.width),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1006 (model_data.ymin, model_data.ymax, model_data.depth),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1007 (model_data.zmin, model_data.zmax, model_data.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1008
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1009 self.vertices.resize(vertex_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1010 self.colors.resize(color_k, refcheck = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1011 self.max_layers = len(self.layer_stops) - 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1012 self.num_layers_to_draw = self.max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1013 self.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1014 self.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1015 self.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1016
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1017 t_end = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1018
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1019 logging.debug(_('Initialized 3D visualization in %.2f seconds') % (t_end - t_start))
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
1020 logging.debug(_('Vertex count: %d') % (len(self.vertices) // 3))
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1021 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1022
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1023 def copy(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1024 copy = GcodeModelLight()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1025 for var in ["vertices", "colors", "max_layers",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1026 "num_layers_to_draw", "printed_until",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1027 "layer_stops", "dims", "only_current",
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1028 "layer_idxs_map", "gcode"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1029 setattr(copy, var, getattr(self, var))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1030 copy.loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1031 copy.fully_loaded = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1032 copy.initialized = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1033 return copy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1034
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1035 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1036 # DRAWING
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1037 # ------------------------------------------------------------------------
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1038
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1039 def init(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1040 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1041 self.layers_loaded = self.max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1042 self.initialized = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1043 if self.buffers_created:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1044 self.vertex_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1045 self.vertex_color_buffer.delete()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1046 self.vertex_buffer = numpy2vbo(self.vertices, use_vbos = self.use_vbos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1047 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
1048 if self.fully_loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1049 # Delete numpy arrays after creating VBOs after full load
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1050 self.vertices = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1051 self.colors = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1052 self.buffers_created = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1053
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1054 def display(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1055 with self.lock:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1056 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1057 glTranslatef(self.offset_x, self.offset_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1058 glEnableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1059 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1060
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1061 self._display_movements(mode_2d)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1062
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1063 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1064 glDisableClientState(GL_VERTEX_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1065 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1066
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1067 def _display_movements(self, mode_2d=False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1068 self.vertex_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1069 has_vbo = isinstance(self.vertex_buffer, VertexBufferObject)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1070 if has_vbo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1071 glVertexPointer(3, GL_FLOAT, 0, None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1072 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1073 glVertexPointer(3, GL_FLOAT, 0, self.vertex_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1074
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1075 self.vertex_color_buffer.bind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1076 if has_vbo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1077 glColorPointer(4, GL_FLOAT, 0, None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1078 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1079 glColorPointer(4, GL_FLOAT, 0, self.vertex_color_buffer.ptr)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1080
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1081 # Prevent race condition by using the number of currently loaded layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1082 max_layers = self.layers_loaded
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1083
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1084 start = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1085 if self.num_layers_to_draw <= max_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1086 end_prev_layer = self.layer_stops[self.num_layers_to_draw - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1087 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1088 end_prev_layer = -1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1089 end = self.layer_stops[min(self.num_layers_to_draw, max_layers)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1090
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1091 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1092
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1093 glColor4f(*self.color_printed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1094
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1095 # Draw printed stuff until end or end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1096 cur_end = min(self.printed_until, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1097 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1098 if 0 <= end_prev_layer <= cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1099 glDrawArrays(GL_LINES, start, end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1100 elif cur_end >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1101 glDrawArrays(GL_LINES, start, cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1102
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1103 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1104
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1105 # Draw nonprinted stuff until end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1106 start = max(cur_end, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1107 if end_prev_layer >= start:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1108 if not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1109 glDrawArrays(GL_LINES, start, end_prev_layer - start)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1110 cur_end = end_prev_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1111
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1112 # Draw current layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1113 if end_prev_layer >= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1114 glDisableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1115
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1116 # Backup & increase line width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1117 orig_linewidth = (GLfloat)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1118 glGetFloatv(GL_LINE_WIDTH, orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1119 glLineWidth(2.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1120
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1121 glColor4f(*self.color_current_printed)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1122
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1123 if cur_end > end_prev_layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1124 glDrawArrays(GL_LINES, end_prev_layer, cur_end - end_prev_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1125
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1126 glColor4f(*self.color_current)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1127
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1128 if end > cur_end:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1129 glDrawArrays(GL_LINES, cur_end, end - cur_end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1130
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1131 # Restore line width
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1132 glLineWidth(orig_linewidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1133
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1134 glEnableClientState(GL_COLOR_ARRAY)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1135
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1136 # Draw non printed stuff until end (if not ending at a given layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1137 start = max(self.printed_until, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1138 end = end - start
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1139 if end_prev_layer < 0 and end > 0 and not self.only_current:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1140 glDrawArrays(GL_LINES, start, end)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1141
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1142 self.vertex_buffer.unbind()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1143 self.vertex_color_buffer.unbind()

mercurial