1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture, version 1.0 beta 4 *
* (c) 2006-2009 MGH, INRIA, USTL, UJF, CNRS *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
*******************************************************************************
* SOFA :: Modules *
* *
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#ifndef SOFA_COMPONENT_COLLISION_RAYPICKINTERACTOR_H
#define SOFA_COMPONENT_COLLISION_RAYPICKINTERACTOR_H
#include <sofa/component/collision/RayModel.h>
#include <sofa/core/BehaviorModel.h>
#include <sofa/component/container/MechanicalObject.h>
#include <sofa/simulation/common/Node.h>
#include <sofa/component/collision/BarycentricContactMapper.h>
#include <sofa/component/mapping/VoidMapping.h>
#include <sofa/defaulttype/Vec.h>
#include <sofa/defaulttype/Mat.h>
#include <sofa/defaulttype/Quat.h>
#include <sofa/core/componentmodel/collision/DetectionOutput.h>
#include <sofa/component/collision/TopologicalChangeManager.h>
#include <sofa/helper/Factory.h>
namespace sofa
{
namespace component
{
namespace collision
{
using namespace sofa::defaulttype;
struct PickPoints
{
core::CollisionModel* model1;
int elem1;
core::CollisionModel* model2;
core::componentmodel::behavior::BaseMechanicalState* mstate2;
int elem2;
double dist;
Vector3 p1, p2;
PickPoints() : model1(NULL), elem1(0), model2(NULL), mstate2(NULL), elem2(0), dist(0) {}
};
class SOFA_COMPONENT_COLLISION_API BasePickingManager : public virtual sofa::core::objectmodel::BaseObject
{
public:
virtual ~BasePickingManager();
virtual bool attach(int /*button*/, core::CollisionElementIterator /*elem1*/, core::CollisionElementIterator /*elem2*/, double /*dist*/, Vector3 /*p1*/, Vector3 /*p2*/)
{
return false;
}
virtual bool attach(int button, PickPoints& a)
{
if (a.model1 && a.model2)
return attach(button, core::CollisionElementIterator(a.model1, a.elem1), core::CollisionElementIterator(a.model2, a.elem2), a.dist, a.p1, a.p2);
else
return false;
}
virtual bool isAttached() = 0;
virtual void release() = 0;
virtual void update() = 0;
typedef helper::Factory<std::string, BasePickingManager, core::objectmodel::BaseContext*> PickingManagerFactory;
static BasePickingManager* Create(std::string name, core::objectmodel::BaseContext* context);
static int CreateAll(std::vector<BasePickingManager*>& instances, core::objectmodel::BaseContext* context);
template <class RealObject>
static bool canCreate( RealObject*& /*obj*/, core::objectmodel::BaseContext* /*context*/)
{
return true;
}
template <class RealObject>
static void create( RealObject*& obj, core::objectmodel::BaseContext* context)
{
if (RealObject::canCreate(obj, context))
{
obj = new RealObject;
//if (context != NULL)
// context->addObject(obj);
}
}
};
class SOFA_COMPONENT_COLLISION_API RayPickInteractor : public RayModel, public core::BehaviorModel, public mapping::VoidMapping
{
public:
RayPickInteractor();
~RayPickInteractor();
virtual void init();
virtual bool isActive();
virtual bool pickParticles(double /*rayOx*/, double /*rayOy*/, double /*rayOz*/, double /*rayDx*/, double /*rayDy*/, double /*rayDz*/, double /*radius0*/, double /*dRadius*/,
std::multimap< double, std::pair<sofa::core::componentmodel::behavior::BaseMechanicalState*, int> >& /*particles*/)
{
return false;
}
/// Computation of a new simulation step.
virtual void updatePosition(double dt);
/// Interactor interface
virtual void newPosition(const Vector3& translation, const Quat& rotation, const Mat4x4d& transformation);
virtual void newEvent(const std::string& command);
/// Set ray radius, at positions (0,0,0) and (0,0,1) in last transmitted transformation
virtual void setRayRadius(double r0, double r1);
virtual void draw();
virtual bool isAttached();
Data<bool> useCollisions;
protected:
void pickBody();
void releaseBody();
bool findFirstCollision(const Ray& ray, PickPoints& result, bool create = false);
// TODO: remove the FIRST_INPUT, SECOND_INPUT, IS_CUT states
enum { DISABLED, PRESENT, ACTIVE, ATTACHED, FIRST_INPUT, SECOND_INPUT, IS_CUT } state; // FIRST_INPUT and SECOND_INPUT are states to control inputs points for incision in a triangular mesh
int button; // index of activated button (only valid in ACTIVE state)
Mat4x4d transform;
Real rayRadius0, rayRadius1;
helper::vector<BasePickingManager*> pickManagers;
helper::vector<PickPoints*> pickPoints;
// TopologicalChangeManager object to handle topological changes
TopologicalChangeManager topo_changeManager;
};
template<class TDataTypes, class ContactForceField>
class DefaultPickingManager : public BasePickingManager
{
public:
typedef TDataTypes DataTypes;
typedef typename DataTypes::Real Real;
typedef typename DataTypes::Coord Coord;
Data<int> button;
DefaultPickingManager();
virtual bool attach(int button, PickPoints& a)
{
if (button == this->button.getValue())
{
if (a.model1 && a.model2)
return attach(core::CollisionElementIterator(a.model1, a.elem1), core::CollisionElementIterator(a.model2, a.elem2), a.dist, a.p1, a.p2);
else if (a.model1 && a.mstate2)
return attach(core::CollisionElementIterator(a.model1, a.elem1), a.mstate2, a.elem2, a.dist, a.p1, a.p2);
}
return false;
}
virtual bool isAttached()
{
return !attachedPoints.empty();
}
virtual void release();
virtual void update();
virtual void draw();
template <class RealObject>
static bool canCreate( RealObject*& obj, core::objectmodel::BaseContext* context)
{
if (context != NULL)
{
core::componentmodel::behavior::MechanicalState<DataTypes>* p = NULL;
context->get(p, core::objectmodel::BaseContext::SearchRoot);
if (p == NULL)
return false;
}
return BasePickingManager::canCreate(obj, context);
}
protected:
virtual component::MechanicalObject<DataTypes>* getMState(core::objectmodel::BaseContext* context);
virtual bool attach(core::CollisionElementIterator elem1, core::CollisionElementIterator elem2, double dist, Vector3 p1, Vector3 p2);
virtual bool attach(core::CollisionElementIterator elem1, core::componentmodel::behavior::BaseMechanicalState* mstate2, int elem2, double dist, Vector3 p1, Vector3 p2);
virtual void addContact(ContactForceField* ff, int index1, int index2, double stiffness, double mu_v, double length, const Vector3& p1, const Vector3& p2);
sofa::helper::vector< std::pair<Ray,double> > attachedPoints;
component::MechanicalObject<DataTypes>* mechanicalObject;
sofa::helper::vector<core::componentmodel::behavior::BaseForceField*> forcefields;
sofa::helper::vector<BaseContactMapper<DataTypes>*> mappers;
};
} // namespace collision
} // namespace component
} // namespace sofa
#endif
|