Repository navigation
Expand file tree
/
Copy pathprimitives.py
More file actions
170 lines (118 loc) · 4.27 KB
/
Copy pathprimitives.py
File metadata and controls
170 lines (118 loc) · 4.27 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
import numba as nb
from numba import literal_unroll
import numpy as np
from ray import Ray
@nb.experimental.jitclass([
("position", nb.float64[:]),
("radius", nb.float64),
])
class Sphere():
def __init__(self, position, radius):
self.position = position
self.radius = radius
def normal(self, P):
return (P - self.position) / self.radius
def intersect(self, ray):
a = np.dot(ray.direction, ray.direction)
b = 2*np.dot(ray.origin-self.position, ray.direction)
c = np.dot(ray.origin-self.position, ray.origin-self.position) - self.radius**2
discriminant = b**2 - 4*a*c
if discriminant < 0:
return [np.inf], [np.array([0,0,0],dtype=np.float64)]
else:
t1 = (-b - np.sqrt(discriminant))/(2*a)
t2 = (-b + np.sqrt(discriminant))/(2*a)
return [t1,t2], [self.normal(ray.P(t)) for t in [t1,t2]]
@nb.njit()
def get_node_children(node_index):
left_index = 2*node_index+1
right_index = 2*node_index+2
return left_index, right_index
@nb.njit()
def intersect_tree(tree, ray):
stack = [(0, 0)] # start with the root and depth 0
nearest = -1
while stack:
idx, depth = stack.pop()
if idx >= len(tree) or np.all(tree[idx] == np.zeros(3)-1):
continue
axis = 0
next_idx = None
opposite_idx = None
left_idx, right_idx = get_node_children(idx)
if ray.origin[axis] < tree[idx][axis]:
next_idx = left_idx
opposite_idx = right_idx
else:
next_idx = right_idx
opposite_idx = left_idx
if nearest == -1 or np.linalg.norm(tree[nearest] - ray.origin) > np.linalg.norm(tree[idx] - ray.origin):
nearest = idx
# We push opposite_idx first, so that next_idx is processed next in the loop
# stack.append((opposite_idx, depth + 1))
stack.append((next_idx, depth + 1))
return nearest
@nb.njit()
def _intersect(tree, triangles, ray):
triangle_index = intersect_tree(tree, ray)
if triangle_index == -1:
return [np.inf], [np.array([0,0,0],dtype=np.float64)]
triangle = triangles[triangle_index]
A,B,C = triangle
edgeAB = B - A
edgeAC = C - A
normal_vector = np.cross(edgeAB, edgeAC)
ao = ray.origin - A
determinant = -np.dot(ray.direction, normal_vector)
invDet = 1 / determinant
t = np.dot(ao, normal_vector) * invDet
if t < ray.tmin or t > ray.tmax:
pass
return [t], [normal_vector]
@nb.experimental.jitclass([
("tree", nb.float64[:,:]),
("min_ax", nb.float64[:]),
("max_ax", nb.float64[:]),
("triangles", nb.float64[:,:,:]),
])
class Mesh():
def __init__(self, tree, min_ax, max_ax, triangles):
self.tree = tree
self.min_ax = min_ax
self.max_ax = max_ax
self.triangles = triangles
def get_node_parent(self, node_index):
return int((node_index-1)/2)
def intersect(self, ray):
return _intersect(self.tree, self.triangles, ray)
@nb.njit
def closest_intersection(ray, objects):
closest_t = np.inf
closest_index = -1
closest_normal = np.array([0,0,0], dtype=np.float64)
i = -1
for obj in literal_unroll(objects):
i += 1
ts, normals = obj.intersect(ray)
for t, normal in zip(ts, normals):
if ray.tmin<=t<=ray.tmax:
if t<closest_t:
closest_t = t
closest_index = i
closest_normal = normal
return closest_t, closest_index, closest_normal
if __name__ == "__main__":
ss = (Sphere(np.array([0,0,0], np.float64), 1),
Sphere(np.array([0,0,1], np.float64), 1))
r = Ray(np.array([0,0,-2], np.float64), np.array([0,0,1], np.float64))
closest_object, closest_t, closest_index, closest_normal = closest_intersection(r, ss)
P = r.P(closest_t)
print(closest_object.normal(P))
print(closest_normal)
from load_obj import load_obj
vertices, faces, triangles = load_obj("objects/teapot.obj")
triangles = np.array(triangles, dtype=np.float64)
teapot = Mesh(vertices, faces, triangles)
r = Ray(np.array([0,0,-2], np.float64), np.array([0,0,1], np.float64))
ts, normals = teapot.intersect(r)
print(normals)