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 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
/******************************************************************************
* 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_INL
#define SOFA_COMPONENT_COLLISION_RAYPICKINTERACTOR_INL
#include <sofa/component/collision/RayPickInteractor.h>
#include <sofa/component/collision/RayContact.h>
#include <sofa/component/collision/SphereModel.h>
#include <sofa/component/collision/TriangleModel.h>
#include <sofa/component/collision/TetrahedronModel.h>
#include <sofa/component/collision/SphereTreeModel.h>
#include <sofa/component/collision/DistanceGridCollisionModel.h>
#include <sofa/component/mapping/BarycentricMapping.h>
#include <sofa/component/mapping/RigidMapping.h>
#include <sofa/component/mapping/SkinningMapping.h>
#include <sofa/component/forcefield/StiffSpringForceField.h>
#include <sofa/component/forcefield/VectorSpringForceField.h>
#include <sofa/component/container/MechanicalObject.h>
#include <sofa/core/componentmodel/behavior/MechanicalState.h>
#include <sofa/helper/system/gl.h>
namespace sofa
{
namespace component
{
namespace collision
{
typedef mapping::SkinningMapping<core::componentmodel::behavior::MechanicalMapping<core::componentmodel::behavior::MechanicalState<defaulttype::Rigid3Types>, core::componentmodel::behavior::MechanicalState<defaulttype::Vec3Types> > > MySkinningMapping;
using namespace sofa::defaulttype;
template<class DataTypes, class ContactForceField>
DefaultPickingManager<DataTypes,ContactForceField>::DefaultPickingManager()
: button( initData( &button, 0, "button", "mouse button which this manager responds to (0 = Left, 1 = Right)" ) ) //", 2 = Middle" has been removed as, in the current state, middle is hard coded for cutting
, mechanicalObject(NULL)
{
}
template<class DataTypes, class ContactForceField>
void DefaultPickingManager<DataTypes,ContactForceField>::release()
{
// release the attached body
for (unsigned int i=0; i<forcefields.size(); i++)
{
sout << "DefaultPickingManager: Deleting "<<forcefields[i]->getClassName()<<" "<<forcefields[i]->getName()<<sendl;
forcefields[i]->cleanup();
forcefields[i]->getContext()->removeObject(forcefields[i]);
delete forcefields[i];
}
forcefields.clear();
for (unsigned int i=0;i<mappers.size();i++)
{
mappers[i]->cleanup();
delete mappers[i];
}
mappers.clear();
attachedPoints.clear();
}
template<class DataTypes, class ContactForceField>
void DefaultPickingManager<DataTypes,ContactForceField>::update()
{
if (mechanicalObject!=NULL && !attachedPoints.empty())
{
typename DataTypes::VecCoord& x = *mechanicalObject->getX();
for (unsigned int i=0;i<attachedPoints.size();i++)
{
Ray& ray = attachedPoints[i].first;
x[i] = ray.origin()+ray.direction()*attachedPoints[i].second;
}
}
}
template<class DataTypes, class ContactForceField>
void DefaultPickingManager<DataTypes,ContactForceField>::draw()
{
if (!forcefields.empty())
{
glColor4f(1,1,1,1);
glLineWidth(3);
for (unsigned int i=0;i<forcefields.size();i++)
{
if (forcefields[i] != NULL && forcefields[i]->getContext() != NULL)
{
// Hack to make forcefields visible
bool b = forcefields[i]->getContext()->getShowInteractionForceFields();
forcefields[i]->getContext()->setShowInteractionForceFields(true);
forcefields[i]->draw();
forcefields[i]->getContext()->setShowInteractionForceFields(b);
}
}
glLineWidth(1);
}
}
template<class DataTypes, class ContactForceField>
component::MechanicalObject<DataTypes>* DefaultPickingManager<DataTypes,ContactForceField>::getMState(core::objectmodel::BaseContext* context)
{
if (mechanicalObject == NULL)
{
mechanicalObject = new component::MechanicalObject<DataTypes>();
simulation::Node* parent = dynamic_cast<simulation::Node*>(context);
if (parent != NULL)
{
std::string name = "contactMouse";
name += DataTypes::Name();
simulation::Node* child = simulation::getSimulation()->newNode(name);
parent->addChild(child);
child->updateContext();
child->addObject(mechanicalObject);
if (parent->get<sofa::core::componentmodel::behavior::OdeSolver>()) // we are below a solver, use a VoidMapping to indicate that it should not touch this object
child->addObject(new component::mapping::VoidMapping);
}
else
{
context->addObject(mechanicalObject);
//mechanicalObject->setContext(context);
}
mechanicalObject->init();
}
return mechanicalObject;
}
template<class DataTypes, class ContactForceField>
bool DefaultPickingManager<DataTypes,ContactForceField>::attach(core::CollisionElementIterator elem1, core::CollisionElementIterator elem2, double dist, Vector3 p1, Vector3 p2)
{
core::CollisionModel* model1 = elem1.getCollisionModel();
core::CollisionModel* model2 = elem2.getCollisionModel();
RayModel* rayModel = dynamic_cast<RayModel*>(model1);
if (rayModel == NULL) return false;
BaseContactMapper<DataTypes>* mapper2 = BaseContactMapper<DataTypes>::Create(model2);
if (mapper2 == NULL)
{
serr << "DefaultPickingManager: ContactMapper from "<<model2->getClassName()<<"<"<<model2->getTemplateName()<<"> to "<<DataTypes::Name()<<" not found."<<sendl;
//sofa::helper::printFactoryLog();
return false;
}
component::MechanicalObject<DataTypes>* mstate1 = getMState(rayModel->getContext());
std::string name = "contactMouse";
name += DataTypes::Name();
MechanicalState<DataTypes>* mstate2 = mapper2->createMapping(name.c_str());
ContactForceField* ff = new ContactForceField(mstate1, mstate2);
name = this->getName();
name += "-";
name += model2->getClassName();
ff->setName(name);
ff->clear(1);
mapper2->resize(1);
int index1 = attachedPoints.size();
mstate1->resize(index1+1);
int index2 = elem2.getIndex();
Real r1 = 0.0;
Real r2 = 0.0;
// Create mapping for second point
Coord p; p = p2;
index2 = mapper2->addPoint(p, index2, r2);
mapper2->update();
p2 = (*mstate2->getX())[index2];
double stiffness = (elem1.getContactStiffness() * elem2.getContactStiffness());
double mu_v = (elem1.getContactFriction() + elem2.getContactFriction());
addContact(ff, index1, index2, stiffness, mu_v, r1+r2, p1, p2);
mstate2->getContext()->addObject(ff);
ff->init();
forcefields.push_back(ff);
mappers.push_back(mapper2);
attachedPoints.push_back(std::make_pair(Ray(rayModel,elem1.getIndex()), dist));
return true;
}
template<class DataTypes, class ContactForceField>
bool DefaultPickingManager<DataTypes,ContactForceField>::attach(core::CollisionElementIterator elem1, core::componentmodel::behavior::BaseMechanicalState* model2, int elem2, double dist, Vector3 p1, Vector3 p2)
{
core::CollisionModel* model1 = elem1.getCollisionModel();
RayModel* rayModel = dynamic_cast<RayModel*>(model1);
if (rayModel == NULL) return false;
MechanicalState<DataTypes>* mstate2 = dynamic_cast<MechanicalState<DataTypes>*>(model2);
if (mstate2 == NULL)
{
serr << "DefaultPickingManager: MechanicalState "<<model2->getClassName()<<"<"<<model2->getTemplateName()<<"> not supported."<<sendl;
return false;
}
component::MechanicalObject<DataTypes>* mstate1 = getMState(rayModel->getContext());
std::string name = "contactMouse";
name += DataTypes::Name();
ContactForceField* ff = new ContactForceField(mstate1, mstate2);
name = this->getName();
name += "-";
name += model2->getClassName();
ff->setName(name);
ff->clear(1);
int index1 = attachedPoints.size();
mstate1->resize(index1+1);
int index2 = elem2;
Real r1 = 0.0;
Real r2 = 0.0;
// Create mapping for second point
Coord p; p = p2;
p2 = (*mstate2->getX())[index2];
double stiffness = (elem1.getContactStiffness());
double mu_v = (elem1.getContactFriction());
addContact(ff, index1, index2, stiffness, mu_v, r1+r2, p1, p2);
mstate2->getContext()->addObject(ff);
ff->init();
forcefields.push_back(ff);
attachedPoints.push_back(std::make_pair(Ray(rayModel,elem1.getIndex()), dist));
return true;
}
template<>
void DefaultPickingManager<defaulttype::Vec3Types,forcefield::VectorSpringForceField<defaulttype::Vec3Types> >::addContact(forcefield::VectorSpringForceField<defaulttype::Vec3Types>* ff, int index1, int index2, double stiffness, double mu_v, double /*length*/, const Vector3& p1, const Vector3& p2);
template<>
void DefaultPickingManager<defaulttype::Vec3Types,forcefield::StiffSpringForceField<defaulttype::Vec3Types> >::addContact(forcefield::StiffSpringForceField<defaulttype::Vec3Types>* ff, int index1, int index2, double stiffness, double mu_v, double length, const Vector3& /*p1*/, const Vector3& /*p2*/);
} // namespace collision
} // namespace component
} // namespace sofa
#endif
|