-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuaternion_helper.py
More file actions
62 lines (42 loc) · 2.01 KB
/
Copy pathQuaternion_helper.py
File metadata and controls
62 lines (42 loc) · 2.01 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
from typing import overload
class Quaternion:
def __init__(self, a, b, c, d) -> None:
self.a = a
self.b = b
self.c = c
self.d = d
def conj(self):
return Quaternion(self.a, -self.b, -self.c, -self.d)
def toPoint(self):
if(not abs(self.a) < 0.01 ):
raise Exception("Cannot convert Quaternion to a Point. Real part is not 0")
return Point3D(self.b, self.c, self.d)
def __add__(self, other):
return Quaternion(self.a + other.a, self.b + other.b, self.c + other.c, self.d + other.d)
def __sub__(self, other):
return Quaternion(self.a - other.a, self.b - other.b, self.c - other.c, self.d - other.d)
def __mul__(self, other):
return Quaternion(self.a * other.a - self.b * other.b - self.c * other.c - self.d * other.d,
(self.a * other.b + self.b * other.a + self.c * other.d - self.d * other.c),
(self.a * other.c - self.b * other.d + self.c * other.a + self.d * other.b),
(self.a * other.d + self.b * other.c - self.c * other.b + self.d * other.a))
def __str__(self) -> str:
return f"({f'{self.a} + ' if not self.a == 0 else ''}{self.b}i + {self.c}j + {self.d}k)"
def __repr__(self) -> str:
return f"({f'{self.a} + ' if not self.a == 0 else ''}{self.b}i + {self.c}j + {self.d}k)"
class Point3D(Quaternion):
def __init__(self, x, y, z) -> None:
super().__init__(0, x, y, z)
self.x = x
self.y = y
self.z = z
def transform(self, quaternion):
return (quaternion*self*quaternion.conj()).toPoint()
def __add__(self, other):
return Point3D(self.x + other.x, self.y + other.y, self.z + other.z)
def __sub__(self, other):
return Point3D(self.x + other.x, self.y + other.y, self.z + other.z)
def __str__(self) -> str:
return f"({self.x}, {self.y}, {self.z})"
def __repr__(self) -> str:
return f"({self.x}, {self.y}, {self.z})"