-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
172 lines (140 loc) · 4.13 KB
/
utils.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
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
import numpy as np
import time
# Local imports
import constants
from material import Material
import material
MTL_DIFFUSE_BLUE = Material(material.COLOR_BLUE, material.TYPE_DIFFUSE)
rng = np.random.default_rng()
def normalize(arr):
"""
Normalize a vector using numpy.
Args:
arr(ndarray): Input vector
Returns:
ndarray: Normalized input vector
"""
norm = np.linalg.norm(arr)
if norm == 0:
return arr
return arr / norm
def distance(p1, p2):
"""
Get the distance between points p1 and p2
Args:
p1(ndarray): Point 1
p2(ndarray): Point 2
Returns:
float: Distance
"""
dist = np.linalg.norm(p1 - p2)
return dist
def humanize_time(secs):
"""
Extracted from http://testingreflections.com/node/6534
"""
mins, secs = divmod(secs, 60)
hours, mins = divmod(mins, 60)
return '%02d:%02d:%02d' % (hours, mins, secs)
def degree2radians(degrees):
return (degrees / float(360)) * 2 * np.pi
def random_unit_vector():
# 3D random vector
v = np.random.random_sample(3)
v_unit = normalize(v)
return v_unit
def rotate_x(v, theta):
rot_mat = np.array([
[1, 0, 0],
[0, np.cos(theta), -np.sin(theta)],
[0, np.sin(theta), np.cos(theta)]
])
rotated_v = np.dot(rot_mat, v)
return rotated_v
def random_hemisphere(v):
"""
Create a new random vector around the hemisphere in the given vector.
Using y-axis as up.
Args:
v: a vector
Returns:
ndarray: a new vector
"""
n1 = normalize(v)
n0 = normalize(np.cross(n1, np.array([0.0, 0.0, 1.0])))
n2 = normalize(np.cross(n0, n1))
phi = rng.random() * 2 * np.pi
y = rng.random()
theta = np.arccos(y)
x = np.sin(theta) * np.cos(phi)
z = np.sin(theta) * np.sin(phi)
new_vector = x * n0 + y * n1 + z * n2
return new_vector
def blerp(img_arr, x, y):
# Interpolate values of pixel neighborhood of x and y
i = int(np.round(x))
j = int(np.round(y))
# But not in the borders
height, width, _ = img_arr.shape
if i == 0 or j == 0 or i == width or j == height:
if i == width:
i -= 1
if j == height:
j -= 1
return img_arr[j][i]
# t and s are interpolation parameters that go from 0 to 1
t = x - i + 0.5
s = y - j + 0.5
# Bilinear interpolation
color = (
img_arr[j - 1][i - 1] * (1 - t) * (1 - s)
+ img_arr[j - 1][i] * t * (1 - s)
+ img_arr[j][i - 1] * (1 - t) * s
+ img_arr[j][i] * t * s
)
return color
def color_matching(color):
xyz = np.zeros(3)
# Exception for color in RGB
if len(color) == 3:
new_color_matching = np.array([
constants.COLOR_MATCHING[constants.RGB_INDEX[0]],
constants.COLOR_MATCHING[constants.RGB_INDEX[1]],
constants.COLOR_MATCHING[constants.RGB_INDEX[2]]
])
for i in range(len(color)):
intensity = color[i]
xyz[0] += intensity * new_color_matching[i][1]
xyz[1] += intensity * new_color_matching[i][2]
xyz[2] += intensity * new_color_matching[i][3]
else:
for i in range(len(color)):
intensity = color[i]
xyz[0] += intensity * constants.COLOR_MATCHING[i][1]
xyz[1] += intensity * constants.COLOR_MATCHING[i][2]
xyz[2] += intensity * constants.COLOR_MATCHING[i][3]
return xyz
def xyz_to_rgb(color):
rgb_color = np.matmul(constants.XYZ_TO_RGB, color)
rgb_color = np.clip(rgb_color, 0, 1)
return rgb_color
class Timer:
def __init__(self):
self.start_time = 0
self.end_time = 0
self.elapsed_time = 0
def start(self):
self.start_time = time.time()
def stop(self):
self.end_time = time.time()
self.elapsed_time = self.end_time - self.start_time
def __str__(self):
return humanize_time(self.elapsed_time)
# class Position(object):
# """Position in 3D space"""
#
# def __init__(self, x=0, y=0, z=0):
# self.x = x
# self.y = y
# self.z = z
# self.data = np.array([x, y, z])