17 #ifndef __itkConstNeighborhoodIterator_txx
18 #define __itkConstNeighborhoodIterator_txx
22 template<
class TImage,
class TBoundaryCondition>
27 if (m_IsInBoundsValid)
33 for (
unsigned int i=0; i<Dimension; i++)
35 if (m_Loop[i] < m_InnerBoundsLow[i] || m_Loop[i] >= m_InnerBoundsHigh[i])
37 m_InBounds[i] = ans =
false;
45 m_IsInBoundsValid =
true;
50 template<
class TImage,
class TBoundaryCondition>
58 if (!m_NeedToUseBoundaryCondition)
61 return (m_NeighborhoodAccessorFunctor.Get(this->operator[](n)));
64 register unsigned int i;
73 return (m_NeighborhoodAccessorFunctor.Get(this->operator[](n)));
77 temp = this->ComputeInternalIndex(n);
82 for (i=0; i<Dimension; ++i)
91 OverlapLow = m_InnerBoundsLow[i] - m_Loop[i];
94 ( (m_Loop[i]+2) - m_InnerBoundsHigh[i] ));
97 if (temp[i] < OverlapLow)
100 offset[i] = OverlapLow - temp[i];
102 else if ( OverlapHigh < temp[i] )
105 offset[i] = OverlapHigh - temp[i];
114 return ( m_NeighborhoodAccessorFunctor.Get(this->operator[](n)) );
119 return( m_NeighborhoodAccessorFunctor.BoundaryCondition(
120 temp, offset,
this, this->m_BoundaryCondition) );
126 template<
class TImage,
class TBoundaryCondition>
132 long D = (long)Dimension;
134 r = (
unsigned long)n;
135 for (
long i = D-1; i >= 0; --i)
138 r = r % this->GetStride(i);
144 template <
class TImage,
class TBoundaryCondition>
151 ans.SetIndex(this->GetIndex(zero));
152 ans.SetSize(this->GetSize());
157 template<
class TImage,
class TBoundaryCondition>
166 m_BeginIndex.Fill(0);
171 m_Region.SetIndex(zeroIndex);
172 m_Region.SetSize(zeroSize);
174 m_WrapOffset.Fill(0);
176 for (
unsigned int i=0; i < Dimension; i++)
177 { m_InBounds[i] =
false; }
179 this->ResetBoundaryCondition();
181 m_IsInBounds =
false;
182 m_IsInBoundsValid =
false;
185 template<
class TImage,
class TBoundaryCondition>
190 m_Bound = orig.m_Bound;
191 m_Begin = orig.m_Begin;
192 m_BeginIndex = orig.m_BeginIndex;
193 m_ConstImage = orig.m_ConstImage;
195 m_EndIndex = orig.m_EndIndex;
196 m_Loop = orig.m_Loop;
197 m_Region = orig.m_Region;
198 m_WrapOffset = orig.m_WrapOffset;
200 m_InternalBoundaryCondition = orig.m_InternalBoundaryCondition;
201 m_NeedToUseBoundaryCondition = orig.m_NeedToUseBoundaryCondition;
202 for (
unsigned int i = 0; i < Dimension; ++i)
204 m_InBounds[i] = orig.m_InBounds[i];
206 m_IsInBoundsValid = orig.m_IsInBoundsValid;
207 m_IsInBounds = orig.m_IsInBounds;
209 m_InnerBoundsLow = orig.m_InnerBoundsLow;
210 m_InnerBoundsHigh = orig.m_InnerBoundsHigh;
214 if ( orig.m_BoundaryCondition ==
215 static_cast<ImageBoundaryConditionConstPointerType>(
216 &orig.m_InternalBoundaryCondition ))
218 this->ResetBoundaryCondition();
221 { m_BoundaryCondition = orig.m_BoundaryCondition; }
223 m_NeighborhoodAccessorFunctor = orig.m_NeighborhoodAccessorFunctor;
227 template<
class TImage,
class TBoundaryCondition>
232 if (m_Region.GetNumberOfPixels() > 0)
234 m_EndIndex = m_Region.GetIndex();
235 m_EndIndex[Dimension-1] = m_Region.GetIndex()[Dimension-1] +
236 static_cast<long>(m_Region.GetSize()[Dimension-1]);
241 m_EndIndex = m_Region.GetIndex();
245 template<
class TImage,
class TBoundaryCondition>
250 register unsigned int i;
251 OffsetType OverlapLow, OverlapHigh, temp, offset;
261 if (m_NeedToUseBoundaryCondition ==
false)
263 for (ans_it = ans.
Begin(), this_it = this->Begin();
264 this_it < _end; ans_it++, this_it++)
265 { *ans_it = m_NeighborhoodAccessorFunctor.Get(*this_it); }
269 for (ans_it = ans.
Begin(), this_it = this->Begin();
270 this_it < _end; ans_it++, this_it++)
271 { *ans_it = m_NeighborhoodAccessorFunctor.Get(*this_it); }
276 for (i=0; i<Dimension; i++)
278 OverlapLow[i] = m_InnerBoundsLow[i] - m_Loop[i];
281 - m_InnerBoundsHigh[i] );
286 for (ans_it = ans.
Begin(), this_it = this->Begin();
287 this_it < _end; ans_it++, this_it++)
292 for (i=0; i<Dimension; ++i)
294 if (m_InBounds[i]) offset[i] = 0;
297 if (temp[i] < OverlapLow[i])
300 offset[i] = OverlapLow[i] - temp[i];
302 else if ( OverlapHigh[i] < temp[i] )
305 offset[i] = OverlapHigh[i] - temp[i];
311 if (flag) *ans_it = m_NeighborhoodAccessorFunctor.Get(*this_it);
312 else *ans_it = m_NeighborhoodAccessorFunctor.BoundaryCondition(
313 temp, offset,
this, this->m_BoundaryCondition);
315 m_BoundaryCondition->operator()(temp, offset,
this);
317 for (i=0; i<Dimension; ++i)
320 if ( temp[i] == static_cast<OffsetValueType>(this->GetSize(i)) )
329 template<
class TImage,
class TBoundaryCondition>
334 this->SetLocation( m_BeginIndex );
337 template<
class TImage,
class TBoundaryCondition>
342 this->SetLocation( m_EndIndex );
345 template<
class TImage,
class TBoundaryCondition>
354 this->SetRadius(radius);
355 this->SetBeginIndex(region.GetIndex());
356 this->SetLocation(region.GetIndex());
357 this->SetBound(region.GetSize());
360 m_Begin = ptr->GetBufferPointer() + ptr->ComputeOffset(regionIndex);
362 m_End = ptr->GetBufferPointer() + ptr->ComputeOffset( m_EndIndex );
370 long overlapLow, overlapHigh;
372 m_NeedToUseBoundaryCondition =
false;
373 for (
unsigned long i = 0; i < Dimension; ++i)
375 overlapLow =
static_cast<long>((rStart[i] - radius[i]) - bStart[i]);
376 overlapHigh=
static_cast<long>((bStart[i] + bSize[i]) -
377 (rStart[i] + rSize[i] + radius[i]));
381 m_NeedToUseBoundaryCondition =
true;
387 m_NeedToUseBoundaryCondition =
true;
392 m_IsInBoundsValid =
false;
393 m_IsInBounds =
false;
396 template<
class TImage,
class TBoundaryCondition>
401 Superclass::operator=(orig);
419 for (
unsigned int i = 0; i < Dimension; ++i)
429 static_cast<ImageBoundaryConditionConstPointerType>(
432 this->ResetBoundaryCondition();
440 template<
class TImage,
class TBoundaryCondition>
447 const Iterator _end = Superclass::End();
451 m_IsInBoundsValid =
false;
454 for (it = Superclass::Begin(); it < _end; ++it)
460 for (i=0; i<Dimension; ++i)
463 if ( m_Loop[i] == m_Bound[i] )
465 m_Loop[i] = m_BeginIndex[i];
466 for (it = Superclass::Begin(); it < _end; ++it)
468 (*it) += m_WrapOffset[i];
476 template<
class TImage,
class TBoundaryCondition>
483 const Iterator _end = Superclass::End();
487 m_IsInBoundsValid =
false;
490 for (it = Superclass::Begin(); it < _end; ++it)
496 for (i=0; i<Dimension; ++i)
498 if (m_Loop[i] == m_BeginIndex[i])
500 m_Loop[i]= m_Bound[i] - 1;
501 for (it = Superclass::Begin(); it < _end; ++it)
503 (*it) -= m_WrapOffset[i];
515 template<
class TImage,
class TBoundaryCondition>
522 os <<
"ConstNeighborhoodIterator {this= " <<
this;
523 os <<
", m_Region = { Start = {";
524 for (i=0; i < Dimension; ++i) os << m_Region.GetIndex()[i] <<
" ";
525 os <<
"}, Size = { ";
526 for (i=0; i < Dimension; ++i) os << m_Region.GetSize()[i] <<
" ";
528 os <<
", m_BeginIndex = { ";
529 for (i=0; i < Dimension; ++i) os << m_BeginIndex[i] <<
" ";
530 os <<
"} , m_EndIndex = { ";
531 for (i=0; i < Dimension; ++i) os << m_EndIndex[i] <<
" ";
532 os <<
"} , m_Loop = { ";
533 for (i=0; i < Dimension; ++i) os << m_Loop[i] <<
" ";
534 os <<
"}, m_Bound = { ";
535 for (i=0; i < Dimension; ++i) os << m_Bound[i] <<
" ";
536 os <<
"}, m_IsInBounds = {" << m_IsInBounds;
537 os <<
"}, m_IsInBoundsValid = {" << m_IsInBoundsValid;
538 os <<
"}, m_WrapOffset = { ";
539 for (i=0; i < Dimension; ++i) os << m_WrapOffset[i] <<
" ";
540 os <<
", m_Begin = " << m_Begin;
541 os <<
", m_End = " << m_End;
542 os <<
"}" << std::endl;
544 os << indent <<
", m_InnerBoundsLow = { ";
545 for (i = 0; i<Dimension; i++) os << m_InnerBoundsLow[i] <<
" ";
546 os <<
"}, m_InnerBoundsHigh = { ";
547 for (i = 0; i<Dimension; i++) os << m_InnerBoundsHigh[i] <<
" ";
548 os <<
"} }" << std::endl;
549 Superclass::PrintSelf(os, indent.GetNextIndent());
552 template<
class TImage,
class TBoundaryCondition>
556 SizeType radius = this->GetRadius();
564 for (
unsigned int i=0; i<Dimension; ++i)
566 m_Bound[i] = m_BeginIndex[i] +
static_cast<IndexValueType>(size[i]);
568 + ( imageBRSize[i]) - static_cast<SizeValueType>(radius[i]) );
572 - ( m_Bound[i] - m_BeginIndex[i] )) * offset[i];
574 m_WrapOffset[Dimension-1] = 0;
579 template<
class TImage,
class TBoundaryCondition>
583 const Iterator _end = Superclass::End();
586 const SizeType size = this->GetSize();
588 const SizeType radius = this->GetRadius();
593 for (i=0; i<Dimension; ++i) loop[i]=0;
596 Iit = ptr->GetBufferPointer() + ptr->ComputeOffset(pos);
598 for (i = 0; i<Dimension; ++i)
600 Iit -= radius[i] * OffsetTable[i];
604 for (Nit = Superclass::Begin(); Nit != _end; ++Nit)
608 for (i = 0; i <Dimension; ++i)
611 if ( loop[i] == size[i] )
613 if (i==Dimension-1)
break;
614 Iit += OffsetTable[i+1] - OffsetTable[i] *
static_cast<long>(size[i]);
622 template<
class TImage,
class TBoundaryCondition>
631 const OffsetValueType* stride = this->GetImagePointer()->GetOffsetTable();
635 m_IsInBoundsValid =
false;
638 accumulator += idx[0];
645 for (i = 1; i< Dimension; ++i)
647 accumulator += idx[i] * stride[i];
651 for (it = this->Begin(); it < _end; ++it)
653 (*it) += accumulator;
662 template<
class TImage,
class TBoundaryCondition>
671 const OffsetValueType* stride = this->GetImagePointer()->GetOffsetTable();
675 m_IsInBoundsValid =
false;
678 accumulator += idx[0];
685 for (i = 1; i< Dimension; ++i)
687 accumulator += idx[i] * stride[i];
691 for (it = this->Begin(); it < _end; ++it)
693 (*it) -= accumulator;