@@ -31,9 +31,9 @@ cdef class vector(object):
31
31
self ._z = args[2 ]
32
32
elif len (args) == 1 and isinstance (args[0 ], vector): # make a copy of a vector
33
33
other = args[0 ]
34
- self ._x = other.x
35
- self ._y = other.y
36
- self ._z = other.z
34
+ self ._x = other._x
35
+ self ._y = other._y
36
+ self ._z = other._z
37
37
else :
38
38
raise TypeError (' A vector needs 3 components.' )
39
39
self .on_change = self .ignore
@@ -43,14 +43,14 @@ cdef class vector(object):
43
43
44
44
property value :
45
45
def __get__ (self ):
46
- return [self .x , self .y , self .z ]
46
+ return [self ._x , self ._y , self ._z ]
47
47
def __set__ (self , other ):
48
- self ._x = other.x
49
- self ._y = other.y
50
- self ._z = other.z
48
+ self ._x = other._x
49
+ self ._y = other._y
50
+ self ._z = other._z
51
51
52
52
def __neg__ (self ): # # seems like this must come before properties (???)
53
- return vector(- self .x , - self .y , - self .z )
53
+ return vector(- self ._x , - self ._y , - self ._z )
54
54
55
55
def __pos__ (self ):
56
56
return self
@@ -62,24 +62,34 @@ cdef class vector(object):
62
62
return ' <{:.6g}, {:.6g}, {:.6g}>' .format(self ._x, self ._y, self ._z)
63
63
64
64
def __add__ (self ,other ):
65
- return vector(self .x + other.x , self .y + other.y , self .z + other.z )
65
+ return vector(self ._x + other._x , self ._y + other._y , self ._z + other._z )
66
66
67
67
def __truediv__ (self , other ): # Python 3, or Python 2 + future division
68
68
if isinstance (other, (int , float )):
69
- return vector(self .x / other, self .y / other, self .z / other)
69
+ return vector(self ._x / other, self ._y / other, self ._z / other)
70
70
raise TypeError (' a vector can only be divided by a scalar' )
71
71
72
72
def __sub__ (self ,other ):
73
- return vector(self .x - other.x , self .y - other.y , self .z - other.z )
73
+ return vector(self ._x - other._x , self ._y - other._y , self ._z - other._z )
74
74
75
75
def __mul__ (self , other ): # # in cython order of arguments is arbitrary, rmul doesn't exist
76
76
if isinstance (other, (int , float )):
77
- return vector(self .x * other, self .y * other, self . z * other)
77
+ return vector(self ._x * other, self ._y * other, self ._z * other)
78
78
elif isinstance (self , (int , float )):
79
- return vector(self * other.x , self * other.y , self * other.z )
79
+ return vector(self * other._x , self * other._y , self * other._z )
80
80
else :
81
81
raise TypeError (' a vector can only be multiplied by a scalar' , self , other)
82
82
83
+ def __eq__ (self ,other ):
84
+ if type (self ) is vector and type (other) is vector:
85
+ return self .equals(other)
86
+ return False
87
+
88
+ def __ne__ (self ,other ):
89
+ if type (self ) is vector and type (other) is vector:
90
+ return not self .equals(other)
91
+ return True
92
+
83
93
property x :
84
94
def __get__ (self ):
85
95
return self ._x
@@ -103,18 +113,18 @@ cdef class vector(object):
103
113
104
114
property mag :
105
115
def __get__ (self ):
106
- return sqrt(self .x ** 2 + self .y ** 2 + self .z ** 2 )
116
+ return sqrt(self ._x ** 2 + self ._y ** 2 + self ._z ** 2 )
107
117
def __set__ (self , value ):
108
118
cdef vector normA
109
119
normA = self .hat
110
- self .x = value * normA.x
111
- self .y = value * normA.y
112
- self .z = value * normA.z
120
+ self .x = value * normA._x
121
+ self .y = value * normA._y
122
+ self .z = value * normA.vz
113
123
self .on_change()
114
124
115
125
property mag2 :
116
126
def __get__ (self ):
117
- return (self .x ** 2 + self .y ** 2 + self .z ** 2 )
127
+ return (self ._x ** 2 + self ._y ** 2 + self ._z ** 2 )
118
128
def __set__ (self , value ):
119
129
cdef double v
120
130
v = sqrt(value)
@@ -134,30 +144,30 @@ cdef class vector(object):
134
144
smag = self .mag
135
145
cdef vector normA
136
146
normA = value.hat
137
- self .x = smag * normA.x
138
- self .y = smag * normA.y
139
- self .z = smag * normA.z
147
+ self .x = smag * normA._x
148
+ self .y = smag * normA._y
149
+ self .z = smag * normA._z
140
150
self .on_change()
141
151
142
152
143
153
cpdef vector norm(self ):
144
154
return self .hat
145
155
146
156
cpdef double dot(self ,other):
147
- return ( self .x * other.x + self .y * other.y + self .z * other.z )
157
+ return ( self ._x * other._x + self ._y * other._y + self ._z * other._z )
148
158
149
159
cpdef vector cross(self ,other):
150
- return vector( self .y * other.z - self .z * other.y ,
151
- self .z * other.x - self .x * other.z ,
152
- self .x * other.y - self .y * other.x )
160
+ return vector( self ._y * other._z - self ._z * other._y ,
161
+ self ._z * other._x - self ._x * other._z ,
162
+ self ._x * other._y - self ._y * other._x )
153
163
154
164
cpdef vector proj(self ,other):
155
165
cdef vector normB
156
166
normB = other.hat
157
167
return self .dot(normB) * normB
158
168
159
169
cpdef bint equals(self ,other):
160
- return ( self .x == other.x and self .y == other.y and self .z == other.z )
170
+ return ( self ._x == other._x and self ._y == other._y and self ._z == other._z )
161
171
162
172
cpdef double comp(self ,other): # # result is a scalar
163
173
cdef vector normB
@@ -175,16 +185,16 @@ cdef class vector(object):
175
185
176
186
cpdef vector rotate(self , double angle = 0. , vector axis = None ):
177
187
cdef vector u
178
- if axis == None :
188
+ if axis is None :
179
189
u = vector(0 ,0 ,1 )
180
190
else :
181
191
u = axis.hat
182
192
cdef double c = cos(angle)
183
193
cdef double s = sin(angle)
184
194
cdef double t = 1.0 - c
185
- cdef double x = u.x
186
- cdef double y = u.y
187
- cdef double z = u.z
195
+ cdef double x = u._x
196
+ cdef double y = u._y
197
+ cdef double z = u._z
188
198
cdef double m11 = t* x* x+ c
189
199
cdef double m12 = t* x* y- z* s
190
200
cdef double m13 = t* x* z+ y* s
@@ -194,25 +204,25 @@ cdef class vector(object):
194
204
cdef double m31 = t* x* z- y* s
195
205
cdef double m32 = t* y* z+ x* s
196
206
cdef double m33 = t* z* z+ c
197
- cdef double sx = self .x
198
- cdef double sy = self .y
199
- cdef double sz = self .z
207
+ cdef double sx = self ._x
208
+ cdef double sy = self ._y
209
+ cdef double sz = self ._z
200
210
return vector( (m11* sx + m12* sy + m13* sz),
201
211
(m21* sx + m22* sy + m23* sz),
202
212
(m31* sx + m32* sy + m33* sz) )
203
213
204
214
cpdef rotate_in_place(self , double angle = 0. , vector axis = None ):
205
215
cdef vector u
206
- if axis == None :
216
+ if axis is None :
207
217
u = vector(0 ,0 ,1 )
208
218
else :
209
219
u = axis.hat
210
220
cdef double c = cos(angle)
211
221
cdef double s = sin(angle)
212
222
cdef double t = 1.0 - c
213
- cdef double x = u.x
214
- cdef double y = u.y
215
- cdef double z = u.z
223
+ cdef double x = u._x
224
+ cdef double y = u._y
225
+ cdef double z = u._z
216
226
cdef double m11 = t* x* x+ c
217
227
cdef double m12 = t* x* y- z* s
218
228
cdef double m13 = t* x* z+ y* s
@@ -222,9 +232,9 @@ cdef class vector(object):
222
232
cdef double m31 = t* x* z- y* s
223
233
cdef double m32 = t* y* z+ x* s
224
234
cdef double m33 = t* z* z+ c
225
- cdef double sx = self .x
226
- cdef double sy = self .y
227
- cdef double sz = self .z
235
+ cdef double sx = self ._x
236
+ cdef double sy = self ._y
237
+ cdef double sz = self ._z
228
238
self ._x = m11* sx + m12* sy + m13* sz
229
239
self ._y = m21* sx + m22* sy + m23* sz
230
240
self ._z = m31* sx + m32* sy + m33* sz
@@ -234,9 +244,9 @@ cpdef object_rotate(vector objaxis, vector objup, double angle, vector axis):
234
244
cdef double c = cos(angle)
235
245
cdef double s = sin(angle)
236
246
cdef double t = 1.0 - c
237
- cdef double x = u.x
238
- cdef double y = u.y
239
- cdef double z = u.z
247
+ cdef double x = u._x
248
+ cdef double y = u._y
249
+ cdef double z = u._z
240
250
cdef double m11 = t* x* x+ c
241
251
cdef double m12 = t* x* y- z* s
242
252
cdef double m13 = t* x* z+ y* s
@@ -246,15 +256,15 @@ cpdef object_rotate(vector objaxis, vector objup, double angle, vector axis):
246
256
cdef double m31 = t* x* z- y* s
247
257
cdef double m32 = t* y* z+ x* s
248
258
cdef double m33 = t* z* z+ c
249
- cdef double sx = objaxis.x
250
- cdef double sy = objaxis.y
251
- cdef double sz = objaxis.z
259
+ cdef double sx = objaxis._x
260
+ cdef double sy = objaxis._y
261
+ cdef double sz = objaxis._z
252
262
objaxis._x = m11* sx + m12* sy + m13* sz # avoid creating a new vector object
253
263
objaxis._y = m21* sx + m22* sy + m23* sz
254
264
objaxis._z = m31* sx + m32* sy + m33* sz
255
- sx = objup.x
256
- sy = objup.y
257
- sz = objup.z
265
+ sx = objup._x
266
+ sy = objup._y
267
+ sz = objup._z
258
268
objup._x = m11* sx + m12* sy + m13* sz
259
269
objup._y = m21* sx + m22* sy + m23* sz
260
270
objup._z = m31* sx + m32* sy + m33* sz
@@ -294,7 +304,7 @@ cpdef vector rotate(vector A, double angle = 0., vector axis = None):
294
304
cpdef vector adjust_up(vector oldaxis, vector newaxis, vector up, vector save_oldaxis): # adjust up when axis is changed
295
305
cdef double angle
296
306
cdef vector rotaxis
297
- if abs (newaxis.x ) + abs (newaxis.y ) + abs (newaxis.z ) == 0 :
307
+ if abs (newaxis._x ) + abs (newaxis._y ) + abs (newaxis._z ) == 0 :
298
308
# If axis has changed to <0,0,0>, must save the old axis to restore later
299
309
if save_oldaxis is None : save_oldaxis = oldaxis
300
310
return save_oldaxis
@@ -321,7 +331,7 @@ cpdef vector adjust_up(vector oldaxis, vector newaxis, vector up, vector save_ol
321
331
cpdef vector adjust_axis(vector oldup, vector newup, vector axis, vector save_oldup): # adjust axis when up is changed
322
332
cdef double angle
323
333
cdef vector rotaxis
324
- if abs (newup.x ) + abs (newup.y ) + abs (newup.z ) == 0 :
334
+ if abs (newup._x ) + abs (newup._y ) + abs (newup._z ) == 0 :
325
335
# If up will be set to <0,0,0>, must save the old up to restore later
326
336
if save_oldup is None : save_oldup = oldup
327
337
return save_oldup
0 commit comments