fw4spl
AddLandmark.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 #include "uiMeasurement/action/AddLandmark.hpp"
8 
9 #include <fwCom/Signal.hpp>
10 #include <fwCom/Signal.hxx>
11 #include <fwCom/Signals.hpp>
12 
13 #include <fwCore/base.hpp>
14 
15 #include <fwData/Boolean.hpp>
16 #include <fwData/Point.hpp>
17 #include <fwData/PointList.hpp>
18 #include <fwData/String.hpp>
19 
20 #include <fwDataTools/fieldHelper/Image.hpp>
21 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
22 
23 #include <fwGui/dialog/InputDialog.hpp>
24 #include <fwGui/dialog/MessageDialog.hpp>
25 
26 #include <fwServices/macros.hpp>
27 
28 #include <exception>
29 
30 namespace uiMeasurement
31 {
32 namespace action
33 {
34 
35 fwServicesRegisterMacro( ::fwGui::IActionSrv, ::uiMeasurement::action::AddLandmark, ::fwData::Image );
36 
37 //------------------------------------------------------------------------------
38 
39 AddLandmark::AddLandmark( ) noexcept
40 {
41 }
42 
43 //------------------------------------------------------------------------------
44 
45 AddLandmark::~AddLandmark() noexcept
46 {
47 }
48 
49 //------------------------------------------------------------------------------
50 
51 void AddLandmark::info(std::ostream& _sstream )
52 {
53  _sstream << "Action for remove distance" << std::endl;
54 }
55 
56 //------------------------------------------------------------------------------
57 
58 // return true if label setting is NOT Canceled , name is modified !!!
59 bool defineLabel(std::string& name)
60 {
61  bool res = false;
62  static int count = 1;
63  name = "Label" + ::boost::lexical_cast< std::string >(count );
64 
65  ::fwData::String::sptr url = ::fwData::String::New();
67  inputDlg.setTitle("Label");
68  inputDlg.setMessage("Which label for the point?");
69  inputDlg.setInput(name);
70  std::string inputText = inputDlg.getInput();
71 
72  if(!inputText.empty())
73  {
74  name = inputText;
75  count++; // reset count if operation canceled
76  res = true;
77  }
78  return res;
79 }
80 
81 //------------------------------------------------------------------------------
82 
84 {
86  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >("image");
87 
89  {
91  "Add landmarks",
92  "It is impossible to add image landmarks. There is no loaded image in the software.",
93  ::fwGui::dialog::IMessageDialog::WARNING);
94  return;
95  }
96 
97  std::string value;
98  if ( defineLabel(value) )
99  {
100  //get landmarks
102  ::fwData::PointList::sptr landmarks = image->getField< ::fwData::PointList >(
104  SLM_ASSERT("landmarks not instanced", landmarks);
105 
106  // create a new point
107  ::fwData::Point::sptr newPoint = ::fwDataTools::fieldHelper::MedicalImageHelpers::getImageSliceIndices( image );
108  // transform slice to mm
109  std::transform( newPoint->getCoord().begin(), newPoint->getCoord().end(),
110  image->getSpacing().begin(),
111  newPoint->getCoord().begin(),
112  std::multiplies<double>() );
113  std::transform( newPoint->getCoord().begin(), newPoint->getCoord().end(),
114  image->getOrigin().begin(),
115  newPoint->getCoord().begin(),
116  std::plus<double>() );
117  // append to landmark
118  landmarks->getPoints().push_back( newPoint );
119 
120  // append to point the label
121  ::fwData::String::sptr label = ::fwData::String::New();
122  label->value() = value;
123  newPoint->setField( ::fwDataTools::fieldHelper::Image::m_labelId, label );
124 
125  image->setField("ShowLandmarks", ::fwData::Boolean::New(true));
126 
127  // notify
129  sig->asyncEmit(newPoint);
130  }
131 }
132 
133 //------------------------------------------------------------------------------
134 
136 {
138 }
139 
140 //------------------------------------------------------------------------------
141 
143 {
145 }
146 
147 //------------------------------------------------------------------------------
148 
150 {
152 }
153 
154 //------------------------------------------------------------------------------
155 
156 } // namespace action
157 
158 } // namespace uiMeasurement
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
Definition: AddLandmark.cpp:83
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
static FWGUI_API IMessageDialog::Buttons showMessageDialog(const std::string &title, const std::string &message,::fwGui::dialog::IMessageDialog::Icons icon=INFO)
The namespace uiMeasurement contains actions to add/show/remove distances and landmarks.
Definition: AddDistance.hpp:13
This action adds landmaks on image.
Definition: AddLandmark.hpp:33
virtual FWGUI_API void setMessage(const std::string &msg) override
Set the message.
void starting() override
Initialize the service activity.
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
UIMEASUREMENT_API void info(std::ostream &_sstream) override
Write information in a stream.
Definition: AddLandmark.cpp:51
virtual FWGUI_API void setTitle(const std::string &title) override
Set the title of the input dialog.
virtual FWGUI_API std::string getInput() override
Get the input text in the input field.
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_LANDMARK_ADDED_SIG
Type of signal when image&#39;s buffer is added.
#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
FWGUI_API void initialize()
Initialize the action.
Definition: IActionSrv.cpp:69
Defines the generic input dialog for IHM. Use the Delegate design pattern.
static FWDATATOOLS_API const std::string m_labelId
to assign a label
This class defines a list of points.
void configuring() override
Configure the service before starting. Apply the configuration to service.
static FWDATATOOLS_API bool checkLandmarks(::fwData::Image::sptr _pImg)
Check if the image has a landmark field.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
This class defines an image.
virtual FWGUI_API void setInput(const std::string &text) override
Set the input text in the input field.
static FWDATATOOLS_API::fwData::Point::sptr getImageSliceIndices(::fwData::Image::sptr _pImg)
Get the X,Y,Z slice position.
void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...