Orfeo Toolbox  3.16
otbSpatialObjectToImageDrawingFilter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbSpatialObjectToImageDrawingFilter_txx
19 #define __otbSpatialObjectToImageDrawingFilter_txx
20 
23 
24 namespace otb
25 {
26 
28 template <class TInputSpatialObject, class TOutputImage>
31 {
32  this->SetNumberOfRequiredInputs(1);
33  m_ChildrenDepth = 1;
34  m_Size.Fill(0);
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 
55 template <class TInputSpatialObject, class TOutputImage>
56 void
59 {
60 
61  // Process object is not const-correct so the const_cast is required here
63  const_cast<InputSpatialObjectType *>(input));
64 }
65 
67 template <class TInputSpatialObject, class TOutputImage>
68 void
70 ::SetInput(unsigned int index, const TInputSpatialObject * object)
71 {
72  // Process object is not const-correct so the const_cast is required here
74  const_cast<TInputSpatialObject *>(object));
75 }
76 
78 template <class TInputSpatialObject, class TOutputImage>
82 {
83  if (this->GetNumberOfInputs() < 1)
84  {
85  return 0;
86  }
87 
88  return static_cast<const TInputSpatialObject *>
90 }
91 
93 template <class TInputSpatialObject, class TOutputImage>
96 ::GetInput(unsigned int idx)
97 {
98  return static_cast<const TInputSpatialObject *>
99  (this->itk::ProcessObject::GetInput(idx));
100 }
101 
102 //----------------------------------------------------------------------------
103 template <class TInputSpatialObject, class TOutputImage>
104 void
106 ::SetSpacing(const SpacingType& spacing)
107 {
108  unsigned int i;
109  for (i = 0; i < TOutputImage::ImageDimension; ++i)
110  {
111  if ((double) spacing[i] != m_Spacing[i])
112  {
113  break;
114  }
115  }
116  if (i < TOutputImage::ImageDimension)
117  {
118  for (i = 0; i < TOutputImage::ImageDimension; ++i)
119  {
120  m_Spacing[i] = spacing[i];
121  }
122  this->Modified();
123  }
124 }
125 
126 //----------------------------------------------------------------------------
127 template <class TInputSpatialObject, class TOutputImage>
128 void
130 ::SetSpacing(const double* spacing)
131 {
132  unsigned int i;
133  for (i = 0; i < OutputImageDimension; ++i)
134  {
135  if (spacing[i] != m_Spacing[i])
136  {
137  break;
138  }
139  }
140  if (i < OutputImageDimension)
141  {
142  for (i = 0; i < OutputImageDimension; ++i)
143  {
144  m_Spacing[i] = spacing[i];
145  }
146  }
147 }
148 
149 template <class TInputSpatialObject, class TOutputImage>
150 void
152 ::SetSpacing(const float* spacing)
153 {
154  unsigned int i;
155  for (i = 0; i < OutputImageDimension; ++i)
156  {
157  if ((double) spacing[i] != m_Spacing[i])
158  {
159  break;
160  }
161  }
162  if (i < OutputImageDimension)
163  {
164  for (i = 0; i < OutputImageDimension; ++i)
165  {
166  m_Spacing[i] = spacing[i];
167  }
168  }
169 }
170 
171 template <class TInputSpatialObject, class TOutputImage>
172 const double *
175 {
176  return m_Spacing;
177 }
178 
179 //----------------------------------------------------------------------------
180 template <class TInputSpatialObject, class TOutputImage>
181 void
183 ::SetOrigin(const PointType& origin)
184 {
185  unsigned int i;
186  for (i = 0; i < TOutputImage::ImageDimension; ++i)
187  {
188  if ((double) origin[i] != m_Origin[i])
189  {
190  break;
191  }
192  }
193  if (i < TOutputImage::ImageDimension)
194  {
195  for (i = 0; i < TOutputImage::ImageDimension; ++i)
196  {
197  m_Origin[i] = origin[i];
198  }
199  this->Modified();
200  }
201 }
202 
203 //----------------------------------------------------------------------------
204 template <class TInputSpatialObject, class TOutputImage>
205 void
207 ::SetOrigin(const double* origin)
208 {
209  unsigned int i;
210  for (i = 0; i < OutputImageDimension; ++i)
211  {
212  if (origin[i] != m_Origin[i])
213  {
214  break;
215  }
216  }
217  if (i < OutputImageDimension)
218  {
219  for (i = 0; i < OutputImageDimension; ++i)
220  {
221  m_Origin[i] = origin[i];
222  }
223  this->Modified();
224  }
225 }
226 
227 template <class TInputSpatialObject, class TOutputImage>
228 void
230 ::SetOrigin(const float* origin)
231 {
232  unsigned int i;
233  for (i = 0; i < OutputImageDimension; ++i)
234  {
235  if ((double) origin[i] != m_Origin[i])
236  {
237  break;
238  }
239  }
240  if (i < OutputImageDimension)
241  {
242  for (i = 0; i < OutputImageDimension; ++i)
243  {
244  m_Origin[i] = origin[i];
245  }
246  this->Modified();
247  }
248 }
249 
250 template <class TInputSpatialObject, class TOutputImage>
251 const double *
253 ::GetOrigin() const
254 {
255  return m_Origin;
256 }
257 
258 template <class TInputSpatialObject, class TOutputImage>
259 void
262 {
263  const InputSpatialObjectType * InputObject = this->GetInput();
264  OutputImagePointer OutputImage = this->GetOutput();
265 
266  unsigned int i;
267  // Generate the image
268  SizeType size;
269  double origine[ObjectDimension];
270  InputObject->ComputeBoundingBox();
271  bool originspecified = false;
272  if (!strcmp(InputObject->GetNameOfClass(), "GroupSpatialObject"))
273  {
274  ChildrenType children;
275  children = InputObject->GetChildren(0);
276  IteratorType iter = children->begin();
277  IteratorType end = children->end();
278  double minimum[ObjectDimension];
279 
280  (*iter)->ComputeBoundingBox();
281  for (i = 0; i < ObjectDimension; ++i)
282  {
283  minimum[i] = (*iter)->GetBoundingBox()->GetMinimum()[i];
284  }
285 
286  while (iter != end)
287  {
288  (*iter)->ComputeBoundingBox();
289  for (i = 0; i < ObjectDimension; ++i)
290  {
291  if ((*iter)->GetBoundingBox()->GetMinimum()[i] < minimum[i])
292  {
293  minimum[i] = (*iter)->GetBoundingBox()->GetMinimum()[i];
294  }
295  }
296  ++iter;
297  }
298 
299  for (i = 0; i < ObjectDimension; ++i)
300  {
301  size[i] = (long unsigned int) (InputObject->GetBoundingBox()->GetMaximum()[i] - minimum[i]) + 1;
302  origine[i] = (long int) minimum[i];
303  originspecified = true;
304  }
305 
306  itkDebugMacro(
307  << "minx= " << minimum[0] << ", miny= " << minimum[0] << ", maxx= " <<
308  InputObject->GetBoundingBox()->GetMaximum()[0] << ", maxy= " << InputObject->GetBoundingBox()->GetMaximum()[1]);
309  }
310  else
311  {
312 
313  for (i = 0; i < ObjectDimension; ++i)
314  {
315  size[i] = (long int) (InputObject->GetBoundingBox()->GetMaximum()[i]
316  - InputObject->GetBoundingBox()->GetMinimum()[i]);
317  }
318  }
319 
320  typename OutputImageType::IndexType index;
321  index.Fill(0);
322  typename OutputImageType::RegionType region;
323 
324  // If the size of the output has been explicitly specified, the filter
325  // will set the output size to the explicit size, otherwise the size from the spatial
326  // object's bounding box will be used as default.
327 
328  bool specified = false;
329  for (i = 0; i < OutputImageDimension; ++i)
330  {
331  if (m_Size[i] != 0)
332  {
333  specified = true;
334  break;
335  }
336  }
337 
338  if (specified)
339  {
340  region.SetSize(m_Size);
341  }
342  else
343  {
344  region.SetSize (size);
345  m_Size = size;
346  }
347  region.SetIndex(index);
348 
349  OutputImage->SetLargestPossibleRegion(region);
350  specified = false;
351  for (i = 0; i < OutputImageDimension; ++i)
352  {
353  if (m_Spacing[i] != 0)
354  {
355  specified = true;
356  break;
357  }
358  }
359 
360  if (specified)
361  {
362  OutputImage->SetSpacing(this->m_Spacing); // set spacing
363  }
364  else
365  {
366  OutputImage->SetSpacing(InputObject->GetIndexToObjectTransform()->GetScaleComponent()); // set spacing
367  m_Spacing[0] = InputObject->GetIndexToObjectTransform()->GetScaleComponent()[0];
368  m_Spacing[1] = InputObject->GetIndexToObjectTransform()->GetScaleComponent()[1];
369  }
370 
371  if (originspecified)
372  {
373  OutputImage->SetOrigin(origine); // and origin
374  m_Origin[0] = OutputImage->GetOrigin()[0];
375  m_Origin[1] = OutputImage->GetOrigin()[1];
376 
377  }
378  else OutputImage->SetOrigin(m_Origin);
379 }
380 
381 //----------------------------------------------------------------------------
382 
384 template <class TInputSpatialObject, class TOutputImage>
385 void
388 {
389  itkDebugMacro(<< "SpatialObjectToImageDrawingFilter::Update() called");
390 
391  // Get the input and output pointers
392  const InputSpatialObjectType * InputObject = this->GetInput();
393  OutputImagePointer OutputImage = this->GetOutput();
394 
395  typename OutputImageType::RegionType region = OutputImage->GetLargestPossibleRegion();
396  OutputImage->SetBufferedRegion(region); // set the region
397  OutputImage->SetRequestedRegion(region); //
398 
399  OutputImage->Allocate(); // allocate the image
400 
402 
403  myIteratorType it(OutputImage, region);
404 
406 
407  while (!it.IsAtEnd())
408  {
409 
410  // ValueAt requires the point to be in physical coordinate i.e
411  for (unsigned int i = 0; i < ObjectDimension; ++i)
412  {
413  point[i] = (int) (it.GetIndex()[i] * m_Spacing[i]) + m_Origin[i];
414  }
415  double val = 0;
416 
417  InputObject->ValueAt(point, val, 99999);
418  if (m_InsideValue != 0 || m_OutsideValue != 0)
419  {
420  if (val)
421  {
422  if (m_UseObjectValue)
423  {
424  it.Set(static_cast<ValueType>(val));
425  }
426  else
427  {
428  it.Set(m_InsideValue);
429  }
430  }
431  else
432  {
433  it.Set(m_OutsideValue);
434  }
435  }
436  else
437  {
438  it.Set(static_cast<ValueType>(val));
439  }
440  ++it;
441  }
442 
443  itkDebugMacro(<< "SpatialObjectToImageDrawingFilter::Update() finished");
444 
445 } // end update function
446 
447 template<class TInputSpatialObject, class TOutputImage>
448 void
450 ::PrintSelf(std::ostream& os, itk::Indent indent) const
451 {
452  Superclass::PrintSelf(os, indent);
453  os << indent << "Size : " << m_Size << std::endl;
454  os << indent << "Children depth : " << m_ChildrenDepth << std::endl;
455  os << indent << "Inside Value : " << m_InsideValue << std::endl;
456  os << indent << "Outside Value : " << m_OutsideValue << std::endl;
457  if (m_UseObjectValue)
458  {
459  os << indent << "Using Object Value : ON" << std::endl;
460  }
461  else
462  {
463  os << indent << "Using Object Value : OFF" << std::endl;
464  }
465 }
466 
467 } // end namespace otb
468 
469 #endif

Generated at Sun May 19 2013 00:50:16 for Orfeo Toolbox with doxygen 1.8.3.1