Author: JonNeal
Date: 2008-03-11 20:40:18 -0400 (Tue, 11 Mar 2008)
New Revision: 1136

Modified:
   trunk/pysoy/src/_datatypes/BodyPosition.pxi
Log:
Ticket #946: More math support


Modified: trunk/pysoy/src/_datatypes/BodyPosition.pxi
===================================================================
--- trunk/pysoy/src/_datatypes/BodyPosition.pxi 2008-03-11 23:55:17 UTC (rev 
1135)
+++ trunk/pysoy/src/_datatypes/BodyPosition.pxi 2008-03-12 00:40:18 UTC (rev 
1136)
@@ -48,17 +48,190 @@
       return soy._datatypes.BodyPosition((_position[0] + value,
                                           _position[1] + value,
                                           _position[2] + value))
-    if isinstance(value, soy._datatypes.BodyPosition) :
-      return soy._datatypes.BodyPosition((_position[0] + value.x,
-                                          _position[1] + value.y,
-                                          _position[2] + value.z))
-    if type(value) == tuple or type(value) == list and len(value) == 3 :
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
       return soy._datatypes.BodyPosition((_position[0] + value[0],
                                           _position[1] + value[1],
                                           _position[2] + value[2]))
     return NotImplemented
-    
 
+  def __radd__(BodyPosition self, value) :
+    return self+value
+
+  def __sub__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((_position[0] - value,
+                                          _position[1] - value,
+                                          _position[2] - value))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((_position[0] - value[0],
+                                          _position[1] - value[1],
+                                          _position[2] - value[2]))
+    return NotImplemented
+
+  def __rsub__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((value - _position[0],
+                                          value - _position[1],
+                                          value - _position[2]))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((value[0] - _position[0],
+                                          value[1] - _position[1],
+                                          value[2] - _position[2]))
+    return NotImplemented
+
+  def __div__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((_position[0] / value,
+                                          _position[1] / value,
+                                          _position[2] / value))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((_position[0] / value[0],
+                                          _position[1] / value[1],
+                                          _position[2] / value[2]))
+    return NotImplemented
+
+  def __rdiv__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((value / _position[0],
+                                          value / _position[1],
+                                          value / _position[2]))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((value[0] / _position[0],
+                                          value[1] / _position[1],
+                                          value[2] / _position[2]))
+    return NotImplemented
+
+  def __mul__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((_position[0] * value,
+                                          _position[1] * value,
+                                          _position[2] * value))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((_position[0] * value[0],
+                                          _position[1] * value[1],
+                                          _position[2] * value[2]))
+    return NotImplemented
+  
+  def __rmul__(BodyPosition self, value) :
+    return self*value
+
+  def __mod__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((_position[0] % value,
+                                          _position[1] % value,
+                                          _position[2] % value))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((_position[0] % value[0],
+                                          _position[1] % value[1],
+                                          _position[2] % value[2]))
+    return NotImplemented
+
+  def __rmod__(BodyPosition self, value) :
+    cdef float* _position
+    #
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if type(value) == int or type(value) == float :
+      return soy._datatypes.BodyPosition((value % _position[0],
+                                          value % _position[1],
+                                          value % _position[2]))
+    if type(value) == tuple or type(value) == list and len(value) == 3 \
+       or isinstance(value, soy._datatypes.BodyPosition) :
+      return soy._datatypes.BodyPosition((value[0] % _position[0],
+                                          value[1] % _position[1],
+                                          value[2] % _position[2]))
+
+  def __richcmp__(BodyPosition self, BodyPosition that, oper) :
+    if not isinstance(that, soy._datatypes.BodyPosition) :
+      return NotImplemented
+    cdef float* _position, _that_position[3]
+    cdef int _x, _y, _z
+    if self._body == NULL :
+      _position = self._position
+    else :
+      _position = (<soy.bodies.Body> self._body)._position
+    if self._position == that._position :
+      stdio.printf("Yay")
+    else :
+      stdio.printf("%f \n",(<soy.bodies.Body> that._body)._position)
+    if that._body == NULL :
+      _that_position = that._position
+    else :
+      _that_position = (<soy.bodies.Body> that._body)._position
+    _x = (_position[0] <= _that_position[0],
+          _position[0] == _that_position[0],
+          _position[0] == _that_position[0],
+          _position[0] >= _that_position[0])[oper]
+    _y = (_position[1] <= _that_position[1],
+          _position[1] == _that_position[1],
+          _position[1] == _that_position[1],
+          _position[1] >= _that_position[1])[oper]
+    _z = (_position[2] <= _that_position[2],
+          _position[2] == _that_position[2],
+          _position[2] == _that_position[2],
+          _position[2] >= _that_position[2])[oper]
+    if oper == 3 :
+      return (True, False)[_x&_y&_z]
+    else :
+      return (False, True)[_x&_y&_z]
+
+  def __neg__(BodyPosition self) :
+    if self > 0 :
+      return self *-1
+    else :
+      return self
+
+  def __pos__(BodyPosition self) :
+    if self < 0 :
+      return self *-1
+    else :
+     return self
+
   def __getitem__(self, index) :
     if type(index) != int :
       raise TypeError('Position index must be an int')
@@ -92,7 +265,7 @@
       raise TypeError('Position index must be an int')
     if index > 2 or index < 0 :
       raise IndexError('Position index out of range')
-    if self._body == NULL :
+    if self._body :
       if (<soy.bodies.Body> self._body)._bodyID == NULL :
         #
         # If not in a scene set value directly
@@ -118,7 +291,6 @@
                                value)
         (<soy.scenes.Scene> (<soy.bodies.Body> 
self._body)._scene)._stepUnLock()
     else :
-      stdio.printf("Test 1\n")
       self._position[index] = value
 
 

_______________________________________________
PySoy-SVN mailing list
PySoy-SVN@pysoy.org
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to