fw4spl
RemoveDistance.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/RemoveDistance.hpp"
8 
9 #include <fwCom/Signal.hxx>
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwData/Image.hpp>
14 #include <fwData/Point.hpp>
15 #include <fwData/PointList.hpp>
16 #include <fwData/Vector.hpp>
17 
18 #include <fwDataTools/fieldHelper/Image.hpp>
19 #include <fwDataTools/fieldHelper/MedicalImageHelpers.hpp>
20 
21 #include <fwGui/dialog/SelectorDialog.hpp>
22 
23 #include <fwServices/macros.hpp>
24 
25 #include <boost/lexical_cast.hpp>
26 
27 #include <exception>
28 #include <sstream>
29 
30 namespace uiMeasurement
31 {
32 namespace action
33 {
34 
36 
37 static const ::fwServices::IService::KeyType s_IMAGE_INOUT = "image";
38 
39 //------------------------------------------------------------------------------
40 
41 RemoveDistance::RemoveDistance( ) noexcept
42 {
43 }
44 
45 //------------------------------------------------------------------------------
46 
47 RemoveDistance::~RemoveDistance() noexcept
48 {
49 }
50 
51 //------------------------------------------------------------------------------
52 
53 std::string distanceToStr(double dist)
54 {
55  std::stringstream ss;
56  ss.precision(3);
57  ss << dist << " mm";
58  return ss.str();
59 }
60 
61 //------------------------------------------------------------------------------
62 
63 ::fwData::PointList::sptr getDistanceToRemove(::fwData::Image::sptr image, bool& removeAll)
64 {
65  ::fwData::PointList::sptr distToRemove;
66  removeAll = false;
67  ::fwData::Vector::sptr vectDist;
69 
70  if(vectDist)
71  {
72  std::vector< std::string > selections;
73  selections.push_back("ALL");
74  std::map< std::string, ::fwData::PointList::sptr > correspondance;
75 
76  for(::fwData::Object::sptr obj : *vectDist)
77  {
78  ::fwData::PointList::sptr pl = ::fwData::PointList::dynamicCast(obj);
79 
80  if ( pl->getPoints().size() != 2 )
81  {
82  continue;
83  } // we skip no paired pointList
84  ::fwData::Point::sptr pt1 = pl->getPoints().front();
85  ::fwData::Point::sptr pt2 = pl->getPoints().back();
86 
87  double dist = 0;
88  double delta = pt1->getCoord()[0] - pt2->getCoord()[0];
89  dist += delta*delta;
90  delta = pt1->getCoord()[1] - pt2->getCoord()[1];
91  dist += delta*delta;
92  delta = pt1->getCoord()[2] - pt2->getCoord()[2];
93  dist += delta*delta;
94  dist = sqrt(dist);
95 
96  selections.push_back( distanceToStr(dist) );
97  correspondance[ selections.back() ] = pl;
98  }
99 
100  if ( !selections.empty() )
101  {
102  ::fwGui::dialog::SelectorDialog::sptr selector = ::fwGui::dialog::SelectorDialog::New();
103  selector->setTitle("Select a distance to remove");
104  selector->setSelections(selections);
105  std::string selection = selector->show();
106  if( !selection.empty() )
107  {
108  if (selection == "ALL")
109  {
110  removeAll = true;
111  }
112  else
113  {
114  removeAll = false;
115  distToRemove = correspondance[selection];
116  }
117  }
118  }
119  }
120  return distToRemove;
121 }
122 
123 //------------------------------------------------------------------------------
124 
125 void RemoveDistance::notifyDeleteDistance(const ::fwData::Image::csptr& image,
126  const ::fwData::PointList::csptr& distance) const
127 {
129  sig->asyncEmit(distance);
130 }
131 
132 //------------------------------------------------------------------------------
133 
134 void RemoveDistance::notifyNewDistance(const ::fwData::Image::csptr& image,
135  const ::fwData::PointList::sptr& distance) const
136 {
138  sig->asyncEmit(distance);
139 }
140 
141 //------------------------------------------------------------------------------
142 
144 {
145  ::fwData::Image::sptr image = this->getInOut< ::fwData::Image >(s_IMAGE_INOUT);
146  if (!image)
147  {
148  FW_DEPRECATED_KEY(s_IMAGE_INOUT, "inout", "18.0");
149  image = this->getObject< ::fwData::Image >();
150  }
151 
152  ::fwData::Vector::sptr vectDist;
154 
156  && vectDist)
157  {
158  bool requestAll;
159  ::fwData::PointList::sptr distToRemove = getDistanceToRemove(image, requestAll );
160 
161  // perform action only available distance
162  if ( distToRemove )
163  {
164  SLM_ASSERT("No Field ImageDistancesId", vectDist);
165  ::fwData::Vector::IteratorType newEnd = std::remove(vectDist->begin(), vectDist->end(), distToRemove);
166  vectDist->getContainer().erase(newEnd, vectDist->end());
167 
168  this->notifyDeleteDistance(image, distToRemove);
169  }
170  if ( requestAll )
171  {
172  // backup
173  ::fwData::PointList::sptr backupDistance = image->getField< ::fwData::PointList >(
175 
177  this->notifyNewDistance(image, backupDistance);
178  }
179  }
180 }
181 
182 //------------------------------------------------------------------------------
183 
185 {
187 }
188 
189 //------------------------------------------------------------------------------
190 
192 {
194 }
195 
196 //------------------------------------------------------------------------------
197 
199 {
201 }
202 
203 //------------------------------------------------------------------------------
204 
205 } // namespace action
206 } // namespace uiMeasurement
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
FWGUI_API void actionServiceStarting()
Method called when the action service is starting.
Definition: IActionSrv.cpp:160
The namespace uiMeasurement contains actions to add/show/remove distances and landmarks.
Definition: AddDistance.hpp:13
void configuring() override
Configure the service before starting. Apply the configuration to service.
FWDATA_API void removeField(const FieldNameType &name)
Removes field with specified name.
FWGUI_API void actionServiceStopping()
Method called when the action service is stopping.
Definition: IActionSrv.cpp:153
Defines the service interface managing the menu items.
Definition: IActionSrv.hpp:24
This class defines a vector of objects.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_ADDED_SIG
Type of signal when image&#39;s buffer is added.
void updating() override
Perform some computations according to object (this service is attached to) attribute values and its ...
#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
void stopping() override
Uninitialize the service activity. The stop() method is always invoked before destroying a service...
This class defines a list of points.
static FWDATATOOLS_API bool checkImageValidity(::fwData::Image::csptr _pImg)
Check if the image is valid.
This class defines an image.
void starting() override
Initialize the service activity.
static FWDATA_APIconst::fwCom::Signals::SignalKeyType s_DISTANCE_REMOVED_SIG
Type of signal when image&#39;s buffer is added.
This action removes distances from the image field.