Orfeo Toolbox  4.0
otbVectorDataToLabelMapWithAttributesFilter.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 __otbVectorDataToLabelMapWithAttributesFilter_txx
19 #define __otbVectorDataToLabelMapWithAttributesFilter_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);
40  m_InitialLabel = itk::NumericTraits<LabelType>::Zero;
41 
42  m_AutomaticSizeComputation=true;
43  m_VectorDataProperties = VectorDataPropertiesType::New();
44 }
45 
46 //----------------------------------------------------------------------------
47 template <class TVectorData, class TLabelMap>
48 void
50 ::SetSpacing(const SpacingType& spacing)
51 {
52  if (this->m_Spacing != spacing)
53  {
54  this->m_Spacing = spacing;
55  this->Modified();
56  }
57 }
58 
59 //----------------------------------------------------------------------------
60 template <class TVectorData, class TLabelMap>
61 void
63 ::SetSpacing(const double spacing[2])
64 {
65  SpacingType s(spacing);
66  this->SetSpacing(s);
67 }
68 
69 //----------------------------------------------------------------------------
70 template <class TVectorData, class TLabelMap>
71 void
73 ::SetSpacing(const float spacing[2])
74 {
75  itk::Vector<float, 2> sf(spacing);
76  SpacingType s;
77  s.CastFrom(sf);
78  this->SetSpacing(s);
79 }
80 
81 //----------------------------------------------------------------------------
82 template <class TVectorData, class TLabelMap>
83 void
85 ::SetOrigin(const double origin[2])
86 {
87  OriginType p(origin);
88  this->SetOrigin(p);
89 }
90 
91 //----------------------------------------------------------------------------
92 template <class TVectorData, class TLabelMap>
93 void
95 ::SetOrigin(const float origin[2])
96 {
97  itk::Point<float, 2> of(origin);
98  OriginType p;
99  p.CastFrom(of);
100  this->SetOrigin(p);
101 }
102 
103 
108 template <class TVectorData, class TLabelMap>
111 {
112  // we can't call the superclass method here.
113 
114  // get pointers to the input and output
115  OutputLabelMapType * outputPtr = this->GetOutput();
116 
117  if (!outputPtr)
118  {
119  return;
120  }
121 
122  RegionType outputLargestPossibleRegion;
123  if(m_AutomaticSizeComputation==false)
124  {
125  // Set the size of the output region
126  outputLargestPossibleRegion.SetSize(m_Size);
127  outputLargestPossibleRegion.SetIndex(m_StartIndex);
128  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
129 
130  otbGenericMsgDebugMacro(<< "LargestPossibleRegion " << outputPtr->GetLargestPossibleRegion());
131 
132  // Set spacing and origin
133  outputPtr->SetSpacing(m_Spacing);
134  outputPtr->SetOrigin(m_Origin);
135  outputPtr->SetDirection(m_Direction);
136  }
137  else
138  {
139  typename PolygonType::RegionType region;
140  OriginType origin;
141  m_VectorDataProperties->SetVectorDataObject(this->GetInput());
142  m_VectorDataProperties->SetBoundingRegion(region);
143  //Compute the global bounding box of the vectordata
144  m_VectorDataProperties->ComputeBoundingRegion();
145  origin[0] = m_VectorDataProperties->GetBoundingRegion().GetImageRegion().GetIndex(0);
146  origin[1] = m_VectorDataProperties->GetBoundingRegion().GetImageRegion().GetIndex(1);
147 
148  // Set spacing and origin
149  outputLargestPossibleRegion.SetSize(m_VectorDataProperties->GetBoundingRegion().GetImageRegion().GetSize());
150  outputLargestPossibleRegion.SetIndex(m_StartIndex);
151 
152  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
153  outputPtr->SetSpacing(this->GetInput()->GetSpacing());
154  outputPtr->SetOrigin(origin);
155  outputPtr->SetDirection(m_Direction);
156  }
157  return;
158 }
159 /*
160 template <class TVectorData, class TLabelMap >
161 void
162  VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap >
163 ::GenerateInputRequestedRegion()
164 {
165 
166  //call the superclass' implementation of this method
167  Superclass::GenerateInputRequestedRegion();
168 
169  // We need all the input.
170  InputVectorDataPointer input = const_cast<InputVectorDataType *>(this->GetInput());
171  if( !input )
172  {
173  return;
174  }
175  input->SetRequestedRegionToLargestPossibleRegion ();
176 }
177 
178 
179 template <class TVectorData, class TLabelMap >
180 void
181 VectorDataToLabelMapWithAttributesFilter<TVectorData, TLabelMap >
182 ::EnlargeOutputRequestedRegion(itk::DataObject *)
183 {
184  this->GetOutput()
185  ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
186 }
187 */
188 
189 template<class TVectorData, class TLabelMap>
190 void
193 {
194 // Process object is not const-correct so the const_cast is required here
196  const_cast<InputVectorDataType *>(input));
197 }
198 
199 template<class TVectorData, class TLabelMap>
200 void
202 ::SetInput(unsigned int idx, const InputVectorDataType *input)
203 {
204  // Process object is not const-correct so the const_cast is required here
206  const_cast<InputVectorDataType *>(input));
207 }
208 
209 template<class TVectorData, class TLabelMap>
213 {
214  if (this->GetNumberOfInputs() < 1)
215  {
216  return 0;
217  }
218 
219  return static_cast<const TVectorData *>
220  (this->itk::ProcessObject::GetInput(0));
221 }
222 
223 template<class TVectorData, class TLabelMap>
226 ::GetInput(unsigned int idx)
227 {
228  return static_cast<const TVectorData *>
229  (this->itk::ProcessObject::GetInput(idx));
230 }
231 
232 template <class TVectorData, class TLabelMap>
233 void
236 {
237  // Allocate the output
238  this->AllocateOutputs();
239 
240  OutputLabelMapType * output = this->GetOutput();
241 
242  //For each input
243  for (unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
244  {
245  if (this->GetInput(idx))
246  {
247 
248  InputVectorDataConstPointer input = this->GetInput(idx);
249  InternalTreeNodeType * inputRoot = const_cast<InternalTreeNodeType *>(input->GetDataTree()->GetRoot());
250  //Use our own value for the background
251  output->SetBackgroundValue(m_BackgroundValue);
252  //Set the value of the first label
253  m_lab = m_InitialLabel;
254 // otbGenericMsgDebugMacro(<<"input " << idx);
255 
256  //The projection information
257  output->SetMetaDataDictionary(input->GetMetaDataDictionary());
258  ProcessNode(inputRoot);
259 
260  }
261  }
262 }
263 
264 template<class TVectorData, class TLabelMap>
265 void
268 {
269 
270  // Get the children list from the input node
271  ChildrenListType children = source->GetChildrenList();
272 
273  // For each child
274  for (typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
275  {
276  // Copy input DataNode info
277  DataNodePointerType dataNode = (*it)->Get();
278  otbGenericMsgDebugMacro(<< "Type of node " << dataNode->GetNodeType() << " id" << dataNode->GetNodeId());
279  switch (dataNode->GetNodeType())
280  {
281  case otb::ROOT:
282  {
283  ProcessNode((*it));
284  break;
285  }
286  case otb::DOCUMENT:
287  {
288  ProcessNode((*it));
289  break;
290  }
291  case otb::FOLDER:
292  {
293  ProcessNode((*it));
294  break;
295  }
296  case FEATURE_POINT:
297  {
298  otbGenericMsgDebugMacro(<< "Insert Point from vectorData");
299  IndexType index;
300  this->GetOutput()->TransformPhysicalPointToIndex(dataNode->GetPoint(), index);
301 
302  this->GetOutput()->SetPixel(index, m_lab);
303  m_lab += 1;
304  break;
305  }
306  case otb::FEATURE_LINE:
307  {
308  //TODO Bresenham
309  itkExceptionMacro(
310  << "This type (FEATURE_LINE) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
311  break;
312  }
313  case FEATURE_POLYGON:
314  {
315 
317  CorrectFunctorType correct;
318  PolygonPointerType correctPolygonExtRing = correct(dataNode->GetPolygonExteriorRing());
319 
320  typedef typename DataNodeType::PolygonType PolygonType;
321  typedef typename PolygonType::RegionType RegionType;
322  typedef typename PolygonType::VertexType VertexType;
323  typedef typename IndexType::IndexValueType IndexValueType;
324  typedef typename VertexType::ValueType VertexValueType;
325  RegionType polygonExtRingBoundReg = correctPolygonExtRing->GetBoundingRegion();
326 
327  VertexType vertex;
328 
329  // For each position in the bounding region of the polygon
330  for (double i = polygonExtRingBoundReg.GetOrigin(0);
331  i < polygonExtRingBoundReg.GetOrigin(0) + polygonExtRingBoundReg.GetSize(0);
332  i += this->GetOutput()->GetSpacing()[0])
333  {
334  vertex[0] = static_cast<VertexValueType>(i);
335  for (double j = polygonExtRingBoundReg.GetOrigin(1);
336  j < polygonExtRingBoundReg.GetOrigin(1) + polygonExtRingBoundReg.GetSize(1);
337  j += this->GetOutput()->GetSpacing()[1])
338  {
339  vertex[1] = static_cast<VertexValueType>(j);
340 
341  if (correctPolygonExtRing->IsInside(vertex) || correctPolygonExtRing->IsOnEdge (vertex))
342  {
343  IndexType index;
344 // index[0] = static_cast<IndexValueType>(vertex[0] - polygonExtRingBoundReg.GetOrigin(0));
345 // index[1] = static_cast<IndexValueType>(vertex[1] - polygonExtRingBoundReg.GetOrigin(1));
346  index[0] = static_cast<IndexValueType>(vertex[0]-this->GetOutput()->GetOrigin()[0]);
347  index[1] = static_cast<IndexValueType>(vertex[1]-this->GetOutput()->GetOrigin()[1]);
348 // index[0] += this->GetOutput()->GetOrigin()[0];
349 // index[1] += this->GetOutput()->GetOrigin()[1];
350 // std::cout << "index " << index << std::endl;
351  if (this->GetOutput()->HasLabel(m_lab))
352  {
353  if (!this->GetOutput()->GetLabelObject(m_lab)->HasIndex(index))
354  { //Add a pixel to the current labelObject
355  this->GetOutput()->SetPixel(index, m_lab);
356  }
357  }
358  else
359  {
360  //Add a pixel to the current labelObject
361  this->GetOutput()->SetPixel(index, m_lab);
362  }
363  //add attributes
364  AttributesValueType fieldValue;
365  for(unsigned int ii=0; ii<dataNode->GetFieldList().size(); ii++)
366  {
367  fieldValue = static_cast<AttributesValueType>( dataNode->GetFieldAsString(dataNode->GetFieldList()[ii]) );
368  this->GetOutput()->GetLabelObject(m_lab)->SetAttribute( dataNode->GetFieldList()[ii].c_str(), fieldValue );
369  }
370  }
371  }
372  }
373  //Modify the label for the next layer
374  m_lab += 1;
375  break;
376  }
377  case FEATURE_MULTIPOINT:
378  {
379  itkExceptionMacro(
380  <<
381  "This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
382  break;
383  }
384  case FEATURE_MULTILINE:
385  {
386  itkExceptionMacro(
387  << "This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
388  break;
389  }
391  {
392  itkExceptionMacro(
393  <<
394  "This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
395  break;
396  }
397  case FEATURE_COLLECTION:
398  {
399  itkExceptionMacro(
400  <<
401  "This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
402  break;
403  }
404  }
405  }
406 }
407 
408 template <class TVectorData, class TLabelMap>
409 void
411 ::PrintSelf(std::ostream& os, itk::Indent indent) const
412 {
413  Superclass::PrintSelf(os, indent);
414  os << indent << "BackgroundValue: " << static_cast<typename itk::NumericTraits<OutputLabelMapPixelType>::PrintType>(
415  m_BackgroundValue) << std::endl;
416 }
417 
418 } // end namespace otb
419 
420 #endif

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