PointSubset.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <sofa/component/topology/PointSubset.h>
00027 #include <sofa/component/topology/PointSetTopologyChange.h>
00028
00029 namespace sofa
00030 {
00031
00032 namespace component
00033 {
00034
00035 namespace topology
00036 {
00037
00041
00042
00043 void PointSubset::handleTopologyEvents( std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator changeIt,
00044 std::list< const core::componentmodel::topology::TopologyChange *>::const_iterator &end,
00045 const unsigned int totalPointSetArraySize)
00046 {
00047 setTotalPointSetArraySize(totalPointSetArraySize);
00048 while( changeIt != end )
00049 {
00050 core::componentmodel::topology::TopologyChangeType changeType = (*changeIt)->getChangeType();
00051
00052 switch( changeType )
00053 {
00054 case core::componentmodel::topology::POINTSINDICESSWAP:
00055 {
00056 unsigned int i1 = ( static_cast< const PointsIndicesSwap * >( *changeIt ) )->index[0];
00057 unsigned int i2 = ( static_cast< const PointsIndicesSwap* >( *changeIt ) )->index[1];
00058 swap( i1, i2 );
00059 break;
00060 }
00061 case core::componentmodel::topology::POINTSADDED:
00062 {
00063 unsigned int nbPoints = ( static_cast< const PointsAdded * >( *changeIt ) )->getNbAddedVertices();
00064 helper::vector< helper::vector< unsigned int > > ancestors = ( static_cast< const PointsAdded * >( *changeIt ) )->ancestorsList;
00065 helper::vector< helper::vector< double > > coefs = ( static_cast< const PointsAdded * >( *changeIt ) )->coefs;
00066 add( nbPoints, ancestors, coefs);
00067 lastPointIndex+=nbPoints;
00068 break;
00069 }
00070 case core::componentmodel::topology::POINTSREMOVED:
00071 {
00072 const sofa::helper::vector<unsigned int> tab = ( static_cast< const PointsRemoved * >( *changeIt ) )->getArray();
00073 remove( tab );
00074 break;
00075 }
00076 case core::componentmodel::topology::POINTSRENUMBERING:
00077 {
00078 const sofa::helper::vector<unsigned int> &tab = ( static_cast< const PointsRenumbering * >( *changeIt ) )->getinv_IndexArray();
00079 renumber( tab );
00080 break;
00081 }
00082 default:
00083
00084 break;
00085 };
00086
00087 ++changeIt;
00088 }
00089 }
00090
00091 void PointSubset::swap( unsigned int i1, unsigned int i2 )
00092 {
00093 helper::vector<unsigned int>::iterator it= find(m_subset.begin(),m_subset.end(),i1);
00094 if (it!=m_subset.end())
00095 (*it)=i2;
00096 }
00097
00098 void PointSubset::add( unsigned int nbPoints,
00099 const sofa::helper::vector< sofa::helper::vector< unsigned int > >& ancestors,
00100 const sofa::helper::vector< sofa::helper::vector< double > >& coefs)
00101 {
00102
00103 unsigned int size = m_subset.size();
00104 bool test;
00105 for (unsigned int i = 0; i < nbPoints; ++i)
00106 {
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 test=(*m_testNewPointFunc)( size + i, m_testParam, empty_vecint, empty_vecdouble);
00112 }
00113 else
00114 test=(*m_testNewPointFunc)( size + i, m_testParam, ancestors[i], coefs[i] );
00115
00116 if (test)
00117 m_subset.push_back( size+i );
00118 }
00119 }
00120
00121 void PointSubset::remove( const sofa::helper::vector<unsigned int> &index)
00122 {
00123 unsigned int it1;
00124 unsigned int it2;
00125
00126 for (unsigned int i = 0; i < index.size(); ++i)
00127 {
00128 it1=0;
00129 while(it1<m_subset.size())
00130 {
00131 if(m_subset[it1]==index[i])
00132 {
00133 break;
00134 }
00135 else
00136 {
00137 it1+=1;
00138 }
00139 }
00140
00141 if (it1<m_subset.size())
00142 {
00143 it2=0;
00144 while(it2<m_subset.size())
00145 {
00146 if(m_subset[it2]==lastPointIndex)
00147 {
00148 break;
00149 }
00150 else
00151 {
00152 it2+=1;
00153 }
00154 }
00155
00156 if (it2<m_subset.size())
00157 {
00158 m_subset[it2]=index[i];
00159 }
00160
00161 m_subset[it1]=m_subset[m_subset.size()-1];
00162 m_removalFunc( index[i], m_removeParam);
00163 m_subset.resize(m_subset.size()-1);
00164
00165 }
00166 else
00167 {
00168 it2=0;
00169 while(it2<m_subset.size())
00170 {
00171 if(m_subset[it2]==lastPointIndex)
00172 {
00173 break;
00174 }
00175 else
00176 {
00177 it2+=1;
00178 }
00179 }
00180
00181 if (it2<m_subset.size())
00182 {
00183 m_subset[it2]=index[i];
00184 }
00185 }
00186 --lastPointIndex;
00187 }
00188 }
00189
00190 void PointSubset::renumber( const sofa::helper::vector<unsigned int> &index )
00191 {
00192 for (unsigned int i = 0; i < m_subset.size(); ++i)
00193 {
00194 m_subset[i] =index[m_subset[i] ];
00195 }
00196 }
00197
00199 std::ostream& operator<< (std::ostream& os, const PointSubset& pointSubset)
00200 {
00201 return pointSubset.m_subset.write(os);
00202 }
00203
00205 std::istream& operator>>(std::istream& i, PointSubset& pointSubset)
00206 {
00207 return pointSubset.m_subset.read(i);
00208 }
00209
00210 }
00211
00212 }
00213
00214 }
00215