-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcartesian_3d_vectors.py
353 lines (263 loc) · 10.1 KB
/
cartesian_3d_vectors.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
"""
Copyright (c) 2017, 2019 Tor Olav Kristensen, http://subcube.com
https://github.com/t-o-k/scikit-vectors
Use of this source code is governed by a BSD-license that can be found in the LICENSE file.
"""
import skvectors.helper_functions as hf
from skvectors.cartesian_vectors import create_class_Cartesian_Vector
def create_class_Cartesian_3D_Vector(name, component_names, *, brackets='<>', sep=', ', cnull=0, cunit=1, functions=None):
"""Function that creates a cartesian vector class with 3 dimensions"""
hf.verify_class_name(name)
dimensions = 3
component_names = [ *component_names ]
if len(component_names) != dimensions:
msg = "The number of component names must be {dimensions}"
raise ValueError(msg.format_map(vars()))
if functions is None:
functions = { }
CV = \
create_class_Cartesian_Vector(
name = 'CV_' + name,
component_names = component_names,
brackets = brackets,
sep = sep,
cnull = cnull,
cunit = cunit,
functions = functions
)
def make_rotate_0_method(cls):
def rotate_0(self, angle):
"""A vector rotated around the {axis_name}-axis by an angle in radians"""
cnull = self._cnull
cunit = self._cunit
angle = cunit * angle
cos = self.component_cos(angle)
sin = self.component_sin(angle)
vector = \
self._mmult(
self._vector((cunit, cnull, cnull)),
self._vector((cnull, cos, -sin)),
self._vector((cnull, sin, cos))
)
return vector
axis_name = cls._cnames[0]
rotate_0.__doc__ = rotate_0.__doc__.format_map(vars())
method_name = 'rotate_' + axis_name
rotate_0.__name__ = method_name
setattr(cls, method_name, rotate_0)
def make_rotate_1_method(cls):
def rotate_1(self, angle):
"""A vector rotated around the {axis_name}-axis by an angle in radians"""
cnull = self._cnull
cunit = self._cunit
angle = cunit * angle
cos = self.component_cos(angle)
sin = self.component_sin(angle)
vector = \
self._mmult(
self._vector(( cos, cnull, sin)),
self._vector((cnull, cunit, cnull)),
self._vector(( -sin, cnull, cos))
)
return vector
axis_name = cls._cnames[1]
rotate_1.__doc__ = rotate_1.__doc__.format_map(vars())
method_name = 'rotate_' + axis_name
rotate_1.__name__ = method_name
setattr(cls, method_name, rotate_1)
def make_rotate_2_method(cls):
def rotate_2(self, angle):
"""A vector rotated around the {axis_name}-axis by an angle in radians"""
cnull = self._cnull
cunit = self._cunit
angle = cunit * angle
cos = self.component_cos(angle)
sin = self.component_sin(angle)
vector = \
self._mmult(
self._vector(( cos, -sin, cnull)),
self._vector(( sin, cos, cnull)),
self._vector((cnull, cnull, cunit))
)
return vector
axis_name = cls._cnames[2]
rotate_2.__doc__ = rotate_2.__doc__.format_map(vars())
method_name = 'rotate_' + axis_name
rotate_2.__name__ = method_name
setattr(cls, method_name, rotate_2)
def init_Cartesian_3D_Vector(cls):
"""Initialize class"""
hf.setup_vector_class(cls=cls, name=name, functions=functions)
make_rotate_0_method(cls)
make_rotate_1_method(cls)
make_rotate_2_method(cls)
return cls
@init_Cartesian_3D_Vector
class Cartesian_3D_Vector(CV):
"""
A cartesian vector class with {dimensions} dimensions and the component names '{cs_cnames}'
"""
_internal_functions = \
[
# 'eq',
# 'ne',
# 'and',
# 'or',
# 'all',
# 'min',
# 'max',
# 'floor',
# 'ceil',
# 'trunc',
# 'pi',
# 'atan2',
'cos',
'sin'
]
@classmethod
def from_polar(cls, radius, azimuth, inclination):
"""A vector created from polar coordinates"""
### Add check for negative radius
cunit = cls._cunit
azimuth = cunit * azimuth
cos_a = cls.component_cos(azimuth)
sin_a = cls.component_sin(azimuth)
inclination = cunit * inclination
cos_i = cls.component_cos(inclination)
sin_i = cls.component_sin(inclination)
radius = cunit * radius
vector = \
cls(
radius * cos_i * cos_a,
radius * cos_i * sin_a,
radius * sin_i,
_internal = True
)
return vector
@hf.ensure_other_is_vector
def cross(self, other):
"""The cross product of two vectors"""
cvs0, cvs1, cvs2 = self._cvalues
cvo0, cvo1, cvo2 = other._cvalues
vector = \
self._vector(
(
cvs1 * cvo2 - cvs2 * cvo1,
cvs2 * cvo0 - cvs0 * cvo2,
cvs0 * cvo1 - cvs1 * cvo0
)
)
return vector
@hf.ensure_others_are_vectors
def stp(self, other, other_):
"""The scalar triple product of three vectors"""
scalar = self.dot(other.cross(other_))
return scalar
@hf.ensure_others_are_vectors
def vtp(self, other, other_):
"""The vector triple product of three vectors"""
vector = self.cross(other.cross(other_))
return vector
@hf.ensure_other_is_vector
def sin(self, other, clip=False):
"""The sine of the angle between two vectors (from cnull to +cunit)"""
ls = self.length()
lo = other.length()
lcr = self.cross(other).length()
try:
sine = lcr / (ls * lo)
except ZeroDivisionError as err:
msg = "One (or both) of the vectors is a zero vector"
raise ZeroDivisionError(msg) from err
if clip:
cnull = self._cnull
cunit = self._cunit
sine = self.clip(sine, cnull, cunit)
return sine
def _axis_rot(self, *, axis, cos, sin):
cunit = self._cunit
la1 = axis.length()
la2 = la1**2
vcr1 = self.cross(axis)
vcr2 = vcr1.cross(axis)
vector = self + vcr2 / la2 * (cunit - cos) - vcr1 / la1 * sin
return vector
@hf.ensure_other_is_vector
def axis_rotate(self, other, angle):
"""A vector rotated around another by an angle in radians"""
cunit = self._cunit
angle = cunit * angle
cos = self.component_cos(angle)
sin = self.component_sin(angle)
try:
vector = self._axis_rot(axis=other, cos=cos, sin=sin)
except ZeroDivisionError as err:
msg = "The axis vector is a zero vector"
raise ZeroDivisionError(msg) from err
return vector
@hf.ensure_others_are_vectors
def reorient(self, other, other_):
"""Reorient a vector from one direction to another direction"""
axis = other.cross(other_)
try:
cos = other.cos(other_)
sin = other.sin(other_)
except ZeroDivisionError as err:
msg = "One (or both) of the direction vectors is a zero vector"
raise ZeroDivisionError(msg) from err
try:
vector = self._axis_rot(axis=axis, cos=cos, sin=sin)
except ZeroDivisionError:
parallel = True
else:
parallel = False
if parallel:
von = other.normalize()
von_ = other_.normalize()
dot = von.dot(von_)
if self._equal_cunit(-dot):
msg = "The direction vectors are pointing in opposite directions"
raise ZeroDivisionError(msg)
assert self._equal_cunit(dot)
vector = self.copy()
return vector
@hf.ensure_other_is_vector
def are_parallel(self, other):
"""Check if two vectors are parallel"""
# vsn = self.normalize()
# von = other.normalize()
# cross = vsn.cross(von)
cross = self.cross(other)
parallel = cross.is_zero_vector()
### Are these needed with NumPy ?
# parallel = self.component_or(parallel, self.is_zero_vector())
# parallel = self.component_or(parallel, other.is_zero_vector())
return parallel
def polar_as_dict(self):
"""Polar coordinates of a vector as a dictionary"""
result = \
{
'radius': self.radius,
'azimuth': self.azimuth,
'inclination': self.inclination
}
return result
@property
def radius(self):
"""TODO"""
length = self.length()
return length
@property
def azimuth(self):
"""TODO"""
cvs0, cvs1, cvs2 = self._cvalues
angle = self.component_atan2(cvs1, cvs0)
return angle
@property
def inclination(self):
"""TODO"""
cvs0, cvs1, cvs2 = self._cvalues
r = (cvs0**(cunit * 2) + cvs1**(cunit * 2))**(cunit / 2)
angle = self.component_atan2(cvs2, r)
return angle
return Cartesian_3D_Vector