-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvector.py
More file actions
609 lines (488 loc) · 15.4 KB
/
vector.py
File metadata and controls
609 lines (488 loc) · 15.4 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
## Copyright (c) 2020-2023 The PyUnity Team
## This file is licensed under the MIT License.
## See https://docs.pyunity.x10.bz/en/latest/license.html
__all__ = ["Vector", "Vector2", "Vector3"]
from . import Mathf
from .abc import ABCMeta, abstractmethod, abstractproperty
from .other import LockedLiteral
from collections.abc import Iterable
import operator
def conv(num):
"""Convert float to string and removing decimal place as necessary."""
if isinstance(num, float) and num.is_integer():
return str(int(num))
return str(num)
class Vector(LockedLiteral, metaclass=ABCMeta):
def __repr__(self):
return f"{self.__class__.__name__}({', '.join(map(conv, self))})"
def __str__(self):
return f"{self.__class__.__name__}({', '.join(map(conv, self))})"
def __getitem__(self, i):
return list(self)[i]
@abstractmethod
def __iter__(self):
pass
def __list__(self):
return list(iter(self))
def __hash__(self):
return hash(list(self))
@abstractmethod
def __len__(self):
pass
def __bool__(self):
return all(self)
@abstractmethod
def _o1(self, f):
pass
@abstractmethod
def _o2(self, other, f):
pass
@abstractmethod
def _o2r(self, other, f):
pass
def __add__(self, other):
return self._o2(other, operator.add)
def __radd__(self, other):
return self._o2r(other, operator.add)
def __sub__(self, other):
return self._o2(other, operator.sub)
def __rsub__(self, other):
return self._o2r(other, operator.sub)
def __mul__(self, other):
return self._o2(other, operator.mul)
def __rmul__(self, other):
return self._o2r(other, operator.mul)
def __div__(self, other):
return self._o2(other, operator.div)
def __rdiv__(self, other):
return self._o2r(other, operator.div)
def __floordiv__(self, other):
return self._o2(other, operator.floordiv)
def __rfloordiv__(self, other):
return self._o2r(other, operator.floordiv)
def __truediv__(self, other):
return self._o2(other, operator.truediv)
def __rtruediv__(self, other):
return self._o2r(other, operator.truediv)
def __mod__(self, other):
return self._o2(other, operator.mod)
def __rmod__(self, other):
return self._o2r(other, operator.mod)
def __lshift__(self, other):
return self._o2(other, operator.lshift)
def __rlshift__(self, other):
return self._o2r(other, operator.lshift)
def __rshift__(self, other):
return self._o2(other, operator.rshift)
def __rrshift__(self, other):
return self._o2r(other, operator.rshift)
def __eq__(self, other):
if not isinstance(other, type(self)):
return False
return all(self._o2(other, operator.eq))
def __ne__(self, other):
return any(self._o2(other, operator.ne))
def __gt__(self, other):
return all(self._o2(other, operator.gt))
def __lt__(self, other):
return all(self._o2(other, operator.lt))
def __ge__(self, other):
return all(self._o2(other, operator.ge))
def __le__(self, other):
return all(self._o2(other, operator.le))
def __and__(self, other):
return self._o2(other, operator.and_)
def __rand__(self, other):
return self._o2r(other, operator.and_)
def __or__(self, other):
return self._o2(other, operator.or_)
def __ror__(self, other):
return self._o2r(other, operator.or_)
def __xor__(self, other):
return self._o2(other, operator.xor)
def __rxor__(self, other):
return self._o2r(other, operator.xor)
def __neg__(self):
return self._o1(operator.neg)
def __pos__(self):
return self._o1(operator.pos)
def __abs__(self):
return self.length
def abs(self):
return self._o1(abs)
def __round__(self, other=None):
return self._o2(other, round)
def __invert__(self):
return self._o1(operator.invert)
@abstractproperty
def length(self):
pass
@property
def intTuple(self):
"""Return the x, y and z values of this vector as ints"""
return tuple(map(int, self))
@abstractmethod
def replace(self, num, value):
pass
class Vector2(Vector):
def __init__(self, xOrList=None, y=None):
super(Vector2, self).__init__()
if xOrList is not None:
if y is None:
if hasattr(xOrList, "x") and hasattr(xOrList, "y"):
l = [xOrList.x, xOrList.y]
else:
l = xOrList
else:
l = [xOrList, y]
else:
l = [0, 0]
l = [x if isinstance(x, (int, float)) else float(x) for x in l]
self.x, self.y = l
self._lock()
def __iter__(self):
yield self.x
yield self.y
def __len__(self):
return 2
def _o1(self, f):
"""Unary operator"""
return Vector2(f(self.x), f(self.y))
def _o2(self, other, f):
"""Any two-operator operation where the left operand is a Vector2"""
if isinstance(other, Iterable):
return Vector2(f(self.x, other[0]), f(self.y, other[1]))
else:
return Vector2(f(self.x, other), f(self.y, other))
def _o2r(self, other, f):
"""Any two-operator operation where the right operand is a Vector2"""
if isinstance(other, Iterable):
return Vector2(f(other[0], self.x), f(other[1], self.y))
else:
return Vector2(f(other, self.x), f(other, self.y))
def replace(self, num, value):
l = list(self)
l[num] = value
return Vector2(l)
def copy(self):
"""Makes a copy of the Vector2"""
return Vector2(self.x, self.y)
def getLengthSqrd(self):
"""
Gets the length of the vector squared. This
is much faster than finding the length.
Returns
-------
float
The length of the vector squared
"""
return self.x ** 2 + self.y ** 2
@property
def length(self):
"""Gets the magnitude of the vector"""
return Mathf.Sqrt(self.x ** 2 + self.y ** 2)
def normalized(self):
"""
Get a normalized copy of the vector, or Vector2(0, 0)
if the length is 0.
Returns
-------
Vector2
A normalized vector
"""
length = self.length
if length != 0:
return 1 / length * self
return self.copy()
def getDistance(self, other):
"""
The distance between this vector and the other vector
Returns
-------
float
The distance
"""
return Mathf.Sqrt((self.x - other[0]) ** 2 + (self.y - other[1]) ** 2)
def getDistSqrd(self, other):
"""
The distance between this vector and the other vector, squared.
It is more efficient to call this than to call
:meth:`Vector2.getDistance` and square it.
Returns
-------
float
The squared distance
"""
return (self.x - other[0]) ** 2 + (self.y - other[1]) ** 2
def clamp(self, min, max):
"""
Returns a clamped vector between two other vectors,
resulting in the vector being as close to the
edge of a bounding box created as possible.
Parameters
----------
min : Vector2
Min vector
max : Vector2
Max vector
Returns
-------
Vector3
A vector inside or on the surface of the
bounding box specified by min and max.
"""
x = Mathf.Clamp(self.x, min.x, max.x)
y = Mathf.Clamp(self.y, min.y, max.y)
return Vector2(x, y)
def dot(self, other):
"""
Dot product of two vectors.
Parameters
----------
other : Vector2
Other vector
Returns
-------
float
Dot product of the two vectors
"""
return self.x * other[0] + self.y * other[1]
def cross(self, other):
"""
Cross product of two vectors. In 2D this
is a scalar.
Parameters
----------
other : Vector2
Other vector
Returns
-------
float
Cross product of the two vectors
"""
z = self.x * other[1] - self.y * other[0]
return z
@staticmethod
def min(a, b):
return a._o2(b, min)
@staticmethod
def max(a, b):
return a._o2(b, max)
@staticmethod
def zero():
"""A vector of zero length"""
return Vector2(0, 0)
@staticmethod
def one():
"""A vector of ones"""
return Vector2(1, 1)
@staticmethod
def left():
"""Vector2 pointing in the negative x axis"""
return Vector2(-1, 0)
@staticmethod
def right():
"""Vector2 pointing in the postive x axis"""
return Vector2(1, 0)
@staticmethod
def up():
"""Vector2 pointing in the postive y axis"""
return Vector2(0, 1)
@staticmethod
def down():
"""Vector2 pointing in the negative y axis"""
return Vector2(0, -1)
class Vector3(Vector):
def __init__(self, xOrList=None, y=None, z=None):
super(Vector3, self).__init__()
if xOrList is not None:
if y is None:
if hasattr(xOrList, "x") and hasattr(xOrList, "y") and hasattr(xOrList, "z"):
l = [xOrList.x, xOrList.y, xOrList.z]
else:
l = xOrList
else:
if z is None:
raise ValueError("Expected 3 arguments, got 2")
l = [xOrList, y, z]
else:
l = [0, 0, 0]
l = [x if isinstance(x, (int, float)) else float(x) for x in l]
self.x, self.y, self.z = l
self._lock()
def __iter__(self):
yield self.x
yield self.y
yield self.z
def __len__(self):
return 3
def _o1(self, f):
"""Unary operator"""
return Vector3(f(self.x), f(self.y), f(self.z))
def _o2(self, other, f):
"""Any two-operator operation where the left operand is a Vector3"""
if isinstance(other, Vector3):
return Vector3(f(self.x, other.x), f(self.y, other.y), f(self.z, other.z))
elif isinstance(other, Iterable):
return Vector3(f(self.x, other[0]), f(self.y, other[1]), f(self.z, other[2]))
else:
return Vector3(f(self.x, other), f(self.y, other), f(self.z, other))
def _o2r(self, other, f):
"""Any two-operator operation where the right operand is a Vector3"""
if isinstance(other, Iterable):
return Vector3(f(other[0], self.x), f(other[1], self.y), f(other[2], self.z))
else:
return Vector3(f(other, self.x), f(other, self.y), f(other, self.z))
def replace(self, num, value):
l = list(self)
l[num] = value
return Vector3(l)
def copy(self):
"""
Makes a copy of the Vector3
Returns
-------
Vector3
A shallow copy of the vector
"""
return Vector3(self.x, self.y, self.z)
def getLengthSqrd(self):
"""
Gets the length of the vector squared. This
is much faster than finding the length.
Returns
-------
float
The length of the vector squared
"""
return self.x ** 2 + self.y ** 2 + self.z ** 2
@property
def length(self):
"""Gets the magnitude of the vector"""
return Mathf.Sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def normalized(self):
"""
Get a normalized copy of the vector, or Vector3(0, 0, 0)
if the length is 0.
Returns
-------
Vector3
A normalized vector
"""
length = self.length
if length != 0:
return 1 / length * self
return self.copy()
def getDistance(self, other):
"""
The distance between this vector and the other vector
Returns
-------
float
The distance
"""
return Mathf.Sqrt((self.x - other[0]) ** 2 + (self.y - other[1]) ** 2 + (self.z - other[2]) ** 2)
def getDistSqrd(self, other):
"""
The distance between this vector and the other vector, squared.
It is more efficient to call this than to call
:meth:`Vector3.getDistance` and square it.
Returns
-------
float
The squared distance
"""
return (self.x - other[0]) ** 2 + (self.y - other[1]) ** 2 + (self.z - other[2]) ** 2
def clamp(self, min, max):
"""
Returns a clamped vector between two other vectors,
resulting in the vector being as close to the
edge of a bounding box created as possible.
Parameters
----------
min : Vector3
Min vector
max : Vector3
Max vector
Returns
-------
Vector3
A vector inside or on the surface of the
bounding box specified by min and max.
"""
x = Mathf.Clamp(self.x, min.x, max.x)
y = Mathf.Clamp(self.y, min.y, max.y)
z = Mathf.Clamp(self.z, min.z, max.z)
return Vector3(x, y, z)
def dot(self, other):
"""
Dot product of two vectors.
Parameters
----------
other : Vector3
Other vector
Returns
-------
float
Dot product of the two vectors
"""
return self.x * other[0] + self.y * other[1] + self.z * other[2]
def cross(self, other):
"""
Cross product of two vectors
Parameters
----------
other : Vector3
Other vector
Returns
-------
Vector3
Cross product of the two vectors
"""
if isinstance(other, Vector3):
x = self.y * other.z - self.z * other.y
y = self.z * other.x - self.x * other.z
z = self.x * other.y - self.y * other.x
else:
x = self.y * other[2] - self.z * other[1]
y = self.z * other[0] - self.x * other[2]
z = self.x * other[1] - self.y * other[0]
return Vector3(x, y, z)
@staticmethod
def min(a, b):
return a._o2(b, min)
@staticmethod
def max(a, b):
return a._o2(b, max)
@staticmethod
def zero():
"""A vector of zero length"""
return Vector3(0, 0, 0)
@staticmethod
def one():
"""A vector of ones"""
return Vector3(1, 1, 1)
@staticmethod
def forward():
"""Vector3 pointing in the positive z axis"""
return Vector3(0, 0, 1)
@staticmethod
def back():
"""Vector3 pointing in the negative z axis"""
return Vector3(0, 0, -1)
@staticmethod
def left():
"""Vector3 pointing in the negative x axis"""
return Vector3(-1, 0, 0)
@staticmethod
def right():
"""Vector3 pointing in the postive x axis"""
return Vector3(1, 0, 0)
@staticmethod
def up():
"""Vector3 pointing in the postive y axis"""
return Vector3(0, 1, 0)
@staticmethod
def down():
"""Vector3 pointing in the negative y axis"""
return Vector3(0, -1, 0)