Skip to content

Commit

Permalink
Modifcation due to virtualization changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpaul committed Apr 9, 2024
1 parent 9e8ef99 commit 4d4fd08
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 25 deletions.
2 changes: 1 addition & 1 deletion scenes/iGTLinkMouseInteractor_Lagrange.scn
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
<PointSetTopologyContainer name="Container" />
<PointSetTopologyModifier name="Modifier"/>
<MechanicalObject name="mstate" template="Vec3d" position="0.5 0.05 0.05" drawMode="2" showObjectScale="0.01" showObject="false"/>
<iGTLinkMouseInteractor pickingType="spring" position="0 0 0" reactionTime="20000" destCollisionModel="@../FEM/Surface/Torus8CMT"/>
<iGTLinkMouseInteractor pickingType="constraint" position="0 0 0" reactionTime="20000" destCollisionModel="@../FEM/Surface/Torus8CMT"/>

</Node>

Expand Down
2 changes: 1 addition & 1 deletion src/sofa/igtlink/utils/iGTLinkMouseInteractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace sofa::openigtlink {
SOFA_DECL_CLASS(iGTLinkMouseInteractor)

int iGTLinkMouseInteractorClass = core::RegisterObject("")
.add<iGTLinkMouseInteractor>(true);
.add<iGTLinkMouseInteractor<defaulttype::Vec3Types>>(true);

}
13 changes: 9 additions & 4 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@

namespace sofa::openigtlink {

class iGTLinkMouseInteractor : public sofa::gui::component::performer::MouseInteractor<defaulttype::Vec3Types> {
template<class DataTypes>
class iGTLinkMouseInteractor : public sofa::gui::component::performer::MouseInteractor<DataTypes> {
public:
SOFA_CLASS(iGTLinkMouseInteractor, SOFA_TEMPLATE(sofa::gui::component::performer::MouseInteractor, defaulttype::Vec3Types));
SOFA_CLASS(SOFA_TEMPLATE(iGTLinkMouseInteractor,DataTypes), SOFA_TEMPLATE(sofa::gui::component::performer::MouseInteractor,DataTypes));

Data<sofa::type::vector<type::Vec3> > d_positions;
typedef typename DataTypes::Coord Coord;
typedef typename DataTypes::VecCoord VecCoord;


Data<sofa::type::vector<Coord> > d_positions;
Data< sofa::helper::OptionsGroup > d_pickingType;
Data< double > d_springStiffness;
Data< unsigned > d_reactionTime;
Expand All @@ -46,7 +51,7 @@ class iGTLinkMouseInteractor : public sofa::gui::component::performer::MouseInte
sofa::core::objectmodel::DataCallback c_positions;
sofa::core::objectmodel::DataCallback c_attachmentType;
std::chrono::high_resolution_clock::time_point m_lastChange;
std::unique_ptr<sofa::gui::component::performer::BaseAttachBodyPerformer> m_performer;
std::unique_ptr<sofa::gui::component::performer::BaseAttachBodyPerformer<DataTypes>> m_performer;

bool m_performerStarted;

Expand Down
46 changes: 27 additions & 19 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.inl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <sofa/simulation/AnimateEndEvent.h>
#include <sofa/gui/component/performer/ConstraintAttachBodyPerformer.h>
#include <sofa/gui/component/performer/AttachBodyPerformer.h>
#include <sofa/gui/component/performer/BaseAttachBodyPerformer.inl>
#include <sofa/component/collision/geometry/LineModel.h>
#include <sofa/component/collision/geometry/TriangleModel.h>
#include <sofa/component/collision/geometry/PointModel.h>
Expand All @@ -13,7 +14,8 @@
namespace sofa::openigtlink
{

iGTLinkMouseInteractor::iGTLinkMouseInteractor()
template<class DataTypes>
iGTLinkMouseInteractor<DataTypes>::iGTLinkMouseInteractor()
: d_pickingType(initData(&d_pickingType,"pickingType","Mouse interaction type, could be \'constraint\' or \'spring\'"))
, d_positions(initData(&d_positions, "position", "Position"))
, d_springStiffness(initData(&d_springStiffness,10.0,"springStiffness","Stiffness of attachment spring used for interaction"))
Expand All @@ -40,56 +42,60 @@ iGTLinkMouseInteractor::iGTLinkMouseInteractor()
}


void iGTLinkMouseInteractor::positionChanged()
template<class DataTypes>
void iGTLinkMouseInteractor<DataTypes>::positionChanged()
{
m_lastChange = std::chrono::high_resolution_clock::now();
}

void iGTLinkMouseInteractor::attachmentChanged()
template<class DataTypes>
void iGTLinkMouseInteractor<DataTypes>::attachmentChanged()
{
if(m_performer)
{
m_performer->clear();
}
if(d_pickingType.getValue().getSelectedItem() == std::string("constraint"))
{
m_performer.reset(new sofa::gui::component::performer::ConstraintAttachBodyPerformer<defaulttype::Vec3Types>(this));
m_performer.reset(new sofa::gui::component::performer::ConstraintAttachBodyPerformer<DataTypes>(this));
}
else
{
m_performer.reset(new sofa::gui::component::performer::AttachBodyPerformer<defaulttype::Vec3Types>(this));
m_performer.reset(new sofa::gui::component::performer::AttachBodyPerformer<DataTypes>(this));
}
}


void iGTLinkMouseInteractor::updatePosition( SReal dt)
template<class DataTypes>
void iGTLinkMouseInteractor<DataTypes>::updatePosition( SReal dt)
{
SOFA_UNUSED(dt);
//Do nothing, see handleEvent
}

void iGTLinkMouseInteractor::startPerformer()
template<class DataTypes>
void iGTLinkMouseInteractor<DataTypes>::startPerformer()
{
if(!m_performer || m_performerStarted || !d_positions.getValue().size())
return;

sofa::gui::component::performer::BodyPicked bodyPicked;
bodyPicked.indexCollisionElement = findCollidingElem(d_positions.getValue()[0]);
bodyPicked.mstate = l_destCollisionModel->getContext()->get<sofa::core::behavior::BaseMechanicalState>();
bodyPicked.mstate = l_destCollisionModel->getContext()->template get<sofa::core::behavior::BaseMechanicalState>();
bodyPicked.point = d_positions.getValue()[0];
bodyPicked.body = l_destCollisionModel.get();
this->setBodyPicked(bodyPicked);
this->setMouseAttached(true);

m_performer->start_partial(bodyPicked);
m_performer->startPartial(bodyPicked);
m_performer->getInteractionObject()->init();
m_performer->getInteractionObject()->bwdInit();
m_performer->getInteractionObject()->f_listening.setValue(true);
m_performerStarted = true;
}


void iGTLinkMouseInteractor::stopPerformer()
template<class DataTypes>
void iGTLinkMouseInteractor<DataTypes>::stopPerformer()
{
if(!m_performer || !m_performerStarted)
return;
Expand All @@ -99,15 +105,16 @@ void iGTLinkMouseInteractor::stopPerformer()
m_performerStarted = false;
}

sofa::Index iGTLinkMouseInteractor::findCollidingElem(const type::Vec3& pos) const
template<class DataTypes>
sofa::Index iGTLinkMouseInteractor<DataTypes>::findCollidingElem(const type::Vec3& pos) const
{
sofa::Index closestElem = l_destCollisionModel->begin().getIndex();
double closestDist = std::numeric_limits<double>::max();
auto* topo = l_destCollisionModel->getCollisionTopology();
auto* mstate = l_destCollisionModel->getContext()->get<sofa::core::behavior::MechanicalState<defaulttype::Vec3Types> >();
sofa::helper::ReadAccessor<Data <defaulttype::Vec3Types::VecCoord> > destPositions = mstate->read(core::VecCoordId::position());
auto* mstate = l_destCollisionModel->getContext()->template get<sofa::core::behavior::MechanicalState<DataTypes> >();
sofa::helper::ReadAccessor<Data <VecCoord> > destPositions = mstate->read(core::VecCoordId::position());

if(dynamic_cast<sofa::component::collision::geometry::PointCollisionModel<defaulttype::Vec3Types>* >(l_destCollisionModel.get()))
if(dynamic_cast<sofa::component::collision::geometry::PointCollisionModel<DataTypes>* >(l_destCollisionModel.get()))
{
for(unsigned id = 0; id < destPositions.size(); ++id)
{
Expand All @@ -119,7 +126,7 @@ sofa::Index iGTLinkMouseInteractor::findCollidingElem(const type::Vec3& pos) con
}
}
}
else if(dynamic_cast<sofa::component::collision::geometry::LineCollisionModel<defaulttype::Vec3Types>* >(l_destCollisionModel.get()))
else if(dynamic_cast<sofa::component::collision::geometry::LineCollisionModel<DataTypes>* >(l_destCollisionModel.get()))
{
for(unsigned id = 0; id < topo->getLines().size(); ++id)
{
Expand All @@ -131,7 +138,7 @@ sofa::Index iGTLinkMouseInteractor::findCollidingElem(const type::Vec3& pos) con
}
}
}
else if(dynamic_cast<sofa::component::collision::geometry::TriangleCollisionModel<defaulttype::Vec3Types>* >(l_destCollisionModel.get()))
else if(dynamic_cast<sofa::component::collision::geometry::TriangleCollisionModel<DataTypes>* >(l_destCollisionModel.get()))
{
for(unsigned id = 0; id < topo->getTriangles().size(); ++id)
{
Expand All @@ -147,7 +154,8 @@ sofa::Index iGTLinkMouseInteractor::findCollidingElem(const type::Vec3& pos) con
}


void iGTLinkMouseInteractor::handleEvent(sofa::core::objectmodel::Event *event)
template<class DataTypes>
void iGTLinkMouseInteractor<DataTypes>::handleEvent(sofa::core::objectmodel::Event *event)
{
if (dynamic_cast<sofa::simulation::AnimateEndEvent*>(event))
{
Expand All @@ -166,7 +174,7 @@ void iGTLinkMouseInteractor::handleEvent(sofa::core::objectmodel::Event *event)
if(!m_performerStarted)
startPerformer();

auto* mstate = this->getContext()->get<sofa::core::behavior::MechanicalState<defaulttype::Vec3Types> >();
auto* mstate = this->getContext()->template get<sofa::core::behavior::MechanicalState<DataTypes> >();
sofa::helper::WriteAccessor<Data <defaulttype::Vec3Types::VecCoord> > positions = mstate->write(core::VecCoordId::position());

mstate->resize(1);
Expand Down

0 comments on commit 4d4fd08

Please sign in to comment.