Skip to content

Commit

Permalink
[BeamAdapterActionController] Update Actions reading/writting and IRC…
Browse files Browse the repository at this point in the history
…trl control (#111)

* Change BeamAdapterActions

(cherry picked from commit ed6c66761b7e838ae0cb62d9e5a53854ee3d9b30)

* Fix reload of BeamActions

* Add BeamActions warning at init

* [IRCtrl] Unactive InterventionalRadiologyController events if a BeamAdapterActionController is already linked to it. For backward compatinility

* Fix applyInterventionalRadioCtrl need to be called after action are called, move the call directly in the beamAction ctrl to avoid problem of component order in scene...
  • Loading branch information
epernod authored Nov 15, 2023
1 parent e06c1d6 commit 6f2b6e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ using namespace sofa::beamadapter;

template <class DataTypes>
BeamAdapterActionController<DataTypes>::BeamAdapterActionController()
: d_writeMode(initData(&d_writeMode, false, "writeMode", "If true, will accumulate actions from keyboard and dump the actions and times when key 'E' is pressed."))
: d_writeMode(initData(&d_writeMode, true, "writeMode", "If true, will accumulate actions from keyboard and dump the actions and times when key 'E' is pressed."))
, d_actions(initData(&d_actions, "actions", "List of actions to script the BeamAdapter"))
, d_actionString(initData(&d_actionString, "actionString", "List of actions as string to script the BeamAdapter"))
, d_timeSteps(initData(&d_timeSteps, "timeSteps", "List of key times corresponding to the actions"))
Expand All @@ -48,11 +48,20 @@ void BeamAdapterActionController<DataTypes>::init()
{
msg_error() << "No l_interventionController given. Component will be set to Invalid.";
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
return;
}

// the controller must listen to the event (in particular BeginAnimationStep event)
this->f_listening.setValue(true);

if (d_writeMode.getValue() && (d_actions.isSet() || d_actionString.isSet()))
{
msg_warning() << "WriteMode is set to on but a list of actions has been set as input. The list will be overwritten.";
}

interventionCtrl* ctrl = l_interventionController.get();
ctrl->useBeamAction(true);

this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);
}

Expand Down Expand Up @@ -83,32 +92,16 @@ void BeamAdapterActionController<DataTypes>::onKeyPressedEvent(core::objectmodel
m_currAction = BeamAdapterAction::USE_TOOL_0;
break;
case 20: // droite = 20
if (m_currAction == BeamAdapterAction::SPIN_RIGHT)
m_currAction = BeamAdapterAction::NO_ACTION;
else
m_currAction = BeamAdapterAction::SPIN_RIGHT;

m_currAction = BeamAdapterAction::SPIN_RIGHT;
break;
case 18: // gauche = 18
if (m_currAction == BeamAdapterAction::SPIN_LEFT)
m_currAction = BeamAdapterAction::NO_ACTION;
else
m_currAction = BeamAdapterAction::SPIN_LEFT;

m_currAction = BeamAdapterAction::SPIN_LEFT;
break;
case 19: // fleche haut = 19
if (m_currAction == BeamAdapterAction::MOVE_FORWARD)
m_currAction = BeamAdapterAction::NO_ACTION;
else
m_currAction = BeamAdapterAction::MOVE_FORWARD;

m_currAction = BeamAdapterAction::MOVE_FORWARD;
break;
case 21: // bas = 21
if (m_currAction == BeamAdapterAction::MOVE_BACKWARD)
m_currAction = BeamAdapterAction::NO_ACTION;
else
m_currAction = BeamAdapterAction::MOVE_BACKWARD;

m_currAction = BeamAdapterAction::MOVE_BACKWARD;
break;
default:
m_currAction = BeamAdapterAction::NO_ACTION;
Expand All @@ -121,56 +114,47 @@ template <class DataTypes>
void BeamAdapterActionController<DataTypes>::onBeginAnimationStep(const double /*dt*/)
{
const auto currentTime = this->getContext()->getTime();
interventionCtrl* ctrl = l_interventionController.get();

if (d_writeMode.getValue())
{
interventionCtrl* ctrl = l_interventionController.get();
ctrl->applyAction(m_currAction);
if (m_currAction == BeamAdapterAction::NO_ACTION)
return ctrl->applyInterventionalRadiologyController();

if (m_lastAction != m_currAction)
{
auto times = sofa::helper::WriteAccessor(d_timeSteps);
auto actions = sofa::helper::WriteAccessor(d_actions);
times.push_back(currentTime);
actions.push_back(int(m_currAction));

if (m_exportActions)
{
std::cout << "timeSteps='" << times.wref() << "'" << std::endl;
std::cout << "actions='" << actions.wref() << "'" << std::endl;
}
ctrl->applyAction(m_currAction);

m_lastAction = m_currAction;
}
auto times = sofa::helper::WriteAccessor(d_timeSteps);
auto actions = sofa::helper::WriteAccessor(d_actions);
times.push_back(currentTime);
actions.push_back(int(m_currAction));

if (m_currAction >= BeamAdapterAction::SWITCH_NEXT_TOOL) // action regarding tool needs only to be triggered once
if (m_exportActions)
{
m_currAction = BeamAdapterAction::NO_ACTION;
std::cout << "timeSteps='" << times.wref() << "'" << std::endl;
std::cout << "actions='" << actions.wref() << "'" << std::endl;
}

m_lastAction = m_currAction;
m_currAction = BeamAdapterAction::NO_ACTION;
}
else
{
const type::vector<Real>& times = d_timeSteps.getValue();
if (!times.empty())
{
if (m_readStep < times.size())
{
Real time = times[m_readStep];
if (currentTime >= time) // check if another key time has been reached and change action
{
m_currAction = BeamAdapterAction(d_actions.getValue()[m_readStep]);
m_readStep++;
}
}
if (!times.empty() && m_readStep < times.size())
{
const Real& time = times[m_readStep];

interventionCtrl* ctrl = l_interventionController.get();
ctrl->applyAction(m_currAction);
if (currentTime >= time) // check if another key time has been reached and change action
{
m_currAction = BeamAdapterAction(d_actions.getValue()[m_readStep]);
m_readStep++;

if (m_currAction >= BeamAdapterAction::SWITCH_NEXT_TOOL) // action regarding tool needs only to be triggered once
{
m_currAction = BeamAdapterAction::NO_ACTION;
ctrl->applyAction(m_currAction);
}
}
}

ctrl->applyInterventionalRadiologyController();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class InterventionalRadiologyController : public MechanicalStateController<DataT
int getTotalNbEdges()const;

void applyAction(sofa::beamadapter::BeamAdapterAction action);
/// Method to warn this controller that a BeamActionController is controlling the scene. Will bypass the event handling in this component.
void useBeamAction(bool value) { m_useBeamActions = value; }

/// Getter to the tools curviline abscisses sorted @sa m_nodeCurvAbs at the current timestep.
[[nodiscard]] const type::vector<Real>& getCurrentCurvAbscisses() const { return m_nodeCurvAbs; }
Expand Down Expand Up @@ -165,7 +167,7 @@ class InterventionalRadiologyController : public MechanicalStateController<DataT
Data<unsigned int> d_indexFirstNode; // First Node simulated



bool m_useBeamActions = false;
bool m_FF, m_RW, m_sensored;
FixedConstraint<DataTypes> * m_fixedConstraint;
type::vector<int> m_droppedInstruments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ void InterventionalRadiologyController<DataTypes>::onMouseEvent(MouseEvent * mev
template <class DataTypes>
void InterventionalRadiologyController<DataTypes>::onKeyPressedEvent(KeypressedEvent *kev)
{
if (m_useBeamActions)
return;

/// Control keys for interventonal Radiology simulations:
switch(kev->getKey())
{
Expand Down Expand Up @@ -309,6 +312,9 @@ void InterventionalRadiologyController<DataTypes>::onBeginAnimationStep(const do
{
SOFA_UNUSED(dt);

if (m_useBeamActions)
return;

BaseContext* context = getContext();
auto xInstrTip = sofa::helper::getWriteOnlyAccessor(d_xTip);
if(m_FF || m_RW)
Expand Down

0 comments on commit 6f2b6e0

Please sign in to comment.