TriangleData.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_TRIANGLEDATA_INL
00026 #define SOFA_COMPONENT_TOPOLOGY_TRIANGLEDATA_INL
00027 
00028 #include <sofa/component/topology/TriangleData.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, typename Alloc>
00046     void TriangleData<T,Alloc>::handleTopologyEvents( std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator changeIt, 
00047                                                      std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator &end ) 
00048     {
00049         while( changeIt != end )
00050         {
00051             core::componentmodel::topology::TopologyChangeType changeType = (*changeIt)->getChangeType();
00052 
00053             switch( changeType ) 
00054             {
00055             case core::componentmodel::topology::TETRAHEDRAADDED:
00056             {
00057                 if (m_createTetrahedronFunc) 
00058                 {
00059                     const TetrahedraAdded *ea = static_cast< const TetrahedraAdded* >( *changeIt );
00060                     (*m_createTetrahedronFunc)(ea->tetrahedronIndexArray, m_createParam, *this);
00061                 }
00062                 break;
00063             }
00064             case core::componentmodel::topology::TETRAHEDRAREMOVED:
00065             {
00066                 if (m_destroyTetrahedronFunc) 
00067                 {
00068                     const TetrahedraRemoved *er = static_cast< const TetrahedraRemoved * >( *changeIt );
00069                     (*m_destroyTetrahedronFunc)(er->getArray(), m_createParam, *this);
00070                 }
00071                 break;
00072             }       
00073             case core::componentmodel::topology::TRIANGLESADDED:
00074             {
00075                 const TrianglesAdded *ta = static_cast< const TrianglesAdded * >( *changeIt );
00076                 add( ta->getNbAddedTriangles(), ta->triangleArray, ta->ancestorsList, ta->coefs );
00077                 break;
00078             }
00079 
00080             case core::componentmodel::topology::TRIANGLESREMOVED:
00081             {
00082                 const sofa::helper::vector<unsigned int> &tab = ( static_cast< const TrianglesRemoved *>( *changeIt ) )->getArray();
00083                 remove( tab );
00084                 break;
00085             }
00086 
00087             default:
00088             // Ignore events that are not Triangle or Point related.
00089             break;
00090             }; // switch( changeType )
00091 
00092             ++changeIt;
00093         }
00094     }
00095 
00096     template <typename T, typename Alloc>
00097     void TriangleData<T,Alloc>::swap( unsigned int i1, unsigned int i2 ) 
00098     {
00099         T tmp = (*this)[i1];
00100         (*this)[i1] = (*this)[i2];
00101         (*this)[i2] = tmp;
00102     }
00103 
00104     template <typename T, typename Alloc>
00105     void TriangleData<T,Alloc>::add( unsigned int nbTriangles, 
00106                                     const sofa::helper::vector< Triangle >& triangle, 
00107                                     const sofa::helper::vector< sofa::helper::vector< unsigned int > >& ancestors, 
00108                                     const sofa::helper::vector< sofa::helper::vector< double > >& coefs ) 
00109     {
00110         // Using default values
00111         unsigned int i0 = this->size();
00112         this->resize(i0+nbTriangles);
00113 
00114         for (unsigned int i = 0; i < nbTriangles; ++i)
00115         {
00116             T& t = (*this)[i0+i];
00117             if (ancestors.empty() || coefs.empty())
00118             {
00119                 const sofa::helper::vector< unsigned int > empty_vecint;
00120                 const sofa::helper::vector< double > empty_vecdouble;
00121                 m_createFunc( i0+i, m_createParam, t, triangle[i], empty_vecint, empty_vecdouble );
00122             }
00123             else
00124                 m_createFunc( i0+i, m_createParam, t, triangle[i], ancestors[i], coefs[i] );
00125         }
00126     }
00127 
00128     template <typename T, typename Alloc>
00129     void TriangleData<T,Alloc>::remove( const sofa::helper::vector<unsigned int> &index ) 
00130     {
00131         unsigned int last = this->size() -1;
00132 
00133         for (unsigned int i = 0; i < index.size(); ++i)
00134         {
00135             m_destroyFunc( index[i], m_destroyParam, (*this)[index[i]] );
00136             swap( index[i], last );
00137             --last;
00138         }
00139 
00140         resize( this->size() - index.size() );
00141     }
00142 
00143 } // namespace topology
00144 
00145 } // namespace component
00146 
00147 } // namespace sofa
00148 
00149 #endif // _TriangleDATA_INL_

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