forked from mamo91/Dongleless-myo
-
Notifications
You must be signed in to change notification settings - Fork 3
/
vector.py
131 lines (104 loc) · 3.87 KB
/
vector.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
# Copyright (c) 2015 Niklas Rosenstein
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Provides a 3 dimensional vector class.
"""
import math
class Vector(object):
__slots__ = ("x", "y", "z")
def __init__(self, x=0.0, y=0.0, z=0.0):
super(Vector, self).__init__()
self.x = float(x)
self.y = float(y)
self.z = float(z)
def __mul__(self, rhs):
"""
Multiplies the vector with *rhs* which can be either a scalar
to retrieve a new Vector or another vector to compute the dot
product.
"""
if isinstance(rhs, (int, float)):
return Vector(self.x * rhs, self.y * rhs, self.z * rhs)
else:
return self.dot(rhs)
def __add__(self, rhs):
"""
Adds *self* to *rhs* and returns a new vector.
"""
if isinstance(rhs, (int, float)):
return Vector(self.x + rhs, self.y + rhs, self.z + rhs)
else:
return Vector(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z)
def __sub__(self, rhs):
"""
Substracts *self* from *rhs* and returns a new vector.
"""
if isinstance(rhs, (int, float)):
return Vector(self.x - rhs, self.y - rhs, self.z - rhs)
else:
return Vector(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z)
def __iter__(self):
return iter((self.x, self.y, self.z))
def __repr__(self):
return "Vector({0}, {1}, {2})".format(
round(self.x, 4), round(self.y, 4), round(self.z, 4)
)
def __invert__(self):
"""
Returns the inversion of the vector.
"""
return Vector(-self.x, -self.y, -self.z)
def __getitem__(self, index):
return (self.x, self.y, self.z)[index]
def copy(self):
"""
Returns a shallow copy of the vector.
"""
return Vector(self.x, self.y, self.z)
def magnitude(self):
"""
Return the magnitude of this vector.
"""
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2)
def normalized(self):
"""
Returns a normalized copy of this vector.
"""
norm = self.magnitude()
return Vector(self.x / norm, self.y / norm, self.z / norm)
def dot(self, rhs):
"""
Return the dot product of this vector and *rhs*.
"""
return self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
def cross(self, rhs):
"""
Return the cross product of this vector and *rhs*.
"""
return Vector(
self.y * rhs.z - self.z * rhs.y,
self.z * rhs.x - self.x * rhs.z,
self.x * rhs.y - self.y * rhs.x,
)
def angle_to(self, rhs):
"""
Return the angle between this vector and *rhs* in radians.
"""
return math.acos(self.dot(rhs) / (self.magnitude() * rhs.magnitude()))