Repository navigation
Expand file tree
/
Copy pathskybox.py
More file actions
192 lines (148 loc) · 5.29 KB
/
Copy pathskybox.py
File metadata and controls
192 lines (148 loc) · 5.29 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
import numba as nb
import numpy as np
@nb.experimental.jitclass([
("image", nb.float64[:,:,:]),
("images", nb.float64[:,:,:,:]),
])
class SkyBox():
def __init__(self, image):
self.image = image
height, total_width, _ = self.image.shape
face_width = total_width // 4
self.images = np.array([
self.image[face_width:2*face_width, 2*face_width:3*face_width], # POSITIVE X
self.image[face_width:2*face_width, 0:face_width], # NEGATIVE X
self.image[0:face_width, face_width:2*face_width], # POSITIVE Y
self.image[2*face_width:3*face_width, face_width:2*face_width], # NEGATIVE Y
self.image[face_width:2*face_width, face_width:2*face_width], # POSITIVE Z
self.image[face_width:2*face_width, 3*face_width:4*face_width], # NEGATIVE Z
], dtype=np.float64)
def sphere2pixel(self, P):
x,y,z = P
abs_x, abs_y, abs_z = np.abs(P)
x_positive = x>0
y_positive = y>0
z_positive = z>0
# POSITIVE X
if x_positive and abs_x >= abs_y and abs_x >= abs_z:
max_axis = abs_x
uc = -z
vc = y
index = 0
# NEGATIVE X
if not x_positive and abs_x >= abs_y and abs_x >= abs_z:
max_axis = abs_x
uc = z
vc = y
index = 1
# POSITIVE Y
if y_positive and abs_y >= abs_x and abs_y >= abs_z:
max_axis = abs_y
uc = x
vc = -z
index = 2
# NEGATIVE Y
if not y_positive and abs_y >= abs_x and abs_y >= abs_z:
max_axis = abs_y
uc = x
vc = z
index = 3
# POSITIVE Z
if z_positive and abs_z >= abs_x and abs_z >= abs_y:
max_axis = abs_z
uc = x
vc = y
index = 4
# NEGATIVE Z
if not z_positive and abs_z >= abs_x and abs_z >= abs_y:
max_axis = abs_z
uc = -x
vc = y
index = 5
# convert from (-1,1) to (0,1)
u = 0.5 * (uc / max_axis + 1)
v = 0.5 * (vc / max_axis + 1)
pixel = [v,u]
pixel = (int(self.images[index].shape[0]-pixel[0]*self.images[index].shape[0]),
int(pixel[1]*self.images[index].shape[1])-1)
return pixel[0], pixel[1], index
def get_color(self, P):
x,y, index = self.sphere2pixel(P)
return self.images[index][x,y]
@nb.experimental.jitclass([
("image", nb.float64[:,:,:]),
("images", nb.float64[:,:,:,:]),
])
class SkyBox():
def __init__(self, image):
self.image = image
height, total_width, _ = self.image.shape
face_width = total_width // 4
self.images = np.zeros((6,face_width,face_width,3), dtype=np.float64)
images = [
self.image[face_width:2*face_width, 2*face_width:3*face_width], # POSITIVE X
self.image[face_width:2*face_width, 0:face_width], # NEGATIVE X
self.image[0:face_width, face_width:2*face_width], # POSITIVE Y
self.image[2*face_width:3*face_width, face_width:2*face_width], # NEGATIVE Y
self.image[face_width:2*face_width, face_width:2*face_width], # POSITIVE Z
self.image[face_width:2*face_width, 3*face_width:4*face_width], # NEGATIVE Z
]
for i, image in enumerate(images):
self.images[i] = image
def sphere2pixel(self, P):
x,y,z = P
abs_x, abs_y, abs_z = np.abs(P)
x_positive = x>0
y_positive = y>0
z_positive = z>0
# POSITIVE X
if x_positive and abs_x >= abs_y and abs_x >= abs_z:
max_axis = abs_x
uc = -z
vc = y
index = 0
# NEGATIVE X
if not x_positive and abs_x >= abs_y and abs_x >= abs_z:
max_axis = abs_x
uc = z
vc = y
index = 1
# POSITIVE Y
if y_positive and abs_y >= abs_x and abs_y >= abs_z:
max_axis = abs_y
uc = x
vc = -z
index = 2
# NEGATIVE Y
if not y_positive and abs_y >= abs_x and abs_y >= abs_z:
max_axis = abs_y
uc = x
vc = z
index = 3
# POSITIVE Z
if z_positive and abs_z >= abs_x and abs_z >= abs_y:
max_axis = abs_z
uc = x
vc = y
index = 4
# NEGATIVE Z
if not z_positive and abs_z >= abs_x and abs_z >= abs_y:
max_axis = abs_z
uc = -x
vc = y
index = 5
# convert from (-1,1) to (0,1)
u = 0.5 * (uc / max_axis + 1)
v = 0.5 * (vc / max_axis + 1)
pixel = [v,u]
pixel = (int(self.images[index].shape[0]-pixel[0]*self.images[index].shape[0]),
int(pixel[1]*self.images[index].shape[1])-1)
return pixel[0], pixel[1], index
def get_color(self, P):
x,y, index = self.sphere2pixel(P)
return self.images[index][x,y]/255
if __name__ == "__main__":
from PIL import Image
image = Image.open("skyboxes/miramar.jpeg")
image = np.array(image, dtype=np.float64)
SkyBox(image)