DMRITool  v0.1.1-139-g860d86b4
Diffusion MRI Tool
itkSpatiallyDenseSparseVectorImage.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Spatially Dense Sparse Vector Image
4 
5  Copyright (c) Pew-Thian Yap. All rights reserved.
6  See http://www.unc.edu/~ptyap/ for details.
7 
8  This software is distributed WITHOUT ANY WARRANTY; without even
9  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  PURPOSE. See the above copyright notices for more information.
11 
12  =========================================================================*/
13 
14 #ifndef __itkSpatiallyDenseSparseVectorImage_h
15 #define __itkSpatiallyDenseSparseVectorImage_h
16 
17 #include "itkImage.h"
21 #include "itkNeighborhoodAccessorFunctor.h"
22 #include "itkVariableLengthVector.h"
23 #include "itkSparseVector.h"
24 
25 
26 namespace itk
27 {
28 
39 template <class TValueType, unsigned int VImageDimension, typename TKeyType = unsigned long>
41  public ImageBase< VImageDimension >
42 {
43 public:
46  typedef ImageBase< VImageDimension > Superclass;
47  typedef SmartPointer<Self> Pointer;
48  typedef SmartPointer<const Self> ConstPointer;
49  typedef WeakPointer<const Self> ConstWeakPointer;
50 
52  itkNewMacro(Self);
53 
55  itkTypeMacro(SpatiallyDenseSparseVectorImage, Image);
56 
58  typedef VariableLengthVector< TValueType > PixelType;
59 
61  typedef TValueType ValueType;
62  typedef TKeyType KeyType;
64 
66  typedef InternalPixelType IOPixelType;
67 
72  itkStaticConstMacro( ImageDimension, unsigned int, VImageDimension );
73 
75  typedef ImportImageContainer< SizeValueType, InternalPixelType > PixelContainer;
76 
78  typedef typename PixelContainer::Pointer PixelContainerPointer;
79  typedef typename PixelContainer::ConstPointer PixelContainerConstPointer;
80 
83  typedef typename PixelMapType::iterator PixelMapIterator;
84  typedef typename PixelMapType::const_iterator PixelMapConstIterator;
85 
87  typedef typename Superclass::IndexType IndexType;
88 
90  typedef typename Superclass::OffsetType OffsetType;
91 
93  typedef typename Superclass::SizeType SizeType;
94 
96  typedef typename Superclass::DirectionType DirectionType;
97 
99  typedef typename Superclass::RegionType RegionType;
100 
103  typedef typename Superclass::SpacingType SpacingType;
104 
107  typedef typename Superclass::PointType PointType;
108 
110  typedef typename Superclass::OffsetValueType OffsetValueType;
111 
112  typedef unsigned long VectorLengthType;
113 
117  ValueType, KeyType > AccessorType;
118 
122 
126 
129 // void Allocate();
130  void Allocate(bool UseDefaultConstructor = false) ITK_OVERRIDE;
131 
135 // using Superclass::SetRegions;
136  void SetRegions(const RegionType & region) ITK_OVERRIDE
137  {
138  this->SetLargestPossibleRegion(region);
139  this->SetBufferedRegion(region);
140  this->SetRequestedRegion(region);
141  }
142 
143  void SetRegions(const SizeType & size) ITK_OVERRIDE
144  {
145  RegionType region; region.SetSize(size);
146  this->SetLargestPossibleRegion(region);
147  this->SetBufferedRegion(region);
148  this->SetRequestedRegion(region);
149  }
150 
154  virtual void SetBufferedRegion(const RegionType &region) ITK_OVERRIDE
155  {
156  this->ComputeOffsetTable();
157  this->Modified();
158  }
159 
163  virtual const RegionType& GetBufferedRegion() const ITK_OVERRIDE
164  { return this->GetLargestPossibleRegion(); }
165 
168  virtual void Initialize() ITK_OVERRIDE;
169 
170  OffsetValueType ComputeOffset(const IndexType &ind) const
171  {
172  OffsetValueType offset=0;
173  const OffsetValueType *offsetTable = this->GetOffsetTable();
174 
175  for (int i=ImageDimension-1; i > 0; i--)
176  {
177  offset += ind[i]*offsetTable[i];
178  }
179  offset += ind[0];
180 
181  return offset;
182  }
183 
186  void FillBuffer(const PixelType& value);
187 
193  void SetPixel( const IndexType &index, const PixelType &value )
194  {
195  PixelMapType *map = (this->GetInternalPixel(index)).GetDataPointer();
196 
197  map->clear();
198 
199  for ( VectorLengthType i = 0; i < m_VectorLength; i++ )
200  {
201  if ( value[i] != 0 )
202  {
203  (*map)[i] = value[i];
204  }
205  }
206  }
207 
213  const PixelType GetPixel(const IndexType &index) const
214  {
215  PixelType pixel;
216  pixel.SetSize(m_VectorLength);
217 
218  const PixelMapType *map = (this->GetInternalPixel(index)).GetDataPointer();
219 
220  for ( VectorLengthType i = 0; i < m_VectorLength; i++ )
221  {
222  typename PixelMapType::const_iterator it = map->find( i );
223 
224  if ( it == map->end() )
225  {
226  pixel[i] = m_FillBufferValue[i];
227  }
228  else
229  {
230  pixel[i] = it->second;
231  }
232  }
233 
234  return pixel;
235  }
236 
241  PixelType GetPixel(const IndexType &index)
242  {
243  PixelType pixel;
244  pixel.SetSize(m_VectorLength);
245 
246  const PixelMapType *map = (this->GetInternalPixel(index)).GetDataPointer();
247 
248  for ( VectorLengthType i = 0; i < m_VectorLength; i++ )
249  {
250  typename PixelMapType::const_iterator it = map->find( i );
251 
252  if ( it == map->end() )
253  {
254  pixel[i] = m_FillBufferValue[i];
255  }
256  else
257  {
258  pixel[i] = it->second;
259  }
260  }
261 
262  return pixel;
263  }
264 
270  const InternalPixelType & GetInternalPixel(const IndexType &index) const
271  {
272  OffsetValueType offset = this->ComputeOffset(index);
273  return (*m_Container)[offset];
274  }
275 
281  InternalPixelType & GetInternalPixel(const IndexType &index)
282  {
283  OffsetValueType offset = this->ComputeOffset(index);
284  return (*m_Container)[offset];
285  }
286 
292  PixelType operator[](const IndexType &index)
293  { return this->GetPixel(index); }
294 
299  PixelType operator[](const IndexType &index) const
300  { return this->GetPixel(index); }
301 
303  PixelContainer* GetPixelContainer()
304  {
305  return m_Container.GetPointer();
306  }
307 
309  const PixelContainer* GetPixelContainer() const
310  {
311  return m_Container.GetPointer();
312  }
313 
315  InternalPixelType * GetBufferPointer()
316  {
317  return m_Container->GetBufferPointer();
318  }
319  const InternalPixelType * GetBufferPointer() const
320  {
321  return m_Container->GetBufferPointer();
322  }
323 
326  void SetPixelContainer( PixelContainer *container );
327 
338  virtual void Graft(const DataObject *data) ITK_OVERRIDE;
339 
341  AccessorType GetPixelAccessor( void )
342  {
343  return AccessorType(m_FillBufferValue, m_VectorLength);
344  }
345 
347  const AccessorType GetPixelAccessor( void ) const
348  {
349  return AccessorType(m_FillBufferValue, m_VectorLength);
350  }
351 
354  {
355  return NeighborhoodAccessorFunctorType(m_FillBufferValue, m_VectorLength);
356  }
357 
360  {
361  return NeighborhoodAccessorFunctorType(m_FillBufferValue, m_VectorLength);
362  }
363 
365  itkSetMacro(VectorLength, VectorLengthType);
366  itkGetConstReferenceMacro(VectorLength, VectorLengthType);
367 
369  virtual unsigned int GetNumberOfComponentsPerPixel() const ITK_OVERRIDE;
370 
371  virtual void SetNumberOfComponentsPerPixel(unsigned int n) ITK_OVERRIDE;
372 
373 protected:
375  void PrintSelf( std::ostream& os, Indent indent ) const ITK_OVERRIDE;
377 
378 private:
379  SpatiallyDenseSparseVectorImage( const Self & ); // purposely not implementated
380  void operator=(const Self&); //purposely not implemented
381 
383  PixelContainerPointer m_Container;
384  PixelType m_FillBufferValue;
385 
387  VectorLengthType m_VectorLength;
388 };
389 
390 
391 } // end namespace itk
392 
393 #ifndef ITK_MANUAL_INSTANTIATION
395 #endif
396 
397 #endif
SpatiallyDenseSparseVectorImagePixelAccessor< ValueType, KeyType > AccessorType
InternalPixelType::InternalDataType PixelMapType
const NeighborhoodAccessorFunctorType GetNeighborhoodAccessor() const
void SetRegions(const SizeType &size) ITK_OVERRIDE
const InternalPixelType & GetInternalPixel(const IndexType &index) const
Get an internal pixel (read only version).
PixelType GetPixel(const IndexType &index)
Get a reference to a pixel (e.g. for editing).
SpatiallyDenseSparseVectorImageNeighborhoodAccessorFunctor< Self > NeighborhoodAccessorFunctorType
const PixelType GetPixel(const IndexType &index) const
Get a pixel (read only version).
SpatiallyDenseSparseVectorImagePixelAccessorFunctor< Self > AccessorFunctorType
ImportImageContainer< SizeValueType, InternalPixelType > PixelContainer
virtual void SetBufferedRegion(const RegionType &region) ITK_OVERRIDE
#define ITK_OVERRIDE
Definition: utlITKMacro.h:46
Represents a sparse array.
PixelType operator[](const IndexType &index) const
Access a pixel.
void ComputeOffsetTable(const std::vector< int > &size, std::vector< int > &offsetTable, const int storedWay)
Definition: utlCore.h:1915
OffsetValueType ComputeOffset(const IndexType &ind) const
virtual const RegionType & GetBufferedRegion() const ITK_OVERRIDE
const InternalPixelType * GetBufferPointer() const
void SetPixel(const IndexType &index, const PixelType &value)
Set a pixel value.
NeighborhoodAccessorFunctorType GetNeighborhoodAccessor()
Provides accessor interfaces to Access pixels and is meant to be used by iterators.
Provides accessor interfaces to Access pixels and is meant to be used on pointers to pixels held by t...
SparseVector< TValueType, TKeyType > InternalPixelType
utl_unordered_map< TKeyType, TValueType > InternalDataType
InternalPixelType & GetInternalPixel(const IndexType &index)
Get an internal pixel (e.g. for editing).
An n-dimensional vector image with a sparse memory model.
PixelType operator[](const IndexType &index)
Access a pixel. This version cannot be an lvalue because the pixel is converted on the fly to a Varia...