-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector3D.py
More file actions
402 lines (291 loc) · 10.5 KB
/
Vector3D.py
File metadata and controls
402 lines (291 loc) · 10.5 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Vector3D.py
Vector3D class.
"""
#from __future__ import annotations
import math
from Point3D import Point3D
from multimethod import multimethod
#from Overload_by_class import Overload_by_class
from typing import Union
Numeric = Union[float, int]
# >>> v1=Vector3D(1.1,2.2,3.3)
# Vector3D constructor #
#class Vector3D(object):
class Vector3D:
'''Construct an object Vector3D.'''
# >> v2=Vector3D()
# # Vector3D of float constructor #
# >>> v2
# Vector3D @ 0x7fee25c54790 [0.0,0.0,0.0]
# >>> v1=Vector3D(1.1,2.2,3.3)
# # Vector3D of Numeric constructor #
# >>> v1
# Vector3D @ 0x7fee25c47cd0 [1.1,2.2,3.3]
@multimethod
def __init__(self):
if __debug__:
print("# Vector3D of float constructor #")
self.x = 0.0
self.y = 0.0
self.z = 0.0
@multimethod
def __init__(self,x : Numeric,y : Numeric,z : Numeric):
if __debug__:
print("# Vector3D of Numeric constructor #")
self.x = x
self.y = y
self.z = z
# create vector AB with points A and B
#v=Vector3D(Point3D(1.0,1.0,1.0),Point3D(3.0,3.0,3.0))
# Point3D constructor #
# Point3D constructor #
# Vector3D (Point3D,Point3D)constructor #
#>>> v
#Vector3D @ 0x7fee274d4fd0 [2.0,2.0,2.0]
@multimethod
def __init__(self,a : Point3D,b : Point3D):
if __debug__:
print("# Vector3D (Point3D,Point3D)constructor #")
self.x = b.x - a.x
self.y = b.y - a.y
self.z = b.z - a.z
#v=Vector3D((1,2,3),(4,5,6))
# Vector3D (tuple,tuple) constructor #
#>>> v
#Vector3D @ 0x7fee2655ced0 [3,3,3]
@multimethod
def __init__(self,a : tuple,b : tuple):
if __debug__:
print("# Vector3D (tuple,tuple) constructor #")
(ax,ay,az)=a
(bx,by,bz)=b
self.x = bx - ax
self.y = by - ay
self.z = bz - az
# v1
# Vector3D @ 0x7f7ded7869d0 [1,2.2,3.3]
def __repr__(self):
return 'Vector3D @ {} [{},{},{}]'.format(hex(id(self)),self.x,self.y,self.z)
# print(v1)
# [1,2.2,3.3]
def __str__(self):
return '[{},{},{}]'.format(self.x,self.y,self.z)
# >>> v1=Vector3D(1,2.2,3.3)
# # Vector3D constructor #
# >>> v2=Vector3D(2,2.2,3.3)
# # Vector3D constructor #
# >>> v1bis=Vector3D(1,2.2,3.3)
# # Vector3D constructor #
# >>> v1 is v2
# False
# >>> v1 == v2
# False
# >>> v1 == v1bis
# True
# >>> v1 is v1bis
# False
#>>> v1=Vector3D(1,2.2,3.3)
# Vector3D constructor #
#>>> v1
#Vector3D @ 0x104a058d0 [1,2.2,3.3]
#>>> v2=v1
#>>> v2
#Vector3D @ 0x104a058d0 [1,2.2,3.3]
#>>> v1 is v2
#True
#>>> v1 == v2
#True
def __eq__(self, other):
if not isinstance(other, Vector3D):
# don't attempt to compare against unrelated types
return NotImplemented
return self.x == other.x and self.y == other.y and self.z == other.z
# >>> p1=Point3D(1.0,2.2,3.3)
# # Point3D constructor #
# >>> p2=Point3D(1.0,3.2,4.3)
# # Point3D constructor #
# >>> v1=Vector3D.set_with_points_backup(p1,p2)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# # Vector3D constructor #
# >>> v1
# Vector3D @ 0x7feed23f6b50 [0.0,1.0,1.0]
# DEPRECATED
@classmethod
def set_with_points_backup(cls,a,b):
ax = a.x
ay = a.y
az = a.z
bx = b.x
by = b.y
bz = b.z
x = bx - ax
y = by - ay
z = bz - az
return cls(x,y,z)
# >>> p1=Point3D()
# # Point3D constructor #
# >>> p2=Point3D()
# # Point3D constructor #
# >>> v2=Vector3D(10.0,2.0,3.0)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# # Vector3D constructor #
# >>> v=Vector3D(1.0,2.0,3.0)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# # Vector3D constructor #
# >>> v.set_with_points(p1,p2)
# >>> v
# Vector3D @ 0x7f328e743450 [0.0,0.0,0.0]
# >>> v=Vector3D(1.0,2.0,3.0)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# # Vector3D constructor #
# >>> v
# Vector3D @ 0x7f328e743b50 [1.0,2.0,3.0]
# >>> v.set_with_points(p1,p2)
# >>> v
# Vector3D @ 0x7f328e743b50 [0.0,0.0,0.0]
def set_with_points(self,a,b):
ax = a.x
ay = a.y
az = a.z
bx = b.x
by = b.y
bz = b.z
self.x = bx - ax
self.y = by - ay
self.z = bz - az
# overload + operator
# >>> v1+v2
# Vector3D constructor #
#Vector3D @ 0x7f7dec6d3390 [2,0.0,6.3]
def __add__(self,v):
return Vector3D(self.x + v.x,
self.y + v.y,
self.z + v.z)
# multiplications
# >>> v1=Vector3D(1.1,2.2,3.3)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# # Vector3D constructor #
# >>> v1*3.0
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __mul__
# OverloadByClass : wrapped_function : key = ('__mul__', ('Vector3D', 'float'))
# Vector3D.py : __mul__ ('Vector3D', float)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# # Vector3D constructor #
# Vector3D @ 0x7f2159337d50 [3.3000000000000003,6.6000000000000005,9.899999999999999]
@multimethod
def __mul__(self,m : Numeric): # self * m
if __debug__:
print("Vector3D.py : __mul__ (Vector3D, Numeric)")
# number multiplication
return Vector3D(self.x * m,
self.y * m,
self.z * m)
# dot product - produit scalaire
# >>> v1=Vector3D(1.1,2.2,3.3)
# # Vector3D of Numeric constructor #
# >>> v2=Vector3D(2.1,2.7,-3.3)
# # Vector3D of Numeric constructor #
# >>> v1*v2
# Vector3D.py : __mul__(Vector3D, 'Vector3D')
# -2.639999999999997
@multimethod
def __mul__(self,m : object): #"Vector3D"): # self * m
if __debug__:
print("Vector3D.py : __mul__(Vector3D, 'Vector3D')")
# dot product - produit scalaire
return self.x * m.x + self.y * m.y + self.z * m.z
# right multiplication (non commutative)
# >>> v1=Vector3D(1.1,2.2,3.3)
# # Vector3D of Numeric constructor #
# >>> m3d = [[1 , 2 , 3],[4 , 5, 6],[7, 8, 9]]
# >>> m3d*v1
# Vector3D.py : __rmul__(Vector3D , list)
# # Vector3D of Numeric constructor #
# Vector3D @ 0x7fee25c47cd0 [15.399999999999999,35.199999999999996,55.0]
# m : multiplicand ,list matrix,....
@multimethod
def __rmul__(self, m : list): # self is at RIGHT of multiplication operand : m * self
if __debug__:
print("Vector3D.py : __rmul__(Vector3D , list)")
x = self.x
y = self.y
z = self.z
return Vector3D(m[0][0] * x + m[0][1] * y + m[0][2] * z,
m[1][0] * x + m[1][1] * y + m[1][2] * z,
m[2][0] * x + m[2][1] * y + m[2][2] * z)
# >>> v1=Vector3D(1.1,2.2,3.3)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# Vector3D constructor #
# >>> 2.0*v1
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __rmul__
# OverloadByClass : wrapped_function : key = ('__rmul__', ('Vector3D', 'float'))
# Vector3D.py : __rmul__('Vector3D',float)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __mul__
# OverloadByClass : wrapped_function : key = ('__mul__', ('Vector3D', 'float'))
# Vector3D.py : __mul__ ('Vector3D', float)
# OverloadByClass.py : Inside wrapped_function()
# OverloadByClass : wrapped_function : name = __init__
# OverloadByClass : wrapped_function : key = ('__init__', ('Vector3D', 'float', 'float', 'float'))
# Vector3D constructor #
# Vector3D @ 0x7f80060e0d10 [2.2,4.4,6.6]
@multimethod
def __rmul__(self, m : Numeric): # self is at RIGHT of multiplication operand : m * self
if __debug__:
print("Vector3D.py : __rmul__(Vector3D,Numeric)")
return self * m # self is at LEFT of operand *
# overload / operator
def __truediv__(self,d):
x = self.x / d
y = self.y / d
z = self.z / d
return Vector3D(x,y,z)
# overload += operator
# >>> v1=Vector3D(1,2.2,3.3)
# # Vector3D constructor #
# >>> v2=Vector3D(2,2.5,3.7)
# # Vector3D constructor #
# >>> v1+=v2
# >>> v1
# Vector3D @ 0x7fa71ff35490 [3,4.7,7.0]
def __iadd__(self,v):
self.x += v.x
self.y += v.y
self.z += v.z
return self
# cross product - produit vectoriel
# operator syntax: v ^ v2
# WARNING: this is not XOR but CROSS PRODUCT
# WARNING: cross product is not associative:
# v1 ^ v2 ^ v3 = (v1 ^ v2) ^ v3 ≠ v1 ^ (v2 ^ v3)
def __xor__(self,v):
x = self.x
y = self.y
z = self.z
return Vector3D(y * v.z - z * v.y,
z * v.x - x * v.z,
x * v.y - y * v.x)
# >>> v1.norm()
# Vector3D.py : __mul__
# 4.090232267243512
def norm(self):
return math.sqrt(self * self)