7 #include "visuVTKAdaptor/SPickerInteractor.hpp" 9 #include <fwCom/Signal.hpp> 10 #include <fwCom/Signal.hxx> 11 #include <fwCom/Signals.hpp> 13 #include <fwCore/HiResClock.hpp> 15 #include <fwData/Composite.hpp> 16 #include <fwData/Material.hpp> 17 #include <fwData/Reconstruction.hpp> 19 #include <fwRenderVTK/vtk/Helpers.hpp> 21 #include <fwServices/macros.hpp> 23 #include <boost/tokenizer.hpp> 25 #include <vtkAbstractPropPicker.h> 27 #include <vtkCellPicker.h> 28 #include <vtkCommand.h> 29 #include <vtkCubeSource.h> 30 #include <vtkPolyDataMapper.h> 31 #include <vtkRenderWindowInteractor.h> 33 #define START_INTERACTION_EVENT vtkCommand::LeftButtonPressEvent 34 #define STOP_INTERACTION_EVENT vtkCommand::LeftButtonReleaseEvent 41 const std::map< unsigned long, ::fwDataTools::PickingInfo::Event > SPickerInteractor::s_vtkEventIDConversion
43 { vtkCommand::LeftButtonReleaseEvent, ::fwDataTools::PickingInfo::Event::MOUSE_LEFT_UP },
44 { vtkCommand::RightButtonReleaseEvent, ::fwDataTools::PickingInfo::Event::MOUSE_RIGHT_UP },
45 { vtkCommand::MiddleButtonReleaseEvent, ::fwDataTools::PickingInfo::Event::MOUSE_MIDDLE_UP },
46 { vtkCommand::MouseWheelForwardEvent, ::fwDataTools::PickingInfo::Event::MOUSE_WHEELFORWARD },
47 { vtkCommand::LeftButtonPressEvent, ::fwDataTools::PickingInfo::Event::MOUSE_LEFT_DOWN },
48 { vtkCommand::RightButtonPressEvent, ::fwDataTools::PickingInfo::Event::MOUSE_RIGHT_DOWN },
49 { vtkCommand::MiddleButtonPressEvent, ::fwDataTools::PickingInfo::Event::MOUSE_MIDDLE_DOWN },
50 { vtkCommand::MouseWheelBackwardEvent, ::fwDataTools::PickingInfo::Event::MOUSE_WHEELBACKWARD },
51 { vtkCommand::MouseMoveEvent, ::fwDataTools::PickingInfo::Event::MOUSE_MOVE },
52 { vtkCommand::KeyPressEvent, ::fwDataTools::PickingInfo::Event::KEY_PRESS }
57 { std::string(
"MOUSE_LEFT_UP"), MOUSE_LEFT_UP },
58 { std::string(
"MOUSE_RIGHT_UP"), MOUSE_RIGHT_UP },
59 { std::string(
"MOUSE_MIDDLE_UP"), MOUSE_MIDDLE_UP },
60 { std::string(
"MOUSE_WHEELBACKWARD"), MOUSE_WHEELBACKWARD },
61 { std::string(
"MOUSE_LEFT_DOWN"), MOUSE_LEFT_DOWN },
62 { std::string(
"MOUSE_RIGHT_DOWN"), MOUSE_RIGHT_DOWN },
63 { std::string(
"MOUSE_MIDDLE_DOWN"), MOUSE_MIDDLE_DOWN },
64 { std::string(
"MOUSE_WHEELBACKWARD"), MOUSE_WHEELBACKWARD },
65 { std::string(
"MOUSE_MOVE"), MOUSE_MOVE },
66 { std::string(
"KEY_PRESS"), KEY_PRESS }
69 const ::fwCom::Signals::SignalKeyType SPickerInteractor::s_PICKED_SIGNAL =
"picked";
95 virtual void Execute( vtkObject* caller,
unsigned long eventId,
void*)
97 SLM_ASSERT(
"m_adaptor not instanced", m_adaptor);
98 SLM_ASSERT(
"m_picker not instanced", m_picker);
100 this->process(vtkRenderWindowInteractor::SafeDownCast(caller), eventId);
110 m_adaptor->getInteractor()->GetEventPosition(x, y);
115 return (m_picker->Pick( display, m_adaptor->getRenderer() ) != 0);
120 void process(vtkRenderWindowInteractor* caller,
unsigned long eventId)
123 if( m_eventId->find( static_cast< SPickerInteractor::EventID>(eventId) ) != m_eventId->end() )
126 if(eventId == SPickerInteractor::MOUSE_MOVE)
131 if( m_skipMove % 10 )
137 if(this->pickSomething())
140 ::fwRenderVTK::vtk::getNearestPickedPosition(m_picker, m_adaptor->getRenderer(), info.
m_worldPos);
144 caller->GetControlKey() ? ::fwDataTools::PickingInfo::CTRL : ::fwDataTools::PickingInfo::NONE;
146 caller->GetShiftKey() ? ::fwDataTools::PickingInfo::SHIFT : ::fwDataTools::PickingInfo::NONE;
148 vtkCellPicker* picker = vtkCellPicker::SafeDownCast( m_picker );
151 info.
m_cellId =
static_cast<int>(picker->GetCellId());
155 const auto iter = SPickerInteractor::s_vtkEventIDConversion.find(eventId);
156 SLM_ASSERT(
"Unknown eventId", iter != SPickerInteractor::s_vtkEventIDConversion.end());
163 info.m_timestamp = ::fwCore::HiResClock::getTimeInMilliSec();
166 SPickerInteractor::s_PICKED_SIGNAL);
167 sig->asyncEmit(info);
170 if(this->GetPassiveObserver() != 1)
180 void setAdaptor( SPickerInteractor::sptr adaptor)
187 void setPicker( vtkAbstractPropPicker* picker)
194 void setEventId(SPickerInteractor::SetEventIdType* eventId)
200 SPickerInteractor::SetEventIdType* m_eventId;
201 SPickerInteractor::sptr m_adaptor;
202 vtkAbstractPropPicker* m_picker;
204 unsigned int m_skipMove = 0u;
211 SPickerInteractor::SPickerInteractor() noexcept :
212 m_interactionCommand(
nullptr)
214 newSignal<PickedSignalType>(s_PICKED_SIGNAL);
219 SPickerInteractor::~SPickerInteractor() noexcept
227 this->configureParams();
229 const ConfigType config = this->getConfigTree().get_child(
"config.<xmlattr>");
231 if (config.count(
"event"))
233 const std::string eventTxt = config.get<std::string>(
"event");
235 ::boost::char_separator<char> sep(
", ;");
236 ::boost::tokenizer< ::boost::char_separator<char> > tok(eventTxt, sep);
237 for(
const auto& it : tok)
239 const auto iter = m_eventIdConversion.find(it);
240 SLM_ASSERT(
"Unknown eventId '"+ it+
"'.", iter != m_eventIdConversion.end());
241 m_eventId.insert(iter->second);
246 for(
auto elt : m_eventIdConversion)
248 m_eventId.insert(elt.second);
252 m_abortOnPick = config.get<
bool>(
"abortOnPick",
false);
262 observer->SetPassiveObserver(static_cast<int>(!m_abortOnPick));
263 observer->setAdaptor( SPickerInteractor::dynamicCast(this->getSptr()) );
264 observer->setPicker(this->getPicker());
265 observer->setEventId(&m_eventId);
267 m_interactionCommand = observer;
269 vtkRenderWindowInteractor* interactor = this->getInteractor();
270 const float priority = 0.999f;
271 interactor->AddObserver(vtkCommand::LeftButtonPressEvent, m_interactionCommand, priority);
272 interactor->AddObserver(vtkCommand::LeftButtonReleaseEvent, m_interactionCommand, priority);
273 interactor->AddObserver(vtkCommand::MiddleButtonPressEvent, m_interactionCommand, priority);
274 interactor->AddObserver(vtkCommand::MiddleButtonReleaseEvent, m_interactionCommand, priority);
275 interactor->AddObserver(vtkCommand::RightButtonPressEvent, m_interactionCommand, priority);
276 interactor->AddObserver(vtkCommand::RightButtonReleaseEvent, m_interactionCommand, priority);
277 interactor->AddObserver(vtkCommand::MouseMoveEvent, m_interactionCommand, priority);
278 interactor->AddObserver(vtkCommand::MouseWheelForwardEvent, m_interactionCommand, priority);
279 interactor->AddObserver(vtkCommand::MouseWheelBackwardEvent, m_interactionCommand, priority);
280 interactor->AddObserver(vtkCommand::KeyPressEvent, m_interactionCommand, priority);
294 vtkRenderWindowInteractor* interactor = this->getInteractor();
295 interactor->RemoveObservers(vtkCommand::LeftButtonPressEvent, m_interactionCommand);
296 interactor->RemoveObservers(vtkCommand::LeftButtonReleaseEvent, m_interactionCommand);
297 interactor->RemoveObservers(vtkCommand::MiddleButtonPressEvent, m_interactionCommand);
298 interactor->RemoveObservers(vtkCommand::MiddleButtonReleaseEvent, m_interactionCommand);
299 interactor->RemoveObservers(vtkCommand::RightButtonPressEvent, m_interactionCommand);
300 interactor->RemoveObservers(vtkCommand::RightButtonReleaseEvent, m_interactionCommand);
301 interactor->RemoveObservers(vtkCommand::MouseMoveEvent, m_interactionCommand);
302 interactor->RemoveObservers(vtkCommand::MouseWheelForwardEvent, m_interactionCommand);
303 interactor->RemoveObservers(vtkCommand::MouseWheelBackwardEvent, m_interactionCommand);
304 interactor->RemoveObservers(vtkCommand::KeyPressEvent, m_interactionCommand);
306 m_interactionCommand->Delete();
307 m_interactionCommand =
nullptr;
This service emits a signal when the user click on a actor in the scene.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
#define OSLM_TRACE(message)
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
static MapEventIdType m_eventIdConversion
map containing the association between 'event text' and 'event ID'.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
std::map< std::string, EventID > MapEventIdType
typedef for the map (seen below).
Base class for VTK adaptors.