7 #include "visuVTKAdaptor/SImageMultiDistances.hpp" 9 #include "visuVTKAdaptor/SDistance.hpp" 10 #include "visuVTKAdaptor/SPointList.hpp" 12 #include <fwCom/Signal.hpp> 13 #include <fwCom/Signal.hxx> 14 #include <fwCom/Slot.hpp> 15 #include <fwCom/Slot.hxx> 16 #include <fwCom/Slots.hpp> 17 #include <fwCom/Slots.hxx> 19 #include <fwData/Boolean.hpp> 20 #include <fwData/Color.hpp> 21 #include <fwData/Image.hpp> 22 #include <fwData/Material.hpp> 23 #include <fwData/String.hpp> 24 #include <fwData/Vector.hpp> 26 #include <fwDataTools/fieldHelper/Image.hpp> 28 #include <fwServices/macros.hpp> 29 #include <fwServices/op/Add.hpp> 31 #include <fwTools/fwID.hpp> 33 #include <boost/assign/std/vector.hpp> 36 #include <vtkAssemblyNode.h> 37 #include <vtkAssemblyPath.h> 38 #include <vtkCamera.h> 39 #include <vtkCellPicker.h> 40 #include <vtkCommand.h> 41 #include <vtkCubeSource.h> 42 #include <vtkInteractorStyle.h> 43 #include <vtkPolyDataMapper.h> 44 #include <vtkRenderer.h> 45 #include <vtkRenderWindow.h> 46 #include <vtkRenderWindowInteractor.h> 56 const ::fwServices::IService::KeyType s_IMAGE_INOUT =
"image";
74 m_picker( vtkSmartPointer< vtkCellPicker>::New() ),
75 m_propCollection( vtkSmartPointer< vtkPropCollection>::New() )
79 m_picker->PickFromListOn();
80 m_picker->SetTolerance(0.001);
89 m_picker->InitializePickList();
90 m_propCollection->RemoveAllItems();
92 m_propCollection->InitTraversal();
96 while ( (prop = m_propCollection->GetNextProp()) )
98 m_picker->AddPickList(prop);
104 virtual void Execute( vtkObject* ,
unsigned long eventId,
void*)
109 if ( eventId == vtkCommand::RightButtonPressEvent )
111 std::copy(pos, pos+1, m_lastPos);
113 else if ( ( eventId == vtkCommand::RightButtonReleaseEvent ) ||
114 (( eventId == vtkCommand::StartInteractionEvent) && std::equal(pos, pos+1, m_lastPos) ))
116 m_display[0] = pos[0];
117 m_display[1] = pos[1];
119 this->fillPickList();
120 if (m_picker->Pick( m_display, m_service->
getRenderer() ) )
122 vtkPropCollection* propc = m_picker->GetActors();
125 propc->InitTraversal();
126 while ( (prop = propc->GetNextProp()) )
131 ::fwData::PointList::csptr plist =
137 sig->asyncEmit(plist);
149 vtkSmartPointer< vtkPicker> m_picker;
150 vtkSmartPointer< vtkPropCollection> m_propCollection;
156 static const ::fwCom::Slots::SlotKeyType s_CREATE_DISTANCE_SLOT =
"createDistance";
157 static const ::fwCom::Slots::SlotKeyType s_REMOVE_DISTANCE_SLOT =
"removeDistance";
161 SImageMultiDistances::SImageMultiDistances() noexcept :
162 m_rightButtonCommand(
nullptr),
163 m_needSubservicesDeletion(
false),
166 newSlot(s_CREATE_DISTANCE_SLOT, &SImageMultiDistances::createDistance,
this);
167 newSlot(s_REMOVE_DISTANCE_SLOT, &SImageMultiDistances::removeDistance,
this);
172 SImageMultiDistances::~SImageMultiDistances() noexcept
180 this->configureParams();
182 const ConfigType config = this->getConfigTree().get_child(
"config.<xmlattr>");
184 const std::string filter = config.get<std::string>(
"filter",
"false");
185 SLM_ASSERT(
"'filter' value must be 'true' or 'false'", filter ==
"true" || filter ==
"false");
186 m_filter = (filter ==
"true");
195 m_rightButtonCommand = vtkDistanceDeleteCallBack::New(
this);
196 this->getInteractor()->AddObserver(
"RightButtonPressEvent", m_rightButtonCommand, 1 );
197 this->getInteractor()->AddObserver(
"RightButtonReleaseEvent", m_rightButtonCommand, 1 );
198 this->getInteractor()->AddObserver(
"StartInteractionEvent", m_rightButtonCommand, 0);
205 ::fwData::Color::sptr generateColor()
209 static std::vector< Color::sptr > colors;
210 static std::vector< Color::sptr >::iterator current;
211 if ( colors.empty() )
213 Color::sptr magenta = Color::New();
214 magenta->setRGBA( 1, 0, 1);
215 colors.push_back( magenta );
217 Color::sptr cyan = Color::New();
218 cyan->setRGBA(0, 1, 1);
219 colors.push_back( cyan );
221 Color::sptr orange = Color::New();
222 orange->setRGBA( 1, 0.647f, 0);
223 colors.push_back( orange );
225 Color::sptr violet = Color::New();
226 violet->setRGBA( .5f, 0.26f, 1);
227 colors.push_back( violet );
229 Color::sptr vertpomme = Color::New();
230 vertpomme->setRGBA( .65f, 1, 0);
231 colors.push_back( vertpomme );
233 Color::sptr jaune = Color::New();
234 jaune->setRGBA( 1, 1, 0);
235 colors.push_back( jaune );
237 current = colors.begin();
240 if ( ++current == colors.end() )
242 current = colors.begin();
245 Color::sptr newColor;
252 void SImageMultiDistances::installSubServices( ::fwData::PointList::sptr pl )
254 if ( pl->getPoints().size() > 1 )
257 auto serviceDistance = this->registerService< ::fwRenderVTK::IAdaptor>(
"::visuVTKAdaptor::SDistance");
260 serviceDistance->registerInput(pl, SDistance::s_POINTLIST_INPUT,
true);
266 serviceDistance->setPickerId( this->getPickerId() );
267 serviceDistance->setRendererId( this->getRendererId() );
268 serviceDistance->setRenderService( this->getRenderService() );
269 serviceDistance->start();
272 IService::Config srvPLConfig;
273 auto servicePointList = this->registerService< ::fwRenderVTK::IAdaptor>(
"::visuVTKAdaptor::SPointList");
275 servicePointList->registerInput(pl, SPointList::s_POINTLIST_INPUT,
true);
277 servicePointList->setPickerId( this->getPickerId() );
278 servicePointList->setRendererId( this->getRendererId() );
279 servicePointList->setRenderService( this->getRenderService() );
280 servicePointList->start();
286 ::fwData::Point::sptr SImageMultiDistances::screenToWorld(
int X,
int Y)
295 if ( this->getPicker() && this->getPicker()->Pick( display, this->getRenderer() ) )
297 world = this->getPicker()->GetPickPosition();
302 vtkCamera* camera = this->getRenderer()->GetActiveCamera();
303 double* clippingCamBackup = camera->GetClippingRange();
305 camera->SetClippingRange( camera->GetDistance() - 0.1, camera->GetDistance() + 0.1 );
313 this->getInteractor()->GetInteractorStyle()->ComputeDisplayToWorld( this->getRenderer(), X, Y, 0, world);
316 camera->SetClippingRange( clippingCamBackup );
319 ::fwData::Point::sptr pt = ::fwData::Point::New();
320 std::copy( world, world +3, pt->getCoord().begin() );
321 this->setVtkPipelineModified();
330 ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
333 ::fwData::Vector::sptr distanceField;
337 isShown = image->
getField(
"ShowDistances", ::fwData::Boolean::New(
true))->value();
339 this->unregisterServices();
341 if( isShown && distanceField )
343 for(::fwData::Object::sptr
object : *distanceField)
345 ::fwData::PointList::sptr distance = ::fwData::PointList::dynamicCast(
object);
346 ::fwData::String::sptr relatedService = distance->getField<
::fwData::String >(
349 if ( m_filter && relatedService )
351 std::string servId = relatedService->
value();
352 if ( this->getRenderService()->getID() != servId )
358 SLM_ASSERT(
"Empty Point List for Distance !!!!", !distance->getPoints().empty() );
359 this->installSubServices(distance);
362 this->setVtkPipelineModified();
367 void SImageMultiDistances::removeDistance(::fwData::PointList::csptr plToRemove )
369 ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
372 this->unregisterServices();
374 ::fwData::Vector::sptr distanceField;
377 ::fwData::Vector::IteratorType iter = std::find(distanceField->begin(), distanceField->end(), plToRemove);
378 if(iter != distanceField->end())
388 void SImageMultiDistances::createDistance()
390 std::string sceneId = getRenderService()->getID();
391 this->createNewDistance(sceneId);
396 void SImageMultiDistances::createNewDistance( std::string sceneId )
398 ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
401 ::fwData::PointList::sptr newPL = ::fwData::PointList::New();
405 ::fwData::Vector::sptr distanceField;
407 ::fwData::Vector::New());
410 OSLM_INFO(
"DistanceField size: " << distanceField->size() );
412 int sizeX = this->getRenderer()->GetRenderWindow()->GetSize()[0];
413 int sizeY = this->getRenderer()->GetRenderWindow()->GetSize()[1];
415 SLM_ASSERT(
"invalid RenderWindow size", sizeX > 0 && sizeY > 0 );
416 ::fwData::Point::sptr pt1 = this->screenToWorld(sizeX/3, sizeY/2);
417 ::fwData::Point::sptr pt2 = this->screenToWorld(2*sizeX/3, sizeY/2);
419 newPL->getPoints().push_back( pt1 );
420 newPL->getPoints().push_back( pt2 );
422 this->installSubServices(newPL);
423 this->setVtkPipelineModified();
428 void SImageMultiDistances::setNeedSubservicesDeletion(
bool _needSubservicesDeletion)
430 m_needSubservicesDeletion = _needSubservicesDeletion;
437 if ( m_rightButtonCommand )
439 this->getInteractor()->RemoveObserver(m_rightButtonCommand);
440 m_rightButtonCommand->Delete();
441 m_rightButtonCommand = 0;
444 this->unregisterServices();
449 void SImageMultiDistances::show(
bool showDistances)
FWRENDERVTK_API vtkRenderWindowInteractor * getInteractor()
Returns the render interactor.
This class is a helper to define the connections of a service and its data.
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
FWRENDERVTK_API vtkRenderer * getRenderer()
Returns the renderer.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
FWRENDERVTK_API IAdaptor::sptr getAssociatedAdaptor(vtkProp *prop, int depth)
Returns the adaptor associated to the vtkProp.
#define OSLM_INFO(message)
FWDATA_API::fwData::Object::sptr getField(const FieldNameType &name,::fwData::Object::sptr defaultValue=::fwData::Object::sptr()) const
Returns a pointer of corresponding field (null if non exist).
T & value() noexcept
Get the value (mutable version).
This class defines a vector of objects.
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_ADDED_SIG
Type of signal when image's buffer is added.
Adaptor to display distance on an image.
#define SLM_ASSERT(message, cond)
work like 'assert' from 'cassert', with in addition a message logged by spylog (with FATAL loglevel) ...
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
FWRENDERVTK_API void getAllSubProps(vtkPropCollection *propc, int depth=-1)
Gets all the vtkProp associated to this adaptor.
This class defines a list of points.
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
ContainerType & getContainer()
get/set the vector of fwData::Object
Contains the representation of the data objects used in the framework.
std::shared_ptr< DATATYPE > getInOut(const KeyType &key) const
Return the inout object at the given key. Asserts if the data is not of the right type...
This class defines an image.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_REMOVED_SIG
Type of signal when image's buffer is added.
This class contains an std::string value.
Base class for VTK adaptors.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_DISPLAYED_SIG
Type of signal when image's buffer is added.