-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathobjlib.py
More file actions
92 lines (75 loc) · 2.33 KB
/
objlib.py
File metadata and controls
92 lines (75 loc) · 2.33 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
#
# Objects Library
# All the objects and their interset methods will be defined here.
#
import numpy as np
def add_cone():
# Define cone here.
# Method is unfinished
return None
def intersect_cone():
# Define cone here.
# Method is unfinished
return None
def add_cylinder():
# Define cone here.
# Method is unfinished
return None
def intersect_cylinder():
# Define cone here.
# Method is unfinished
return None
def add_cube():
# Define cone here.
# Method is unfinished
return None
def intersect_cube():
# Define cone here.
# Method is unfinished
return None
def add_pyramid():
# Define cone here.
# Method is unfinished
return None
def intersect_pyramid():
# Define cone here.
# Method is unfinished
return None
def add_sphere(position, radius, color):
return dict(type='sphere', position=np.array(position),
radius=np.array(radius), color=np.array(color), reflection=.5)
def intersect_sphere(O, D, S, R):
# Return the distance from O to the intersection of the ray (O, D) with the
# sphere (S, R), or +inf if there is no intersection.
# O and S are 3D points, D (direction) is a normalized vector, R is a scalar.
a = np.dot(D, D)
OS = O - S
b = 2 * np.dot(D, OS)
c = np.dot(OS, OS) - R * R
disc = b * b - 4 * a * c
if disc > 0:
distSqrt = np.sqrt(disc)
q = (-b - distSqrt) / 2.0 if b < 0 else (-b + distSqrt) / 2.0
t0 = q / a
t1 = c / q
t0, t1 = min(t0, t1), max(t0, t1)
if t1 >= 0:
return t1 if t0 < 0 else t0
return np.inf
def add_plane(position, normal, color_plane0, color_plane1):
return dict(type='plane', position=np.array(position),
normal=np.array(normal),
color=lambda M: (color_plane0
if (int(M[0] * 2) % 2) == (int(M[2] * 2) % 2) else color_plane1),
diffuse_c=.75, specular_c=.5, reflection=.25)
def intersect_plane(O, D, P, N):
# Return the distance from O to the intersection of the ray (O, D) with the
# plane (P, N), or +inf if there is no intersection.
# O and P are 3D points, D and N (normal) are normalized vectors.
denom = np.dot(D, N)
if np.abs(denom) < 1e-6:
return np.inf
d = np.dot(P - O, N) / denom
if d < 0:
return np.inf
return d