Applied some fixes from Bullet: issues with btDbvtBroadphase, and btSoftBody, and...
[blender-staging.git] / extern / bullet2 / src / BulletSoftBody / btSoftBody.cpp
index dfb48e2ff2d85e6e34cc32355e344b3a092d9ee0..91b9d6f1bbd40ac756d3f9f5a7ca80e5578fa7a5 100644 (file)
@@ -97,8 +97,7 @@ btSoftBody::~btSoftBody()
        delete m_collisionShape;        
        int i;
 
        delete m_collisionShape;        
        int i;
 
-       for(i=0;i<m_clusters.size();++i)
-               btAlignedFree(m_clusters[i]);
+       releaseClusters();
        for(i=0;i<m_materials.size();++i) 
                btAlignedFree(m_materials[i]);
        for(i=0;i<m_joints.size();++i) 
        for(i=0;i<m_materials.size();++i) 
                btAlignedFree(m_materials[i]);
        for(i=0;i<m_joints.size();++i) 
@@ -749,19 +748,27 @@ int i,ni;
 }
 
 //
 }
 
 //
-int                            btSoftBody::generateClusters(int k,int maxiterations)
+void                   btSoftBody::releaseCluster(int index)
 {
 {
-       int i;
-
-for(i=0;i<m_clusters.size();++i)
-       {
-       if(m_clusters[i]->m_leaf) m_cdbvt.remove(m_clusters[i]->m_leaf);
-       btAlignedFree(m_clusters[i]);
-       }
-m_clusters.resize(btMin(k,m_nodes.size()));
-
+Cluster*       c=m_clusters[index];
+if(c->m_leaf) m_cdbvt.remove(c->m_leaf);
+c->~Cluster();
+btAlignedFree(c);
+m_clusters.remove(c);
+}
 
 
+//
+void                   btSoftBody::releaseClusters()
+{
+while(m_clusters.size()>0) releaseCluster(0);
+}
 
 
+//
+int                            btSoftBody::generateClusters(int k,int maxiterations)
+{
+int i;
+releaseClusters();
+m_clusters.resize(btMin(k,m_nodes.size()));
 for(i=0;i<m_clusters.size();++i)
        {
        m_clusters[i]                   =       new(btAlignedAlloc(sizeof(Cluster),16)) Cluster();
 for(i=0;i<m_clusters.size();++i)
        {
        m_clusters[i]                   =       new(btAlignedAlloc(sizeof(Cluster),16)) Cluster();
@@ -870,10 +877,7 @@ if(k>0)
                {
                if(m_clusters[i]->m_nodes.size()==0)
                        {
                {
                if(m_clusters[i]->m_nodes.size()==0)
                        {
-                       btAlignedFree(m_clusters[i]);
-                       btSwap(m_clusters[i],m_clusters[m_clusters.size()-1]);
-                       m_clusters.pop_back();
-                       --i;
+                       releaseCluster(i--);
                        }
                }
                
                        }
                }
                
@@ -2002,7 +2006,7 @@ for(i=0;i<m_clusters.size();++i)
                                mi.setMin(c.m_nodes[j]->m_x);
                                mx.setMax(c.m_nodes[j]->m_x);
                                }                       
                                mi.setMin(c.m_nodes[j]->m_x);
                                mx.setMax(c.m_nodes[j]->m_x);
                                }                       
-                       const btDbvtVolume      bounds=btDbvtVolume::FromMM(mi,mx);
+                       const ATTRIBUTE_ALIGNED16(btDbvtVolume) bounds=btDbvtVolume::FromMM(mi,mx);
                        if(c.m_leaf)
                                m_cdbvt.update(c.m_leaf,bounds,c.m_lv*m_sst.sdt*3,m_sst.radmrg);
                                else
                        if(c.m_leaf)
                                m_cdbvt.update(c.m_leaf,bounds,c.m_lv*m_sst.sdt*3,m_sst.radmrg);
                                else
@@ -2532,7 +2536,7 @@ switch(m_cfg.collisions&fCollision::RVSmask)
                const btScalar          basemargin=getCollisionShape()->getMargin();
                btVector3                       mins;
                btVector3                       maxs;
                const btScalar          basemargin=getCollisionShape()->getMargin();
                btVector3                       mins;
                btVector3                       maxs;
-               btDbvtVolume            volume;
+               ATTRIBUTE_ALIGNED16(btDbvtVolume)               volume;
                pco->getCollisionShape()->getAabb(      pco->getInterpolationWorldTransform(),
                                                                                        mins,
                                                                                        maxs);
                pco->getCollisionShape()->getAabb(      pco->getInterpolationWorldTransform(),
                                                                                        mins,
                                                                                        maxs);