def rotate4D(verts4D):
cos = np.cos(angle)
sin = np.sin(angle)
rotation4d_xy = np.array(
[[cos, -sin, 0, 0], [sin, cos, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
)
rotation4d_zw = np.array(
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, cos, -sin], [0, 0, sin, cos]]
)
distance = 2
projected_marix = np.zeros((16, 3))
for i, vert in enumerate(verts4D):
rotated_3D = np.dot(rotation4d_xy, vert)
rotated_3D = np.dot(rotation4d_zw, rotated_3D)
w = 1 / (distance - rotated_3D[3])
proj_mat4D = np.array([[w, 0, 0, 0], [0, w, 0, 0], [0, 0, w, 0]])
projeced_mat3D = np.dot(proj_mat4D, rotated_3D)
projected_marix[i] = projeced_mat3D # vertices to be proj (3D)
return projected_marix
def connect_points(verts3D):
lines = np.array([])
len_vert = len(verts3D)
for i in range(len_vert - 1):
if i < 8:
lines = np.append(lines, [verts3D[i], verts3D[i + 8]])
if i == 7:
pass
else:
lines = np.append(lines, [verts3D[i], verts3D[i + 1]])
if i % 4 == 0:
lines = np.append(lines, [verts3D[i], verts3D[i + 3]])
for i in range(3):
lines = np.append(lines, [verts3D[i], verts3D[i + 5]])
lines = np.append(lines, [verts3D[i + 8], verts3D[i + 5 + 8]])
return np.reshape(lines, (-1, 2, 3))
scene = fury.window.Scene()
scene.set_camera(
position=(0, 10, -1), focal_point=(0.0, 0.0, 0.0), view_up=(0.0, 0.0, 0.0)
)
showm = fury.window.ShowManager(scene=scene, size=(1920, 1080), order_transparent=True)
verts3D = rotate4D(verts4D)
if not wireframe:
points = fury.actor.point(verts3D, colors=p_color)
point_verts = fury.utils.vertices_from_actor(points)
no_vertices = len(point_verts) / 16
initial_verts = point_verts.copy() - np.repeat(verts3D, no_vertices, axis=0)
scene.add(points)
lines = connect_points(verts3D)
edges = fury.actor.line(
lines=lines, colors=e_color, lod=False, fake_tube=True, linewidth=4
)
lines_verts = fury.utils.vertices_from_actor(edges)
initial_lines = lines_verts.copy() - np.reshape(lines, (-1, 3))
scene.add(edges)
tb = fury.ui.TextBlock2D(text="Tesseract", position=(900, 950), font_size=20)
showm.scene.add(tb)
counter = itertools.count()
end = 200
def timer_callback(_obj, _event):
global verts3D, angle
cnt = next(counter)
verts3D = rotate4D(verts4D)
if not wireframe:
point_verts[:] = initial_verts + np.repeat(verts3D, no_vertices, axis=0)
fury.utils.update_actor(points)
lines = connect_points(verts3D)
lines_verts[:] = initial_lines + np.reshape(lines, (-1, 3))
fury.utils.update_actor(edges)
showm.render()
angle += dtheta
if cnt == end:
showm.exit()
showm.add_timer_callback(True, 20, timer_callback)
showm.start()
fury.window.record(scene=showm.scene, size=(600, 600), out_path="viz_tesseract.png")
No comments:
Post a Comment