-
Notifications
You must be signed in to change notification settings - Fork 0
/
vec3.py
184 lines (142 loc) · 4.4 KB
/
vec3.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
173
174
175
176
177
178
179
180
181
182
183
184
import math
import numpy as np
from _constants import MATH_EPS
class Vec3:
"""
Vec3 represents a 3-dimensional vector or point
in space, consisting of coordinates x, y and z.
"""
def __init__(self, _x: float = 0, _y: float = 0, _z: float = 0):
self.x = _x
self.y = _y
self.z = _z
"""
python overrides
"""
def __getitem__(self, key):
if key == 0:
return self.x
elif key == 1:
return self.y
elif key == 2:
return self.z
else:
assert False # vec3 only has 3 things
def __setitem__(self, key, value):
if key == 0:
self.x = value
elif key == 1:
self.y = value
elif key == 2:
self.z = value
else:
assert False # vec3 only has 3 things
def __str__(self):
return "x: {}, y: {}, z:{}".format(self.x, self.y, self.z)
def __repr__(self):
return 'Vec3({}, {}, {})'.format(self.x, self.y, self.z)
def __add__(self, rhs: "Vec3"):
return Vec3(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
def __iadd__(self, rhs: "Vec3"):
self.x += rhs.x
self.y += rhs.y
self.z += rhs.z
return self
def __sub__(self, rhs: "Vec3"):
return Vec3(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
def __isub__(self, rhs: "Vec3"):
self.x -= rhs.x
self.y -= rhs.y
self.z -= rhs.z
return self
def __mul__(self, scalar: float):
return Vec3(self.x * scalar, self.y * scalar, self.z * scalar)
def __rmul__(self, scalar: float):
return Vec3(self.x * scalar, self.y * scalar, self.z * scalar)
def __imul__(self, scalar: float):
self.x *= scalar
self.y *= scalar
self.z *= scalar
return self
def __truediv__(self, scalar: float):
return Vec3(self.x / scalar, self.y / scalar, self.z / scalar)
def __idiv__(self, scalar: float):
self.x /= scalar
self.y /= scalar
self.z /= scalar
return self
def __neg__(self):
return Vec3(-self.x, -self.y, -self.z)
def __eq__(self, rhs: "Vec3"):
return (
(abs(self.x - rhs.x) <= MATH_EPS)
and (abs(self.y - rhs.y) <= MATH_EPS)
and (abs(self.z - rhs.z) <= MATH_EPS)
)
def __ne__(self, rhs: "Vec3"):
return (
(abs(self.x - rhs.x) > MATH_EPS)
or (abs(self.y - rhs.y) > MATH_EPS)
or (abs(self.z - rhs.z) > MATH_EPS)
)
"""
useful functions
"""
def dot(self, rhs: "Vec3"):
return self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
def cross(self, rhs: "Vec3"):
return Vec3(
self.y * rhs.z - self.z * rhs.y,
self.z * rhs.x - self.x * rhs.z,
self.x * rhs.y - self.y * rhs.x,
)
def length_squared(self):
return self.x * self.x + self.y * self.y + self.z * self.z
def length(self):
return math.sqrt(self.length_squared())
def distance_squared(self, b: "Vec3"):
delta = b - self
return delta.length_squared()
def distance(self, b: "Vec3"):
delta = b - self
return delta.length()
def is_normalized(self):
return abs(self.length() - 1.0) <= MATH_EPS
def normalize(self):
length = self.length()
self.x /= length
self.y /= length
self.z /= length
def normalized(self):
return self / self.length()
def project_to(self, b: "Vec3"):
l2 = b.length_squared()
return b * (self.dot(b) / l2)
def project_to_plane(self, normal: "Vec3"):
return self - self.project_to(normal)
def is_nan(self):
return not np.isfinite(self.x + self.y + self.z)
def is_finite(self):
return np.isfinite(self.x + self.y + self.z)
"""
constructors + serialization
"""
@staticmethod
def from_numpy(np_arr: np.ndarray):
np_arr = np_arr.squeeze()
assert len(np_arr) == 3
return Vec3(np_arr[0], np_arr[1], np_arr[2])
def to_numpy(self):
return np.array([self.x, self.y, self.z])
@staticmethod
def zero():
return Vec3(0, 0, 0)
@staticmethod
def x_axis():
return Vec3(1, 0, 0)
@staticmethod
def y_axis():
return Vec3(0, 1, 0)
@staticmethod
def z_axis():
return Vec3(0, 0, 1)