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_POINTSETGEOMETRYALGORITHMS_INL 00026 #define SOFA_COMPONENT_TOPOLOGY_POINTSETGEOMETRYALGORITHMS_INL 00027 00028 #include <sofa/component/topology/PointSetGeometryAlgorithms.h> 00029 #include <sofa/core/componentmodel/behavior/MechanicalState.h> 00030 #include <sofa/defaulttype/VecTypes.h> 00031 #include <sofa/defaulttype/DataTypeInfo.h> 00032 00033 namespace sofa 00034 { 00035 00036 namespace component 00037 { 00038 00039 namespace topology 00040 { 00041 using namespace sofa::defaulttype; 00042 00043 template <class DataTypes> 00044 void PointSetGeometryAlgorithms< DataTypes >::init() 00045 { 00046 object = this->getContext()->core::objectmodel::BaseContext::get< core::componentmodel::behavior::MechanicalState< DataTypes > >(); 00047 core::componentmodel::topology::GeometryAlgorithms::init(); 00048 this->m_topology = this->getContext()->getMeshTopology(); 00049 } 00050 00051 template <class DataTypes> 00052 typename DataTypes::Coord PointSetGeometryAlgorithms<DataTypes>::getPointSetCenter() const 00053 { 00054 typename DataTypes::Coord center; 00055 // get restPosition 00056 typename DataTypes::VecCoord& p = *(object->getX0()); 00057 00058 const int numVertices = this->m_topology->getNbPoints(); 00059 for(int i=0; i<numVertices; ++i) 00060 { 00061 center += p[i]; 00062 } 00063 00064 center /= numVertices; 00065 return center; 00066 } 00067 00068 template<class DataTypes> 00069 void PointSetGeometryAlgorithms<DataTypes>::getEnclosingSphere(typename DataTypes::Coord ¢er, 00070 typename DataTypes::Real &radius) const 00071 { 00072 // get restPosition 00073 typename DataTypes::VecCoord& p = *(object->getX0()); 00074 00075 const unsigned int numVertices = this->m_topology->getNbPoints(); 00076 for(unsigned int i=0; i<numVertices; ++i) 00077 { 00078 center += p[i]; 00079 } 00080 center /= numVertices; 00081 radius = (Real) 0; 00082 00083 for(unsigned int i=0; i<numVertices; ++i) 00084 { 00085 const Coord dp = center-p[i]; 00086 const Real val = dot(dp,dp); 00087 if(val > radius) 00088 radius = val; 00089 } 00090 radius = (Real)sqrt((double) radius); 00091 } 00092 00093 template<class DataTypes> 00094 void PointSetGeometryAlgorithms<DataTypes>::getAABB(typename DataTypes::Real bb[6] ) const 00095 { 00096 // get restPosition 00097 typename DataTypes::VecCoord& p = *(object->getX0()); 00098 00099 bb[0] = (Real) p[0][0]; 00100 bb[1] = (Real) p[0][1]; 00101 bb[2] = (Real) p[0][2]; 00102 bb[3] = (Real) p[0][0]; 00103 bb[4] = (Real) p[0][1]; 00104 bb[5] = (Real) p[0][2]; 00105 00106 for(unsigned int i=1; i<p.size(); ++i) 00107 { 00108 // min 00109 if(bb[0] > (Real) p[i][0]) bb[0] = (Real) p[i][0]; // x 00110 if(bb[1] > (Real) p[i][1]) bb[1] = (Real) p[i][1]; // y 00111 if(bb[2] > (Real) p[i][2]) bb[2] = (Real) p[i][2]; // z 00112 00113 // max 00114 if(bb[3] < (Real) p[i][0]) bb[3] = (Real) p[i][0]; // x 00115 if(bb[4] < (Real) p[i][1]) bb[4] = (Real) p[i][1]; // y 00116 if(bb[5] < (Real) p[i][2]) bb[5] = (Real) p[i][2]; // z 00117 } 00118 } 00119 00120 } // namespace topology 00121 00122 } // namespace component 00123 00124 } // namespace sofa 00125 00126 #endif
1.5.7.1