TetrahedronSetGeometryAlgorithms.inl
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 #ifndef SOFA_COMPONENT_TOPOLOGY_TETRAHEDRONSETGEOMETRYALGORITHMS_INL
00026 #define SOFA_COMPONENT_TOPOLOGY_TETRAHEDRONSETGEOMETRYALGORITHMS_INL
00027
00028 #include <sofa/component/topology/TetrahedronSetGeometryAlgorithms.h>
00029
00030 namespace sofa
00031 {
00032
00033 namespace component
00034 {
00035
00036 namespace topology
00037 {
00038 using namespace sofa::defaulttype;
00039
00040 template< class DataTypes>
00041 typename DataTypes::Real TetrahedronSetGeometryAlgorithms< DataTypes >::computeTetrahedronVolume( const unsigned int i) const
00042 {
00043 const Tetrahedron &t = this->m_topology->getTetra(i);
00044 const VecCoord& p = *(this->object->getX());
00045 Real volume = (Real)(tripleProduct(p[t[1]]-p[t[0]],p[t[2]]-p[t[0]],p[t[3]]-p[t[0]])/6.0);
00046 return volume;
00047 }
00048
00049 template< class DataTypes>
00050 typename DataTypes::Real TetrahedronSetGeometryAlgorithms< DataTypes >::computeRestTetrahedronVolume( const unsigned int i) const
00051 {
00052 const Tetrahedron &t=this->m_topology->getTetra(i);
00053 const VecCoord& p = *(this->object->getX0());
00054 Real volume = (Real)(tripleProduct(p[t[1]]-p[t[0]],p[t[2]]-p[t[0]],p[t[3]]-p[t[0]])/6.0);
00055 return volume;
00056 }
00057
00059 template<class DataTypes>
00060 void TetrahedronSetGeometryAlgorithms<DataTypes>::computeTetrahedronVolume( BasicArrayInterface<Real> &ai) const
00061 {
00062 const sofa::helper::vector<Tetrahedron> &ta = this->m_topology->getTetras();
00063 const typename DataTypes::VecCoord& p = *(this->object->getX());
00064 for (unsigned int i=0; i<ta.size(); ++i)
00065 {
00066 const Tetrahedron &t = ta[i];
00067 ai[i] = (Real)(tripleProduct(p[t[1]]-p[t[0]],p[t[2]]-p[t[0]],p[t[3]]-p[t[0]])/6.0);
00068 }
00069 }
00070
00072 template<class DataTypes>
00073 void TetrahedronSetGeometryAlgorithms< DataTypes >::getTetraInBall(unsigned int ind_ta, unsigned int ind_tb,
00074 sofa::helper::vector<unsigned int> &indices)
00075 {
00076 const typename DataTypes::VecCoord& vect_c = *(this->object->getX());
00077
00078 const Tetrahedron &ta=this->m_topology->getTetra(ind_ta);
00079 const Tetrahedron &tb=this->m_topology->getTetra(ind_tb);
00080
00081 const typename DataTypes::Coord& ca=(vect_c[ta[0]]+vect_c[ta[1]]+vect_c[ta[2]]+vect_c[ta[3]])*0.25;
00082 const typename DataTypes::Coord& cb=(vect_c[tb[0]]+vect_c[tb[1]]+vect_c[tb[2]]+vect_c[tb[3]])*0.25;
00083 Vec<3,Real> pa;
00084 Vec<3,Real> pb;
00085 pa[0] = (Real) (ca[0]);
00086 pa[1] = (Real) (ca[1]);
00087 pa[2] = (Real) (ca[2]);
00088 pb[0] = (Real) (cb[0]);
00089 pb[1] = (Real) (cb[1]);
00090 pb[2] = (Real) (cb[2]);
00091
00092 Real d = (pa-pb)*(pa-pb);
00093
00094 unsigned int t_test=ind_ta;
00095 indices.push_back(t_test);
00096
00097 std::map<unsigned int, unsigned int> IndexMap;
00098 IndexMap.clear();
00099 IndexMap[t_test]=0;
00100
00101 sofa::helper::vector<unsigned int> ind2test;
00102 ind2test.push_back(t_test);
00103 sofa::helper::vector<unsigned int> ind2ask;
00104 ind2ask.push_back(t_test);
00105
00106 while(ind2test.size()>0)
00107 {
00108 ind2test.clear();
00109 for (unsigned int t=0; t<ind2ask.size(); t++)
00110 {
00111 unsigned int ind_t = ind2ask[t];
00112 sofa::component::topology::TetrahedronTriangles adjacent_triangles = this->m_topology->getTriangleTetraShell(ind_t);
00113
00114 for (unsigned int i=0; i<adjacent_triangles.size(); i++)
00115 {
00116 sofa::helper::vector< unsigned int > tetras_to_remove = this->m_topology->getTetraTriangleShell(adjacent_triangles[i]);
00117
00118 if(tetras_to_remove.size()==2)
00119 {
00120 if(tetras_to_remove[0]==ind_t)
00121 {
00122 t_test=tetras_to_remove[1];
00123 }
00124 else
00125 {
00126 t_test=tetras_to_remove[0];
00127 }
00128
00129 std::map<unsigned int, unsigned int>::iterator iter_1 = IndexMap.find(t_test);
00130 if(iter_1 == IndexMap.end())
00131 {
00132 IndexMap[t_test]=0;
00133
00134 const Tetrahedron &tc=this->m_topology->getTetra(t_test);
00135 const typename DataTypes::Coord& cc = (vect_c[tc[0]]
00136 + vect_c[tc[1]]
00137 + vect_c[tc[2]]
00138 + vect_c[tc[3]]) * 0.25;
00139 Vec<3,Real> pc;
00140 pc[0] = (Real) (cc[0]);
00141 pc[1] = (Real) (cc[1]);
00142 pc[2] = (Real) (cc[2]);
00143
00144 Real d_test = (pa-pc)*(pa-pc);
00145
00146 if(d_test<d)
00147 {
00148 ind2test.push_back(t_test);
00149 indices.push_back(t_test);
00150 }
00151 }
00152 }
00153 }
00154 }
00155
00156 ind2ask.clear();
00157 for (unsigned int t=0; t<ind2test.size(); t++)
00158 {
00159 ind2ask.push_back(ind2test[t]);
00160 }
00161 }
00162
00163 return;
00164 }
00165
00167 template <typename DataTypes>
00168 void TetrahedronSetGeometryAlgorithms<DataTypes>::writeMSHfile(const char *filename)
00169 {
00170 std::ofstream myfile;
00171 myfile.open (filename);
00172
00173 const typename DataTypes::VecCoord& vect_c = *(this->object->getX());
00174
00175 const unsigned int numVertices = vect_c.size();
00176
00177 myfile << "$NOD\n";
00178 myfile << numVertices <<"\n";
00179
00180 for (unsigned int i=0; i<numVertices; ++i)
00181 {
00182 double x = (double) vect_c[i][0];
00183 double y = (double) vect_c[i][1];
00184 double z = (double) vect_c[i][2];
00185
00186 myfile << i+1 << " " << x << " " << y << " " << z <<"\n";
00187 }
00188
00189 myfile << "$ENDNOD\n";
00190 myfile << "$ELM\n";
00191
00192 const sofa::helper::vector<Tetrahedron> &tea = this->m_topology->getTetras();
00193
00194 myfile << tea.size() <<"\n";
00195
00196 for (unsigned int i=0; i<tea.size(); ++i)
00197 {
00198 myfile << i+1 << " 4 1 1 4 " << tea[i][0]+1 << " " << tea[i][1]+1 << " " << tea[i][2]+1 << " " << tea[i][3]+1 <<"\n";
00199 }
00200
00201 myfile << "$ENDELM\n";
00202
00203 myfile.close();
00204 }
00205
00207 template<typename real>
00208 inline real tripleProduct(const Vec<3,real>& a, const Vec<3,real>& b,const Vec<3,real> &c)
00209 {
00210 return dot(a,cross(b,c));
00211 }
00212
00214 template <typename real>
00215 inline real tripleProduct(const Vec<2,real>& , const Vec<2,real>& ,const Vec<2,real> &)
00216 {
00217 assert(false);
00218 return (real)0;
00219 }
00220
00222 template <typename real>
00223 inline real tripleProduct(const Vec<1,real>& , const Vec<1,real>& ,const Vec<1,real> &)
00224 {
00225 assert(false);
00226 return (real)0;
00227 }
00228
00229 }
00230
00231 }
00232
00233 }
00234
00235 #endif // SOFA_COMPONENTS_TETEAHEDRONSETGEOMETRYALGORITHMS_INL