fw4spl
SImageText.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/SImageText.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 #include <fwData/Integer.hpp>
16 
17 #include <fwDataTools/fieldHelper/Image.hpp>
18 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
19 #include <fwDataTools/helper/Image.hpp>
20 
21 #include <fwServices/macros.hpp>
22 
23 #include <boost/format.hpp>
24 
25 #include <vtkRenderer.h>
26 #include <vtkTextActor.h>
27 
28 #include <sstream>
29 
30 fwServicesRegisterMacro( ::fwRenderVTK::IAdaptor, ::visuVTKAdaptor::SImageText);
31 
32 namespace visuVTKAdaptor
33 {
34 
35 static const ::fwCom::Slots::SlotKeyType s_UPDATE_SLICE_INDEX_SLOT = "updateSliceIndex";
36 
37 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
38 static const ::fwServices::IService::KeyType s_TF_INOUT = "tf";
39 
40 //-----------------------------------------------------------------------------
41 
42 SImageText::SImageText() noexcept
43 {
44  this->installTFSlots(this);
45  newSlot(s_UPDATE_SLICE_INDEX_SLOT, &SImageText::updateSliceIndex, this);
46 }
47 
48 //-----------------------------------------------------------------------------
49 
50 SImageText::~SImageText() noexcept
51 {
52 }
53 
54 //-----------------------------------------------------------------------------
55 
57 {
58  this->SText::starting();
59 
60  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
61  SLM_ASSERT("Missing image", image);
62  ::fwData::TransferFunction::sptr tf = this->getInOut< ::fwData::TransferFunction >(s_TF_INOUT);
63 
64  this->setOrCreateTF(tf, image);
65 
66  this->updateImageInfos(image);
67  this->updating();
68 }
69 
70 //-----------------------------------------------------------------------------
71 
73 {
74  this->removeTFConnections();
75  this->SText::stopping();
76 }
77 
78 //-----------------------------------------------------------------------------
79 
81 {
82  this->SText::configuring();
83 }
84 
85 //-----------------------------------------------------------------------------
86 
88 {
89  std::stringstream ss;
90  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
91  SLM_ASSERT("Missing image", image);
92 
94  {
95  ::fwDataTools::helper::Image imageHelper(image);
96  size_t axialIndex = static_cast<size_t>(m_axialIndex->value());
97  size_t frontalIndex = static_cast<size_t>(m_frontalIndex->value());
98  size_t sagittalIndex = static_cast<size_t>(m_sagittalIndex->value());
99 
100  ::fwData::TransferFunction::sptr tf = this->getTransferFunction();
101 
102  double min = tf->getLevel() - tf->getWindow()/2.0;
103  double max = tf->getLevel() + tf->getWindow()/2.0;
104 
105  double window = max - min;
106  double level = min + window*0.5;
107 
108  ss << ( ::boost::format("[% 3li,% 3li]") % min % max ) << std::endl;
109  ss << ( ::boost::format("W:% 3lg L:% 3lg") % window % level ) << std::endl;
110  ss << ( ::boost::format("(% 4li,% 4li,% 4li): %s") % sagittalIndex % frontalIndex % axialIndex %
111  imageHelper.getPixelAsString(sagittalIndex, frontalIndex, axialIndex ));
112  }
113 
114  this->setText(ss.str());
115 
116  this->setVtkPipelineModified();
117  this->requestRender();
118 }
119 
120 //------------------------------------------------------------------------------
121 
122 void SImageText::swapping(const KeyType& key)
123 {
124  if (key == s_TF_INOUT)
125  {
126  ::fwData::TransferFunction::sptr tf = this->getInOut< ::fwData::TransferFunction >(s_TF_INOUT);
127  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
128  SLM_ASSERT("Missing image", image);
129 
130  this->setOrCreateTF(tf, image);
131  this->updating();
132  }
133 }
134 
135 //------------------------------------------------------------------------------
136 
137 void SImageText::updateSliceIndex(int axial, int frontal, int sagittal)
138 {
139  m_axialIndex->value() = axial;
140  m_frontalIndex->value() = frontal;
141  m_sagittalIndex->value() = sagittal;
142 
143  this->updating();
144 }
145 
146 //------------------------------------------------------------------------------
147 
149 {
150  this->updating();
151 }
152 
153 //------------------------------------------------------------------------------
154 
155 void SImageText::updateTFWindowing(double /*window*/, double /*level*/)
156 {
157  this->updating();
158 }
159 
160 //------------------------------------------------------------------------------
161 
163 {
164  KeyConnectionsMap connections;
165  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_MODIFIED_SIG, s_UPDATE_SLOT);
166  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_SLICE_INDEX_MODIFIED_SIG, s_UPDATE_SLICE_INDEX_SLOT);
167  connections.push(s_IMAGE_INOUT, ::fwData::Image::s_BUFFER_MODIFIED_SIG, s_UPDATE_SLOT);
168 
169  return connections;
170 }
171 
172 //------------------------------------------------------------------------------
173 
174 } //namespace visuVTKAdaptor
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_SLICE_INDEX_MODIFIED_SIG
Type of signal when image&#39;s buffer is added.
void updateSliceIndex(int axial, int frontal, int sagittal)
Slot: update image slice index.
Definition: SImageText.cpp:137
This class is a helper to define the connections of a service and its data.
Definition: IService.hpp:454
FWDATATOOLS_API::fwData::TransferFunction::sptr getTransferFunction() const
Get the current transfer function.
FWDATATOOLS_API void installTFSlots(::fwCom::HasSlots *hasslots)
Install the slots to managed TF modifications.
The namespace visuVTKAdaptor contains the list of adaptors available for the generic scene...
::fwData::Integer::sptr m_axialIndex
Axial slice index.
virtual VISUVTKADAPTOR_API KeyConnectionsMap getAutoConnections() const override
Returns proposals to connect service slots to associated object signals, this method is used for obj/...
Definition: SImageText.cpp:162
virtual VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SText.cpp:133
virtual VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: SText.cpp:109
VISUVTKADAPTOR_API void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
Definition: SImageText.cpp:72
FWRENDERVTK_API void requestRender()
notify a render request iff vtkPipeline is modified
virtual void swapping()
Swap the service from associated object to another object.
Definition: IService.hpp:613
FWRENDERVTK_API void setVtkPipelineModified()
End-user have to call this method when a vtk structure has been modified, thus a render request will ...
::fwData::Integer::sptr m_sagittalIndex
Sagittal slice index.
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
VISUVTKADAPTOR_API void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: SImageText.cpp:87
FWDATATOOLS_API const std::string getPixelAsString(SizeType::value_type x, SizeType::value_type y, SizeType::value_type z)
Helpers for 3D images.
VISUVTKADAPTOR_API void starting() override
Initialize the service activity.
Definition: SImageText.cpp:56
FWDATATOOLS_API void updateImageInfos(::fwData::Image::sptr image)
Update the image information (slice index, min/max,...)
Adaptor to display window image information (width, heigth, picked position)
Definition: SImageText.hpp:71
#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 configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SImageText.cpp:80
virtual VISUVTKADAPTOR_API void updateTFWindowing(double window, double level) override
Update the text according to the new windowing.
Definition: SImageText.cpp:155
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.
::fwData::Integer::sptr m_frontalIndex
Frontal slice index.
FWDATATOOLS_API void removeTFConnections()
Remove the TF connections.
virtual VISUVTKADAPTOR_API void updateTFPoints() override
Update the text according to the new windowing.
Definition: SImageText.cpp:148
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
virtual VISUVTKADAPTOR_API void configuring() override
Configure the service before starting. Apply the configuration to service.
Definition: SText.cpp:61
FWDATATOOLS_API void setOrCreateTF(const ::fwData::TransferFunction::sptr &_tf, const fwData::Image::sptr &_image)
Sets the transfer function, creates one if _tf is null (.
static FWSERVICES_APIconst::fwCom::Slots::SlotKeyType s_UPDATE_SLOT
Slot to call start method.
Definition: IService.hpp:177