-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlight.py
More file actions
199 lines (157 loc) · 5.31 KB
/
Copy pathlight.py
File metadata and controls
199 lines (157 loc) · 5.31 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
193
194
195
196
197
198
199
import math
import numpy as np
# Local modules
from constants import DEFAULT_N0, DEFAULT_N1, MAX_COLOR_VALUE, RGB_CHANNELS
import utils
MAX_DISTANCE_LIGHT = np.inf
# Default horizontal and vertical number of samples for area lights
AREA_LIGHT_M = 8
AREA_LIGHT_N = 8
#
class Light:
"""
Light for a Scene.
Attributes:
position (numpy.array): 3D position of the light
"""
def __init__(self, position):
self.position = position
# TEMPORARY
self.color = np.ones(RGB_CHANNELS) * MAX_COLOR_VALUE
self.nl = DEFAULT_N0
def get_dist(self, ph):
"""
Get distance from the light to the point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
float: distance from the light to the point ph
"""
dist = np.linalg.norm(self.position - ph)
return dist
def get_l(self, ph):
"""
Get unit vector l that points to the light from hit point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
numpy.array: unit vector pointing to the light
"""
l = np.array([0, 1, 0], dtype=float)
return l
class DirectionalLight(Light):
"""
Directional Light for a scene.
"""
def get_dist(self, ph):
"""
Get distance from the light to the point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
float: distance from the light to the point ph
"""
return MAX_DISTANCE_LIGHT
def get_l(self, ph):
"""
Get unit vector l that points to the light from hit point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
numpy.array: unit vector pointing to the light
"""
l = utils.normalize(-self.position)
return l
class PointLight(Light):
"""
Point Light for a scene.
"""
def get_l(self, ph):
"""
Get unit vector l that points to the light from hit point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
numpy.array: unit vector pointing to the light
"""
l = utils.normalize(self.position - ph)
return l
class SpotLight(Light):
"""
Spot Light for a Scene.
Attributes:
position (numpy.array): 3D position of the light
theta (float): The angle for directional light in radians
nl (numpy.array): Unit vector in the direction of the spot light
cos_theta (float): Value for cos(theta), used for calculations
"""
def __init__(self, position, theta=0, nl=None):
Light.__init__(self, position)
self.theta = theta
self.nl = nl
self.cos_theta = math.cos(theta)
def get_l(self, ph):
"""
Get unit vector l that points to the light from hit point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
numpy.array: unit vector pointing to the light, 0 if outside cone
"""
l = utils.normalize(self.position - ph)
# This light won't illuminate if the point is outside the cone
if np.dot(-1 * l, self.nl) < self.cos_theta:
l = np.zeros(3)
return l
class AreaLight(Light):
"""
Area Light for a Scene.
Attributes:
position (numpy.array): Center of the area light
s0 (float): width of area light in world coordinates
s1 (float): height of area light in world coordinates
n0 (numpy.array): Unit vector for horizontal direction
n1 (numpy.array): Unit vector for vertical direction
p00 (numpy.array): Origin point for area light
"""
def __init__(self, position, s0, s1, n0=DEFAULT_N0, n1=DEFAULT_N1):
Light.__init__(self, position)
self.s0 = s0
self.s1 = s1
self.n0 = n0
self.n1 = n1
self.p00 = position - (float(s0) / 2) * n0 - (float(s1) / 2) * n1
def get_l(self, ph):
"""
Get unit vector l that points to a random sample inside the area from
hit point ph.
Args:
ph (numpy.array): 3D point of hit between ray and object
Returns:
numpy.array: unit vector pointing to random sample
"""
r0, r1 = np.random.random_sample(2)
x = r0 * self.s0
y = r1 * self.s1
p = self.p00 + x * self.n0 + y * self.n1
l = utils.normalize(p - ph)
return l
def get_samples(self, m=AREA_LIGHT_M, n=AREA_LIGHT_N):
"""
Get sample points from the area light dividing the area m horizontally
and n vertically and using random jitter.
Args:
m (int): Number of horizontal samples to use
n (int): Number of vertical samples to use
Returns:
list of numpy.array: sample positions in world space
"""
samples = []
for i in range(m):
for j in range(n):
r0, r1 = np.random.random_sample(2)
x = ((i + r0) / m) * self.s0
y = ((j + r1) / n) * self.s1
p = self.p00 + x * self.n0 + y * self.n1
samples.append(p)
return samples