Skip to content

Commit 6f2b6e0

Browse files
authored
[BeamAdapterActionController] Update Actions reading/writting and IRCtrl 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...
1 parent e06c1d6 commit 6f2b6e0

3 files changed

Lines changed: 48 additions & 56 deletions

File tree

src/BeamAdapter/component/controller/BeamAdapterActionController.inl

Lines changed: 39 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using namespace sofa::beamadapter;
3131

3232
template <class DataTypes>
3333
BeamAdapterActionController<DataTypes>::BeamAdapterActionController()
34-
: 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."))
34+
: 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."))
3535
, d_actions(initData(&d_actions, "actions", "List of actions to script the BeamAdapter"))
3636
, d_actionString(initData(&d_actionString, "actionString", "List of actions as string to script the BeamAdapter"))
3737
, d_timeSteps(initData(&d_timeSteps, "timeSteps", "List of key times corresponding to the actions"))
@@ -48,11 +48,20 @@ void BeamAdapterActionController<DataTypes>::init()
4848
{
4949
msg_error() << "No l_interventionController given. Component will be set to Invalid.";
5050
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Invalid);
51+
return;
5152
}
5253

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

57+
if (d_writeMode.getValue() && (d_actions.isSet() || d_actionString.isSet()))
58+
{
59+
msg_warning() << "WriteMode is set to on but a list of actions has been set as input. The list will be overwritten.";
60+
}
61+
62+
interventionCtrl* ctrl = l_interventionController.get();
63+
ctrl->useBeamAction(true);
64+
5665
this->d_componentState.setValue(sofa::core::objectmodel::ComponentState::Valid);
5766
}
5867

@@ -83,32 +92,16 @@ void BeamAdapterActionController<DataTypes>::onKeyPressedEvent(core::objectmodel
8392
m_currAction = BeamAdapterAction::USE_TOOL_0;
8493
break;
8594
case 20: // droite = 20
86-
if (m_currAction == BeamAdapterAction::SPIN_RIGHT)
87-
m_currAction = BeamAdapterAction::NO_ACTION;
88-
else
89-
m_currAction = BeamAdapterAction::SPIN_RIGHT;
90-
95+
m_currAction = BeamAdapterAction::SPIN_RIGHT;
9196
break;
9297
case 18: // gauche = 18
93-
if (m_currAction == BeamAdapterAction::SPIN_LEFT)
94-
m_currAction = BeamAdapterAction::NO_ACTION;
95-
else
96-
m_currAction = BeamAdapterAction::SPIN_LEFT;
97-
98+
m_currAction = BeamAdapterAction::SPIN_LEFT;
9899
break;
99100
case 19: // fleche haut = 19
100-
if (m_currAction == BeamAdapterAction::MOVE_FORWARD)
101-
m_currAction = BeamAdapterAction::NO_ACTION;
102-
else
103-
m_currAction = BeamAdapterAction::MOVE_FORWARD;
104-
101+
m_currAction = BeamAdapterAction::MOVE_FORWARD;
105102
break;
106103
case 21: // bas = 21
107-
if (m_currAction == BeamAdapterAction::MOVE_BACKWARD)
108-
m_currAction = BeamAdapterAction::NO_ACTION;
109-
else
110-
m_currAction = BeamAdapterAction::MOVE_BACKWARD;
111-
104+
m_currAction = BeamAdapterAction::MOVE_BACKWARD;
112105
break;
113106
default:
114107
m_currAction = BeamAdapterAction::NO_ACTION;
@@ -121,56 +114,47 @@ template <class DataTypes>
121114
void BeamAdapterActionController<DataTypes>::onBeginAnimationStep(const double /*dt*/)
122115
{
123116
const auto currentTime = this->getContext()->getTime();
117+
interventionCtrl* ctrl = l_interventionController.get();
118+
124119
if (d_writeMode.getValue())
125120
{
126-
interventionCtrl* ctrl = l_interventionController.get();
127-
ctrl->applyAction(m_currAction);
121+
if (m_currAction == BeamAdapterAction::NO_ACTION)
122+
return ctrl->applyInterventionalRadiologyController();
128123

129-
if (m_lastAction != m_currAction)
130-
{
131-
auto times = sofa::helper::WriteAccessor(d_timeSteps);
132-
auto actions = sofa::helper::WriteAccessor(d_actions);
133-
times.push_back(currentTime);
134-
actions.push_back(int(m_currAction));
135-
136-
if (m_exportActions)
137-
{
138-
std::cout << "timeSteps='" << times.wref() << "'" << std::endl;
139-
std::cout << "actions='" << actions.wref() << "'" << std::endl;
140-
}
124+
ctrl->applyAction(m_currAction);
141125

142-
m_lastAction = m_currAction;
143-
}
126+
auto times = sofa::helper::WriteAccessor(d_timeSteps);
127+
auto actions = sofa::helper::WriteAccessor(d_actions);
128+
times.push_back(currentTime);
129+
actions.push_back(int(m_currAction));
144130

145-
if (m_currAction >= BeamAdapterAction::SWITCH_NEXT_TOOL) // action regarding tool needs only to be triggered once
131+
if (m_exportActions)
146132
{
147-
m_currAction = BeamAdapterAction::NO_ACTION;
133+
std::cout << "timeSteps='" << times.wref() << "'" << std::endl;
134+
std::cout << "actions='" << actions.wref() << "'" << std::endl;
148135
}
136+
137+
m_lastAction = m_currAction;
138+
m_currAction = BeamAdapterAction::NO_ACTION;
149139
}
150140
else
151141
{
152142
const type::vector<Real>& times = d_timeSteps.getValue();
153-
if (!times.empty())
154-
{
155-
if (m_readStep < times.size())
156-
{
157-
Real time = times[m_readStep];
158-
if (currentTime >= time) // check if another key time has been reached and change action
159-
{
160-
m_currAction = BeamAdapterAction(d_actions.getValue()[m_readStep]);
161-
m_readStep++;
162-
}
163-
}
143+
if (!times.empty() && m_readStep < times.size())
144+
{
145+
const Real& time = times[m_readStep];
164146

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

168-
if (m_currAction >= BeamAdapterAction::SWITCH_NEXT_TOOL) // action regarding tool needs only to be triggered once
169-
{
170-
m_currAction = BeamAdapterAction::NO_ACTION;
152+
ctrl->applyAction(m_currAction);
171153
}
172154
}
173155
}
156+
157+
ctrl->applyInterventionalRadiologyController();
174158
}
175159

176160

src/BeamAdapter/component/controller/InterventionalRadiologyController.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class InterventionalRadiologyController : public MechanicalStateController<DataT
110110
int getTotalNbEdges()const;
111111

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

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

167169

168-
170+
bool m_useBeamActions = false;
169171
bool m_FF, m_RW, m_sensored;
170172
FixedConstraint<DataTypes> * m_fixedConstraint;
171173
type::vector<int> m_droppedInstruments;

src/BeamAdapter/component/controller/InterventionalRadiologyController.inl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ void InterventionalRadiologyController<DataTypes>::onMouseEvent(MouseEvent * mev
248248
template <class DataTypes>
249249
void InterventionalRadiologyController<DataTypes>::onKeyPressedEvent(KeypressedEvent *kev)
250250
{
251+
if (m_useBeamActions)
252+
return;
253+
251254
/// Control keys for interventonal Radiology simulations:
252255
switch(kev->getKey())
253256
{
@@ -309,6 +312,9 @@ void InterventionalRadiologyController<DataTypes>::onBeginAnimationStep(const do
309312
{
310313
SOFA_UNUSED(dt);
311314

315+
if (m_useBeamActions)
316+
return;
317+
312318
BaseContext* context = getContext();
313319
auto xInstrTip = sofa::helper::getWriteOnlyAccessor(d_xTip);
314320
if(m_FF || m_RW)

0 commit comments

Comments
 (0)