fw4spl
SDistance.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #ifndef ANDROID
8 
9 #include "visuVTKAdaptor/SDistance.hpp"
10 
11 #include <fwData/Material.hpp>
12 #include <fwData/PointList.hpp>
13 #include <fwData/Reconstruction.hpp>
14 
15 #include <fwDataTools/fieldHelper/Image.hpp>
16 
17 #include <fwServices/macros.hpp>
18 
19 #include <vtkActor.h>
20 #include <vtkAxisActor2D.h>
21 #include <vtkCommand.h>
22 #include <vtkDistanceRepresentation2D.h>
23 #include <vtkHandleRepresentation.h>
24 #include <vtkLineSource.h>
25 #include <vtkPolyDataMapper.h>
26 #include <vtkProperty.h>
27 #include <vtkProperty2D.h>
28 #include <vtkRenderer.h>
29 #include <vtkTextProperty.h>
30 
31 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SDistance, ::fwData::PointList );
32 
33 namespace visuVTKAdaptor
34 {
35 
36 const ::fwServices::IService::KeyType SDistance::s_POINTLIST_INPUT = "pointList";
37 
38 SDistance::SDistance() noexcept :
39  m_distanceRepresentation( vtkDistanceRepresentation2D::New()),
40  m_lineActor(vtkActor::New()),
41  m_lineSource(vtkLineSource::New())
42 {
43  m_distanceRepresentation->InstantiateHandleRepresentation();
44  m_distanceRepresentation->SetLabelFormat("%-#6.3gmm");
45 
46  m_lineSource->SetResolution(1);
47  vtkPolyDataMapper* lineMapper = vtkPolyDataMapper::New();
48  lineMapper->SetInputConnection(m_lineSource->GetOutputPort());
49 
50  m_lineActor->SetMapper(lineMapper);
51  m_lineActor->GetProperty()->SetLineWidth(5.);
52  m_lineActor->GetProperty()->SetColor(1., 1., 0.);
53  m_lineActor->GetProperty()->SetOpacity(0.4);
54 
55  lineMapper->Delete();
56  m_distanceRepresentation->GetAxis()->GetProperty()->SetColor(1.0, 0.0, 1.);
57  m_distanceRepresentation->GetAxis()->SetTitlePosition(0.9);
58 
59  //fixed font size for displaying distance
60  //m_distanceRepresentation->GetAxis()->SizeFontRelativeToAxisOn();
61  //m_distanceRepresentation->GetAxis()->GetMapper();
62  //m_distanceRepresentation->GetAxis()->SetFontFactor(0.8);
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 SDistance::~SDistance() noexcept
68 {
69  m_lineSource->Delete();
70  m_lineActor->Delete();
71  m_distanceRepresentation->Delete();
72 }
73 
74 //------------------------------------------------------------------------------
75 
76 void SDistance::setAxisColor( ::fwData::Color::sptr newColor) noexcept
77 {
78  SLM_ASSERT("newColor not instanced", newColor);
79  m_distanceRepresentation->GetAxis()->GetProperty()->SetColor(
80  newColor->red(), newColor->green(), newColor->blue() );
81  m_distanceRepresentation->GetAxis()->GetProperty()->SetOpacity(newColor->alpha() );
82  m_lineActor->GetProperty()->SetColor(newColor->red(), newColor->green(), newColor->blue());
83  m_lineActor->GetProperty()->SetOpacity( newColor->alpha() * 0.4);
84 
85  m_distanceRepresentation->GetAxis()->GetTitleTextProperty()->SetColor(
86  newColor->red(), newColor->green(), newColor->blue() );
87  this->setVtkPipelineModified();
88 }
89 
90 //------------------------------------------------------------------------------
91 
93 {
94  this->configureParams();
95 }
96 
97 //------------------------------------------------------------------------------
98 
100 {
101  this->initialize();
102 
103  ::fwData::PointList::csptr ptList = this->getInput< ::fwData::PointList >(s_POINTLIST_INPUT);
104 
105  m_point1 = ptList->getPoints().front();
106  m_point2 = ptList->getPoints().back();
107 
108  m_point1Connection = m_point1.lock()->signal(::fwData::Object::s_MODIFIED_SIG)->connect(
110  m_point2Connection = m_point2.lock()->signal(::fwData::Object::s_MODIFIED_SIG)->connect(
112 
113  // set color to distance if Point List have Color Field
114  if ( ptList->getField( ::fwDataTools::fieldHelper::Image::m_colorId ) )
115  {
116  ::fwData::Color::sptr color;
117  color = ptList->getField< ::fwData::Color >( ::fwDataTools::fieldHelper::Image::m_colorId );
118  this->setAxisColor( color );
119  }
120 
121  this->addToRenderer(m_lineActor);
122  this->addToRenderer(m_distanceRepresentation);
123 
124  this->updating();
125 }
126 
127 //------------------------------------------------------------------------------
128 
130 {
131  ::fwData::Point::csptr p1 = m_point1.lock();
132  ::fwData::Point::csptr p2 = m_point2.lock();
133 
134  double ps1[3];
135  std::copy(p1->getCoord().begin(), (p1)->getCoord().end(), ps1 );
136 
137  double ps2[3];
138  std::copy(p2->getCoord().begin(), (p2)->getCoord().end(), ps2 );
139 
140  m_distanceRepresentation->GetPoint1Representation()->SetWorldPosition(ps1);
141  m_distanceRepresentation->GetPoint2Representation()->SetWorldPosition(ps2);
142 
143  m_distanceRepresentation->SetPoint1DisplayPosition(ps1);
144  m_distanceRepresentation->SetPoint2DisplayPosition(ps2);
145 
146  m_distanceRepresentation->BuildRepresentation();
147 
148  m_lineSource->SetPoint1(ps1);
149  m_lineSource->SetPoint2(ps2);
150  this->setVtkPipelineModified();
151  this->requestRender();
152 }
153 
154 //------------------------------------------------------------------------------
155 
157 {
158  m_point1Connection.disconnect();
159  m_point2Connection.disconnect();
160 
161  m_point1.reset();
162  m_point2.reset();
163 
165 }
166 
167 //------------------------------------------------------------------------------
168 
170 {
171  KeyConnectionsMap connections;
172 
173  connections.push(s_POINTLIST_INPUT, ::fwData::Object::s_MODIFIED_SIG, s_UPDATE_SLOT);
174 
175  return connections;
176 }
177 
178 //------------------------------------------------------------------------------
179 
180 } //namespace visuVTKAdaptor
181 
182 #endif // ANDROID
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SDistance.cpp:156
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
FWRENDERVTK_API void addToRenderer(vtkProp *prop)
Adds the vtkProp to the renderer.
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: SDistance.cpp:129
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: SDistance.cpp:99
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
FWRENDERVTK_API void configureParams()
Parse the xml configuration for renderer, picker and transform.
Display the distance between the two points of the point list.
Definition: SDistance.hpp:44
FWRENDERVTK_API void requestRender()
notify a render request iff vtkPipeline is modified
FWRENDERVTK_API void setVtkPipelineModified()
End-user have to call this method when a vtk structure has been modified, thus a render request will ...
FWRENDERVTK_API void removeAllPropFromRenderer()
Removes all the vtkProp from the renderer.
This class defines color object.
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SDistance.cpp:92
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: SDistance.cpp:169
This class defines a list of points.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
VISUVTKADAPTOR_API void setAxisColor(::fwData::Color::sptr newColor) noexcept
set Distance Axis color AND alpha
Definition: SDistance.cpp:76
void disconnect()
Disconnect related Connection.
Definition: Connection.hpp:50
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177