TriangleSubsetData.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_EDGESUBSETDATA_INL
00026 #define SOFA_COMPONENT_TOPOLOGY_EDGESUBSETDATA_INL
00027 
00028 #include <sofa/component/topology/TriangleSubsetData.h>
00029 #include <sofa/component/topology/TriangleSetTopologyChange.h>
00030 #include <sofa/component/topology/TetrahedronSetTopologyChange.h>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace topology
00039 {
00040 
00044 
00045     template <typename T>
00046     void TriangleSubsetData<T>::handleTopologyEvents( std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator changeIt, 
00047                                                     std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator &end,
00048                                                     const unsigned int totalTriangleSetArraySize) 
00049     {
00050         setTotalTriangleSetArraySize(totalTriangleSetArraySize);
00051 
00052         while( changeIt != end )
00053         {
00054             core::componentmodel::topology::TopologyChangeType changeType = (*changeIt)->getChangeType();
00055 
00056             switch( changeType ) 
00057             {
00058              case core::componentmodel::topology::TETRAHEDRAADDED:
00059              {
00060                  if (m_createTetrahedronFunc) {
00061                      const TetrahedraAdded *ea=static_cast< const TetrahedraAdded* >( *changeIt );
00062                      (*m_createTetrahedronFunc)(ea->tetrahedronIndexArray,m_createParam,*this);
00063                  }
00064                  break;
00065              }
00066              case core::componentmodel::topology::TETRAHEDRAREMOVED:
00067              {
00068                  if (m_destroyTetrahedronFunc) {
00069                      const TetrahedraRemoved *er=static_cast< const TetrahedraRemoved * >( *changeIt );
00070                      (*m_destroyTetrahedronFunc)(er->getArray(),m_createParam,*this);
00071                  }
00072                  break;
00073              }      
00074              case core::componentmodel::topology::TRIANGLESADDED:
00075              {
00076                  const TrianglesAdded *ea=static_cast< const TrianglesAdded * >( *changeIt );
00077                  add( ea->getNbAddedTriangles(), ea->triangleArray, ea->ancestorsList, ea->coefs );
00078                  break;
00079              }
00080              case core::componentmodel::topology::TRIANGLESREMOVED:
00081              {
00082                  const std::vector<unsigned int> &tab = ( static_cast< const TrianglesRemoved *>( *changeIt ) )->getArray();
00083                  remove( tab );
00084                  break;
00085              }
00086              default:
00087                  // Ignore events that are not Triangle or Point related.
00088                  break;
00089             }; // switch( changeType )
00090 
00091             ++changeIt;
00092         } 
00093     }
00094 
00095     template <typename T>
00096     void TriangleSubsetData<T>::add( unsigned int nbTriangles, 
00097                                     const helper::vector< Triangle >& triangle, 
00098                                     const helper::vector< helper::vector< unsigned int > >& ancestors,
00099                                     const helper::vector< helper::vector< double > >& coefs ) 
00100     {
00101         // Using default values
00102         unsigned int s = this->size();
00103 
00104         for (unsigned int i = 0; i < nbTriangles; ++i)
00105         {
00106             T t;
00107             if (ancestors.empty() || coefs.empty())
00108             {
00109                 const sofa::helper::vector< unsigned int > empty_vecint;
00110                 const sofa::helper::vector< double > empty_vecdouble;
00111                 m_createFunc( s + i, m_createParam, t, triangle[i], empty_vecint, empty_vecdouble);
00112             }
00113             else
00114                 m_createFunc( s + i, m_createParam, t, triangle[i], ancestors[i], coefs[i] );
00115 
00116             // test that there are no item with the key = s+i
00117             assert(this->find(s+i)==this->end());
00118             insert(std::pair<unsigned int,T>(s+i,t));
00119         }
00120         lastTriangleIndex+=nbTriangles;
00121     }
00122 
00123     template <typename T>
00124     void TriangleSubsetData<T>::remove( const helper::vector<unsigned int> &index ) 
00125     {
00126         typename std::map<unsigned int,T>::iterator it,itend;
00127 
00128         for (unsigned int i = 0; i < index.size(); ++i)
00129         {
00130             it=this->find(index[i]);
00131             if (it!=this->end()) 
00132             {
00133                 m_destroyFunc( index[i], m_destroyParam, (*this)[index[i]] );
00135                 itend=this->find(lastTriangleIndex);
00136 
00137                 if (itend!=this->end()) 
00138                 {
00140                     T tmp=(*itend).second;
00141                     erase(itend);
00142                     insert(std::pair<unsigned int,T>(index[i],tmp));
00143                 }
00144                 erase(it);
00145             }
00146             --lastTriangleIndex;
00147         }
00148         //std::cout << "TriangleSubsetData: vector has now "<<this->size()<<" entries."<<std::endl;
00149     }
00150 
00151 } // namespace topology
00152 
00153 } // namespace component
00154 
00155 } // namespace sofa
00156 
00157 #endif // _TriangleDATA_INL_

Generated on Sun Nov 23 03:47:40 2008 for Sofa by  doxygen 1.5.7.1