Skip to content

Commit aae7ee9

Browse files
isHarryhK0lb3
authored andcommitted
refactor(math): Vector3
1 parent ad8799d commit aae7ee9

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

UnityPy/math/Vector3.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
from dataclasses import dataclass
22
from math import sqrt
3+
from typing import Sequence
34

45

56
kEpsilon = 0.00001
67

78

89
@dataclass
910
class Vector3:
11+
'''https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector3.cs'''
12+
1013
X: float = 0.0
1114
Y: float = 0.0
1215
Z: float = 0.0
1316

1417
def __init__(self, *args):
15-
if len(args) == 3 or len(args) == 1 and isinstance(args[0], (tuple, list)):
16-
self.X, self.Y, self.Z = args
17-
elif len(args) == 1:
18-
# dirty patch for Vector4
19-
self.__dict__ = args[0].__dict__
18+
if len(args) == 1:
19+
args = args[0]
20+
21+
if isinstance(args, Sequence):
22+
if len(args) == 3: # args=(x, y, z)
23+
self.X, self.Y, self.Z = args
24+
return
25+
if len(args) == 0: # args=()
26+
self.X = self.Y = self.Z = 0.0
27+
return
28+
else: # dirty patch for Vector4
29+
self.X, self.Y, self.Z = args.X, args.Y, args.Z
30+
return
31+
32+
raise TypeError("Invalid arguments for Vector3")
2033

2134
def __getitem__(self, index):
2235
return (self.X, self.Y, self.Z)[index]
@@ -32,13 +45,11 @@ def __setitem__(self, index, value):
3245
raise IndexError("Index out of range")
3346

3447
def __hash__(self):
35-
return self.X.__hash__() ^ (self.Y.__hash__() << 2) ^ (self.Z.__hash__() >> 2)
36-
37-
def __eq__(self, other):
38-
if isinstance(other, Vector3):
39-
return self.X == other.X and self.Y == other.Y and self.Z == other.Z
40-
else:
41-
return False
48+
return (
49+
self.X.__hash__() ^
50+
(self.Y.__hash__() << 2) ^
51+
(self.Z.__hash__() >> 2)
52+
)
4253

4354
def normalize(self):
4455
length = self.length()
@@ -48,21 +59,19 @@ def normalize(self):
4859
self.Y *= invNorm
4960
self.Z *= invNorm
5061
else:
51-
X = 0
52-
Y = 0
53-
Z = 0
62+
self.X = self.Y = self.Z = 0.0
5463

55-
def Normalize(self):
56-
self.normalize()
64+
Normalize = normalize
5765

5866
def length(self):
59-
return sqrt(self.LengthSquared())
67+
return sqrt(self.lengthSquared())
68+
69+
Length = length
6070

61-
def Length(self):
62-
return self.length()
71+
def lengthSquared(self):
72+
return self.X ** 2 + self.Y ** 2 + self.Z ** 2
6373

64-
def LengthSquared(self):
65-
return self.X ** 2 + self.Y ** 2 + self.Y ** 2
74+
LengthSquared = lengthSquared
6675

6776
@staticmethod
6877
def Zero():
@@ -81,11 +90,14 @@ def __sub__(a, b):
8190
def __mul__(a, d):
8291
return Vector3(a.X * d, a.Y * d, a.Z * d)
8392

84-
def __div__(a, d):
93+
def __truediv__(a, d):
8594
return Vector3(a.X / d, a.Y / d, a.Z / d)
8695

8796
def __eq__(lhs, rhs):
88-
return (lhs - rhs).LengthSquared() < kEpsilon
97+
if isinstance(rhs, Vector3):
98+
diff = lhs - rhs
99+
return diff.lengthSquared() < kEpsilon * kEpsilon
100+
return False
89101

90102
def __ne__(lhs, rhs):
91103
return not (lhs == rhs)

0 commit comments

Comments
 (0)