fw4spl
MedicalImageHelpers.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 "fwDataTools/fieldHelper/MedicalImageHelpers.hpp"
8 
9 #include "fwDataTools/fieldHelper/Image.hpp"
10 
11 #include <fwData/Composite.hpp>
12 #include <fwData/Integer.hpp>
13 #include <fwData/PointList.hpp>
14 #include <fwData/ResectionDB.hpp>
15 #include <fwData/String.hpp>
16 
17 #include <fwMath/MeshFunctions.hpp>
18 
19 namespace fwDataTools
20 {
21 
22 namespace fieldHelper
23 {
24 
25 //------------------------------------------------------------------------------
26 
27 bool MedicalImageHelpers::checkLandmarks( ::fwData::Image::sptr _pImg )
28 {
29  bool fieldIsModified = false;
30 
31  // Manage image landmarks
32  if ( !_pImg->getField( ::fwDataTools::fieldHelper::Image::m_imageLandmarksId ) )
33  {
34  ::fwData::PointList::sptr pl = ::fwData::PointList::New();
36  fieldIsModified = true;
37  }
38 
39  return fieldIsModified;
40 }
41 
42 //------------------------------------------------------------------------------
43 
44 bool MedicalImageHelpers::checkImageValidity( ::fwData::Image::csptr _pImg )
45 {
47 
48  // Test if the image is allocated
49  bool dataImageIsAllocated = (_pImg != ::fwData::Image::sptr());
50 
51  if (dataImageIsAllocated)
52  {
53  size_t nbDim = _pImg->getNumberOfDimensions();
54  dataImageIsAllocated &= nbDim > 1;
55 
56  for ( size_t k = 0; dataImageIsAllocated && k < nbDim; ++k )
57  {
58  dataImageIsAllocated = dataImageIsAllocated && ( _pImg->getSize()[k] >= 1 );
59  }
60  }
61 
62  return dataImageIsAllocated;
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 bool MedicalImageHelpers::checkImageSliceIndex( ::fwData::Image::sptr _pImg )
68 {
69  SLM_ASSERT("_pImg pointer null", _pImg);
70 
71  bool fieldIsModified = false;
72 
73  const ::fwData::Image::SizeType& imageSize = _pImg->getSize();
74 
75  ::fwData::Integer::sptr axialIdx = _pImg->getField< ::fwData::Integer >(
77  ::fwData::Integer::sptr frontalIdx = _pImg->getField< ::fwData::Integer >(
79  ::fwData::Integer::sptr sagittalIdx = _pImg->getField< ::fwData::Integer >(
81 
82  // Manage image landmarks
83  if ( !(axialIdx && frontalIdx && sagittalIdx) )
84  {
85  // Set value
86  axialIdx = ::fwData::Integer::New(-1);
87  _pImg->setField( ::fwDataTools::fieldHelper::Image::m_axialSliceIndexId, axialIdx );
88 
89  frontalIdx = ::fwData::Integer::New(-1);
90  _pImg->setField( ::fwDataTools::fieldHelper::Image::m_frontalSliceIndexId, frontalIdx );
91 
92  sagittalIdx = ::fwData::Integer::New(-1);
93  _pImg->setField( ::fwDataTools::fieldHelper::Image::m_sagittalSliceIndexId, sagittalIdx );
94 
95  fieldIsModified = true;
96  }
97 
98  SLM_ASSERT(
99  "Information on image slice index is not correct, miss one of these fields : m_axialSliceIndexId, m_frontalSliceIndexId, m_sagittalSliceIndexId.",
100  axialIdx && frontalIdx && sagittalIdx
101  );
102 
103  // Get value
104  if( axialIdx->value() < 0 || imageSize[2] < axialIdx->value() )
105  {
106  axialIdx->value() = static_cast< ::fwData::Integer::ValueType >(imageSize[2] / 2);
107  fieldIsModified = true;
108  }
109 
110  if( frontalIdx->value() < 0 || imageSize[1] < frontalIdx->value() )
111  {
112  frontalIdx->value() = static_cast< ::fwData::Integer::ValueType >(imageSize[1] / 2);
113  fieldIsModified = true;
114  }
115 
116  if( sagittalIdx->value() < 0 || imageSize[0] < sagittalIdx->value() )
117  {
118  sagittalIdx->value() = static_cast< ::fwData::Integer::ValueType >(imageSize[0] / 2);
119  fieldIsModified = true;
120  }
121 
122  return fieldIsModified;
123 }
124 
125 //------------------------------------------------------------------------------
126 
127 ::fwData::Point::sptr MedicalImageHelpers::getImageSliceIndices( ::fwData::Image::sptr _pImg )
128 {
129  SLM_ASSERT("_pImg pointer null", _pImg);
130 
131  ::fwData::Point::sptr point = ::fwData::Point::New();
132 
134 
135  point->getCoord()[0] =
137  point->getCoord()[1] =
139  point->getCoord()[2] =
141 
142  return point;
143 }
144 
145 //------------------------------------------------------------------------------
146 
147 bool MedicalImageHelpers::checkComment( ::fwData::Image::sptr _pImg )
148 {
149  SLM_ASSERT("_pImg pointer null", _pImg);
150 
151  bool fieldIsModified = false;
152 
153  if ( !_pImg->getField( ::fwDataTools::fieldHelper::Image::m_commentId ) )
154  {
155  // Set value
156  ::fwData::String::sptr param = ::fwData::String::New("Original image");
157  _pImg->setField( ::fwDataTools::fieldHelper::Image::m_commentId, param );
158  fieldIsModified = true;
159  }
160 
161  return fieldIsModified;
162 }
163 
164 //------------------------------------------------------------------------------
165 
166 ::fwData::Image::sptr MedicalImageHelpers::initialize( ::fwData::Image::sptr imgSrc,
167  ::fwData::Image::sptr imgToInitialize)
168 {
169  SLM_ASSERT("Image source must be initialized", imgSrc);
170  SLM_ASSERT("Image source must be valid", MedicalImageHelpers::checkImageValidity(imgSrc));
171 
172  if(!imgToInitialize)
173  {
174  imgToInitialize = ::fwData::Image::New();
175  }
176  ::fwData::Array::sptr imgData = imgSrc->getDataArray();
177  imgSrc->setDataArray(::fwData::Array::sptr(), false);
178 
179  imgToInitialize = ::fwData::Object::copy(imgSrc);
180 
181  imgSrc->setDataArray(imgData, false);
182 
183  imgToInitialize->allocate();
184 
185  return imgToInitialize;
186 }
187 
188 //------------------------------------------------------------------------------
189 
190 bool MedicalImageHelpers::isBufNull(const ::fwData::Image::BufferType* buf, const unsigned int len)
191 {
192  bool isNull;
193  const ::fwData::Image::BufferType* ucbuf = static_cast< const ::fwData::Image::BufferType*> (buf);
194  isNull = 0 == std::accumulate(
195  ucbuf,
196  ucbuf+len,
197  0,
199  );
200  return isNull;
201 }
202 
203 //------------------------------------------------------------------------------
204 
205 } // fieldHelper
206 } // fwDataTools
#define SLM_TRACE_FUNC()
Trace contextual function signature.
Definition: spyLog.hpp:329
This class contains an integer value. Integer object is essentially used as a field in other objects...
Definition: Integer.hpp:24
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
FWDATA_API::fwData::Object::sptr getField(const FieldNameType &name,::fwData::Object::sptr defaultValue=::fwData::Object::sptr()) const
Returns a pointer of corresponding field (null if non exist).
static FWDATATOOLS_API bool checkImageSliceIndex(::fwData::Image::sptr _pImg)
Check if the image has slice index fields.
#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
static FWDATA_API::fwData::Object::sptr copy(const ::fwData::Object::csptr &source)
return a copy of the source. if source is a null pointer, return a null pointer.
static FWDATATOOLS_API bool checkComment(::fwData::Image::sptr _pImg)
Check if the image has a comment field.
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.
static FWDATATOOLS_API::fwData::Point::sptr getImageSliceIndices(::fwData::Image::sptr _pImg)
Get the X,Y,Z slice position.
static FWDATATOOLS_API::fwData::Image::sptr initialize(::fwData::Image::sptr imgSrc,::fwData::Image::sptr imgToInitialize=::fwData::Image::sptr())
Initialized an image to 0 from image source (same field, pixel type, buffer size...).