SphereModel.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
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
00110
00111
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 }
00155
00156 }
00157
00158 }
00159
00160 #endif