SphereTreeModel.h
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
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
00128
00129
00130 int getSize() const { return Inherit::getSize(); }
00131
00132
00133
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 }
00209
00210 }
00211
00212 }
00213
00214 #endif
00215
00216