forked from SarielD/mMath
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvec.py
213 lines (143 loc) · 4.68 KB
/
vec.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
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
import math
class Vec (object):
"""
This is a vector class that works in a friendly maya way, aka you can
get a translation value and initialize directly a Vec class
"""
def __init__(self, values):
"""
This is the constructor
Args:
:values: list[float], the values to initialize the vector with
"""
self.values = values
def as_list(self):
"""
This function returns the content of the vector as a list
This function is kept moslty for consistency with the other vector
class, keeping a uniform and consistant class interface
"""
return self.values
def dot(self , V):
"""
This procedure performs a dot product with the given vector
Args:
:V: Vec, second vector to perform the operation with
:return: float
"""
return sum([self.values[x] * V[x] for x in range(len(self))])
def cross(self , V):
"""
This procedure performs the cross product on the two vectors ,
the two vectors need lenght 3
Args:
:V: Vec, second vector to perform the operation with
:return: Vec instance
"""
assert len(self) == 3 and len(V) == 3
A = self.values
x = (A[1] *V[2]) - (A[2] *V[1])
y = (A[2] *V[0]) - (A[0] *V[2])
z = (A[0] *V[1]) - (A[1] *V[0])
return Vec([x,y,z])
def length(self):
"""
Return the magnitude of the vector
:return: float
"""
return math.sqrt(sum(x*x for x in self.values))
def normalize(self):
"""
Return the normalized image of the class
:return: Vec instance
"""
tempLen = self.length()
return Vec([x/tempLen for x in self.values])
def __getitem__ (self , index):
"""
The indexing operator
Args:
:index: the index to acces
"""
return self.values[index]
def __setitem__ (self , index , value):
"""
This procedure allow item set with indexing
Args:
:index: int , the index to access
:value: float , the value To set to given index
"""
self.values[index] = value
def __neg__ (self):
"""
This procedure returns the negation of the vector
:return: Vec instance
"""
return Vec([ -1.0 * x for x in self.values])
def __eq__ (self , V):
"""
This procedure checks if this vector is equal to the given one
Args:
:V: Vec() , the vector to check agains
:return : bool
"""
for i,v in enumerate(V) :
if self.values[i] != v :
return False
return True
def __radd__(self, other):
"Hack to allow sum(...) to work with vectors"
if other == 0:
return self
def __len__(self):
"""
This procedure returns the length of the vector
:return: int
"""
return len(self.values)
def __add__(self ,V):
"""
This procedure sums the two vectors togheter
Args:
:V: Vec() the vector to sum
:return : Vec()
"""
assert len(self) == len(V)
return Vec([self.values[x] + V[x] for x in range(len(self))])
def __sub__(self ,V):
"""
This procedure subtracts two vectors
Args:
:V: Vec() the vector to subtract
:return : Vec()
"""
return self +(-V)
def __str__(self):
"""
Pretty print of the class
"""
return str([x for x in self.values])
def __mul__(self , V):
"""
This procedure implements the * operator performing a dot product
Args:
:V: Vec() , the other vector to use for the computation\
:return: Vec()
"""
return self.dot(V)
def __rmul__ (self, scal):
"""
Right multiplication , assuming int or float , performing scalar multiplication
Args:
:scal: int/float , scalar value for the operation
:return: Vec instance
"""
return Vec([ x * scal for x in self.values])
def __xor__ (self , V):
"""
Cross product operator
Args:
:V: Vec, the second vector for the operation
:return: Vec
"""
return self.cross(V)