fw4spl
SImageSliceOrientationText.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 #include "visuVTKAdaptor/SImageSliceOrientationText.hpp"
8 
9 #include <fwCom/Slot.hpp>
10 #include <fwCom/Slot.hxx>
11 #include <fwCom/Slots.hpp>
12 #include <fwCom/Slots.hxx>
13 
14 #include <fwData/Image.hpp>
15 
16 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
17 
18 #include <fwServices/macros.hpp>
19 
20 #include <boost/algorithm/string.hpp>
21 
22 #include <vtkActor2D.h>
23 #include <vtkRenderer.h>
24 #include <vtkSmartPointer.h>
25 #include <vtkTextMapper.h>
26 #include <vtkTextProperty.h>
27 
29 
30 namespace visuVTKAdaptor
31 {
32 
34 {
35 public:
36  typedef vtkSmartPointer<vtkActor2D> TextActorPtr;
37  typedef vtkSmartPointer<vtkTextMapper> TextMapperPtr;
38 
39  //------------------------------------------------------------------------------
40 
41  void configure(TextActorPtr& actor, TextMapperPtr& mapper)
42  {
43  actor = TextActorPtr::New();
44  mapper = TextMapperPtr::New();
45 
46  mapper->GetTextProperty()->SetFontFamilyToCourier(); // Fixed-width font
47  mapper->GetTextProperty()->ShadowOn(); // better contrast
48  mapper->GetTextProperty()->BoldOn();
49  actor->SetMapper(mapper);
50  actor->GetPositionCoordinate()->SetCoordinateSystemToNormalizedViewport();
51  actor->GetPosition2Coordinate()->SetCoordinateSystemToNormalizedViewport();
52  }
53 
55  {
56  configure(m_right, m_rightMapper);
57  configure(m_left, m_leftMapper);
58  configure(m_top, m_topMapper);
59  configure(m_bottom, m_bottomMapper);
60 
61  vtkTextProperty* textProp;
62 
63  m_right->SetPosition(0.99, 0.5);
64  m_rightMapper->SetInput("");
65  textProp = m_rightMapper->GetTextProperty();
66  textProp->SetJustificationToRight();
67  textProp->SetVerticalJustificationToCentered();
68 
69  m_left->SetPosition(0.01, 0.5);
70  m_leftMapper->SetInput("");
71  textProp = m_leftMapper->GetTextProperty();
72  textProp->SetJustificationToLeft();
73  textProp->SetVerticalJustificationToCentered();
74 
75  m_top->SetPosition(0.5, 0.99);
76  m_topMapper->SetInput("");
77  textProp = m_topMapper->GetTextProperty();
78  textProp->SetJustificationToCentered();
79  textProp->SetVerticalJustificationToTop();
80 
81  m_bottom->SetPosition(0.5, 0.01);
82  m_bottomMapper->SetInput("");
83  textProp = m_bottomMapper->GetTextProperty();
84  textProp->SetJustificationToCentered();
85  textProp->SetVerticalJustificationToBottom();
86  }
87 
88  //------------------------------------------------------------------------------
89 
90  void setText(const std::string& str)
91  {
92  m_rightStr = "-";
93  m_leftStr = "-";
94  m_anteriorStr = "-";
95  m_porteriorStr = "-";
96  m_superiorStr = "-";
97  m_inferiorStr = "-";
98 
99  if (!str.empty())
100  {
101  std::vector<std::string> locations;
102  ::boost::algorithm::split( locations, str, ::boost::algorithm::is_any_of(",") );
103  SLM_ASSERT("Six location should be given, got : " << locations.size() << ":" << str, locations.size());
104 
105  m_rightStr = (locations.size() > 0) ? locations[0] : "";
106  m_leftStr = (locations.size() > 1) ? locations[1] : "";
107  m_anteriorStr = (locations.size() > 2) ? locations[2] : "";
108  m_porteriorStr = (locations.size() > 3) ? locations[3] : "";
109  m_superiorStr = (locations.size() > 4) ? locations[4] : "";
110  m_inferiorStr = (locations.size() > 5) ? locations[5] : "";
111  }
112 
113  }
114 
115  //------------------------------------------------------------------------------
116 
117  void setOrientation( ::fwDataTools::helper::MedicalImageAdaptor::Orientation orientation )
118  {
119  switch (orientation)
120  {
121  case ::fwDataTools::helper::MedicalImageAdaptor::X_AXIS: // Sagittal
122  m_rightMapper->SetInput(m_anteriorStr.c_str());
123  m_leftMapper->SetInput(m_porteriorStr.c_str());
124  m_topMapper->SetInput(m_superiorStr.c_str());
125  m_bottomMapper->SetInput(m_inferiorStr.c_str());
126  break;
127  case ::fwDataTools::helper::MedicalImageAdaptor::Y_AXIS: // Frontal
128  m_rightMapper->SetInput(m_leftStr.c_str());
129  m_leftMapper->SetInput(m_rightStr.c_str());
130  m_topMapper->SetInput(m_superiorStr.c_str());
131  m_bottomMapper->SetInput(m_inferiorStr.c_str());
132  break;
133  case ::fwDataTools::helper::MedicalImageAdaptor::Z_AXIS: // Axial
134  m_rightMapper->SetInput(m_leftStr.c_str());
135  m_leftMapper->SetInput(m_rightStr.c_str());
136  m_topMapper->SetInput(m_anteriorStr.c_str());
137  m_bottomMapper->SetInput(m_porteriorStr.c_str());
138  break;
139  default:
140  m_rightMapper->SetInput("");
141  m_leftMapper->SetInput("");
142  m_topMapper->SetInput("");
143  m_bottomMapper->SetInput("");
144  SLM_ASSERT("bad orientation : " << orientation, 0);
145  }
146  }
147 
148  std::string m_rightStr;
149  std::string m_leftStr;
150  std::string m_anteriorStr;
151  std::string m_porteriorStr;
152  std::string m_superiorStr;
153  std::string m_inferiorStr;
154 
155  TextMapperPtr m_rightMapper;
156  TextMapperPtr m_leftMapper;
157  TextMapperPtr m_topMapper;
158  TextMapperPtr m_bottomMapper;
159  TextActorPtr m_right;
160  TextActorPtr m_left;
161  TextActorPtr m_top;
162  TextActorPtr m_bottom;
163 };
164 
165 //------------------------------------------------------------------------------
166 
167 static const ::fwCom::Slots::SlotKeyType s_UPDATE_SLICE_TYPE_SLOT = "updateSliceType";
168 
169 static const ::fwServices::IService::KeyType s_IMAGE_INPUT = "image";
170 
171 //------------------------------------------------------------------------------
172 
173 SImageSliceOrientationText::SImageSliceOrientationText() noexcept :
174  m_pimpl( new ImageSliceOrientationTextPImpl )
175 {
176  newSlot(s_UPDATE_SLICE_TYPE_SLOT, &SImageSliceOrientationText::updateSliceType, this);
177 }
178 
179 //------------------------------------------------------------------------------
180 
181 SImageSliceOrientationText::~SImageSliceOrientationText() noexcept
182 {
183 }
184 
185 //------------------------------------------------------------------------------
186 
188 {
189  this->initialize();
190 
191  if(m_initialOrientation == "axial")
192  {
193  this->setOrientation(Z_AXIS);
194  }
195  else if(m_initialOrientation == "frontal")
196  {
197  this->setOrientation(Y_AXIS);
198  }
199  else // sagittal
200  {
201  this->setOrientation(X_AXIS);
202  }
203 
204  if(!m_locations.empty())
205  {
206  // this->getRenderer()->AddActor2D(m_pimpl->m_right);
207  this->addToRenderer( m_pimpl->m_right );
208  this->addToRenderer( m_pimpl->m_left );
209  this->addToRenderer( m_pimpl->m_top );
210  this->addToRenderer( m_pimpl->m_bottom );
211  }
212  this->updating();
213 }
214 
215 //------------------------------------------------------------------------------
216 
218 {
219  this->removeAllPropFromRenderer();
220 }
221 
222 //------------------------------------------------------------------------------
223 
225 {
226  m_pimpl->setText(m_locations);
227  m_pimpl->setOrientation(m_orientation);
228 
229  this->requestRender();
230 }
231 
232 //------------------------------------------------------------------------------
233 
235 {
237  m_pimpl->setOrientation(orientation);
238 }
239 
240 //-----------------------------------------------------------------------------
241 
242 void SImageSliceOrientationText::updateSliceType(int from, int to)
243 {
244  if( to == static_cast<int>(m_orientation) )
245  {
246  this->setOrientation( static_cast< Orientation >( from ));
247  }
248  else if(from == static_cast<int>(m_orientation))
249  {
250  this->setOrientation( static_cast< Orientation >( to ));
251  }
252 }
253 
254 //------------------------------------------------------------------------------
255 
257 {
258  this->configureParams();
259 
260  const ConfigType srvconfig = this->getConfigTree().get_child("config");
261 
262  m_locations = srvconfig.get("locations", "");
263 
264  // R,L,A,P,S,I, right, left, anterior, posterior, superior, inferior, referenced by :
265  // http://en.wikipedia.org/wiki/Anatomical_terms_of_location#Human_anatomy
266  m_locations = (m_locations == "default") ? "R,L,A,P,S,I" : m_locations;
267 
268  m_initialOrientation = srvconfig.get("initialOrientation", "axial");
269  SLM_TRACE("initialOrientation " + m_initialOrientation);
270 }
271 
272 //------------------------------------------------------------------------------
273 
275 {
276  KeyConnectionsMap connections;
277  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT);
278  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_SLICE_TYPE_MODIFIED_SIG, s_UPDATE_SLICE_TYPE_SLOT);
279  connections.push(s_IMAGE_INPUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT);
280 
281  return connections;
282 }
283 
284 //------------------------------------------------------------------------------
285 
286 } //namespace visuVTKAdaptor
Shows image orientation information (right, left, ...) This adaptor show locations labels in the four...
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
VISUVTKADAPTOR_API void starting() override
IService API implementation.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
virtual FWDATATOOLS_API void setOrientation(Orientation orientation)
Set the image orientation.
VISUVTKADAPTOR_API void configuring() override
IService API implementation.
#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
VISUVTKADAPTOR_API void stopping() override
IService API implementation.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_BUFFER_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_MODIFIED_SIG
Key in m_signals map of signal m_sigModified.
#define SLM_TRACE(message)
Definition: spyLog.hpp:228
VISUVTKADAPTOR_API void setOrientation(Orientation orientation) override
Overides MedicalImageAdaptor&#39;s setOrientation.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_TYPE_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
VISUVTKADAPTOR_API void updating() override
IService API implementation.