Orfeo Toolbox  3.16
itkSpatialObjectToImageFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: itkSpatialObjectToImageFilter.txx,v $
5 Language: C++
6 Date: $Date: 2009-04-01 22:14:59 $
7 Version: $Revision: 1.23 $
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 __itkSpatialObjectToImageFilter_txx
18 #define __itkSpatialObjectToImageFilter_txx
19 
22 
23 namespace itk
24 {
25 
27 template <class TInputSpatialObject, class TOutputImage>
30 {
31  this->SetNumberOfRequiredInputs( 1 );
32  m_ChildrenDepth = 999999;
33  m_Size.Fill( 0 );
34  m_Direction.SetIdentity();
35 
36  for (unsigned int i = 0; i < OutputImageDimension; i++)
37  {
38  m_Spacing[i] = 1.0;
39  m_Origin[i] = 0.;
40  }
41 
42  m_InsideValue = 0;
43  m_OutsideValue = 0;
44  m_UseObjectValue = false;
45 }
46 
48 template <class TInputSpatialObject, class TOutputImage>
51 {
52 }
53 
54 
56 template <class TInputSpatialObject, class TOutputImage>
57 void
60 {
61  // Process object is not const-correct so the const_cast is required here
63  const_cast< InputSpatialObjectType * >( input ) );
64 }
65 
66 
68 template <class TInputSpatialObject, class TOutputImage>
69 void
71 ::SetInput( unsigned int index, const TInputSpatialObject * object )
72 {
73  // Process object is not const-correct so the const_cast is required here
74  this->ProcessObject::SetNthInput(index,
75  const_cast< TInputSpatialObject *>( object ) );
76 }
77 
79 template <class TInputSpatialObject, class TOutputImage>
82 ::GetInput(void)
83 {
84  if (this->GetNumberOfInputs() < 1)
85  {
86  return 0;
87  }
88 
89  return static_cast<const TInputSpatialObject * >
90  (this->ProcessObject::GetInput(0) );
91 }
92 
94 template <class TInputSpatialObject, class TOutputImage>
97 ::GetInput(unsigned int idx)
98 {
99  return static_cast< const TInputSpatialObject * >
100  (this->ProcessObject::GetInput(idx));
101 }
102 
103 
104 //----------------------------------------------------------------------------
105 template <class TInputSpatialObject, class TOutputImage>
106 void
108 ::SetSpacing(const SpacingType& spacing )
109 {
110  unsigned int i;
111  for (i=0; i<TOutputImage::ImageDimension; i++)
112  {
113  if ( (double)spacing[i] != m_Spacing[i] )
114  {
115  break;
116  }
117  }
118  if ( i < TOutputImage::ImageDimension )
119  {
120  for (i=0; i<TOutputImage::ImageDimension; i++)
121  {
122  m_Spacing[i] = spacing[i];
123  }
124  this->Modified();
125  }
126 }
127 
128 //----------------------------------------------------------------------------
129 template <class TInputSpatialObject, class TOutputImage>
130 void
132 ::SetSpacing(const double* spacing)
133 {
134  unsigned int i;
135  for (i=0; i<OutputImageDimension; i++)
136  {
137  if ( spacing[i] != m_Spacing[i] )
138  {
139  break;
140  }
141  }
142  if ( i < OutputImageDimension )
143  {
144  for (i=0; i<OutputImageDimension; i++)
145  {
146  m_Spacing[i] = spacing[i];
147  }
148  this->Modified();
149  }
150 }
151 
152 template <class TInputSpatialObject, class TOutputImage>
153 void
155 ::SetSpacing(const float* spacing)
156 {
157  unsigned int i;
158  for (i=0; i<OutputImageDimension; i++)
159  {
160  if ( (double)spacing[i] != m_Spacing[i] )
161  {
162  break;
163  }
164  }
165  if ( i < OutputImageDimension )
166  {
167  for (i=0; i<OutputImageDimension; i++)
168  {
169  m_Spacing[i] = spacing[i];
170  }
171  this->Modified();
172  }
173 }
174 
175 template <class TInputSpatialObject, class TOutputImage>
176 const double *
179 {
180  return m_Spacing;
181 }
182 
183 
184 //----------------------------------------------------------------------------
185 template <class TInputSpatialObject, class TOutputImage>
186 void
188 ::SetOrigin(const PointType& origin)
189 {
190  unsigned int i;
191  for (i=0; i<OutputImageDimension; i++)
192  {
193  if ( (double)origin[i] != m_Origin[i] )
194  {
195  break;
196  }
197  }
198  if ( i < OutputImageDimension )
199  {
200  for (i=0; i<OutputImageDimension; i++)
201  {
202  m_Origin[i] = origin[i];
203  }
204  this->Modified();
205  }
206 }
207 
208 //----------------------------------------------------------------------------
209 template <class TInputSpatialObject, class TOutputImage>
210 void
212 ::SetOrigin(const double* origin)
213 {
214  unsigned int i;
215  for (i=0; i<OutputImageDimension; i++)
216  {
217  if ( origin[i] != m_Origin[i] )
218  {
219  break;
220  }
221  }
222  if ( i < OutputImageDimension )
223  {
224  for (i=0; i<OutputImageDimension; i++)
225  {
226  m_Origin[i] = origin[i];
227  }
228  this->Modified();
229  }
230 }
231 
232 template <class TInputSpatialObject, class TOutputImage>
233 void
235 ::SetOrigin(const float* origin)
236 {
237  unsigned int i;
238  for (i=0; i<OutputImageDimension; i++)
239  {
240  if ( (double)origin[i] != m_Origin[i] )
241  {
242  break;
243  }
244  }
245  if ( i < OutputImageDimension )
246  {
247  for (i=0; i<OutputImageDimension; i++)
248  {
249  m_Origin[i] = origin[i];
250  }
251  this->Modified();
252  }
253 }
254 
255 template <class TInputSpatialObject, class TOutputImage>
256 const double *
258 ::GetOrigin() const
259 {
260  return m_Origin;
261 }
262 
263 //----------------------------------------------------------------------------
264 
265 template <class TInputSpatialObject, class TOutputImage>
266 void
269 {
270  m_Direction = dir;
271  this->Modified();
272 }
273 
274 
275 template <class TInputSpatialObject, class TOutputImage>
278 ::GetDirection( void ) const
279 {
280  return m_Direction;
281 }
282 
283 
284 //----------------------------------------------------------------------------
285 
287 template <class TInputSpatialObject, class TOutputImage>
288 void
291 {
292  unsigned int i;
293  itkDebugMacro(<< "SpatialObjectToImageFilter::Update() called");
294 
295  // Get the input and output pointers
296  const InputSpatialObjectType * InputObject = this->GetInput();
297  OutputImagePointer OutputImage = this->GetOutput();
298 
299  // Generate the image
300  SizeType size;
301 
302  InputObject->ComputeBoundingBox();
303  for(i=0;i<ObjectDimension;i++)
304  {
305  size[i] = (long unsigned int)(InputObject->GetBoundingBox()->GetMaximum()[i]
306  - InputObject->GetBoundingBox()->GetMinimum()[i]);
307  }
308 
309  typename OutputImageType::IndexType index;
310  index.Fill(0);
311  typename OutputImageType::RegionType region;
312 
313  // If the size of the output has been explicitly specified, the filter
314  // will set the output size to the explicit size, otherwise the size from the spatial
315  // object's bounding box will be used as default.
316 
317  bool specified = false;
318  for (i = 0; i < OutputImageDimension; i++)
319  {
320  if (m_Size[i] != 0)
321  {
322  specified = true;
323  break;
324  }
325  }
326 
327  if (specified)
328  {
329  region.SetSize( m_Size );
330  }
331  else
332  {
333  region.SetSize( size );
334  }
335  region.SetIndex( index );
336 
337  OutputImage->SetLargestPossibleRegion( region); //
338  OutputImage->SetBufferedRegion( region ); // set the region
339  OutputImage->SetRequestedRegion( region ); //
340  // If the spacing has been explicitly specified, the filter
341  // will set the output spacing to that explicit spacing, otherwise the spacing from
342  // the spatial object is used as default.
343 
344  specified = false;
345  for (i = 0; i < OutputImageDimension; i++)
346  {
347  if (m_Spacing[i] != 0)
348  {
349  specified = true;
350  break;
351  }
352  }
353 
354  if (specified)
355  {
356  OutputImage->SetSpacing(this->m_Spacing); // set spacing
357  }
358  else
359  {
360  OutputImage->SetSpacing(InputObject->GetIndexToObjectTransform()->GetScaleComponent()); // set spacing
361  }
362  OutputImage->SetOrigin( m_Origin ); // and origin
363  OutputImage->SetDirection( m_Direction );
364  OutputImage->Allocate(); // allocate the image
365 
367 
368  myIteratorType it(OutputImage, region);
369 
372 
373  while(!it.IsAtEnd())
374  {
375  // ValueAt requires the point to be in physical coordinate i.e
376  OutputImage->TransformIndexToPhysicalPoint(it.GetIndex(), imagePoint);
377  for(i=0; i<ObjectDimension; i++)
378  {
379  objectPoint[i] = imagePoint[i];
380  }
381 
382  double val = 0;
383 
384  bool evaluable = InputObject->ValueAt(objectPoint, val, m_ChildrenDepth);
385  if( m_InsideValue != 0 || m_OutsideValue != 0 )
386  {
387  if(evaluable)
388  {
389  if(m_UseObjectValue)
390  {
391  it.Set(static_cast<ValueType>(val));
392  }
393  else
394  {
395  it.Set(m_InsideValue);
396  }
397  }
398  else
399  {
400  it.Set(m_OutsideValue);
401  }
402  }
403  else
404  {
405  it.Set(static_cast<ValueType>(val));
406  }
407  ++it;
408  }
409 
410  itkDebugMacro(<< "SpatialObjectToImageFilter::Update() finished");
411 
412 } // end update function
413 
414 
415 template<class TInputSpatialObject, class TOutputImage>
416 void
418 ::PrintSelf(std::ostream& os, Indent indent) const
419 {
420  Superclass::PrintSelf(os, indent);
421  os << indent << "Size : " << m_Size << std::endl;
422  os << indent << "Children depth : " << m_ChildrenDepth << std::endl;
423  os << indent << "Inside Value : " << m_InsideValue << std::endl;
424  os << indent << "Outside Value : " << m_OutsideValue << std::endl;
425  if(m_UseObjectValue)
426  {
427  os << indent << "Using Object Value : ON" << std::endl;
428  }
429  else
430  {
431  os << indent << "Using Object Value : OFF" << std::endl;
432  }
433 }
434 
435 } // end namespace itk
436 
437 #endif

Generated at Sun May 19 2013 00:08:46 for Orfeo Toolbox with doxygen 1.8.3.1