fw4spl
ImageProperties.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 "fwActivities/validator/ImageProperties.hpp"
8 
9 #include "fwActivities/validator/registry/macros.hpp"
10 
11 #include <fwData/Composite.hpp>
12 #include <fwData/Image.hpp>
13 #include <fwData/Vector.hpp>
14 
15 #include <fwMath/Compare.hpp>
16 
17 #include <fwMedData/ImageSeries.hpp>
18 
19 namespace fwActivities
20 {
21 namespace validator
22 {
23 
24 fwActivitiesValidatorRegisterMacro(
25  ::fwActivities::validator::ImageProperties, "::fwActivities::validator::ImageProperties");
26 
27 //-----------------------------------------------------------------------------
28 
30 {
31 }
32 
33 //-----------------------------------------------------------------------------
34 
36 {
37 }
38 
39 //-----------------------------------------------------------------------------
40 
42  const ::fwActivities::registry::ActivityInfo& activityInfo,
43  const ::fwData::Vector::csptr& currentSelection ) const
44 {
45  IValidator::ValidationType validation;
46 
47  if(currentSelection->size() > 1)
48  {
49  validation.first = true;
50  validation.second = "Input images have the same properties.";
51 
52  ::fwMedData::ImageSeries::sptr imgSeries0 = ::fwMedData::ImageSeries::dynamicCast((*currentSelection)[0]);
53  SLM_ASSERT("Failed to retrieve an image series", imgSeries0);
54  ::fwData::Image::sptr img0 = imgSeries0->getImage();
55  SLM_ASSERT("Failed to retrieve image from image series", img0);
56 
57  ::fwData::Image::SizeType size = img0->getSize();
58  ::fwData::Image::SpacingType spacing = img0->getSpacing();
59  ::fwData::Image::OriginType origin = img0->getOrigin();
60 
61  ::fwData::Vector::ContainerType::const_iterator it;
62  for(it = currentSelection->begin() + 1; it != currentSelection->end(); ++it)
63  {
64  ::fwMedData::ImageSeries::sptr imgSeries = ::fwMedData::ImageSeries::dynamicCast(*it);
65  SLM_ASSERT("Failed to retrieve an image series", imgSeries);
66  ::fwData::Image::sptr img = imgSeries->getImage();
67  SLM_ASSERT("Failed to retrieve an image data", img);
68 
69  if ( size != img->getSize() ||
70  !::fwMath::isContainerEqual< const ::fwData::Image::SpacingType >(spacing, img->getSpacing()) ||
71  !::fwMath::isContainerEqual< const ::fwData::Image::OriginType >(origin, img->getOrigin()) )
72  {
73  std::string errorMsg = "Images in selection have not the same properties :\n";
74  errorMsg += (size != img->getSize()) ? "- size\n" : "";
75  errorMsg += (spacing != img->getSpacing()) ? "- spacing\n" : "";
76  errorMsg += (origin != img->getOrigin()) ? "- origin" : "";
77 
78  validation.first = false;
79  validation.second = errorMsg;
80 
81  break;
82  }
83  }
84  }
85  else
86  {
87  validation.first = true;
88  validation.second = "Only one data provided to check images properties, assuming validation as ok.";
89  }
90 
91  return validation;
92 }
93 
94 //-----------------------------------------------------------------------------
95 
96 IValidator::ValidationType ImageProperties::validate(const ::fwData::Object::csptr& currentData ) const
97 {
98  IValidator::ValidationType validation;
99 
100  ::fwData::Vector::csptr vector = ::fwData::Vector::dynamicConstCast(currentData);
101  ::fwData::Composite::csptr composite = ::fwData::Composite::dynamicConstCast(currentData);
102 
103  validation.first = true;
104  validation.second = "Input images have the same properties.";
105 
106  ::fwData::Image::csptr img0;
107 
108  if (vector)
109  {
110  for (::fwData::Object::sptr obj : *vector)
111  {
112  ::fwMedData::ImageSeries::csptr imgSeries = ::fwMedData::ImageSeries::dynamicConstCast(obj);
113  ::fwData::Image::csptr img = ::fwData::Image::dynamicConstCast(obj);
114  if (imgSeries)
115  {
116  img = imgSeries->getImage();
117  }
118 
119  if (img)
120  {
121  if (!img0)
122  {
123  img0 = img;
124  }
125  else
126  {
127  if ( img0->getSize() != img->getSize() ||
128  !::fwMath::isContainerEqual< const ::fwData::Image::SpacingType >(img0->getSpacing(),
129  img->getSpacing()) ||
130  !::fwMath::isContainerEqual< const ::fwData::Image::OriginType >(img0->getOrigin(),
131  img->getOrigin()) )
132  {
133  std::string errorMsg = "Images in selection have not the same properties :\n";
134  errorMsg += (img0->getSize() != img->getSize()) ? "- size\n" : "";
135  errorMsg += (img0->getSpacing() != img->getSpacing()) ? "- spacing\n" : "";
136  errorMsg += (img0->getOrigin() != img->getOrigin()) ? "- origin" : "";
137 
138  validation.first = false;
139  validation.second = errorMsg;
140 
141  break;
142  }
143  }
144  }
145  else
146  {
147  validation.first = false;
148  validation.second = "Given data does not contain valid images.";
149  }
150  }
151  }
152  else if (composite)
153  {
154  for (auto elt : *composite)
155  {
156  ::fwMedData::ImageSeries::sptr imgSeries = ::fwMedData::ImageSeries::dynamicCast(elt.second);
157  ::fwData::Image::sptr img = ::fwData::Image::dynamicCast(elt.second);
158  if (imgSeries)
159  {
160  img = imgSeries->getImage();
161  }
162 
163  if (img)
164  {
165  if (!img0)
166  {
167  img0 = img;
168  }
169  else
170  {
171  if ( img0->getSize() != img->getSize() ||
172  !::fwMath::isContainerEqual< const ::fwData::Image::SpacingType >(img0->getSpacing(),
173  img->getSpacing()) ||
174  !::fwMath::isContainerEqual< const ::fwData::Image::OriginType >(img0->getOrigin(),
175  img->getOrigin()) )
176  {
177  std::string errorMsg = "Images in selection have not the same properties :\n";
178  errorMsg += (img0->getSize() != img->getSize()) ? "- size\n" : "";
179  errorMsg += (img0->getSpacing() != img->getSpacing()) ? "- spacing\n" : "";
180  errorMsg += (img0->getOrigin() != img->getOrigin()) ? "- origin" : "";
181 
182  validation.first = false;
183  validation.second = errorMsg;
184 
185  break;
186  }
187  }
188  }
189  else
190  {
191  validation.first = false;
192  validation.second = "Given data does not contain valid images.";
193  }
194  }
195  }
196  else
197  {
198  validation.first = false;
199  validation.second = "Given data is not a Vector or Composite of images. The validation can not be performed.";
200  }
201  return validation;
202 }
203 
204 //-----------------------------------------------------------------------------
205 
206 } // namespace validator
207 } // namespace fwActivities
Defines a validator which checks that properties of given images are the same.
std::pair< bool, std::string > ValidationType
Defines validation result of an activity. First element tells if the activity is validated or not by ...
Definition: IValidator.hpp:39
FWACTIVITIES_API ImageProperties(::fwActivities::IValidator::Key key)
Constructor. Do nothing.
virtual FWACTIVITIES_API ~ImageProperties()
Destructor. Do nothing.
#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
virtual FWACTIVITIES_API IValidator::ValidationType validate(const ::fwActivities::registry::ActivityInfo &activityInfo, const std::shared_ptr< const ::fwData::Vector > &currentSelection) const override
Validates if the given images have the same properties (origin, spacing, ...)
std::vector< double > SpacingType
Image spacing type.
Namespace containing activities data and builder.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
::fwData::Array::SizeType SizeType
Image size type.
std::vector< double > OriginType
Image origin type.