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