-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMTVector.py
More file actions
158 lines (133 loc) · 4.92 KB
/
Copy pathMTVector.py
File metadata and controls
158 lines (133 loc) · 4.92 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
#-------------------------------------------------------------
#-- vector 3 class
#-------------------------------------------------------------
from math import *
def AutoFloatProperties( *props ):
'''metaclass'''
class _AutoFloatProperties( type ):
def __init__( cls, name, bases, cdict ):
super( _AutoFloatProperties, cls ).__init__( name, bases, cdict )
for attr in props:
def fget( self, _attr='_'+attr ): return getattr( self, _attr )
def fset( self, value, _attr='_'+attr ): setattr( self, _attr, float( value ) )
setattr( cls, attr, property( fget, fset ) )
return _AutoFloatProperties
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
class MTVector:
__metaclass__ = AutoFloatProperties( 'x','y','z' )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __init__( self, x, y, z ):
self.x = x
self.y = y
self.z = z
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def Clone( self ):
return MTVector( self.x, self.y, self.z )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __eq__( self, other ):
return ( ( self.x == other.x ) and ( self.y == other.y ) and ( self.z == other.z ) )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __ne__( self, other ):
return not self.__eq__( self, other )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __add__( self, other ):
return MTVector( self.x + other.x, self.y + other.y, self.z + other.z )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __iadd__( self, other ):
self.x += other.x
self.y += other.y
self.z += other.z
return self
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __sub__( self, other ):
return MTVector( self.x - other.x, self.y - other.y, self.z - other.z )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __isub__( self, other ):
self.x -= other.x
self.y -= other.y
self.z -= other.z
return self
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __mul__( self, other ):
if ( isinstance( other, MTVector ) ):
return MTVector( self.x * other.x, self.y * other.y, self.z * other.z )
return MTVector( self.x * other, self.y * other, self.z * other )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __imul__( self, other ):
if ( isinstance( other, MTVector ) ):
self.x *= other.x
self.y *= other.y
self.z *= other.z
else:
self.x *= other
self.y *= other
self.z *= other
return self
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __div__( self, other ):
if ( isinstance( other, MTVector ) ):
return MTVector( self.x / other.x, self.y / other.y, self.z / other.z )
return MTVector( self.x / other, self.y / other, self.z / other )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __idiv__( self, other ):
if ( isinstance( other, MTVector ) ):
self.x /= other.x
self.y /= other.y
self.z /= other.z
else:
self.x /= other
self.y /= other
self.z /= other
return self
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def Len( self ):
return sqrt( self.x * self.x + self.y * self.y + self.z * self.z )
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def Normalize( self ):
len = self.Len()
if ( len != 0 ):
self.x /= len
self.y /= len
self.z /= len
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def MakeZero( self ):
self.x = 0
self.y = 0
self.z = 0
#-------------------------------------------------------------
#--
#-------------------------------------------------------------
def __str__( self ):
return "( " + str( self.x ) + "; " + str( self.y ) + "; " + str( self.z ) + " )"