SphereModel.h

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_COLLISION_SPHEREMODEL_H
00026 #define SOFA_COMPONENT_COLLISION_SPHEREMODEL_H
00027 
00028 #include <sofa/core/CollisionModel.h>
00029 #include <sofa/component/MechanicalObject.h>
00030 #include <sofa/defaulttype/Vec3Types.h>
00031 
00032 namespace sofa
00033 {
00034 
00035 namespace component
00036 {
00037 
00038 namespace collision
00039 {
00040 
00041 using namespace sofa::defaulttype;
00042 
00043 template<class DataTypes>
00044 class TSphereModel;
00045 
00046 template<class DataTypes>
00047 class TSphere;
00048 
00049 typedef TSphereModel<Vec3Types> SphereModel;
00050 typedef TSphere<Vec3Types> Sphere;
00051 
00052 template<class DataTypes>
00053 class TSphere : public core::TCollisionElementIterator<TSphereModel<DataTypes> >
00054 {
00055 public:
00056     typedef typename DataTypes::Real Real;
00057     typedef typename DataTypes::Coord Coord;
00058     typedef typename DataTypes::Deriv Deriv;
00059     TSphere(TSphereModel<DataTypes>* model, int index);
00060 
00061     explicit TSphere(core::CollisionElementIterator& i);
00062 
00063     const Coord& center() const;
00064 
00065     const Deriv& v() const;
00066 
00067     Real r() const;
00068 };
00069 
00070 template<class TDataTypes>
00071 class TSphereModel : public component::MechanicalObject<TDataTypes>, public core::CollisionModel
00072 {
00073 public:
00074     typedef TDataTypes InDataTypes;
00075     typedef component::MechanicalObject<InDataTypes> Inherit;
00076         typedef typename InDataTypes::Real Real;
00077     typedef typename InDataTypes::VecReal VecReal;
00078         
00079     typedef TDataTypes DataTypes;
00080     typedef TSphere<DataTypes> Element;
00081     friend class TSphere<DataTypes>;
00082 
00083 protected:
00084     Data< VecReal > radius;
00085     
00086     Data< SReal > defaultRadius;
00087     class Loader;
00088 public:
00089     
00090     TSphereModel();
00091     
00092     TSphereModel(SReal radius);
00093 
00094     int addSphere(const Vector3& pos, SReal radius);
00095     void setSphere(int index, const Vector3& pos, SReal radius);
00096 
00097     virtual bool load(const char* filename);
00098     void applyScale (const SReal s);
00099 
00100         sofa::core::componentmodel::behavior::MechanicalState<InDataTypes>* getMechanicalState() { return this; }
00101 
00102     Real getRadius(int i) const { return this->radius.getValue()[i]; }
00103     void setRadius(int i, Real r) { (*this->radius.beginEdit())[i] = r; }
00104 
00105         const VecReal& getR() const { return this->radius.getValue(); }
00106 
00107     virtual void init();
00108     
00109     // -- CollisionModel interface
00110     
00111     // remove ambiguity
00112     int getSize() const { return this->Inherit::getSize(); }
00113 
00114     virtual void resize(int size);
00115 
00116     virtual void computeBoundingTree(int maxDepth=0);
00117 
00118     virtual void computeContinuousBoundingTree(double dt, int maxDepth=0);
00119     
00120     void draw(int index);
00121     
00122     void draw();
00123 };
00124 
00125 template<class TDataTypes>
00126 inline TSphere<TDataTypes>::TSphere(TSphereModel<TDataTypes>* model, int index)
00127 : core::TCollisionElementIterator<TSphereModel<TDataTypes> >(model, index)
00128 {}
00129 
00130 template<class TDataTypes>
00131 inline TSphere<TDataTypes>::TSphere(core::CollisionElementIterator& i)
00132 : core::TCollisionElementIterator<TSphereModel<TDataTypes> >(static_cast<TSphereModel<TDataTypes>*>(i.getCollisionModel()), i.getIndex())
00133 {
00134 }
00135 
00136 template<class TDataTypes>
00137 inline const typename TDataTypes::Coord& TSphere<TDataTypes>::center() const
00138 {
00139     return (*this->model->getX())[this->index];
00140 }
00141 
00142 template<class TDataTypes>
00143 inline const typename TDataTypes::Deriv& TSphere<TDataTypes>::v() const
00144 {
00145     return (*this->model->getV())[this->index];
00146 }
00147 
00148 template<class TDataTypes>
00149 inline typename TDataTypes::Real TSphere<TDataTypes>::r() const
00150 {
00151     return this->model->getRadius(this->index);
00152 }
00153 
00154 } // namespace collision
00155 
00156 } // namespace component
00157 
00158 } // namespace sofa
00159 
00160 #endif

Generated on Sun Nov 23 02:35:23 2008 for Sofa by  doxygen 1.5.7.1