Orfeo Toolbox  3.16
itkImageMaskSpatialObject.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkImageMaskSpatialObject.txx,v $
5  Language: C++
6  Date: $Date: 2009-11-03 12:20:44 $
7  Version: $Revision: 1.21 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkImageMaskSpatialObject_txx
18 #define __itkImageMaskSpatialObject_txx
19 
20 
23 
24 namespace itk
25 {
26 
28 template< unsigned int TDimension>
31 {
32  this->SetTypeName("ImageMaskSpatialObject");
33  this->ComputeBoundingBox();
34 }
35 
37 template< unsigned int TDimension>
40 {
41 }
42 
43 
47 template< unsigned int TDimension >
48 bool
50 ::IsInside( const PointType & point) const
51 {
52  if( !this->GetBounds()->IsInside(point) )
53  {
54  return false;
55  }
56 
57  if( !this->SetInternalInverseTransformToWorldToIndexTransform() )
58  {
59  return false;
60  }
61 
62  PointType p = this->GetInternalInverseTransform()->TransformPoint(point);
63 
64  IndexType index;
65  for(unsigned int i=0; i<TDimension; i++)
66  {
67  index[i] = static_cast<int>( p[i] );
68  }
69 
70  const bool insideBuffer = this->GetImage()->GetBufferedRegion().IsInside( index );
71 
72  if( !insideBuffer )
73  {
74  return false;
75  }
76 
77  const bool insideMask = (this->GetImage()->GetPixel(index) != NumericTraits<PixelType>::Zero);
78 
79  return insideMask;
80 }
81 
82 
84 template< unsigned int TDimension>
85 bool
87 ::IsInside( const PointType & point, unsigned int depth, char * name ) const
88 {
89  if(name == NULL)
90  {
91  if(IsInside(point))
92  {
93  return true;
94  }
95  }
96  else if(strstr(typeid(Self).name(), name))
97  {
98  if(IsInside(point))
99  {
100  return true;
101  }
102  }
103  return SpatialObject<TDimension>::IsInside(point, depth, name);
104 }
105 
106 
107 template< unsigned int TDimension >
111 {
112  // We will use a slice iterator to iterate through slices orthogonal
113  // to each of the axis of the image to find the bounding box. Each
114  // slice iterator iterates from the outermost slice towards the image
115  // center till it finds a mask pixel. For a 3D image, there will be six
116  // slice iterators, iterating from the periphery inwards till the bounds
117  // along each axes are found. The slice iterators save time and avoid
118  // having to walk the whole image. Since we are using slice iterators,
119  // we will implement this only for 3D images.
120 
121  PixelType outsideValue = NumericTraits< PixelType >::Zero;
122  RegionType region;
123 
124  ImagePointer image = this->GetImage();
125 
126  typedef typename SizeType::SizeValueType SizeValueType;
127 
128  IndexType index;
129  SizeType size;
130 
131  if( ImageType::ImageDimension == 3)
132  {
133 
134  for( unsigned int axis = 0; axis < ImageType::ImageDimension; axis++ )
135  {
136  // Two slice iterators along each axis...
137  // Find the orthogonal planes for the slices
138  unsigned int i, j;
139  unsigned int direction[2];
140  for (i = 0, j = 0; i < 3; ++i )
141  {
142  if (i != axis )
143  {
144  direction[j] = i;
145  j++;
146  }
147  }
148 
149 
150  // Create the forward iterator to find lower bound
151  SliceIteratorType fit( image, image->GetRequestedRegion() );
152  fit.SetFirstDirection( direction[1] );
153  fit.SetSecondDirection( direction[0] );
154 
155  fit.GoToBegin();
156  while( !fit.IsAtEnd() )
157  {
158  while( !fit.IsAtEndOfSlice() )
159  {
160  while( !fit.IsAtEndOfLine() )
161  {
162  if( fit.Get() != outsideValue )
163  {
164  index[axis] = fit.GetIndex()[axis];
165  fit.GoToReverseBegin(); // skip to the end
166  break;
167  }
168  ++fit;
169  }
170  fit.NextLine();
171  }
172  fit.NextSlice();
173  }
174 
175 
176  // Create the reverse iterator to find upper bound
177  SliceIteratorType rit( image, image->GetRequestedRegion() );
178  rit.SetFirstDirection( direction[1] );
179  rit.SetSecondDirection( direction[0] );
180 
181  rit.GoToReverseBegin();
182  while( !rit.IsAtReverseEnd() )
183  {
184  while( !rit.IsAtReverseEndOfSlice() )
185  {
186  while( !rit.IsAtReverseEndOfLine() )
187  {
188  if( rit.Get() != outsideValue )
189  {
190  size[axis] = rit.GetIndex()[axis] - index[axis] + 1;
191  rit.GoToBegin(); //Skip to reverse end
192  break;
193  }
194  --rit;
195  }
196  rit.PreviousLine();
197  }
198  rit.PreviousSlice();
199  }
200  }
201 
202  region.SetIndex( index );
203  region.SetSize( size );
204  }
205  else
206  {
207  //itkExceptionMacro( << "ImageDimension must be 3!" );
209  IteratorType it( image, image->GetRequestedRegion() );
210  it.GoToBegin();
211 
212  for ( unsigned int i = 0; i < ImageType::ImageDimension; ++i )
213  {
214  index[ i ] = image->GetRequestedRegion().GetSize( i );
215  size[ i ] = image->GetRequestedRegion().GetIndex( i );
216  }
217 
218  while( !it.IsAtEnd() )
219  {
220  if ( it.Get() != outsideValue )
221  {
222  IndexType tmpIndex = it.GetIndex();
223  for ( unsigned int i = 0; i < ImageType::ImageDimension; ++i )
224  {
225 
226  if ( index[ i ] > tmpIndex[ i ] )
227  {
228  index[ i ] = tmpIndex[ i ];
229  }
230 
231  const SizeValueType tmpSize = static_cast< SizeValueType >( tmpIndex[ i ] );
232 
233  if ( size[ i ] < tmpSize )
234  {
235  size[ i ] = tmpSize;
236  }
237 
238  }
239  }
240  ++it;
241  }
242 
243  for ( unsigned int i = 0; i < ImageType::ImageDimension; ++i )
244  {
245  size[ i ] = size[ i ] - index[ i ] + 1;
246  }
247  region.SetIndex( index );
248  region.SetSize( size );
249  } // end else
250 
251  return region;
252 
253 }
254 
255 
257 template< unsigned int TDimension >
258 void
260 ::PrintSelf( std::ostream& os, Indent indent ) const
261 {
262  Superclass::PrintSelf(os,indent);
263 }
264 
265 
266 } // end namespace itk
267 
268 #endif //__ImageMaskSpatialObject_txx

Generated at Sat Feb 2 2013 23:43:22 for Orfeo Toolbox with doxygen 1.8.1.1