RayModel.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_RAYMODEL_H
00026 #define SOFA_COMPONENT_COLLISION_RAYMODEL_H
00027 
00028 #include <sofa/core/CollisionModel.h>
00029 #include <sofa/component/MechanicalObject.h>
00030 #include <sofa/defaulttype/Vec3Types.h>
00031 #include <set>
00032 
00033 
00034 namespace sofa
00035 {
00036 
00037 namespace component
00038 {
00039 
00040 namespace collision
00041 {
00042 
00043 using namespace sofa::defaulttype;
00044 
00045 class RayModel;
00046 
00047 class Ray : public core::TCollisionElementIterator<RayModel>
00048 {
00049   public:
00050     Ray(RayModel* model, int index);
00051 
00052     explicit Ray(core::CollisionElementIterator& i);
00053 
00054     const Vector3& origin() const;
00055     const Vector3& direction() const;
00056     SReal l() const;
00057 
00058     Vector3& origin();
00059     Vector3& direction();
00060     SReal& l();
00061 };
00062 
00063 class BaseRayContact;
00064 
00065 class RayModel : public component::MechanicalObject<Vec3Types>, public core::CollisionModel
00066 {
00067 protected:
00068     sofa::helper::vector<SReal> length;
00069 
00070     Data<SReal> defaultLength;
00071 
00072     std::set<BaseRayContact*> contacts;
00073 public:
00074     typedef Vec3Types InDataTypes;
00075     typedef Vec3Types DataTypes;
00076     typedef Ray Element;
00077     friend class Ray;
00078     
00079     RayModel(SReal defaultLength=1);
00080 
00081     int addRay(const Vector3& origin, const Vector3& direction, SReal length);
00082     
00083     int getNbRay() const { return size; }
00084     
00085     void setNbRay(int n) { resize(2*n); }
00086     
00087     Ray getRay(int index) { return Ray(this, index); }
00088     
00089     virtual void addContact(BaseRayContact* contact) { contacts.insert(contact); }
00090     
00091     virtual void removeContact(BaseRayContact* contact) { contacts.erase(contact); }
00092 
00093     virtual void resize(int size);
00094 
00095     // -- CollisionModel interface
00096 
00097     virtual void computeBoundingTree(int maxDepth);
00098     
00099     void draw(int index);
00100     
00101     void applyTranslation(const double dx,const double dy,const double dz);
00102     
00103     void draw();
00104 };
00105 
00106 inline Ray::Ray(RayModel* model, int index)
00107 : core::TCollisionElementIterator<RayModel>(model, index)
00108 {}
00109 
00110 inline Ray::Ray(core::CollisionElementIterator& i)
00111 : core::TCollisionElementIterator<RayModel>(static_cast<RayModel*>(i.getCollisionModel()), i.getIndex())
00112 {
00113 }
00114 
00115 inline const Vector3& Ray::origin() const
00116 {
00117     return (*model->getX())[2*index+0];
00118 }
00119 
00120 inline const Vector3& Ray::direction() const
00121 {
00122     return (*model->getX())[2*index+1];
00123 }
00124 
00125 inline Vector3::value_type Ray::l() const
00126 {
00127     return model->length[index];
00128 }
00129 
00130 inline Vector3& Ray::origin()
00131 {
00132     return (*model->getX())[2*index+0];
00133 }
00134 
00135 inline Vector3& Ray::direction()
00136 {
00137     return (*model->getX())[2*index+1];
00138 }
00139 
00140 inline Vector3::value_type& Ray::l()
00141 {
00142     return model->length[index];
00143 }
00144 
00145 } // namespace collision
00146 
00147 } // namespace component
00148 
00149 } // namespace sofa
00150 
00151 #endif

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