Author: ArcRiley
Date: 2007-07-08 02:58:44 -0400 (Sun, 08 Jul 2007)
New Revision: 414

Modified:
   trunk/pysoy/src/_datatypes/VertexList.pxi
   trunk/pysoy/src/_datatypes/soy._datatypes.pxd
   trunk/pysoy/src/atoms/Vertex.pxi
   trunk/pysoy/src/atoms/soy.atoms.pxd
Log:
changing the API for Vertex & VertexList to keep my sanity intact:

You no longer create a Vertex then .append() it to a VertexList, but now 
create a Vertex as part of a Mesh - it's automatically in the list because 
it's part of the Mesh.  This eliminates Vertex EVER existing outside a Mesh
and thus a whole array of condition checking and state copying



Modified: trunk/pysoy/src/_datatypes/VertexList.pxi
===================================================================
--- trunk/pysoy/src/_datatypes/VertexList.pxi   2007-07-08 06:55:48 UTC (rev 
413)
+++ trunk/pysoy/src/_datatypes/VertexList.pxi   2007-07-08 06:58:44 UTC (rev 
414)
@@ -26,34 +26,59 @@
 
      This is a list-like container class for shared vertices.
   '''
-  def __new__(self, parent) :
-    if not isinstance(parent, soy.bodies._bodies.Mesh) :
+  def __new__(self, mesh) :
+    if not isinstance(mesh, soy.bodies._bodies.Mesh) :
       raise TypeError('argument must be of type soy.bodies.Mesh')
-    self._mesh = parent
+    self._mesh = mesh
 
+
   def __len__(self) :
     return (<soy.bodies._bodies.Mesh> self._mesh)._nvert
 
+
   def __str__(self) :
-    return '<VertexList with %d verticies>' % self.__len__()
+    cdef int i
+    cdef object l
+    cdef soy.bodies._bodies.Mesh _mesh
+    l = []
+    _mesh = <soy.bodies._bodies.Mesh> self._mesh
+    _mesh._cvert.lock()
+    for i from 0 <= i < _mesh._nvert :
+      l.append({'position':(_mesh._verts[i].px,
+                            _mesh._verts[i].py,
+                            _mesh._verts[i].pz),
+                'normal'  :(_mesh._verts[i].nx,
+                            _mesh._verts[i].ny,
+                            _mesh._verts[i].nz),
+                'texcoord':(_mesh._verts[i].tx,
+                            _mesh._verts[i].ty,
+                            _mesh._verts[i].tz)})
+    _mesh._cvert.unlock()
+    return str(l)
 
+
   def __repr__(self) :
-    return self.__str__()
+    return '<VertexList with %d verticies>' % self.__len__()
 
-  def __getitem__(self, index) :
-    return soy.atoms.Vertex(self._mesh, index)
 
-  def append(self, item) :
+  def __getitem__(self, _index) :
+    cdef int i
+    cdef soy.atoms.Vertex _vert
     cdef soy.bodies._bodies.Mesh _mesh
-    if not isinstance(item, soy.atoms.Vertex) :
-      raise TypeError('may only add instances of soy.atoms.Vertex to this 
list')
-    if (<soy.atoms.Vertex> item)._index != -1 :
-      raise TypeError('this Vertex is already part of a Mesh, try using 
.clone')
     _mesh = <soy.bodies._bodies.Mesh> self._mesh
+    #
+    # Check to see if there's already a Vertex instance made, if so return it,
+    # otherwise create a new instance and return that.  We do this to prevent
+    # duplicate Vertex instances and to prevent having to internally hold an
+    # instance of every vertex in every mesh - that'd eat way too much memory.
+    #
     _mesh._cvert.lock()
-    _mesh._allocVerts(_mesh._nvert + 1)
-    (<soy.atoms.Vertex> item)._addList(_mesh, _mesh._nvert)
-    (<soy.atoms.Vertex> item)._setList()
-    _mesh._nvert = _mesh._nvert + 1
+    for i from 0 <= i < _mesh._cvert.current :
+      if (<soy.atoms.Vertex> _mesh._cvert.list[i])._index == _index :
+        _vert = <soy.atoms.Vertex> _mesh._cvert.list[i]
+        _index = -1
+        break
     _mesh._cvert.unlock()
-
+    if _index == -1 :
+      _vert = soy.atoms.Vertex(mesh=_mesh, index=_index)
+    return _vert

Modified: trunk/pysoy/src/_datatypes/soy._datatypes.pxd
===================================================================
--- trunk/pysoy/src/_datatypes/soy._datatypes.pxd       2007-07-08 06:55:48 UTC 
(rev 413)
+++ trunk/pysoy/src/_datatypes/soy._datatypes.pxd       2007-07-08 06:58:44 UTC 
(rev 414)
@@ -17,9 +17,10 @@
 #
 # $Id$
 
+
 cdef class FaceList :
   cdef object _mesh
 
+
 cdef class VertexList :
   cdef object _mesh
-

Modified: trunk/pysoy/src/atoms/Vertex.pxi
===================================================================
--- trunk/pysoy/src/atoms/Vertex.pxi    2007-07-08 06:55:48 UTC (rev 413)
+++ trunk/pysoy/src/atoms/Vertex.pxi    2007-07-08 06:58:44 UTC (rev 414)
@@ -22,141 +22,113 @@
 
      An element of VertexList with .position .normal and .texcoord properties
   '''
-  def __new__(self, mesh=None, index=-1) :
-    if isinstance(mesh, soy.bodies._bodies.Mesh) and \
-       index>-1 and index<(<soy.bodies._bodies.Mesh> mesh)._nvert :
-      (<soy.bodies._bodies.Mesh> mesh)._cvert.lock()
-      self._addList(mesh, index)
-      (<soy.bodies._bodies.Mesh> mesh)._cvert.unlock()
+  def __new__(self, soy.bodies._bodies.Mesh mesh, 
+              position=None, normal=None, texcoord=None,
+              index=-1, *args, **keywords) :
+    self._index = -1
+    self._mesh = mesh
+    if index >= 0 :
+      # For an instance of an existing vertex
+      if index >= self._mesh._nvert :
+        raise ValueError('index out of range')
+      self._mesh._cvert.lock()
+      self._mesh._cvert.append(<void *>self)
+      self._index = index
+      self._mesh._cvert.unlock()
     else :
-      self._index = -1 
+      # For a brand new vertex
+      self._mesh._cvert.lock()
+      self._mesh._cvert.append(<void *>self)
+      self._index = self._mesh._nvert
+      self._mesh._allocVerts(self._mesh._nvert + 1)
+      self._mesh._nvert = self._mesh._nvert + 1
+      self._mesh._flagVertVBO(self._index)
+      self._mesh._cvert.unlock()
+    if position :
+      self.position = position
+    if normal :
+      self.normal   = normal
+    if texcoord :   
+      self.texcoord = texcoord
 
 
-  def __repr__(self) :
-    if self._index == -1 :
-      return '<Vertex at (%f, %f, %f), detached>' % ( self._saved.px, 
-                                                      self._saved.py, 
-                                                      self._saved.pz )
-    else :
-      return '<Vertex at (%f, %f, %f), attached as #%d>' % (
-                                           self._mesh._verts[self._index].px,
-                                           self._mesh._verts[self._index].py,
-                                           self._mesh._verts[self._index].pz,
-                                                             self._index )
+  def __dealloc__(self) :
+    if self._index >= 0 :
+      self._mesh._cvert.lock()
+      self._mesh._cvert.remove(<void *>self)
+      self._mesh._cvert.unlock()
 
 
-  cdef void _addList(self, soy.bodies._bodies.Mesh mesh, int index) :
-    self._mesh  = mesh
-    self._index = index
-    self._mesh._cvert.append(<void *>self)
+  def __repr__(self) :
+    return '<Vertex at (%f, %f, %f)>' % (self._mesh._verts[self._index].px,
+                                         self._mesh._verts[self._index].py,
+                                         self._mesh._verts[self._index].pz)
 
 
-  cdef void _setList(self) :
-    self._setListP()
-    self._setListN()
-    self._setListT()
-
-
-  cdef void _setListP(self) :
-    self._mesh._verts[self._index].px = self._saved.px
-    self._mesh._verts[self._index].py = self._saved.py
-    self._mesh._verts[self._index].pz = self._saved.pz
-    self._mesh._flagVertVBO(self._index)
-
-
-  cdef void _setListN(self) :
-    self._mesh._verts[self._index].nx = self._saved.nx
-    self._mesh._verts[self._index].ny = self._saved.ny
-    self._mesh._verts[self._index].nz = self._saved.nz
-    self._mesh._flagVertVBO(self._index)
-
-
-  cdef void _setListT(self) :
-    self._mesh._verts[self._index].tx = self._saved.tx
-    self._mesh._verts[self._index].ty = self._saved.ty
-    self._mesh._verts[self._index].tz = self._saved.tz
-    self._mesh._flagVertVBO(self._index)
-
-
-  cdef void _remList(self) :
-    self._index = -1
-    self._mesh._cvert.lock()
-    self._mesh._cvert.remove(<void *>self)
-    self._mesh._cvert.unlock()
-
-
-  def __dealloc__(self) :
-    if self._index > -1 :
-      self._remList()
-
-  def clone(self) :
-    clone = Vertex()
-    clone.position = self.position
-    clone.normal   = self.normal
-    clone.texcoord = self.texcoord
-    return clone
-
   property position :
     def __get__(self) :
-      if self._index == -1 :
-        return (self._saved.px, 
-                self._saved.py, 
-                self._saved.pz)
-      else :
-        return (self._mesh._verts[self._index].px, 
-                self._mesh._verts[self._index].py, 
-                self._mesh._verts[self._index].pz)
+      cdef float x, y, z
+      cdef object t
+      self._mesh._cvert.lock()
+      t = (self._mesh._verts[self._index].px, 
+           self._mesh._verts[self._index].py, 
+           self._mesh._verts[self._index].pz)
+      self._mesh._cvert.unlock()
+      return t
     def __set__(self, value) :
       if type(value)!=tuple and type(value)!=list :
         raise TypeError('Must provide a tuple or list')
       if len(value)!=3 :
         raise TypeError('Must provide (x,y,z)')
-      self._saved.px = value[0]
-      self._saved.py = value[1]
-      self._saved.pz = value[2]
-      if self._index > -1 :
-        self._setListP()
+      self._mesh._cvert.lock()
+      self._mesh._verts[self._index].px = value[0]
+      self._mesh._verts[self._index].py = value[1]
+      self._mesh._verts[self._index].pz = value[2]
+      self._mesh._flagVertVBO(self._index)
+      self._mesh._cvert.unlock()
 
 
   property normal :
     def __get__(self) :
-      if self._index == -1 :
-        return (self._saved.nx, 
-                self._saved.ny, 
-                self._saved.nz)
-      else :
-        return (self._mesh._verts[self._index].nx, 
-                self._mesh._verts[self._index].ny, 
-                self._mesh._verts[self._index].nz)
+      cdef float x, y, z
+      cdef object t
+      self._mesh._cvert.lock()
+      t = (self._mesh._verts[self._index].nx, 
+           self._mesh._verts[self._index].ny, 
+           self._mesh._verts[self._index].nz)
+      self._mesh._cvert.unlock()
+      return t
     def __set__(self, value) :
       if type(value)!=tuple and type(value)!=list :
         raise TypeError('Must provide a tuple or list')
       if len(value)!=3 :
         raise TypeError('Must provide (x,y,z)')
-      self._saved.nx = value[0]
-      self._saved.ny = value[1]
-      self._saved.nz = value[2]
-      if self._index > -1 :
-        self._setListN()
+      self._mesh._cvert.lock()
+      self._mesh._verts[self._index].nx = value[0]
+      self._mesh._verts[self._index].ny = value[1]
+      self._mesh._verts[self._index].nz = value[2]
+      self._mesh._flagVertVBO(self._index)
+      self._mesh._cvert.unlock()
 
 
   property texcoord :
     def __get__(self) :
-      if self._index == -1 :
-        return (self._saved.tx, 
-                self._saved.ty, 
-                self._saved.tz)
-      else :
-        return (self._mesh._verts[self._index].tx, 
-                self._mesh._verts[self._index].ty, 
-                self._mesh._verts[self._index].tz)
+      cdef float x, y, z
+      cdef object t
+      self._mesh._cvert.lock()
+      t = (self._mesh._verts[self._index].tx, 
+           self._mesh._verts[self._index].ty, 
+           self._mesh._verts[self._index].tz)
+      self._mesh._cvert.unlock()
+      return t
     def __set__(self, value) :
       if type(value)!=tuple and type(value)!=list :
         raise TypeError('Must provide a tuple or list')
       if len(value)!=3 :
         raise TypeError('Must provide (x,y,z)')
-      self._saved.tx = value[0]
-      self._saved.ty = value[1]
-      self._saved.tz = value[2]
-      if self._index > -1 :
-        self._setListT()
+      self._mesh._cvert.lock()
+      self._mesh._verts[self._index].tx = value[0]
+      self._mesh._verts[self._index].ty = value[1]
+      self._mesh._verts[self._index].tz = value[2]
+      self._mesh._flagVertVBO(self._index)
+      self._mesh._cvert.unlock()

Modified: trunk/pysoy/src/atoms/soy.atoms.pxd
===================================================================
--- trunk/pysoy/src/atoms/soy.atoms.pxd 2007-07-08 06:55:48 UTC (rev 413)
+++ trunk/pysoy/src/atoms/soy.atoms.pxd 2007-07-08 06:58:44 UTC (rev 414)
@@ -25,13 +25,6 @@
 cdef class Vertex :
   cdef soy.bodies._bodies.Mesh    _mesh
   cdef int                        _index
-  cdef soy.bodies._bodies.Vert    _saved
-  cdef void                       _addList(self,soy.bodies._bodies.Mesh,int)
-  cdef void                       _remList(self)
-  cdef void                       _setList(self)
-  cdef void                       _setListP(self)
-  cdef void                       _setListN(self)
-  cdef void                       _setListT(self)
 
 cdef class Face :
   cdef soy.bodies._bodies.Mesh    _mesh

_______________________________________________
PySoy-SVN mailing list
[email protected]
http://www.pysoy.org/mailman/listinfo/pysoy-svn

Reply via email to