-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
128 lines (101 loc) · 2.99 KB
/
utils.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
# mcu_utils.py
# various utilities for the other scripts.
from typing import Union, List, Tuple
INVALID = 0x82D3F263
class Vector3:
def __init__(self, x: float, y: float, z: float):
self.x = x
self.y = y
self.z = z
def __getitem__(self, key):
assert type(key) == int and 0 <= key <= 2
return (self.x, self.y, self.z)[key]
def __setitem__(self, key, newvalue):
assert type(key) == int and 0 <= key <= 2
assert type(newvalue) == float
if key == 0:
self.x = newvalue
if key == 1:
self.y = newvalue
if key == 2:
self.z = newvalue
def __len__(self):
return 3
def set_axis(self, axis: int, value: float):
if axis == 0:
self.x = value
elif axis == 1:
self.y = value
elif axis == 2:
self.z = value
else:
raise IndexError("Invalid Axis")
def get_axis(self, axis: int):
if axis == 0:
return self.x
elif axis == 1:
return self.y
elif axis == 2:
return self.z
else:
raise IndexError("Invalid Axis")
def is_valid(self):
return self.x != INVALID and self.y != INVALID and self.z != INVALID
def __str__(self):
return f"({self.x:.4f}, {self.y:.4f}, {self.z:.4f})"
@staticmethod
def new():
return Vector3(0, 0, 0)
@staticmethod
def invalid():
return Vector3(INVALID, INVALID, INVALID)
@staticmethod
def from_arr(i: Union[List, Tuple]):
return Vector3(i[0], i[1], i[2])
class Vector2:
def __init__(self, x: float, y: float):
self.x = x
self.y = y
def __getitem__(self, key):
assert type(key) == int and 0 <= key <= 1
return (self.x, self.y, self.z)[key]
def __setitem__(self, key, newvalue):
assert type(key) == int and 0 <= key <= 1
assert type(newvalue) == float
if key == 0:
self.x = newvalue
if key == 1:
self.y = newvalue
def __len__(self):
return 2
@staticmethod
def new():
return Vector2(0, 0)
class Quaternion:
def __init__(self, w: float, x: float, y: float, z: float):
self.w = w
self.x = x
self.y = y
self.z = z
def __getitem__(self, key):
assert type(key) == int and 0 <= key <= 3
return (self.w, self.x, self.y, self.z)[key]
def __setitem__(self, key, newvalue):
assert type(key) == int and 0 <= key <= 3
assert type(newvalue) == float
if key == 0:
self.w = newvalue
if key == 1:
self.x = newvalue
if key == 2:
self.y = newvalue
if key == 3:
self.z = newvalue
def __len__(self):
return 4
@staticmethod
def new():
return Quaternion(0, 0, 0, 0)
@staticmethod
def from_arr(i: Union[List, Tuple]):
return Quaternion(i[0], i[1], i[2], i[3])