-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcube2equi.py
137 lines (110 loc) · 4.02 KB
/
cube2equi.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
import math
def spherical_coordinates(i, j, w, h):
""" Returns spherical coordinates of the pixel from the output image. """
theta = 2*float(i)/float(w)-1
phi = 2*float(j)/float(h)-1
# phi = lat, theta = long
return phi*(math.pi/2), theta*math.pi
def vector_coordinates(phi, theta):
""" Returns 3D vector which points to the pixel location inside a sphere. """
return (math.cos(phi) * math.cos(theta), # X
math.sin(phi), # Y
math.cos(phi) * math.sin(theta)) # Z
# Assign identifiers to the faces of the cube
FACE_Z_POS = 1 # Left
FACE_Z_NEG = 2 # Right
FACE_Y_POS = 3 # Top
FACE_Y_NEG = 4 # Bottom
FACE_X_NEG = 5 # Front
FACE_X_POS = 6 # Back
def get_face(x, y, z):
""" Uses 3D vector to find which cube face the pixel lies on. """
largest_magnitude = max(abs(x), abs(y), abs(z))
if largest_magnitude - abs(x) < 0.00001:
return FACE_X_POS if x < 0 else FACE_X_NEG
elif largest_magnitude - abs(y) < 0.00001:
return FACE_Y_POS if y < 0 else FACE_Y_NEG
elif largest_magnitude - abs(z) < 0.00001:
return FACE_Z_POS if z < 0 else FACE_Z_NEG
def raw_face_coordinates(face, x, y, z):
"""
Return coordinates with necessary sign (- or +) depending on which face they lie on.
From Open-GL specification (chapter 3.8.10) https://www.opengl.org/registry/doc/glspec41.core.20100725.pdf
"""
if face == FACE_X_NEG:
xc = z
yc = y
ma = x
return xc, yc, ma
elif face == FACE_X_POS:
xc = -z
yc = y
ma = x
return xc, yc, ma
elif face == FACE_Y_NEG:
xc = z
yc = -x
ma = y
return xc, yc, ma
elif face == FACE_Y_POS:
xc = z
yc = x
ma = y
return xc, yc, ma
elif face == FACE_Z_POS:
xc = x
yc = y
ma = z
return xc, yc, ma
elif face == FACE_Z_NEG:
xc = -x
yc = y
ma = z
return xc, yc, ma
def raw_coordinates(xc, yc, ma):
""" Return 2D coordinates on the specified face relative to the bottom-left corner of the face. Also from Open-GL spec."""
return (float(xc)/abs(float(ma)) + 1) / 2, (float(yc)/abs(float(ma)) + 1) / 2
def face_origin_coordinates(face, n):
""" Return bottom-left coordinate of specified face in the input image. """
if face == FACE_X_NEG:
return n, n
elif face == FACE_X_POS:
return 3*n, n
elif face == FACE_Z_NEG:
return 2*n, n
elif face == FACE_Z_POS:
return 0, n
elif face == FACE_Y_POS:
return n, 0
elif face == FACE_Y_NEG:
return n, 2*n
def normalized_coordinates(face, x, y, n):
""" Return pixel coordinates in the input image where the specified pixel lies. """
face_coords = face_origin_coordinates(face, n)
normalized_x = math.floor(x*n)
normalized_y = math.floor(y*n)
# Stop out of bound behaviour
if normalized_x < 0:
normalized_x = 0
elif normalized_x >= n:
normalized_x = n-1
if normalized_y < 0:
normalized_x = 0
elif normalized_y >= n:
normalized_y = n-1
return face_coords[0] + normalized_x, face_coords[1] + normalized_y
def find_corresponding_pixel(i, j, w, h, n):
"""
:param i: X coordinate of output image pixel
:param j: Y coordinate of output image pixel
:param w: Width of output image
:param h: Height of output image
:param n: Height/Width of each square face
:return: Pixel coordinates for the input image that a specified pixel in the output image maps to.
"""
spherical = spherical_coordinates(i, j, w, h)
vector_coords = vector_coordinates(spherical[0], spherical[1])
face = get_face(vector_coords[0], vector_coords[1], vector_coords[2])
raw_face_coords = raw_face_coordinates(face, vector_coords[0], vector_coords[1], vector_coords[2])
cube_coords = raw_coordinates(raw_face_coords[0], raw_face_coords[1], raw_face_coords[2])
return normalized_coordinates(face, cube_coords[0], cube_coords[1], n)