QuadSetGeometryAlgorithms.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_QUADSETGEOMETRYALGORITHMS_INL
00026 #define SOFA_COMPONENT_TOPOLOGY_QUADSETGEOMETRYALGORITHMS_INL 
00027 
00028 #include <sofa/component/topology/QuadSetGeometryAlgorithms.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 QuadSetGeometryAlgorithms< DataTypes >::computeQuadArea( const unsigned int i) const 
00042     {
00043         const Quad &t = this->m_topology->getQuad(i);
00044         const VecCoord& p = *(this->object->getX());
00045         Real area = (Real)((areaProduct(p[t[1]]-p[t[0]],p[t[2]]-p[t[0]])
00046                           + areaProduct(p[t[3]]-p[t[2]],p[t[0]]-p[t[2]])) * (Real) 0.5);
00047         return area;
00048     }
00049 
00050     template< class DataTypes>
00051     typename DataTypes::Real QuadSetGeometryAlgorithms< DataTypes >::computeRestQuadArea( const unsigned int i) const 
00052     {
00053         const Quad &t = this->m_topology->getQuad(i);
00054         const VecCoord& p = *(this->object->getX0());
00055         Real area = (Real)((areaProduct(p[t[1]]-p[t[0]],p[t[2]]-p[t[0]])
00056                           + areaProduct(p[t[3]]-p[t[2]],p[t[0]]-p[t[2]])) * (Real) 0.5);
00057         return area;
00058     }
00059 
00060     template<class DataTypes>
00061     void QuadSetGeometryAlgorithms<DataTypes>::computeQuadArea( BasicArrayInterface<Real> &ai) const
00062     {
00063         //const sofa::helper::vector<Quad> &ta=this->m_topology->getQuads();
00064         int nb_quads = this->m_topology->getNbQuads();
00065         const typename DataTypes::VecCoord& p = *(this->object->getX());
00066 
00067         for(int i=0; i<nb_quads; ++i)
00068         { // ta.size()
00069             const Quad &t = this->m_topology->getQuad(i);  //ta[i];
00070             ai[i] = (Real)((areaProduct(p[t[1]]-p[t[0]],p[t[2]]-p[t[0]])
00071                           + areaProduct(p[t[3]]-p[t[2]],p[t[0]]-p[t[2]])) * (Real) 0.5);
00072         }
00073     }
00074 
00075     // Computes the normal vector of a quad indexed by ind_q (not normed)
00076     template<class DataTypes>
00077     Vec<3,double> QuadSetGeometryAlgorithms< DataTypes >::computeQuadNormal(const unsigned int ind_q)
00078     {
00079         // HYP :  The quad indexed by ind_q is planar
00080 
00081         const Quad &q = this->m_topology->getQuad(ind_q);
00082         const typename DataTypes::VecCoord& vect_c = *(this->object->getX());
00083 
00084         const typename DataTypes::Coord& c0=vect_c[q[0]];
00085         const typename DataTypes::Coord& c1=vect_c[q[1]];
00086         const typename DataTypes::Coord& c2=vect_c[q[2]];
00087         //const typename DataTypes::Coord& c3=vect_c[q[3]];
00088 
00089         Vec<3,Real> p0; 
00090         p0[0] = (Real) (c0[0]); p0[1] = (Real) (c0[1]); p0[2] = (Real) (c0[2]);
00091         Vec<3,Real> p1;
00092         p1[0] = (Real) (c1[0]); p1[1] = (Real) (c1[1]); p1[2] = (Real) (c1[2]);
00093         Vec<3,Real> p2; 
00094         p2[0] = (Real) (c2[0]); p2[1] = (Real) (c2[1]); p2[2] = (Real) (c2[2]);
00095         //Vec<3,Real> p3; 
00096         //p3[0] = (Real) (c3[0]); p3[1] = (Real) (c3[1]); p3[2] = (Real) (c3[2]);
00097 
00098         Vec<3,Real> normal_q=(p1-p0).cross( p2-p0);
00099 
00100         return ((Vec<3,double>) normal_q);
00101     }
00102 
00103 
00104     // Test if a quad indexed by ind_quad (and incident to the vertex indexed by ind_p) is included or not in the plane defined by (ind_p, plane_vect)
00105     template<class DataTypes>
00106     bool QuadSetGeometryAlgorithms< DataTypes >::is_quad_in_plane(const unsigned int ind_q, 
00107                                                                 const unsigned int ind_p,  
00108                                                                 const Vec<3,Real>&plane_vect)
00109     {
00110         const Quad &q = this->m_topology->getQuad(ind_q);
00111 
00112         // HYP : ind_p==q[0] or ind_q==t[1] or ind_q==t[2] or ind_q==q[3] 
00113 
00114         const typename DataTypes::VecCoord& vect_c = *(this->object->getX());
00115 
00116         unsigned int ind_1;
00117         unsigned int ind_2;
00118         unsigned int ind_3;
00119 
00120         if(ind_p==q[0])
00121         {
00122             ind_1=q[1];
00123             ind_2=q[2];
00124             ind_3=q[3];
00125         }
00126         else if(ind_p==q[1])
00127         {
00128             ind_1=q[2];
00129             ind_2=q[3];
00130             ind_3=q[0];
00131         }
00132         else if(ind_p==q[2])
00133         {
00134             ind_1=q[3];
00135             ind_2=q[0];
00136             ind_3=q[1];
00137         }
00138         else
00139         { // ind_p==q[3]
00140             ind_1=q[0];
00141             ind_2=q[1];
00142             ind_3=q[2];
00143         }
00144     
00145         const typename DataTypes::Coord& c0 = vect_c[ind_p];
00146         const typename DataTypes::Coord& c1 = vect_c[ind_1];
00147         const typename DataTypes::Coord& c2 = vect_c[ind_2];
00148         const typename DataTypes::Coord& c3 = vect_c[ind_3];
00149 
00150         Vec<3,Real> p0;
00151         p0[0] = (Real) (c0[0]); p0[1] = (Real) (c0[1]); p0[2] = (Real) (c0[2]);
00152         Vec<3,Real> p1;
00153         p1[0] = (Real) (c1[0]); p1[1] = (Real) (c1[1]); p1[2] = (Real) (c1[2]);
00154         Vec<3,Real> p2; 
00155         p2[0] = (Real) (c2[0]); p2[1] = (Real) (c2[1]); p2[2] = (Real) (c2[2]);
00156         Vec<3,Real> p3; 
00157         p3[0] = (Real) (c3[0]); p3[1] = (Real) (c3[1]); p3[2] = (Real) (c3[2]);
00158 
00159         return((p1-p0)*( plane_vect)>=0.0 && (p2-p0)*( plane_vect)>=0.0 && (p3-p0)*( plane_vect)>=0.0);
00160     }
00161 
00163     template <typename DataTypes>
00164     void QuadSetGeometryAlgorithms<DataTypes>::writeMSHfile(const char *filename)                  
00165     {
00166         std::ofstream myfile;                    
00167         myfile.open (filename); 
00168              
00169         const typename DataTypes::VecCoord& vect_c = *(this->object->getX()); 
00170 
00171         const unsigned int numVertices = vect_c.size(); 
00172 
00173         myfile << "$NOD\n";      
00174         myfile << numVertices <<"\n";    
00175 
00176         for(unsigned int i=0; i<numVertices; ++i)    
00177         {    
00178              double x = (double) vect_c[i][0];   
00179              double y = (double) vect_c[i][1];   
00180              double z = (double) vect_c[i][2];   
00181 
00182              myfile << i+1 << " " << x << " " << y << " " << z <<"\n";   
00183         }    
00184 
00185         myfile << "$ENDNOD\n";   
00186         myfile << "$ELM\n";      
00187 
00188         const sofa::helper::vector<Quad>& qa = this->m_topology->getQuads();     
00189 
00190         myfile << qa.size() <<"\n";      
00191 
00192         for(unsigned int i=0; i<qa.size(); ++i)      
00193         {    
00194              myfile << i+1 << " 3 1 1 4 " << qa[i][0]+1 << " " << qa[i][1]+1 << " " << qa[i][2]+1 << " " << qa[i][3]+1 << "\n";      
00195         }    
00196 
00197         myfile << "$ENDELM\n";   
00198 
00199         myfile.close();      
00200     }
00201 
00203     template< class Real>
00204     Real areaProduct(const Vec<3,Real>& a, const Vec<3,Real>& b)
00205     {
00206         return Vec<3,Real>(a.y()*b.z() - a.z()*b.y(),
00207             a.z()*b.x() - a.x()*b.z(),
00208             a.x()*b.y() - a.y()*b.x()).norm();
00209     }
00210 
00212     template< class Real>
00213     Real areaProduct(const defaulttype::Vec<2,Real>& a, const defaulttype::Vec<2,Real>& b )
00214     {
00215         return a[0]*b[1] - a[1]*b[0];
00216     }
00217 
00219     template< class Real>
00220     Real areaProduct(const defaulttype::Vec<1,Real>& , const defaulttype::Vec<1,Real>&  )
00221     {
00222     //  assert(false);
00223         return (Real)0;
00224     }
00225 
00226 } // namespace topology
00227 
00228 } // namespace component
00229 
00230 } // namespace sofa
00231 
00232 #endif // SOFA_COMPONENTS_QUADSETGEOMETRYALGORITHMS_INL

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