-
Notifications
You must be signed in to change notification settings - Fork 5
/
canvas3d.py
314 lines (251 loc) · 8.17 KB
/
canvas3d.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#
# Copyright 2011, 2012, 2013, 2014 Simon Forman
#
# This file is part of Tkinter3D.
#
# Tkinter3D is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Tkinter3D is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tkinter3D. If not, see <http://www.gnu.org/licenses/>.
#
'''
A Docstring for canvas3d module.
'''
from tkinter import Tk, Canvas
from math3d import (
Vector,
planeNormalAndDistance,
rotx,
roty,
rotz,
radians,
)
from math import tan, pi
from scene import Frame3D
def G(L, a):
return L / tan(a / radians) #the arc of the viewport
class Canvas3D(Canvas):
'''
A Tkinter Canvas adapted to serve as a 3D viewer.
'''
def __init__(self, master=None, fps=20, *args, **kw):
Canvas.__init__(self, master, *args, **kw)
self.frame = Frame3D()
self.items = {}
self.w = float(self['width'])
self.h = float(self['height'])
self.axes_on = True
self._xy_axes()
self.bind('<Configure>', self.resize_callback)
self.fps_ms = 1000 / fps # Frames per second in milliseconds.
self.focal_distance = 2.2
self.x_arc_of_view = 73.0
self.y_arc_of_view = 73.0
self.f = G(self.w, self.x_arc_of_view)
self.u = G(self.h, self.y_arc_of_view)
self._frustum = Frustum(
self.w,
self.h,
self.x_arc_of_view,
self.y_arc_of_view,
self.focal_distance,
)
def visible(self, v):
'''
Return a Boolean indicating if the given Vector is within the
view.
:param v: A Vector representing a 3D point.
:type v: :class:`math3d.Vector`
:rtype: Boolean
'''
return self._frustum.visible(v)
def vector_to_screen(self, v):
'''
Return X and Y coordinates of Vector v projected onto the screen
space.
:param v: A Vector representing a 3D point.
:type v: :class:`math3d.Vector`
:returns: X, Y coordinates on Canvas.
:rtype: two-tuple of :obj:`int`
'''
z = v.z
FD = self.focal_distance
w = self.w / 2
f = self.f
h = self.h / 2
u = self.u
return (
int(w + v.x * f / (z + f * FD)),
int(h - v.y * u / (z + u * FD))
)
def _xy_axes(self):
if not self.axes_on:
return
w, h = int(self.w), int(self.h)
yaxis = self.items.get('yaxis', None)
if yaxis:
self.coords(yaxis, w / 2, 0, w / 2, h)
else:
self.items['yaxis'] = self.create_line(w / 2, 0, w / 2, h)
xaxis = self.items.get('xaxis', None)
if xaxis:
self.coords(xaxis, w, h / 2, 0, h / 2)
else:
self.items['xaxis'] = self.create_line(w, h / 2, 0, h / 2)
def resize_callback(self, event):
self.w = float(event.width)
self.h = float(event.height)
self._xy_axes()
def start_updating(self):
self._updater()
def _updater(self):
self.do_frame()
self.update_idletasks()
self.update()
self.after(int(self.fps_ms), self._updater)
def do_frame(self):
for thing, vector in self.frame.yieldTransformed():
if self._frustum.visible(vector):
x, y = self.vector_to_screen(vector)
thing.render(x, y, 1.0)
else:
thing.hide()
class Frustum:
def __init__(
self,
width=320,
height=240,
x_arc=73.0,
y_arc=73.0,
focal_distance=2.2,
depth=5000.0,
):
self.w, self.h = width, height
self.xarc, self.yarc = x_arc, y_arc
self.FD = focal_distance
self.depth = depth
self.T = Vector()
self.RM = rotx(0) * roty(0) * rotz(0)
# Create a list of eight "blank" new Vectors. These will be the
# corners of the frustum, used to determine the planes beyond
# which a given point is outside the viewable space.
self._frustum = [Vector() for _ in range(8)]
self._reset()
self._getFrustumPlanes()
def _reset(self):
self.f = G(self.w, self.xarc)
self.u = G(self.h, self.yarc)
self._frustum_dirty = True
self._frustum_planes_dirty = True
def transform(self, v):
return (v - self.T) * self.RM
def visible(self, v):
"""
Is vector v in World coordinates within the view frustum?
"""
(lpN, ld), (rpN, rd), (tpN, td), (bpN, bd) = self._getFrustumPlanes()
vx, vy, vz = v.x, v.y, v.z
accs = acl, acr, act, acb = -ld, -rd, -td, -bd
if vx != 0.0:
acl += vx * lpN.x
acr += vx * rpN.x
act += vx * tpN.x
acb += vx * bpN.x
if vy != 0.0:
acl += vy * lpN.y
acr += vy * rpN.y
act += vy * tpN.y
acb += vy * bpN.y
if vz != 0.0:
if (
(acl + vz * lpN.z <= 0.0)
or (acr + vz * rpN.z <= 0.0)
or (act + vz * tpN.z <= 0.0)
):
return False
return acb + vz * bpN.z > 0.0
if acl <= 0.0 or acr <= 0.0 or act <= 0.0:
return False
return acb > 0.0
def _getFrustum(self):
"""
This returns the coordinates of the view frustum in self/model
coordinates.
"""
if self._frustum_dirty:
FD, w, h, f, u = self.FD, self.w, self.h, self.f, self.u
v0, v1, v2, v3, v4, v5, v6, v7 = self._frustum
z = min(f, u)
xw = w / 2.0 * (z + f * FD) / f
x0 = -xw
yh = h / 2.0 * (z + u * FD) / u
y0 = -yh
v0.x, v0.y, v0.z = x0, y0, z
v1.x, v1.y, v1.z = xw, yh, z
v2.x, v2.y, v2.z = xw, y0, z
v3.x, v3.y, v3.z = x0, yh, z
z = self.depth
xw = w / 2.0 * (z + f * FD) / f
x0 = -xw
yh = h / 2.0 * (z + u * FD) / u
y0 = -yh
v4.x, v4.y, v4.z = x0, y0, z
v5.x, v5.y, v5.z = xw, yh, z
v6.x, v6.y, v6.z = xw, y0, z
v7.x, v7.y, v7.z = x0, yh, z
self._frustum_dirty = False
return self._frustum
def _getFrustumPlanes(self):
if self._frustum_planes_dirty:
V = list(map(self.transform, self._getFrustum()))
self._frustum_planes = tuple(
planeNormalAndDistance(V[i0], V[i1], V[i2])
for i0, i1, i2 in (
(0, 4, 3),
(6, 2, 5),
(3, 7, 1),
(2, 6, 0),
)
)
self._frustum_planes_dirty = False
return self._frustum_planes
if __name__ == '__main__':
from scene import Thing3D as dot
root = Tk()
root.title("Rotating Cube Demo")
c = Canvas3D(root)
c.pack(expand=1, fill='both')
## Changing the frustum's "frame".
# c._frustum.T.z += 100.0
# c._frustum._reset()
# Zoom out a little.
c.frame.T.z += -200.0
# Create a dot at world origin (0, 0, 0).
origin = dot(c)
c.frame.things.append(origin)
# Make a cube.
cube_frame = Frame3D()
c.frame.subframes.append(cube_frame)
coords = -1, 1
for x in coords:
for y in coords:
for z in coords:
d = dot(c, 100.0 * x, 100.0 * y, 100.0 * z)
cube_frame.things.append(d)
# Apply a rotation repeatedly to our cube.
N = roty(360/30/3) # 4 degrees.
def delta():
cube_frame.RM *= N
c.after(60, delta)
# Start everything running.
delta()
c.start_updating()
root.mainloop()