PointData.inl

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #ifndef SOFA_COMPONENT_TOPOLOGY_POINTDATA_INL
00026 #define SOFA_COMPONENT_TOPOLOGY_POINTDATA_INL
00027 
00028 #include <sofa/component/topology/PointData.h>
00029 
00030 #include <sofa/component/topology/PointSetTopologyChange.h>
00031 #include <sofa/component/topology/EdgeSetTopologyChange.h>
00032 #include <sofa/component/topology/TriangleSetTopologyChange.h>
00033 #include <sofa/component/topology/QuadSetTopologyChange.h>
00034 #include <sofa/component/topology/TetrahedronSetTopologyChange.h>
00035 #include <sofa/component/topology/HexahedronSetTopologyChange.h>
00036 
00037 namespace sofa
00038 {
00039 
00040 namespace component
00041 {
00042 
00043 namespace topology
00044 {
00045 
00049 
00050     template <typename T, typename Alloc>
00051     void PointData<T,Alloc>::handleTopologyEvents( std::list<  const core::componentmodel::topology::TopologyChange *>::const_iterator changeIt, 
00052                                                 std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator &end ) 
00053     {
00054         while( changeIt != end )
00055         {
00056             core::componentmodel::topology::TopologyChangeType changeType = (*changeIt)->getChangeType();
00057 
00058             switch( changeType ) 
00059             {
00060              case core::componentmodel::topology::TETRAHEDRAADDED:
00061              {
00062                  if (m_createTetrahedronFunc) 
00063                  {
00064                      const TetrahedraAdded *ea=static_cast< const TetrahedraAdded* >( *changeIt );
00065                      (*m_createTetrahedronFunc)(ea->tetrahedronIndexArray,m_createParam,*this);
00066                  }
00067                  break;
00068              }
00069              case core::componentmodel::topology::TETRAHEDRAREMOVED:
00070              {
00071                  if (m_destroyTetrahedronFunc) 
00072                  {
00073                      const TetrahedraRemoved *er=static_cast< const TetrahedraRemoved * >( *changeIt );
00074                      (*m_destroyTetrahedronFunc)(er->getArray(),m_createParam,*this);
00075                  }
00076                  break;
00077              }      
00078              case core::componentmodel::topology::TRIANGLESADDED:
00079              {
00080                  if (m_createTriangleFunc) 
00081                  {
00082                      const TrianglesAdded *ea=static_cast< const TrianglesAdded* >( *changeIt );
00083                      (*m_createTriangleFunc)(ea->triangleIndexArray,m_createParam,*this);
00084                  }
00085                  break;
00086              }
00087              case core::componentmodel::topology::TRIANGLESREMOVED:
00088              {
00089                  if (m_destroyTriangleFunc) 
00090                  {
00091                      const TrianglesRemoved *er=static_cast< const TrianglesRemoved * >( *changeIt );
00092                      (*m_destroyTriangleFunc)(er->getArray(),m_createParam,*this);
00093                  }
00094                  break;
00095              }                       
00096              case core::componentmodel::topology::EDGESADDED:
00097              {
00098                  if (m_createEdgeFunc) 
00099                  {
00100                      const EdgesAdded *ea=static_cast< const EdgesAdded* >( *changeIt );
00101                      (*m_createEdgeFunc)(ea->edgeIndexArray,m_createParam,*this);
00102                  }
00103                  break;
00104              }
00105              case core::componentmodel::topology::EDGESREMOVED:
00106              {
00107                  if (m_destroyEdgeFunc) 
00108                  {
00109                      const EdgesRemoved *er=static_cast< const EdgesRemoved * >( *changeIt );
00110                      (*m_destroyEdgeFunc)(er->getArray(),m_createParam,*this);
00111                  }
00112                  break;
00113              }
00114              case core::componentmodel::topology::POINTSINDICESSWAP:
00115              {
00116                  unsigned int i1 = ( static_cast< const PointsIndicesSwap * >( *changeIt ) )->index[0];
00117                  unsigned int i2 = ( static_cast< const PointsIndicesSwap* >( *changeIt ) )->index[1];
00118                  swap( i1, i2 );
00119                  break;
00120              }
00121              case core::componentmodel::topology::POINTSADDED:
00122              {
00123                  unsigned int nbPoints = ( static_cast< const PointsAdded * >( *changeIt ) )->getNbAddedVertices();
00124                  sofa::helper::vector< sofa::helper::vector< unsigned int > > ancestors = ( static_cast< const PointsAdded * >( *changeIt ) )->ancestorsList;
00125                  sofa::helper::vector< sofa::helper::vector< double       > > coefs     = ( static_cast< const PointsAdded * >( *changeIt ) )->coefs;
00126                  add( nbPoints, ancestors, coefs );
00127                  break;
00128              }
00129              case core::componentmodel::topology::POINTSREMOVED:
00130              {
00131                  const sofa::helper::vector<unsigned int> tab = ( static_cast< const PointsRemoved * >( *changeIt ) )->getArray();
00132                  remove( tab );
00133                  break;
00134              }
00135              case core::componentmodel::topology::POINTSRENUMBERING:
00136              {
00137                  const sofa::helper::vector<unsigned int> &tab = ( static_cast< const PointsRenumbering * >( *changeIt ) )->getinv_IndexArray();
00138                  renumber( tab );
00139                  break;
00140              }
00141              default:
00142                  // Ignore events that are not point related.
00143                  break;
00144             }; // switch( changeType )
00145 
00146             ++changeIt;
00147         }
00148     }
00149 
00150     template <typename T, typename Alloc>
00151     void PointData<T,Alloc>::swap( unsigned int i1, unsigned int i2 ) 
00152     {
00153         T tmp = (*this)[i1];
00154         (*this)[i1] = (*this)[i2];
00155         (*this)[i2] = tmp;
00156     }
00157 
00158     template <typename T, typename Alloc>
00159     void PointData<T,Alloc>::add( unsigned int nbPoints, 
00160                                 const sofa::helper::vector< sofa::helper::vector< unsigned int > >& ancestors, 
00161                                 const sofa::helper::vector< sofa::helper::vector< double > >& coefs) 
00162     {
00163         // Using default values
00164         unsigned int i0 = this->size();
00165         this->resize(i0+nbPoints);
00166 
00167         for (unsigned int i = 0; i < nbPoints; ++i)
00168         {
00169             T& t = (*this)[i0+i];
00170             if (ancestors.empty() || coefs.empty())
00171             {
00172                 const sofa::helper::vector< unsigned int > empty_vecint;
00173                 const sofa::helper::vector< double > empty_vecdouble;
00174                 m_createFunc( i0+i, m_createParam, t, empty_vecint, empty_vecdouble);
00175             }
00176             else
00177                 m_createFunc( i0+i, m_createParam, t, ancestors[i], coefs[i] );
00178         }
00179     }
00180 
00181 
00182 
00183     template <typename T, typename Alloc>
00184     void PointData<T,Alloc>::remove( const sofa::helper::vector<unsigned int> &index ) 
00185     {
00186         unsigned int last = this->size() -1;
00187 
00188         for (unsigned int i = 0; i < index.size(); ++i)
00189         {
00190             m_destroyFunc( index[i], m_destroyParam, (*this)[index[i]] );
00191             swap( index[i], last );
00192             --last;
00193         }
00194 
00195         resize( this->size() - index.size() );
00196     }
00197 
00198 
00199     template <typename T, typename Alloc>
00200     void PointData<T,Alloc>::renumber( const sofa::helper::vector<unsigned int> &index ) 
00201     {
00202         sofa::helper::vector< T,Alloc > copy = (*this); // not very efficient memory-wise, but I can see no better solution...
00203         for (unsigned int i = 0; i < index.size(); ++i)
00204         {
00205             (*this)[i] = copy[ index[i] ];
00206         }
00207     }
00208 
00209 } // namespace topology
00210 
00211 } // namespace component
00212 
00213 } // namespace sofa
00214 
00215 #endif // _POINTDATA_INL_

Generated on Sun Nov 23 03:44:46 2008 for Sofa by  doxygen 1.5.7.1