-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdimentions.py
More file actions
259 lines (226 loc) · 7.71 KB
/
dimentions.py
File metadata and controls
259 lines (226 loc) · 7.71 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
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
import itertools
import colorsys
# Settings
WIDTH, HEIGHT = 950, 620
max_dims = 12
dim = 3
angle = 0
rotation_speed = 0.01
slider_x, slider_y = 50, HEIGHT - 50
slider_w, slider_h = WIDTH - 100, 12
slider_handle_w = 16
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT), DOUBLEBUF | OPENGL)
pygame.display.set_caption("N-Dimensional Hypercube Visualizer")
font = pygame.font.SysFont("consolas", 20)
# Enable blending for UI overlays
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
# Global VBOs and related data
vbo_vertices = None
vbo_colors = None
vbo_points = None
total_lines = 0
num_points = 0
current_dim = -1
def get_hypercube_points(n):
return np.array(list(itertools.product([-1, 1], repeat=n)), dtype=np.float32)
def dynamic_rotation_matrix(n, time):
mat = np.eye(n, dtype=np.float32)
idx = 0
for i in range(n):
for j in range(i + 1, n):
speed = 0.4 + (idx % 7) * 0.2
phase = idx * 0.5
a = time * speed + np.sin(time * 0.3 + phase) * 0.5
c, s = np.cos(a), np.sin(a)
rot = np.eye(n, dtype=np.float32)
rot[i, i] = c
rot[j, j] = c
rot[i, j] = -s
rot[j, i] = s
mat = mat @ rot
idx += 1
return mat
def wobble_matrix(n, time):
mat = np.eye(n, dtype=np.float32)
for i in range(n):
scale = 1.0 + 0.25 * np.sin(time * (0.8 + i * 0.37))
mat[i, i] = scale
return mat
def project(points, n):
if n >= 3:
return points[:, :3]
elif n == 2:
return np.hstack((points, np.zeros((len(points), 1), dtype=np.float32)))
else:
pad = np.zeros((len(points), 3), dtype=np.float32)
pad[:, :n] = points
return pad
def draw_text(text, x, y):
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glDisable(GL_DEPTH_TEST)
surf = font.render(text, True, (255, 255, 255))
text_data = pygame.image.tostring(surf, "RGBA", True)
glWindowPos2d(x, y)
glDrawPixels(surf.get_width(), surf.get_height(), GL_RGBA, GL_UNSIGNED_BYTE, text_data)
glEnable(GL_DEPTH_TEST)
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
def draw_slider(current_dim):
glMatrixMode(GL_PROJECTION)
glPushMatrix(); glLoadIdentity()
glOrtho(0, WIDTH, HEIGHT, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix(); glLoadIdentity()
glDisable(GL_DEPTH_TEST)
glColor4f(0.2, 0.2, 0.2, 0.7)
glBegin(GL_QUADS)
glVertex2f(slider_x, slider_y)
glVertex2f(slider_x + slider_w, slider_y)
glVertex2f(slider_x + slider_w, slider_y + slider_h)
glVertex2f(slider_x, slider_y + slider_h)
glEnd()
ratio = (current_dim - 1) / (max_dims - 1)
hx = slider_x + ratio * (slider_w - slider_handle_w)
glColor4f(0.4, 0.8, 1.0, 0.9)
glBegin(GL_QUADS)
glVertex2f(hx, slider_y - 4)
glVertex2f(hx + slider_handle_w, slider_y - 4)
glVertex2f(hx + slider_handle_w, slider_y + slider_h + 4)
glVertex2f(hx, slider_y + slider_h + 4)
glEnd()
glEnable(GL_DEPTH_TEST)
glPopMatrix()
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
def setup_buffers(dim):
global vbo_colors, total_lines, num_points
num_points = 2 ** dim
total_lines = num_points * (num_points - 1) // 2
colors = np.zeros((2 * total_lines, 3), dtype=np.float32)
count = 0
for i in range(num_points):
for j in range(i + 1, num_points):
h = count / total_lines
r, g, b = colorsys.hsv_to_rgb(h, 1.0, 1.0)
colors[2 * count] = [r, g, b]
colors[2 * count + 1] = [r, g, b] # Same color for both vertices
count += 1
if vbo_colors is None:
vbo_colors = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo_colors)
glBufferData(GL_ARRAY_BUFFER, colors.nbytes, colors, GL_STATIC_DRAW)
def update_vertex_buffer(projected):
global vbo_vertices, total_lines
vertex_array = np.zeros((2 * total_lines, 3), dtype=np.float32)
count = 0
for i in range(len(projected)):
for j in range(i + 1, len(projected)):
vertex_array[2 * count] = projected[i]
vertex_array[2 * count + 1] = projected[j]
count += 1
if vbo_vertices is None:
vbo_vertices = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices)
glBufferData(GL_ARRAY_BUFFER, vertex_array.nbytes, vertex_array, GL_DYNAMIC_DRAW)
def setup_points_vbo(projected):
global vbo_points
if vbo_points is None:
vbo_points = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, vbo_points)
glBufferData(GL_ARRAY_BUFFER, projected.nbytes, projected, GL_DYNAMIC_DRAW)
def gl_draw_lines_vbo():
glEnableClientState(GL_VERTEX_ARRAY)
glEnableClientState(GL_COLOR_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices)
glVertexPointer(3, GL_FLOAT, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, vbo_colors)
glColorPointer(3, GL_FLOAT, 0, None)
glDrawArrays(GL_LINES, 0, 2 * total_lines)
glDisableClientState(GL_COLOR_ARRAY)
glDisableClientState(GL_VERTEX_ARRAY)
def gl_draw_points_vbo():
glPointSize(6)
glEnableClientState(GL_VERTEX_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, vbo_points)
glVertexPointer(3, GL_FLOAT, 0, None)
glColor3f(1.0, 0.5, 0.5)
glDrawArrays(GL_POINTS, 0, num_points)
glDisableClientState(GL_VERTEX_ARRAY)
def main():
global dim, angle, current_dim
clock = pygame.time.Clock()
dragging = False
running = True
glEnable(GL_POINT_SMOOTH)
glClearColor(0.05, 0.05, 0.1, 1)
gluPerspective(40, WIDTH/HEIGHT, 0.1, 100.0)
glTranslatef(0, 0, -10)
setup_buffers(dim) # Initial setup
current_dim = dim
while running:
for e in pygame.event.get():
if e.type == QUIT:
running = False
elif e.type == MOUSEBUTTONDOWN:
mx, my = e.pos
if slider_y - 10 <= my <= slider_y + slider_h + 10:
dragging = True
elif e.type == MOUSEBUTTONUP:
dragging = False
elif e.type == KEYDOWN:
if e.key == K_UP:
dim = min(dim + 1, max_dims)
elif e.key == K_DOWN:
dim = max(dim - 1, 1)
if dragging:
mx = pygame.mouse.get_pos()[0]
ratio = (mx - slider_x) / (slider_w - slider_handle_w)
dim = int(np.clip(round(ratio * (max_dims-1) + 1), 1, max_dims))
if dim != current_dim:
setup_buffers(dim)
current_dim = dim
pts_nd = get_hypercube_points(dim)
rot = dynamic_rotation_matrix(dim, angle)
wobble = wobble_matrix(dim, angle)
transformed = pts_nd @ rot @ wobble
projected = project(transformed, dim)
update_vertex_buffer(projected)
setup_points_vbo(projected)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glPushMatrix()
glRotatef(angle * 20, 1, 1, 0)
gl_draw_lines_vbo()
gl_draw_points_vbo()
glPopMatrix()
draw_slider(dim)
draw_text(f"{dim}D ({2**dim} verts)", 10, 10)
draw_text(f"FPS: {int(clock.get_fps())}", WIDTH - 100, 10)
pygame.display.flip()
clock.tick(60)
angle += rotation_speed
# Cleanup
if vbo_vertices:
glDeleteBuffers(1, [vbo_vertices])
if vbo_colors:
glDeleteBuffers(1, [vbo_colors])
if vbo_points:
glDeleteBuffers(1, [vbo_points])
pygame.quit()
if __name__ == "__main__":
main()