ITK  4.3.0
Insight Segmentation and Registration Toolkit
itkConstNeighborhoodIterator.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkConstNeighborhoodIterator_h
19 #define __itkConstNeighborhoodIterator_h
20 
21 #include <vector>
22 #include <cstring>
23 #include <iostream>
24 #include "itkImage.h"
25 #include "itkNeighborhood.h"
26 #include "itkMacro.h"
28 
29 namespace itk
30 {
49 template< class TImage, class TBoundaryCondition =
50  ZeroFluxNeumannBoundaryCondition< TImage > >
51 class ITK_EXPORT ConstNeighborhoodIterator:
52  public Neighborhood< typename TImage::InternalPixelType *,
53  TImage::ImageDimension >
54 {
55 public:
57  typedef typename TImage::InternalPixelType InternalPixelType;
58  typedef typename TImage::PixelType PixelType;
59 
61  itkStaticConstMacro(Dimension, unsigned int, TImage::ImageDimension);
62 
64  typedef unsigned int DimensionValueType;
65 
68  typedef Neighborhood< InternalPixelType *,
69  itkGetStaticConstMacro(Dimension) > Superclass;
70 
73  typedef typename Superclass::RadiusType RadiusType;
74  typedef typename Superclass::SizeType SizeType;
75  typedef typename Superclass::Iterator Iterator;
77 
79  typedef TImage ImageType;
80  typedef typename TImage::RegionType RegionType;
85 
88 
92  typedef typename ImageType::NeighborhoodAccessorFunctorType
94 
96  typedef TBoundaryCondition BoundaryConditionType;
97 
102 
105 
108 
111 
115  const ImageType *ptr,
116  const RegionType & region)
117  {
118  this->Initialize(radius, ptr, region);
119  for ( unsigned int i = 0; i < Dimension; i++ )
120  { m_InBounds[i] = false; }
121  this->ResetBoundaryCondition();
122  m_NeighborhoodAccessorFunctor = ptr->GetNeighborhoodAccessor();
123  m_NeighborhoodAccessorFunctor.SetBegin( ptr->GetBufferPointer() );
124  }
126 
128  Self & operator=(const Self & orig);
129 
131  virtual void PrintSelf(std::ostream &, Indent) const;
132 
135  OffsetType ComputeInternalIndex(NeighborIndexType n) const;
136 
138  IndexType GetBound() const
139  { return m_Bound; }
140 
144  { return m_Bound[n]; }
145 
147  const InternalPixelType * GetCenterPointer() const
148  { return ( this->operator[]( ( this->Size() ) >> 1 ) ); }
149 
152  PixelType GetCenterPixel() const
153  { return m_NeighborhoodAccessorFunctor.Get( this->GetCenterPointer() ); }
154 
156  const ImageType * GetImagePointer(void) const
157  { return m_ConstImage; }
158 
161  virtual IndexType GetIndex(void) const
162  { return m_Loop; }
163 
166  virtual NeighborhoodType GetNeighborhood() const;
167 
169  virtual PixelType GetPixel(NeighborIndexType i) const
170  {
171  if ( !m_NeedToUseBoundaryCondition )
172  {
173  return ( m_NeighborhoodAccessorFunctor.Get( this->operator[](i) ) );
174  }
175  bool inbounds;
176  return this->GetPixel(i, inbounds);
177  }
179 
185  virtual PixelType GetPixel(NeighborIndexType i, bool & IsInBounds) const;
186 
189  virtual PixelType GetPixel(const OffsetType & o) const
190  {
191  bool inbounds;
192 
193  return ( this->GetPixel(this->GetNeighborhoodIndex(o), inbounds) );
194  }
195 
201  virtual PixelType GetPixel(const OffsetType & o,
202  bool & IsInBounds) const
203  { return ( this->GetPixel(this->GetNeighborhoodIndex(o), IsInBounds) ); }
204 
208  virtual PixelType GetNext(const unsigned axis, NeighborIndexType i) const
209  {
210  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
211  + ( i * this->GetStride(axis) ) ) );
212  }
213 
217  virtual PixelType GetNext(const unsigned axis) const
218  {
219  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
220  + this->GetStride(axis) ) );
221  }
222 
226  virtual PixelType GetPrevious(const unsigned axis, NeighborIndexType i) const
227  {
228  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
229  - ( i * this->GetStride(axis) ) ) );
230  }
231 
235  virtual PixelType GetPrevious(const unsigned axis) const
236  {
237  return ( this->GetPixel( this->GetCenterNeighborhoodIndex()
238  - this->GetStride(axis) ) );
239  }
240 
243  virtual IndexType GetIndex(const OffsetType & o) const
244  { return ( this->GetIndex() + o ); }
245 
248  virtual IndexType GetIndex(NeighborIndexType i) const
249  { return ( this->GetIndex() + this->GetOffset(i) ); }
250 
252  RegionType GetRegion() const
253  { return m_Region; }
254 
257  IndexType GetBeginIndex() const
258  { return m_BeginIndex; }
259 
262  RegionType GetBoundingBoxAsImageRegion() const;
263 
265  OffsetType GetWrapOffset() const
266  { return m_WrapOffset; }
267 
273  OffsetValueType GetWrapOffset(NeighborIndexType n) const
274  { return m_WrapOffset[n]; }
275 
279  virtual void GoToBegin();
280 
283  virtual void GoToEnd();
284 
287  virtual void Initialize(const SizeType & radius, const ImageType *ptr,
288  const RegionType & region);
289 
292  virtual bool IsAtBegin() const
293  { return ( this->GetCenterPointer() == m_Begin ); }
294 
297  virtual bool IsAtEnd() const
298  {
299  if ( this->GetCenterPointer() > m_End )
300  {
301  ExceptionObject e(__FILE__, __LINE__);
302  std::ostringstream msg;
303  msg << "In method IsAtEnd, CenterPointer = " << this->GetCenterPointer()
304  << " is greater than End = " << m_End
305  << std::endl
306  << " " << *this;
307  e.SetDescription( msg.str().c_str() );
308  throw e;
309  }
310  return ( this->GetCenterPointer() == m_End );
311  }
313 
318  Self & operator++();
319 
324  Self & operator--();
325 
329  bool operator==(const Self & it) const
330  { return it.GetCenterPointer() == this->GetCenterPointer(); }
331 
335  bool operator!=(const Self & it) const
336  { return it.GetCenterPointer() != this->GetCenterPointer(); }
337 
341  bool operator<(const Self & it) const
342  { return this->GetCenterPointer() < it.GetCenterPointer(); }
343 
347  bool operator<=(const Self & it) const
348  { return this->GetCenterPointer() <= it.GetCenterPointer(); }
349 
353  bool operator>(const Self & it) const
354  { return this->GetCenterPointer() > it.GetCenterPointer(); }
355 
359  bool operator>=(const Self & it) const
360  { return this->GetCenterPointer() >= it.GetCenterPointer(); }
361 
366  void SetLocation(const IndexType & position)
367  {
368  this->SetLoop(position);
369  this->SetPixelPointers(position);
370  }
372 
376  Self & operator+=(const OffsetType &);
377 
381  Self & operator-=(const OffsetType &);
382 
385  { return m_Loop - b.m_Loop; }
386 
390  bool InBounds() const;
391 
403  bool IndexInBounds(NeighborIndexType n, OffsetType & internalIndex, OffsetType & offset ) const;
404 
410  virtual void OverrideBoundaryCondition(const
412  { m_BoundaryCondition = i; }
413 
416  virtual void ResetBoundaryCondition()
417  { m_BoundaryCondition = &m_InternalBoundaryCondition; }
418 
420  void SetBoundaryCondition(const TBoundaryCondition & c)
421  { m_InternalBoundaryCondition = c; }
422 
424  ImageBoundaryConditionPointerType GetBoundaryCondition() const
425  { return m_BoundaryCondition; }
426 
428  void NeedToUseBoundaryConditionOn()
429  {
430  this->SetNeedToUseBoundaryCondition(true);
431  }
432 
433  void NeedToUseBoundaryConditionOff()
434  {
435  this->SetNeedToUseBoundaryCondition(false);
436  }
437 
438  void SetNeedToUseBoundaryCondition(bool b)
439  {
440  m_NeedToUseBoundaryCondition = b;
441  }
442 
443  bool GetNeedToUseBoundaryCondition() const
444  {
445  return m_NeedToUseBoundaryCondition;
446  }
447 
449  virtual void SetRegion(const RegionType & region);
450 
451 protected:
452 
455  virtual void SetLoop(const IndexType & p)
456  { m_Loop = p; m_IsInBoundsValid = false; }
457 
461  virtual void SetBound(const SizeType &);
462 
467  virtual void SetPixelPointers(const IndexType &);
468 
471  virtual void SetBeginIndex(const IndexType & start)
472  { m_BeginIndex = start; }
473 
476  virtual void SetEndIndex();
477 
481 
484 
486  const InternalPixelType *m_Begin;
487 
489  typename ImageType::ConstWeakPointer m_ConstImage;
490 
492  const InternalPixelType *m_End;
493 
497 
500 
502  RegionType m_Region;
503 
510 
516 
519  mutable bool m_InBounds[Dimension];
520 
522  mutable bool m_IsInBounds;
523 
527  mutable bool m_IsInBoundsValid;
528 
531 
534 
536  TBoundaryCondition m_InternalBoundaryCondition;
537 
540 
543 };
544 
545 template< class TImage >
549  ::OffsetType & ind)
550 {
552  ret = it;
553  ret += ind;
554  return ret;
555 }
556 
557 template< class TImage >
558 inline ConstNeighborhoodIterator< TImage >
560  ::OffsetType & ind,
562 { return ( it + ind ); }
563 
564 template< class TImage >
565 inline ConstNeighborhoodIterator< TImage >
568  ::OffsetType & ind)
569 {
571  ret = it;
572  ret -= ind;
573  return ret;
574 }
575 } // namespace itk
576 
577 #ifndef ITK_MANUAL_INSTANTIATION
578 #include "itkConstNeighborhoodIterator.hxx"
579 #endif
580 
581 #endif
582