Skip to content

Instantly share code, notes, and snippets.

@sergeant-wizard
Created June 6, 2017 05:20
Show Gist options
  • Save sergeant-wizard/c06187afc66cded26a5f99d404ff9466 to your computer and use it in GitHub Desktop.
Save sergeant-wizard/c06187afc66cded26a5f99d404ff9466 to your computer and use it in GitHub Desktop.
import numpy as np
import cv2
img_size = 640
initial_nodes = np.array([
[+1.0, +1.0, +1.0, +1.0],
[+1.0, +1.0, +1.0, -1.0],
[+1.0, +1.0, -1.0, +1.0],
[+1.0, +1.0, -1.0, -1.0],
[+1.0, -1.0, +1.0, +1.0],
[+1.0, -1.0, +1.0, -1.0],
[+1.0, -1.0, -1.0, +1.0],
[+1.0, -1.0, -1.0, -1.0],
[-1.0, +1.0, +1.0, +1.0],
[-1.0, +1.0, +1.0, -1.0],
[-1.0, +1.0, -1.0, +1.0],
[-1.0, +1.0, -1.0, -1.0],
[-1.0, -1.0, +1.0, +1.0],
[-1.0, -1.0, +1.0, -1.0],
[-1.0, -1.0, -1.0, +1.0],
[-1.0, -1.0, -1.0, -1.0],
])
edge_indices = [
# w axis
[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9],
[10, 11],
[12, 13],
[14, 15],
# z axis
[0, 2],
[1, 3],
[4, 6],
[5, 7],
[8, 10],
[9, 11],
[12, 14],
[13, 15],
# y axis
[0, 4],
[1, 5],
[2, 6],
[3, 7],
[8, 12],
[9, 13],
[10, 14],
[11, 15],
# x axis
[0, 8],
[1, 9],
[2, 10],
[3, 11],
[4, 12],
[5, 13],
[6, 14],
[7, 15],
]
isogeometric1 = 0.5 * np.array([
[+1, +1, +1, +1],
[+1, -1, +1, -1],
[+1, +1, -1, -1],
[+1, -1, -1, +1],
])
isogeometric2 = 1.0 / np.sqrt(2) * np.array([
[+1, +1, +0, +0],
[+1, -1, +0, +0],
[+0, +0, +1, +1],
[+0, +0, +1, -1],
])
def perspective_view(v3):
depth_view = 10.0
depth = v3[2]
return np.array([
v3[0] / (depth_view - depth),
v3[1] / (depth_view - depth)
])
def rot4dzw(theta):
c = np.cos(theta)
s = np.sin(theta)
return np.array([
[1.0, 0.0, 0.0, 0.0],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, c, -s],
[0.0, 0.0, s, c],
])
def rot4dxw(theta):
c = np.cos(theta)
s = np.sin(theta)
return np.array([
[c, 0.0, 0.0, -s],
[0.0, 1.0, 0.0, 0.0],
[0.0, 0.0, 1.0, 0.0],
[s, 0.0, 0.0, c],
])
def rot4dzx(theta):
c = np.cos(theta)
s = np.sin(theta)
return np.array([
[c, 0.0, s, 0.0],
[0.0, 1.0, 0.0, 0.0],
[-s, 0.0, c, 0.0],
[0.0, 0.0, 0.0, 1.0],
])
def rot4dyz(theta):
c = np.cos(theta)
s = np.sin(theta)
return np.array([
[1.0, 0.0, 0.0, 0.0],
[0.0, c, s, 0.0],
[0.0, -s, c, 0.0],
[0.0, 0.0, 0.0, 1.0],
])
def rot4d_from_lines(nodes, edge_indices):
ex = nodes[edge_indices[24][0]] - nodes[edge_indices[24][1]]
ey = nodes[edge_indices[16][0]] - nodes[edge_indices[16][1]]
ez = nodes[edge_indices[8][0]] - nodes[edge_indices[8][1]]
ew = nodes[edge_indices[0][0]] - nodes[edge_indices[0][1]]
return np.array([
ex / np.linalg.norm(ex),
ey / np.linalg.norm(ey),
ez / np.linalg.norm(ez),
ew / np.linalg.norm(ew),
])
def rot3d(theta):
c = np.cos(theta)
s = np.sin(theta)
return np.array([
[c, 0.0, s],
[0.0, c, s],
[-s, -s, np.sqrt(1 - 2 * s**2)],
])
def to_pixels(x):
x_min = -4.0
x_max = 4.0
center = np.array([img_size, img_size]) * 0.5
scale = 2.0
x = np.minimum(x_max, np.maximum(x_min, x))
px = center + (x / (x_max - x_min) * img_size) * scale
return px.astype(np.uint)
def get_tesseract_nodes(theta_zw):
nodes = initial_nodes.copy()
theta_zx = np.pi / 4
theta_yz = np.pi / 4
nodes = rot4dzw(theta_zw).dot(nodes.T).T
nodes = rot4dzx(theta_zx).dot(nodes.T).T
nodes = rot4dyz(theta_yz).dot(nodes.T).T
is_perspective = False
if is_perspective:
node3d = nodes[:, :3]
node2d = np.array([
perspective_view(n3d) for n3d in node3d
])
return node2d
else:
node3d = []
for node in nodes:
depth = node[3] - 3.0
node3d.append(node[0:3] / depth)
return np.array(node3d)[:, :2]
def picture():
theta = 0.0
node2d = get_tesseract_nodes(theta)
pixels = to_pixels(node2d)
out = np.zeros((img_size, img_size, 3), dtype=np.uint8)
for pt1_idx, pt2_idx in edge_indices:
pt1 = pixels[pt1_idx]
pt2 = pixels[pt2_idx]
cv2.line(out, tuple(pt1), tuple(pt2), (255, 255, 255))
while True:
cv2.imshow('out', out)
cv2.waitKey(30)
def video():
theta = 0.0
delta_theta = np.pi / 60
while True:
node2d = get_tesseract_nodes(theta)
print(node2d)
pixels = to_pixels(node2d)
out = np.zeros((img_size, img_size, 3), dtype=np.uint8)
for pt1_idx, pt2_idx in edge_indices:
pt1 = pixels[pt1_idx]
pt2 = pixels[pt2_idx]
cv2.line(out, tuple(pt1), tuple(pt2), (255, 255, 255))
cv2.imshow('out', out)
cv2.waitKey(30)
theta += delta_theta
if __name__ == '__main__':
video()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment