Author: ArcRiley
Date: 2008-03-04 14:16:25 -0500 (Tue, 04 Mar 2008)
New Revision: 1027

Modified:
   trunk/pysoy/src/bodies._bodies/Body.pxi
   trunk/pysoy/src/bodies._bodies/soy.bodies._bodies.pxd
   trunk/pysoy/src/bodies.fields/Buoyancy.pxi
   trunk/pysoy/src/bodies.fields/Field.pxi
   trunk/pysoy/src/meshes/Shape.pxi
   trunk/pysoy/src/shapes/Shape.pxi
Log:
Ticket #934 :
  * removed soy.shapes.Shape.body property
  * wrote new .shape property for Body
  * shapes SHOULD now be set/unset properly during scene change
  * This should conclude this ticket.


Modified: trunk/pysoy/src/bodies._bodies/Body.pxi
===================================================================
--- trunk/pysoy/src/bodies._bodies/Body.pxi     2008-03-04 18:57:05 UTC (rev 
1026)
+++ trunk/pysoy/src/bodies._bodies/Body.pxi     2008-03-04 19:16:25 UTC (rev 
1027)
@@ -31,7 +31,7 @@
     #
     # Preset to avoid threading bugs
     self._mesh  = NULL
-    self._shape = None
+    self._shape = NULL
     self._tags  = soy._datatypes.HashTable()
     #
     if scene == None :
@@ -116,16 +116,22 @@
       return self._scene
     #
     def __set__(self, soy.scenes.Scene _newscene) :
-      cdef ode.dReal  _store[25]
+      cdef ode.dReal   _store[25]
+      cdef ode.dGeomID _geomID
       #
+      # Preset _geomID for speed
+      if self._shape == NULL :
+        _geomID = NULL
+      else :
+        _geomID = (<soy.shapes.Shape> self._shape)._geomID
       if self._bodyID != NULL :
         (<soy.scenes.Scene> self._scene)._stepLock()
         self._copyTo(_store)
         self._remove()
         self._destroy()
-        if self._shape :
-          ode.dSpaceRemove((<soy.scenes.Scene> self._scene)._spaceID, 
-                           (<soy.shapes.Shape> self._shape)._geomID )
+        if _geomID != NULL :
+          ode.dGeomSetBody(_geomID, NULL)
+          ode.dSpaceRemove((<soy.scenes.Scene> self._scene)._spaceID, _geomID)
         (<soy.scenes.Scene> self._scene)._stepUnLock()
       else :
         self._copyTo(_store)
@@ -133,9 +139,9 @@
       self._scene = _newscene
       (<soy.scenes.Scene> self._scene)._stepLock()
       self._create()
-      if self._shape :
-        ode.dSpaceAdd((<soy.scenes.Scene> self._scene)._spaceID,
-                      (<soy.shapes.Shape> self._shape)._geomID )
+      if _geomID != NULL :
+        ode.dGeomSetBody(_geomID, self._bodyID)
+        ode.dSpaceAdd((<soy.scenes.Scene> self._scene)._spaceID, _geomID)
       self._copyFrom(_store)
       self._append()
       (<soy.scenes.Scene> self._scene)._stepUnLock()
@@ -295,17 +301,74 @@
     Defaults to None.
     '''
     def __get__(self) :
-      return self._shape
+      if self._shape == NULL :
+        return None
+      return <soy.shapes.Shape> self._shape
+
     def __set__(self, value) :
+      cdef ode.dBodyID      _bodyID
+      cdef ode.dGeomID      _geomID
+      cdef soy.scenes.Scene _scene
+      if not isinstance(value, soy.shapes.Shape) and value != None :
+        raise TypeError('Requires a Shape')
+      #
+      # If this body already has a shape, delete it now
+      del(self.shape)
+      #
+      # If we're setting the value to None, we're done now.
       if value == None :
-        self._shape = None
         return
-      if not isinstance(value, soy.shapes.Shape) :
-        raise TypeError('Requires a Shape')
-      # This call will make any needed changes to this object as well
-      (<soy.shapes.Shape> value).body = self
+      #
+      # Preset these values for speed
+      _geomID = (<soy.shapes.Shape> value)._geomID
+      _bodyID = <ode.dBodyID> ode.dGeomGetBody(_geomID)
+      #
+      # If this shape is already attached, detach it first
+      if _bodyID != NULL :
+        del((<Body> ode.dBodyGetData(_bodyID)).shape)
+      #
+      # Only bother with the rest of this if we're in a scene
+      if self._bodyID != NULL :
+        _scene = <soy.scenes.Scene> self._scene
+        _scene._stepLock()
+        ode.dSpaceAdd(_scene._spaceID, _geomID)
+        ode.dGeomSetBody(_geomID, self._bodyID)
+      # 
+      # Because self._shape is void* we handle INCREF/DECREF ourselves
+      self._shape = <void*> value
+      py.Py_INCREF(<soy.shapes.Shape> self._shape)
+      #
+      # If we're in a scene, unlock before we return
+      if self._bodyID != NULL :
+        _scene._stepUnLock()
 
+    def __del__(self) :
+      cdef ode.dGeomID      _geomID
+      cdef soy.scenes.Scene _scene
+      #
+      # We cannot delete a non-existant shape
+      if self._shape == NULL :
+        return
+      #
+      # Here's to cleaner code
+      _geomID = (<soy.shapes.Shape> self._shape)._geomID
+      #
+      # Only bother with the rest of this if we're in a scene
+      if self._bodyID != NULL :
+        _scene = <soy.scenes.Scene> self._scene
+        _scene._stepLock()
+        ode.dSpaceRemove(_scene._spaceID, _geomID)
+        ode.dGeomSetBody(_geomID, NULL)
+      # 
+      # Because self._shape is void* we handle INCREF/DECREF ourselves
+      py.Py_DECREF(<soy.shapes.Shape> self._shape)
+      self._shape = NULL
+      #
+      # If we're in a scene, unlock before we return
+      if self._bodyID != NULL :
+        _scene._stepUnLock()
 
+
   cdef void _create(self) :
     self._bodyID = ode.dBodyCreate((<soy.scenes.Scene> self._scene)._worldID)
     ode.dBodySetData(self._bodyID, <void *>self)

Modified: trunk/pysoy/src/bodies._bodies/soy.bodies._bodies.pxd
===================================================================
--- trunk/pysoy/src/bodies._bodies/soy.bodies._bodies.pxd       2008-03-04 
18:57:05 UTC (rev 1026)
+++ trunk/pysoy/src/bodies._bodies/soy.bodies._bodies.pxd       2008-03-04 
19:16:25 UTC (rev 1027)
@@ -24,7 +24,7 @@
   cdef ode.dBodyID              _bodyID
   cdef object                   _scene
   cdef void*                    _mesh
-  cdef object                   _shape
+  cdef void*                    _shape
   cdef soy._datatypes.HashTable _tags
   # for loading/saving
   cdef ode.dMass                _mass

Modified: trunk/pysoy/src/bodies.fields/Buoyancy.pxi
===================================================================
--- trunk/pysoy/src/bodies.fields/Buoyancy.pxi  2008-03-04 18:57:05 UTC (rev 
1026)
+++ trunk/pysoy/src/bodies.fields/Buoyancy.pxi  2008-03-04 19:16:25 UTC (rev 
1027)
@@ -27,12 +27,12 @@
     self._affected = soy._internals.Children()
     self._density = 0
 
-  cdef void _give(self, int ccycle) :
-    cdef int i
+  cdef void _give(self, int _ccycle) :
+    cdef int _i
     cdef soy.bodies._bodies.Body _otherBody
-    if (self._shape) ^ (ccycle == 0) :
-      for i from 0 <= i < self._affected.current :
-        _otherBody = <soy.bodies._bodies.Body> self._affected.list[i]
+    if (self._shape != NULL) ^ (_ccycle == 0) :
+      for _i from 0 <= _i < self._affected.current :
+        _otherBody = <soy.bodies._bodies.Body> self._affected.list[_i]
         _otherBody._tags._remove("bmass")
       self._affected.empty()
 
@@ -42,7 +42,7 @@
     ode.dBodyGetMass(_otherBody._bodyID, &_otherBody._mass)
     #
     # Volume-less bodies are not affected
-    if _otherBody._shape == None :
+    if _otherBody._shape == NULL :
       return 0
     _otherShape = <soy.shapes.Shape> _otherBody._shape
     if _otherShape._volume() == 0 :

Modified: trunk/pysoy/src/bodies.fields/Field.pxi
===================================================================
--- trunk/pysoy/src/bodies.fields/Field.pxi     2008-03-04 18:57:05 UTC (rev 
1026)
+++ trunk/pysoy/src/bodies.fields/Field.pxi     2008-03-04 19:16:25 UTC (rev 
1027)
@@ -25,20 +25,22 @@
   '''
 
   cdef int _apply(self) :
-    cdef int i, r
-    r = 1
-    if self._scene :
-      if not self._shape :
-        for i from 0 <= i < self._scene._bodies.current :
-          if (<soy.bodies._bodies.Body> self._scene._bodies.list[i])._bodyID :
-            if not self._exert(<soy.bodies._bodies.Body> 
self._scene._bodies.list[i]) :
-              r = 0
-    return r
+    cdef int              _i, _r
+    cdef soy.scenes.Scene _scene
+    if self._bodyID == NULL :
+      return 1
+    _r = 1
+    _scene = <soy.scenes.Scene> self._scene
+    if not self._shape :
+      for _i from 0 <= _i < _scene._bodies.current :
+        if not self._exert(<soy.bodies._bodies.Body> _scene._bodies.list[_i]) :
+          _r = 0
+    return _r
 
-  cdef int _exert(self, soy.bodies._bodies.Body other) :
+  cdef int _exert(self, soy.bodies._bodies.Body _other) :
     return 1
 
-  cdef void _give(self, int data) :
+  cdef void _give(self, int _data) :
     return
 
   cdef void _commit(self) :

Modified: trunk/pysoy/src/meshes/Shape.pxi
===================================================================
--- trunk/pysoy/src/meshes/Shape.pxi    2008-03-04 18:57:05 UTC (rev 1026)
+++ trunk/pysoy/src/meshes/Shape.pxi    2008-03-04 19:16:25 UTC (rev 1027)
@@ -29,12 +29,17 @@
   def __cinit__(self, material, *args, **keywords) :
     self.material = material
 
+
   cdef void _coreRender(self, void* _body) :
     cdef soy.shapes.Shape _shape
-    cdef int _shapeclass
+    cdef int              _shapeclass
     #
+    # Don't attempt to render a non-existant shape
+    if (<soy.bodies._bodies.Body> _body)._shape == NULL :
+      return
+    #
     # Grab the body's shape
-    _shape = (<soy.bodies._bodies.Body> _body)._shape
+    _shape = <soy.shapes.Shape> (<soy.bodies._bodies.Body> _body)._shape
     _shapeclass = ode.dGeomGetClass(_shape._geomID)
     #
     # Bind the material
@@ -53,6 +58,7 @@
     # Unbind the material
     self._material._coreUnBind()
 
+
   cdef void _coreRenderSphere(self, soy.shapes.Shape _shape) :
     cdef gl.GLUquadricObj* _quadric
     _quadric = gl.gluNewQuadric()
@@ -61,6 +67,7 @@
     gl.gluSphere(_quadric, _shape._radius(), 32, 32)
     gl.gluDeleteQuadric(_quadric)
 
+
   cdef void _coreRenderCapsule(self, soy.shapes.Shape _shape) :
     cdef ode.dReal r, l
     cdef gl.GLUquadricObj* _quadric
@@ -79,6 +86,7 @@
     gl.glPopMatrix()
     gl.gluDeleteQuadric(_quadric)
 
+
   cdef void _coreRenderBox(self, soy.shapes.Shape _shape) :
     cdef ode.dVector3 _xyz
     ode.dGeomBoxGetLengths(_shape._geomID, _xyz)
@@ -118,6 +126,7 @@
     gl.glEnd()
     gl.glPopMatrix()
 
+
   cdef void _coreRenderRay(self, soy.shapes.Shape _shape) :
     cdef ode.dVector3 s, d
     cdef float l
@@ -131,6 +140,7 @@
     gl.glEnd()
     gl.glPopAttrib()
 
+
   property material :
     def __get__(self) :
       return self._material

Modified: trunk/pysoy/src/shapes/Shape.pxi
===================================================================
--- trunk/pysoy/src/shapes/Shape.pxi    2008-03-04 18:57:05 UTC (rev 1026)
+++ trunk/pysoy/src/shapes/Shape.pxi    2008-03-04 19:16:25 UTC (rev 1027)
@@ -40,69 +40,3 @@
 
   cdef int _finite(self) :
     return 0
-
-  property body :
-    '''Shape's associated body, if it has one.
-    
-    Returns None if there is no body associated.
-    '''
-    def __get__(self) :
-      cdef ode.dBodyID b
-      if self._geomID :
-        b = ode.dGeomGetBody(self._geomID)
-        if b == NULL :
-          return None
-        else :
-          return <soy.bodies._bodies.Body> ode.dBodyGetData(b)
-      else :
-        return None
-
-    def __set__(self, object value) :
-      cdef ode.dBodyID _bodyID
-      cdef soy.bodies._bodies.Body _body
-      cdef soy.scenes.Scene _scene
-      if value != None and not isinstance(value, soy.bodies._bodies.Body) :
-        raise TypeError('Requires a Body')
-      if not self._geomID :
-        raise RuntimeError('Cannot use a base Shape object')
-      _bodyID = <ode.dBodyID> ode.dGeomGetBody(self._geomID)
-      #
-      # If there is an old body, disconnect from it.
-      if _bodyID != NULL :
-        _body = <soy.bodies._bodies.Body> ode.dBodyGetData(_bodyID)
-        # If setting to same Body instance, return now
-        if _body == value :
-          return
-        if _body._scene != None :
-          # If we were in a scene's space, get out
-          _scene = <soy.scenes.Scene> _body._scene
-          _scene._stepLock()
-          ode.dSpaceRemove(_scene._spaceID, self._geomID)
-          _body._shape = None
-          _scene._stepUnLock()
-        else :
-          _body._shape = None
-      #
-      # If there's nothing new to connect to, clear that as well
-      if value == None :
-        ode.dGeomSetBody(self._geomID, NULL)
-        return
-      # 
-      # Add new body
-      _body   = <soy.bodies._bodies.Body> value
-      if _body._scene != None :
-        _scene = <soy.scenes.Scene> _body._scene
-        _scene._stepLock()
-        ode.dGeomSetBody(self._geomID, _body._bodyID)
-        ode.dSpaceAdd(_scene._spaceID, self._geomID)
-        #
-        # If the new Body is still connected to a Shape, disconnect it.
-        if _body._shape != None :
-          (<soy.shapes.Shape> _body._shape).body = None
-        #
-        # Link back Body's reference to here.
-        _body._shape = self
-        self._position = ode.dGeomGetPosition(self._geomID)
-        _scene._stepUnLock()
-      else :
-        stdio.printf('This is incomplete!')

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

Reply via email to