diff --git a/src/BeamAdapter/component/controller/BeamAdapterActionController.inl b/src/BeamAdapter/component/controller/BeamAdapterActionController.inl index 312550be8..d573d4b18 100644 --- a/src/BeamAdapter/component/controller/BeamAdapterActionController.inl +++ b/src/BeamAdapter/component/controller/BeamAdapterActionController.inl @@ -31,7 +31,7 @@ using namespace sofa::beamadapter; template BeamAdapterActionController::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")) @@ -48,11 +48,20 @@ void BeamAdapterActionController::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); } @@ -83,32 +92,16 @@ void BeamAdapterActionController::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; @@ -121,56 +114,47 @@ template void BeamAdapterActionController::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& 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(); } diff --git a/src/BeamAdapter/component/controller/InterventionalRadiologyController.h b/src/BeamAdapter/component/controller/InterventionalRadiologyController.h index 8c13790e0..57306b203 100644 --- a/src/BeamAdapter/component/controller/InterventionalRadiologyController.h +++ b/src/BeamAdapter/component/controller/InterventionalRadiologyController.h @@ -110,6 +110,8 @@ class InterventionalRadiologyController : public MechanicalStateController& getCurrentCurvAbscisses() const { return m_nodeCurvAbs; } @@ -165,7 +167,7 @@ class InterventionalRadiologyController : public MechanicalStateController d_indexFirstNode; // First Node simulated - + bool m_useBeamActions = false; bool m_FF, m_RW, m_sensored; FixedConstraint * m_fixedConstraint; type::vector m_droppedInstruments; diff --git a/src/BeamAdapter/component/controller/InterventionalRadiologyController.inl b/src/BeamAdapter/component/controller/InterventionalRadiologyController.inl index a19d93ab2..c141539e8 100644 --- a/src/BeamAdapter/component/controller/InterventionalRadiologyController.inl +++ b/src/BeamAdapter/component/controller/InterventionalRadiologyController.inl @@ -248,6 +248,9 @@ void InterventionalRadiologyController::onMouseEvent(MouseEvent * mev template void InterventionalRadiologyController::onKeyPressedEvent(KeypressedEvent *kev) { + if (m_useBeamActions) + return; + /// Control keys for interventonal Radiology simulations: switch(kev->getKey()) { @@ -309,6 +312,9 @@ void InterventionalRadiologyController::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)