SphereTreeModel.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 ******************************************************************************/
00035 #ifndef SOFA_COMPONENT_COLLISION_SPHERETREEMODEL_H
00036 #define SOFA_COMPONENT_COLLISION_SPHERETREEMODEL_H
00037 
00038 #include <sofa/core/CollisionModel.h>
00039 #include <sofa/component/MechanicalObject.h>
00040 #include <sofa/core/componentmodel/topology/BaseMeshTopology.h>
00041 #include <sofa/defaulttype/Vec3Types.h>
00042 #include <vector>
00043 #include <iostream>
00044 #include <fstream>
00045 
00046 
00047 
00048 
00049 namespace sofa
00050 {
00051 
00052 namespace component
00053 {
00054 
00055 namespace collision
00056 {
00057 
00058 using namespace sofa::defaulttype;
00059 
00060 class SphereTreeModel;
00061 
00067 class SingleSphere : public core::TCollisionElementIterator<SphereTreeModel>
00068 {
00069 public:
00070     typedef Vec3Types DataTypes;
00071     typedef DataTypes::Real Real;
00072     typedef DataTypes::Coord Coord;
00073     typedef DataTypes::Deriv Deriv;
00075     SingleSphere(SphereTreeModel* model, int index);
00076 
00078     explicit SingleSphere(core::CollisionElementIterator& i);
00079 
00082     const Vector3& center() const;
00083 
00086     void setCenter( double x, double y, double z );
00087     
00088     const Vector3& v() const;
00089 
00091     double r() const;
00092     
00093 };
00094 
00099 class SphereTreeModel : public component::MechanicalObject<Vec3Types>, public core::CollisionModel
00100 {
00101 public:
00102     typedef component::MechanicalObject<Vec3Types> Inherit;
00103     typedef Vec3Types InDataTypes;
00104     typedef Vec3Types DataTypes;
00105     typedef SingleSphere Element;
00106     friend class SingleSphere;
00107     DataTypes::VecCoord VecCoord;
00108 
00110     SphereTreeModel(double radius = 1.0);
00111         
00114     int addSphere(const Vector3& pos, SReal radius);
00115 
00117     void setSphere(int index, const Vector3& pos, SReal radius);
00118 
00121     bool load(const char* filename);
00122 
00123     void applyScale (const double s);
00124 
00125         sofa::core::componentmodel::behavior::MechanicalState<InDataTypes>* getMechanicalState() { return this; }
00126 
00127     // -- CollisionModel interface
00128 
00129     // remove ambiguity
00130     int getSize() const { return Inherit::getSize(); }
00131 
00132     /* @brief It "resizes" the vector of DOF's of the collision model. It is normally used after
00133         adding a new element to the tree*/
00134     virtual void resize(int size);
00135 
00137     virtual void computeBoundingTree(int maxDepth=0);
00138 
00141     virtual std::pair<core::CollisionElementIterator,core::CollisionElementIterator> getInternalChildren(int index) const;
00142 
00144     virtual void computeContinuousBoundingTree(double dt, int maxDepth=0);
00145 
00146     void draw(int index);
00147     
00149     int levels;
00150 
00152     int degree;
00153 
00156     bool loadSphereTree( const char *fileName );
00157     
00159     virtual bool isLeaf( int index ) const;
00160 
00161     void draw();
00162 
00163 protected:
00164 
00166     sofa::helper::vector<double> radius;    
00167 
00169     Data<double> defaultRadius;     
00170 
00171 };
00172 
00174 
00176 inline SingleSphere::SingleSphere(SphereTreeModel* model, int index)
00177 : core::TCollisionElementIterator<SphereTreeModel>(model, index)
00178 {
00179 }
00180 
00181 inline SingleSphere::SingleSphere(core::CollisionElementIterator& i)
00182 : core::TCollisionElementIterator<SphereTreeModel>(static_cast<SphereTreeModel*>(i.getCollisionModel()), i.getIndex())
00183 {
00184     
00185 }
00186 
00187 inline const Vector3& SingleSphere::center() const
00188 {
00189     return (*model->getX())[index]; 
00190 }
00191 
00192 inline const Vector3& SingleSphere::v() const
00193 {
00194     return (*model->getV())[index];
00195 }
00196 
00197 inline void SingleSphere::setCenter( double x, double y, double z ) 
00198 {
00199     (*model->getX())[index] = Vector3((SReal)x, (SReal)y, (SReal)z);
00200 }
00201 
00202 inline double SingleSphere::r() const
00203 {
00204     return model->radius[index];
00205 }
00206 
00207 
00208 } // namespace collision
00209 
00210 } // namespace component
00211 
00212 } // namespace sofa
00213 
00214 #endif
00215 
00216 

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