Orfeo Toolbox  4.0
otbVectorDataToLabelMapFilter.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 __otbVectorDataToLabelMapFilter_txx
19 #define __otbVectorDataToLabelMapFilter_txx
20 
22 #include "itkNumericTraits.h"
23 
25 
26 namespace otb
27 {
28 
29 template <class TVectorData, class TLabelMap>
32 {
34  this->SetNumberOfRequiredInputs(1);
35  m_Spacing.Fill(1.0);
36  m_Origin.Fill(0.0);
37  m_Direction.SetIdentity();
38  m_Size.Fill(0);
39  m_StartIndex.Fill(0);
41 }
42 
43 //----------------------------------------------------------------------------
44 template <class TVectorData, class TLabelMap>
45 void
47 ::SetSpacing(const SpacingType& spacing)
48 {
49  if (this->m_Spacing != spacing)
50  {
51  this->m_Spacing = spacing;
52  this->Modified();
53  }
54 }
55 
56 //----------------------------------------------------------------------------
57 template <class TVectorData, class TLabelMap>
58 void
60 ::SetSpacing(const double spacing[2])
61 {
62  SpacingType s(spacing);
63  this->SetSpacing(s);
64 }
65 
66 //----------------------------------------------------------------------------
67 template <class TVectorData, class TLabelMap>
68 void
70 ::SetSpacing(const float spacing[2])
71 {
72  itk::Vector<float, 2> sf(spacing);
73  SpacingType s;
74  s.CastFrom(sf);
75  this->SetSpacing(s);
76 }
77 
78 //----------------------------------------------------------------------------
79 template <class TVectorData, class TLabelMap>
80 void
82 ::SetOrigin(const double origin[2])
83 {
84  OriginType p(origin);
85  this->SetOrigin(p);
86 }
87 
88 //----------------------------------------------------------------------------
89 template <class TVectorData, class TLabelMap>
90 void
92 ::SetOrigin(const float origin[2])
93 {
94  itk::Point<float, 2> of(origin);
95  OriginType p;
96  p.CastFrom(of);
97  this->SetOrigin(p);
98 }
99 
104 template <class TVectorData, class TLabelMap>
107 {
108  // we can't call the superclass method here.
109 
110  // get pointers to the input and output
111  OutputLabelMapType * outputPtr = this->GetOutput();
112 
113  if (!outputPtr)
114  {
115  return;
116  }
117 
118  // Set the size of the output region
119  typename OutputLabelMapType::RegionType outputLargestPossibleRegion;
120  outputLargestPossibleRegion.SetSize(m_Size);
121  outputLargestPossibleRegion.SetIndex(m_StartIndex);
122  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
123 
124  otbGenericMsgDebugMacro(<< "LargestPossibleRegion " << outputPtr->GetLargestPossibleRegion());
125 
126  // Set spacing and origin
127  outputPtr->SetSpacing(m_Spacing);
128  outputPtr->SetOrigin(m_Origin);
129  outputPtr->SetDirection(m_Direction);
130 
131  return;
132 }
133 /*
134 template <class TVectorData, class TLabelMap >
135 void
136  VectorDataToLabelMapFilter<TVectorData, TLabelMap >
137 ::GenerateInputRequestedRegion()
138 {
139 
140  //call the superclass' implementation of this method
141  Superclass::GenerateInputRequestedRegion();
142 
143  // We need all the input.
144  InputVectorDataPointer input = const_cast<InputVectorDataType *>(this->GetInput());
145  if( !input )
146  {
147  return;
148  }
149  input->SetRequestedRegionToLargestPossibleRegion ();
150 }
151 
152 
153 template <class TVectorData, class TLabelMap >
154 void
155 VectorDataToLabelMapFilter<TVectorData, TLabelMap >
156 ::EnlargeOutputRequestedRegion(itk::DataObject *)
157 {
158  this->GetOutput()
159  ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
160 }
161 */
162 
163 template<class TVectorData, class TLabelMap>
164 void
167 {
168 // Process object is not const-correct so the const_cast is required here
170  const_cast<InputVectorDataType *>(input));
171 }
172 
173 template<class TVectorData, class TLabelMap>
174 void
176 ::SetInput(unsigned int idx, const InputVectorDataType *input)
177 {
178  // Process object is not const-correct so the const_cast is required here
180  const_cast<InputVectorDataType *>(input));
181 }
182 
183 template<class TVectorData, class TLabelMap>
187 {
188  if (this->GetNumberOfInputs() < 1)
189  {
190  return 0;
191  }
192 
193  return static_cast<const TVectorData *>
194  (this->itk::ProcessObject::GetInput(0));
195 }
196 
197 template<class TVectorData, class TLabelMap>
200 ::GetInput(unsigned int idx)
201 {
202  return static_cast<const TVectorData *>
203  (this->itk::ProcessObject::GetInput(idx));
204 }
205 
206 template <class TVectorData, class TLabelMap>
207 void
210 {
211  // Allocate the output
212  this->AllocateOutputs();
213 
214  OutputLabelMapType * output = this->GetOutput();
215 
216  //For each input
217  for (unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
218  {
219  if (this->GetInput(idx))
220  {
221 
222  InputVectorDataConstPointer input = this->GetInput(idx);
223  InternalTreeNodeType * inputRoot = const_cast<InternalTreeNodeType *>(input->GetDataTree()->GetRoot());
224  //Use our own value for the background
225  output->SetBackgroundValue(itk::NumericTraits<OutputLabelMapPixelType>::max());
226  //Set the value of the first label
228 // otbGenericMsgDebugMacro(<<"input " << idx);
229 
230  //The projection information
231  output->SetMetaDataDictionary(input->GetMetaDataDictionary());
232  ProcessNode(inputRoot);
233 
234  }
235  }
236 }
237 
238 template<class TVectorData, class TLabelMap>
239 void
242 {
243 
244  // Get the children list from the input node
245  ChildrenListType children = source->GetChildrenList();
246 
247  // For each child
248  for (typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
249  {
250  // Copy input DataNode info
251  DataNodePointerType dataNode = (*it)->Get();
252  otbGenericMsgDebugMacro(<< "Type of node " << dataNode->GetNodeType() << " id" << dataNode->GetNodeId());
253  switch (dataNode->GetNodeType())
254  {
255  case otb::ROOT:
256  {
257  ProcessNode((*it));
258  break;
259  }
260  case otb::DOCUMENT:
261  {
262  ProcessNode((*it));
263  break;
264  }
265  case otb::FOLDER:
266  {
267  ProcessNode((*it));
268  break;
269  }
270  case FEATURE_POINT:
271  {
272  otbGenericMsgDebugMacro(<< "Insert Point from vectorData");
273  IndexType index;
274  this->GetOutput()->TransformPhysicalPointToIndex(dataNode->GetPoint(), index);
275 
276  this->GetOutput()->SetPixel(index, m_lab);
277  m_lab += 10;
278  break;
279  }
280  case otb::FEATURE_LINE:
281  {
282  //TODO Bresenham
283  itkExceptionMacro(
284  << "This type (FEATURE_LINE) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
285  break;
286  }
287  case FEATURE_POLYGON:
288  {
289 
291  CorrectFunctorType correct;
292  PolygonPointerType correctPolygonExtRing = correct(dataNode->GetPolygonExteriorRing());
293 
294  typedef typename DataNodeType::PolygonType PolygonType;
295  typedef typename PolygonType::RegionType RegionType;
296  typedef typename PolygonType::VertexType VertexType;
297  typedef typename IndexType::IndexValueType IndexValueType;
298  typedef typename VertexType::ValueType VertexValueType;
299  RegionType polygonExtRingBoundReg = correctPolygonExtRing->GetBoundingRegion();
300 
301  VertexType vertex;
302  otbMsgDevMacro( "Polygon bounding region " << polygonExtRingBoundReg);
303  otbMsgDevMacro( "output origin " << this->GetOutput()->GetOrigin());
304  otbMsgDevMacro( "spacing " << this->GetOutput()->GetSpacing());
305  // For each position in the bounding region of the polygon
306 
307  for (double i = polygonExtRingBoundReg.GetOrigin(0);
308  i < polygonExtRingBoundReg.GetOrigin(0) + polygonExtRingBoundReg.GetSize(0);
309  i += this->GetOutput()->GetSpacing()[0])
310  {
311  vertex[0] = static_cast<VertexValueType>(i);
312  for (double j = polygonExtRingBoundReg.GetOrigin(1);
313  j < polygonExtRingBoundReg.GetOrigin(1) + polygonExtRingBoundReg.GetSize(1);
314  j += this->GetOutput()->GetSpacing()[1])
315  {
316  vertex[1] = static_cast<VertexValueType>(j);
317 
318  if (correctPolygonExtRing->IsInside(vertex) || correctPolygonExtRing->IsOnEdge (vertex))
319  {
320  IndexType index;
321  index[0] = static_cast<IndexValueType>(vertex[0] - polygonExtRingBoundReg.GetOrigin(0));
322  index[1] = static_cast<IndexValueType>(vertex[1] - polygonExtRingBoundReg.GetOrigin(1));
323 // index[0] += this->GetOutput()->GetOrigin()[0];
324 // index[1] += this->GetOutput()->GetOrigin()[1];
325 // std::cout << "index " << index << std::endl;
326  if (this->GetOutput()->HasLabel(m_lab))
327  {
328  if (!this->GetOutput()->GetLabelObject(m_lab)->HasIndex(index))
329  { //Add a pixel to the current labelObject
330  this->GetOutput()->SetPixel(index, m_lab);
331  }
332  }
333  else
334  {
335  //Add a pixel to the current labelObject
336  this->GetOutput()->SetPixel(index, m_lab);
337  }
338  }
339  }
340  }
341  //Modify the label for the next layer
342  m_lab += 10;
343  break;
344  }
345  case FEATURE_MULTIPOINT:
346  {
347  itkExceptionMacro(
348  <<
349  "This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
350  break;
351  }
352  case FEATURE_MULTILINE:
353  {
354  itkExceptionMacro(
355  << "This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
356  break;
357  }
359  {
360  itkExceptionMacro(
361  <<
362  "This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
363  break;
364  }
365  case FEATURE_COLLECTION:
366  {
367  itkExceptionMacro(
368  <<
369  "This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
370  break;
371  }
372  }
373  }
374 }
375 
376 template <class TVectorData, class TLabelMap>
377 void
379 ::PrintSelf(std::ostream& os, itk::Indent indent) const
380 {
381  Superclass::PrintSelf(os, indent);
382  os << indent << "BackgroundValue: " << static_cast<typename itk::NumericTraits<OutputLabelMapPixelType>::PrintType>(
383  m_BackgroundValue) << std::endl;
384 }
385 
386 } // end namespace otb
387 
388 #endif

Generated at Sat Mar 8 2014 16:24:40 for Orfeo Toolbox with doxygen 1.8.3.1