fw4spl
visuVTKAdaptor/src/visuVTKAdaptor/SAxis.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
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/SAxis.hpp"
10 
11 #include <fwCom/Slots.hxx>
12 
13 #include <fwServices/macros.hpp>
14 
15 #include <boost/lexical_cast.hpp>
16 
17 #include <vtkCommand.h>
18 #include <vtkPolyDataMapper.h>
19 #include <vtkProp3D.h>
20 #include <vtkProperty.h>
21 #include <vtkRenderer.h>
22 #include <vtkSphereSource.h>
23 #include <vtkTransform.h>
24 
25 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SAxis);
26 
27 namespace visuVTKAdaptor
28 {
29 //------------------------------------------------------------------------------
30 
31 const ::fwCom::Slots::SlotKeyType SAxis::s_UPDATE_VISIBILITY_SLOT = "updateVisibility";
32 
33 //------------------------------------------------------------------------------
34 
35 SAxis::SAxis() noexcept :
36  m_axisActor(fwVtkAxesActor::New()),
37  m_length(1.),
38  m_labelOn(true),
39  m_transformAxis(vtkSmartPointer<vtkTransform>::New()),
40  m_sphereActor(nullptr),
41  m_sphereOn(false),
42  m_xLabel("x"),
43  m_yLabel("y"),
44  m_zLabel("z")
45 {
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 SAxis::~SAxis() noexcept
52 {
53  m_axisActor->Delete();
54  m_axisActor = nullptr;
55  if(m_sphereOn)
56  {
57  m_sphereActor = nullptr;
58  }
59 }
60 
61 //------------------------------------------------------------------------------
62 
64 {
65  this->configureParams();
66 
67  const ConfigType config = this->getConfigTree().get_child("config.<xmlattr>.");
68 
69  m_length = config.get<double>("length", 1.0);
70 
71  const std::string label = config.get<std::string>("label", "yes");
72  SLM_ASSERT("value for 'label' must be 'yes' or 'no'", label == "yes" || label == "no");
73  m_labelOn = ( label == "yes" );
74 
75  m_xLabel = config.get<std::string>("xLabel", "x");
76  m_yLabel = config.get<std::string>("yLabel", "y");
77  m_zLabel = config.get<std::string>("zLabel", "z");
78 
79  const std::string marker = config.get<std::string>("marker", "no");
80  SLM_ASSERT("value for 'marker' must be 'yes' or 'no'", marker == "yes" || marker == "no");
81  m_sphereOn = ( marker == "yes" );
82 
83  const std::string markerColor = config.get<std::string>("markerColor", "#FFFFFF");
84  m_color = ::fwData::Color::New();
85  m_color->setRGBA(markerColor);
86 }
87 
88 //------------------------------------------------------------------------------
89 
91 {
92  this->initialize();
93 
94  this->buildPipeline();
95  this->addToRenderer( m_axisActor );
96  if(m_sphereOn)
97  {
99  }
100  this->requestRender();
101 }
102 
103 //------------------------------------------------------------------------------
104 
106 {
108  this->getRenderer()->RemoveActor(m_axisActor);
109  if(m_sphereOn)
110  {
111  this->getRenderer()->RemoveActor(m_sphereActor);
112  }
113  this->requestRender();
114 }
115 
116 //------------------------------------------------------------------------------
117 
119 {
120 }
121 
122 //------------------------------------------------------------------------------
123 
124 void SAxis::buildPipeline()
125 {
126  vtkTransform* transform = m_renderService.lock()->getOrAddVtkTransform(m_transformId);
127  m_axisActor->SetTotalLength( m_length, m_length, m_length );
128  m_axisActor->SetShaftTypeToCylinder();
129  m_axisActor->SetTipTypeToCone();
130  m_axisActor->SetXAxisLabelText( m_xLabel.c_str() );
131  m_axisActor->SetYAxisLabelText( m_yLabel.c_str() );
132  m_axisActor->SetZAxisLabelText( m_zLabel.c_str() );
133 
134  if (!m_labelOn)
135  {
136  m_axisActor->AxisLabelsOff();
137  }
138 
139  if(m_sphereOn)
140  {
141  // build a vtkSphereSource
142  const double sizeRatio = 4.0;
143  auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
144  sphereSource->SetRadius(m_length/sizeRatio);
145  auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
146  mapper->SetInputConnection(sphereSource->GetOutputPort());
147 
148  m_sphereActor = vtkSmartPointer<vtkActor>::New();
149  m_sphereActor->SetMapper(mapper);
150  m_sphereActor->GetProperty()->SetColor(m_color->red(), m_color->green(), m_color->blue());
151  m_sphereActor->GetProperty()->SetOpacity(m_color->alpha());
152  m_sphereActor->SetUserTransform(transform);
153  }
154 
155  m_axisActor->SetUserTransform(transform);
156 
157  this->setVtkPipelineModified();
158 }
159 
160 //------------------------------------------------------------------------------
161 
162 void SAxis::updateVisibility(bool _isVisible)
163 {
164  m_axisActor->SetVisibility(_isVisible);
165  if(m_sphereOn)
166  {
167  m_sphereActor->SetVisibility(_isVisible);
168  }
169 
170  this->setVtkPipelineModified();
171  this->requestRender();
172 }
173 
174 //------------------------------------------------------------------------------
175 
176 } // namespace visuVTKAdaptor
177 
178 #endif // ANDROID
The fwVtkAxesActor class is a specific and movable vtkAxesActor.
static VISUVTKADAPTOR_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_VISIBILITY_SLOT
Slot: update axes visibility (true = visible)
FWRENDERVTK_API void addToRenderer(vtkProp *prop)
Adds the vtkProp to the renderer.
VISUVTKADAPTOR_API void updateVisibility(bool isVisible)
Slot: update axes visibility (true = visible)
FWRENDERVTK_API vtkRenderer * getRenderer()
Returns the renderer.
bool m_sphereOn
boolean to show the sphere marker
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.
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
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.
::fwData::Color::sptr m_color
color of the sphere marker
VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
#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
vtkSmartPointer< vtkActor > m_sphereActor
actor for the sphere marker
FWRENDERVTK_API void initialize()
Initialize the adaptor with the associated render service. (must be call in starting).
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247