-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd4.py
More file actions
373 lines (278 loc) · 9.93 KB
/
d4.py
File metadata and controls
373 lines (278 loc) · 9.93 KB
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 12 19:19:26 2015
@author: Johannes Hartung
"""
import numpy as np
import math
import pygame
import sys
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
# the n-d routines are implemented based on the stuff published at https://ef.gy/ (https://ef.gy/linear-algebra:perspective-projections)
# and explained by Magnus Deininger in his ccc talk at 31c3
# first step show point clouds
def calc_basis(dim, n):
res = np.zeros(dim)
res[n] = 1.0
return res
def calc_normals(dim, *args):
res = np.zeros(dim)
if len(args) == dim - 1:
# calculate normal of dim - 1 vectors
tupleofargs = tuple(v.reshape(dim, 1) for v in args) # for later concatenation
coefficientmatrix = np.concatenate(tupleofargs, axis=1)
mone = 1.0
for i in range(dim):
res += mone*np.linalg.det(np.delete(coefficientmatrix, (i), axis=0))*calc_basis(dim, i)
mone *= -1.0
return res
def calc_lookat(dim, to, frm, *args):
res = np.zeros((dim, dim))
if len(args) == dim - 2:
matrixcolumns = []
matrixcolumns.append(to-frm)
for i in range(dim-1):
necessarybasisvectors = [calc_basis(dim, j) for j in range(i, dim-2)]
#print("basis: ", necessarybasisvectors)
#print(matrixcolumns)
newcol = calc_normals(dim, *(necessarybasisvectors + matrixcolumns))
#print(newcol)
matrixcolumns = [newcol] + matrixcolumns
#print(matrixcolumns)
matrixcolumns = tuple(m.reshape(dim, 1) for m in matrixcolumns)
res = np.concatenate(matrixcolumns, axis=1)
#print(res)
return res
def calc_perspective(dim, eyeangle):
matdiag = np.concatenate((1.0/math.tan(eyeangle/2.)*np.ones(dim-1), np.array([1,1])))
return np.diag(matdiag)
def gen_homogen_matrix(dim, matrix):
temp = np.concatenate((matrix, np.zeros((dim, 1))), axis=1)
vec = calc_basis(dim+1, dim).reshape((1, dim+1))
res = np.concatenate((temp, vec))
return res
def gen_translation_matrix(dim, trans):
res = np.diag(np.ones(dim+1))
res[:dim,dim] = trans
return res
def gen_viewmatrix_nd(dim, to, frm, eyeangle, *args):
res = np.dot(gen_translation_matrix(dim, -frm), np.dot(gen_homogen_matrix(dim, calc_lookat(dim, to, frm, *args)),calc_perspective(dim, eyeangle)))
return res
def gen_viewmatrix_proj(dim, to, frm, *args):
res = np.dot(gen_translation_matrix(dim, -frm), gen_homogen_matrix(dim, calc_lookat(dim, to, frm, *args)))
return res
def gen_rotation_matrix(dim, angle, u, v):
mat = np.diag(np.ones(dim))
mat += math.sin(angle)*(np.kron(v, u) - np.kron(u, v)).reshape((dim, dim))
mat += (math.cos(angle) - 1.0)*(np.kron(u, u) + np.kron(v, v)).reshape((dim, dim))
return gen_homogen_matrix(dim, mat)
class Camera(object):
"""
Camera class. Has one center of mass which can be translated.
And has different rotation angles which are put into a matrix.
All together with some mirror matrix this constitutes the
camera transform. Some projection projects the points in 4D
into screen coordinates by removing the 4th component and
projecting onto screen coordinates.
"""
pass
class PointCloud(object):
"""
Class for managing nD point transformations.
Geometric calculations are also part of this class.
Projection into screen coordinates should not be missing.
Calculations are done via (n+1)D homogenious coordinates.
All changes like translation and rotation are incremental.
"""
def __init__(self):
self.edges = []
def setPoints(self, points):
self.points = points #np.vstack((points.T, np.ones(len(points)).T)).T
def addEdge(self, edges = (0,1)):
if type(edges) == tuple and len(edges) == 2:
self.edges.append(edges)
elif type(edges) == list:
self.edges.extend(edges)
def getEdges(self):
return self.edges
def getEdgeProperties(self, edge):
"""
Returns direction vector, startpoint
and endpoint of an edge.
:param edge (tuple)
:returns (direction, startpoint, endpoint)
"""
startpoint = self.points[edge[0]]
endpoint = self.points[edge[1]]
return (endpoint - startpoint, startpoint, endpoint)
def getEdgeIntersectionAfterProj(self, dim, edge, proj):
eps = 1e-10
projstart = np.dot(self.points[edge[0]], proj)
projend = np.dot(self.points[edge[1]], proj)
print(projstart)
print(projend)
diffproj = projend[dim-1] - projstart[dim-1]
a = projstart[dim-1]
if abs(diffproj) < eps:
if abs(a) < eps:
t = 0.0 # t arbitrary and in particular t=0.0 (eigentlich muesste man a und b zurueckgeben)
else:
t = None # no solution
else:
t = - a/diffproj
if t < 0 or t > 1:
t = None # outside of edge
return t
class Tet(PointCloud):
def __init__(self, dim, points):
super(Tet, self).__init__()
shp = points.shape
print(shp)
numpoints = shp[0]
dimpoints = shp[1]
if numpoints != dim+1 or dimpoints != dim:
pts = np.zeros((dim+1, dim+1))
for n in range(dim+1):
pts[n][dim] = 1.0
pts[n][n] = 1.0
else:
pts = points
print(points)
print(np.ones((numpoints,1)))
pts = np.concatenate((pts, np.ones((numpoints,1))), axis=1)
for n in range(dim+1):
for m in range(n):
self.addEdge((m, n))
self.setPoints(pts)
class PygameApp(object):
def __init__(self):
pygame.init()
self.display = (800,600)
self.screen = pygame.display.set_mode(self.display, DOUBLEBUF|OPENGL)
gluPerspective(90, (self.display[0]/self.display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0, -20.0) # -5
glEnable( GL_DEPTH_TEST )
#glEnable( GL_CULL_FACE )
#// enable color tracking
glEnable(GL_COLOR_MATERIAL)
#// set material properties which will be assigned by glColor
glColorMaterial(GL_FRONT, GL_AMBIENT)
glColorMaterial(GL_FRONT, GL_DIFFUSE)
glColorMaterial(GL_FRONT, GL_SPECULAR)
#// Create light components
self.ambientLight = ( 1.0, 1.0, 1.0, 1.0 )
self.diffuseLight = ( 0.8, 0.8, 0.8, 1.0 )
self.specularLight = ( 0.5, 0.5, 0.5, 1.0 )
#// Assign created components to GL_LIGHT0
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
def drawText(self, position, fcolor, bcolor, textString):
font = pygame.font.Font (None, 64)
textSurface = font.render(textString, True, fcolor, bcolor)
textData = pygame.image.tostring(textSurface, "RGBA", True)
glWindowPos3f(*position)
glDrawPixels(textSurface.get_width(), textSurface.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, textData)
def run(self):
counter = 0
while True:
counter += 1
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN: # one time key events
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_k:
pass
if event.key == pygame.K_l:
pass
if event.key == pygame.K_o:
pass
if event.key == pygame.K_p:
pass
keys_pressed = pygame.key.get_pressed()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glLightfv(GL_LIGHT0, GL_AMBIENT, self.ambientLight)
glLightfv(GL_LIGHT0, GL_DIFFUSE, self.diffuseLight)
glLightfv(GL_LIGHT0, GL_SPECULAR, self.specularLight)
glLightfv(GL_LIGHT0, GL_POSITION, (0., 0. -100.))
self.drawText((400, 200, -100), (255, 0, 0, 255), (0, 0, 0, 0), str(counter))
self.drawText((350, 180, 0), (0, 255, 0, 255), (0, 0, 0, 0), str(counter))
pygame.display.flip()
pygame.time.wait(10)
def close(self):
pass
def main():
pts = np.array((
(1, -1, -1, -1),
(1, 1, -1, -1),
(-1, 1, -1, -1),
(-1, -1, -1, -1),
(1, -1, 1, -1),
(1, 1, 1, -1),
(-1, -1, 1, -1),
(-1, 1, 1, -1),
(1, -1, -1, 1),
(1, 1, -1, 1),
(-1, 1, -1, 1),
(-1, -1, -1, 1),
(1, -1, 1, 1),
(1, 1, 1, 1),
(-1, -1, 1, 1),
(-1, 1, 1, 1)
))
p = PointCloud()
p.setPoints(pts)
p.addEdge(edges =
[
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
(8,9), # 01 OK
(8,11), # 03 OK
(8,12), # 04 OK
(10,9), # 21 OK
(10,11), # 23 OK
(10,15), # 27 OK
(14,11), # 63 OK
(14,12), # 64 OK
(14,15), # 67 OK
(13,9), # 51 OK
(13,12), # 54 OK
(13,15), # 57 OK
(0, 8),
(1, 9),
(2, 10),
(3, 11),
(4, 12),
(5, 13),
(6, 14),
(7, 15)
]
)
pm = gen_viewmatrix_proj(4, np.array([0,0,-10,0]), np.array([1/2,1/2,1/2,1/2]), np.array([1, 0, 0, 0]), np.array([0, 1, 0, 0]))
t = Tet(4, np.array([[0,0,0,0], [1,0,0,0], [0,1,0,0], [0,0,1,0], [0,0,0,1]]))
print(t.points)
print(t.getEdges())
print(t.getEdgeProperties(t.edges[9]))
for a in t.edges:
print(t.getEdgeIntersectionAfterProj(4, a, pm))
print("blub")
pg = PygameApp()
pg.run()
pg.close()
if __name__ == "__main__":
main()