Skip to content

Commit

Permalink
First hollow class for mouse interaction using performer
Browse files Browse the repository at this point in the history
  • Loading branch information
bakpaul committed Mar 25, 2024
1 parent ca30479 commit a263da6
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <sofa/igtlink/utils/iGTLinkMouseInteractor.inl>
#include <sofa/core/ObjectFactory.h>

namespace sofa::openigtlink {

SOFA_DECL_CLASS(iGTLinkMouseInteractor)

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

}
44 changes: 44 additions & 0 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once
#include <sofa/gui/component/config.h>

#include <sofa/gui/component/performer/InteractionPerformer.h>
#include <sofa/component/collision/geometry/RayModel.h>
#include <sofa/component/statecontainer/MechanicalObject.h>

#include <sofa/core/collision/DetectionOutput.h>
#include <sofa/core/BehaviorModel.h>
#include <sofa/gui/component/performer/MouseInteractor.h>
#include <sofa/helper/OptionsGroup.h>
#include <sofa/core/objectmodel/DataCallback.h>
#include <chrono>


namespace sofa::openigtlink {

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

Data<type::Vec3> d_positions;
Data< sofa::helper::OptionsGroup > d_pickingType;
Data< double > d_springStiffness;
Data< unsigned > d_reactionTime;

iGTLinkMouseInteractor();

void updatePosition( SReal dt) override;

void positionChanged();
void attachmentChanged();
void handleEvent(sofa::core::objectmodel::Event *event);

private:
sofa::core::objectmodel::DataCallback c_positions;
sofa::core::objectmodel::DataCallback c_attachmentType;
std::chrono::high_resolution_clock::time_point m_lastChange;



};

}
70 changes: 70 additions & 0 deletions src/sofa/igtlink/utils/iGTLinkMouseInteractor.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include <sofa/igtlink/utils/iGTLinkMouseInteractor.h>
#include <sofa/simulation/AnimateBeginEvent.h>


namespace sofa::openigtlink
{

iGTLinkMouseInteractor::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"))
, d_reactionTime(initData(&d_reactionTime,20u, "reactionTime", "Time in milisecond of no change in the position to output a null stiffness"))
{
sofa::helper::OptionsGroup m_newoptiongroup{"constraint","spring"};
m_newoptiongroup.setSelectedItem("spring");
d_pickingType.setValue(m_newoptiongroup);

this->f_listening.setValue(true);

c_positions.addInput(&d_positions);
c_positions.addCallback(std::bind(&iGTLinkMouseInteractor::positionChanged,this));

c_attachmentType.addInput(&d_pickingType);
c_attachmentType.addCallback(std::bind(&iGTLinkMouseInteractor::attachmentChanged,this));

m_lastChange = std::chrono::high_resolution_clock::now();
//Make sure the constraint is inactive

}


void iGTLinkMouseInteractor::positionChanged()
{
m_lastChange = std::chrono::high_resolution_clock::now();
}

void iGTLinkMouseInteractor::attachmentChanged()
{
//Change performer
}


void iGTLinkMouseInteractor::updatePosition( SReal dt)
{
SOFA_UNUSED(dt);
//Do nothing
}


void iGTLinkMouseInteractor::handleEvent(sofa::core::objectmodel::Event *event)
{
if (dynamic_cast<sofa::simulation::AnimateBeginEvent*>(event))
{
std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - m_lastChange).count() > d_reactionTime.getValue())
{
//Deactivate the constraint
//Reproduce the start emthod from performer here.
}
else
{
//Activate the constraint
//Clear the performer
}
}
}

}

0 comments on commit a263da6

Please sign in to comment.