2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * @file ImageContainerByITKImage.ih
19 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20 * @author Pierre Gueth (\c pierre.gueth@liris.cnrs.fr )
21 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
25 * Implementation of inline methods defined in ImageContainerByITKImage.h
27 * This file is part of the DGtal library.
31 //////////////////////////////////////////////////////////////////////////////
33 //////////////////////////////////////////////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////////
36 // IMPLEMENTATION of inline methods.
37 ///////////////////////////////////////////////////////////////////////////////
39 ///////////////////////////////////////////////////////////////////////////////
40 // ----------------------- Standard services ------------------------------
44 template<typename TDomain, typename TValue>
45 ImageContainerByITKImage<TDomain, TValue>::~ImageContainerByITKImage()
49 template <typename TDomain, typename TValue>
50 ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const ImageContainerByITKImage& other)
51 : myITKImagePointer(ITKImage::New()), myDomain()
53 myITKImagePointer = other.getITKImagePointer();
57 template <typename TDomain, typename TValue>
58 ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const TDomain& aDomain)
59 : myITKImagePointer(ITKImage::New()), myDomain(aDomain)
61 typename ITKImage::IndexType start;
62 typename ITKImage::SizeType size;
64 const Point lowerBound = aDomain.lowerBound();
65 const Point upperBound = aDomain.upperBound();
66 const Point dec = upperBound-lowerBound;
68 for (Dimension k = 0; k < dimension; k++)
70 start[k] = lowerBound[k];
74 const typename ITKImage::RegionType region(start,size);
76 myITKImagePointer->SetRegions(region);
77 myITKImagePointer->Allocate();
78 myITKImagePointer->FillBuffer(0);
81 template <typename TDomain, typename TValue>
82 ImageContainerByITKImage<TDomain, TValue>::ImageContainerByITKImage(const ITKImagePointer &aRef)
83 : myITKImagePointer(aRef)
88 ///////////////////////////////////////////////////////////////////////////////
89 // Interface - public :
91 template <typename TDomain, typename TValue>
93 ImageContainerByITKImage<TDomain, TValue>::updateDomain(const Point & inputDomainShift)
95 myDomainShift = inputDomainShift;
96 const typename ITKImage::RegionType region = myITKImagePointer->GetLargestPossibleRegion();
97 const typename ITKImage::IndexType start = region.GetIndex();
98 const typename ITKImage::SizeType size = region.GetSize();
102 for (Dimension k = 0; k < dimension; k++)
104 lowerBound[k] = static_cast<typename Point::Component>(start[k]+myDomainShift[k]);
105 upperBound[k] = static_cast<typename Point::Component>(start[k]+size[k]+myDomainShift[k]-1);
108 myDomain = TDomain(lowerBound, upperBound);
112 * Get the value of an image at a given position.
114 * @param aPoint position in the image.
115 * @return the value at aPoint.
117 template <typename TDomain, typename TValue>
120 ImageContainerByITKImage<TDomain, TValue>::operator()(const Point &domainPoint) const
122 return myITKImagePointer->GetPixel(getItkIndexFromDomainPoint(domainPoint));
126 * Get the value of an image at a given position.
128 * @param aPoint position in the image.
129 * @return the value at aPoint.
131 template <typename TDomain, typename TValue>
134 ImageContainerByITKImage<TDomain, TValue>::operator()(const ConstIterator &it) const
140 * Get the value of an image at a given position.
142 * @param aPoint position in the image.
143 * @return the value at aPoint.
145 template <typename TDomain, typename TValue>
148 ImageContainerByITKImage<TDomain, TValue>::operator()(const Iterator &it) const
153 template <typename TDomain, typename TValue>
155 DGtal::ImageContainerByITKImage<TDomain,TValue>&
156 DGtal::ImageContainerByITKImage<TDomain,TValue>
157 ::operator=(const ImageContainerByITKImage& other)
161 myITKImagePointer = other.myITKImagePointer;
167 template <typename Domain, typename T>
170 ImageContainerByITKImage<Domain, T>::setValue(const Point &domainPoint, const T &V)
172 myITKImagePointer->SetPixel(getItkIndexFromDomainPoint(domainPoint), V);
175 template <typename Domain, typename T>
178 ImageContainerByITKImage<Domain, T>::setValue(Iterator &it, const T &V)
184 * Writes/Displays the object on an output stream.
185 * @param out the output stream where the object is written.
187 template<typename TDomain, typename TValue>
190 ImageContainerByITKImage<TDomain, TValue>::selfDisplay ( std::ostream & out ) const
192 typename ITKImage::RegionType region = myITKImagePointer->GetLargestPossibleRegion();
194 out << "[ImageContainerByITKImage] valuetype=" << sizeof(TValue) << "bytes "
195 << "domain=" << this->domain() << " "
196 << "domainShift=" << this->getDomainShift() << " "
197 << "refcount=" << myITKImagePointer->GetReferenceCount() << " "
198 << "region=" << region.GetIndex() << "/" << region.GetSize();
201 // ----------------- DomainPoint to/from IndexPoint interface -------------
202 template<typename TDomain, typename TValue>
204 typename ImageContainerByITKImage<TDomain, TValue>::Point
205 ImageContainerByITKImage<TDomain, TValue>::getDomainPointFromIndex ( const Point & indexPoint ) const
207 return indexPoint + myDomainShift;
210 template<typename TDomain, typename TValue>
212 typename ImageContainerByITKImage<TDomain, TValue>::Point
213 ImageContainerByITKImage<TDomain, TValue>::getIndexFromDomainPoint ( const Point & domainPoint ) const
215 return domainPoint - myDomainShift;
218 template<typename TDomain, typename TValue>
220 typename ImageContainerByITKImage<TDomain, TValue>::Point
221 ImageContainerByITKImage<TDomain, TValue>::getDomainPointFromItkIndex (
222 const typename ITKImage::IndexType &itkIndexPoint) const
224 Point dgtal_domain_point;
225 for (Dimension k = 0; k < dimension; k++)
226 dgtal_domain_point[k] = itkIndexPoint[k] + myDomainShift[k];
228 return dgtal_domain_point;
232 template<typename TDomain, typename TValue>
234 typename ImageContainerByITKImage<TDomain, TValue>::ITKImage::IndexType
235 ImageContainerByITKImage<TDomain, TValue>::getItkIndexFromDomainPoint(
236 const Point &domainPoint) const
238 typename ITKImage::IndexType itk_index;
239 for (Dimension k = 0; k < dimension; k++)
240 itk_index[k] = domainPoint[k] - myDomainShift[k];
245 // ------------------------- PhysicalPoint interface ----------------------
246 template<typename TDomain, typename TValue>
248 typename ImageContainerByITKImage<TDomain, TValue>::PhysicalPoint
249 ImageContainerByITKImage<TDomain, TValue>::getPhysicalPointFromDomainPoint(const Point &domainPoint) const
251 // Transform input Point to itk Index.
252 // The index would be different than the domain point if myDomainShift is not zero.
253 const auto itk_index = getItkIndexFromDomainPoint(domainPoint);
254 // ITK performs the transform between index and physical spaces.
255 typename ITKImage::PointType itk_point;
256 myITKImagePointer->template TransformIndexToPhysicalPoint<typename RealPoint::Component>(itk_index, itk_point);
258 // Transform itk physical point to dgtal
259 PhysicalPoint dgtal_real_point;
260 for (Dimension k = 0; k < dimension; k++)
261 dgtal_real_point[k] = itk_point[k];
263 return dgtal_real_point;
266 template<typename TDomain, typename TValue>
268 typename ImageContainerByITKImage<TDomain, TValue>::Point
269 ImageContainerByITKImage<TDomain, TValue>::getDomainPointFromPhysicalPoint(const PhysicalPoint &physicalPoint) const
271 // Transform input dgtal point to itk point (real points)
272 typename ITKImage::PointType itk_point;
273 for (Dimension k = 0; k < dimension; k++)
274 itk_point[k] = physicalPoint[k];
276 // ITK performs the transform between index and physical spaces.
277 typename ITKImage::IndexType itk_index;
278 myITKImagePointer->TransformPhysicalPointToIndex(itk_point, itk_index);
280 return getDomainPointFromItkIndex(itk_index);
283 template<typename TDomain, typename TValue>
285 typename ImageContainerByITKImage<TDomain, TValue>::PhysicalPoint
286 ImageContainerByITKImage<TDomain, TValue>::getLowerBoundAsPhysicalPoint() const
288 return getPhysicalPointFromDomainPoint(myDomain.lowerBound());
291 template<typename TDomain, typename TValue>
293 typename ImageContainerByITKImage<TDomain, TValue>::PhysicalPoint
294 ImageContainerByITKImage<TDomain, TValue>::getUpperBoundAsPhysicalPoint() const
296 return getPhysicalPointFromDomainPoint(myDomain.upperBound());
299 template<typename TDomain, typename TValue>
301 typename ImageContainerByITKImage<TDomain, TValue>::ImageSpacing
302 ImageContainerByITKImage<TDomain, TValue>::getImageSpacing() const
305 auto s = myITKImagePointer->GetSpacing();
306 for (Dimension k = 0; k < dimension; k++)
310 template<typename TDomain, typename TValue>
313 ImageContainerByITKImage<TDomain, TValue>::setImageSpacing(const ImageContainerByITKImage<TDomain, TValue>::ImageSpacing &s) const
315 ITKSpacingValueType is [dimension];
316 for (Dimension k = 0; k < dimension; k++)
318 myITKImagePointer->SetSpacing(is);
321 ///////////////////////////////////////////////////////////////////////////////
322 // Implementation of inline functions //
324 template<typename TDomain, typename TValue>
327 operator<< ( std::ostream & out,
328 const ImageContainerByITKImage<TDomain, TValue> & object )
330 object.selfDisplay( out );
335 ///////////////////////////////////////////////////////////////////////////////