OTB  6.1.0
Orfeo Toolbox
otbVectorDataToLabelMapFilter.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbVectorDataToLabelMapFilter_txx
22 #define otbVectorDataToLabelMapFilter_txx
23 
26 #include "itkNumericTraits.h"
27 
29 
30 namespace otb
31 {
32 
33 template <class TVectorData, class TLabelMap>
36 {
38  this->SetNumberOfRequiredInputs(1);
39  m_Spacing.Fill(1.0);
40  m_Origin.Fill(0.0);
41  m_Direction.SetIdentity();
42  m_Size.Fill(0);
43  m_StartIndex.Fill(0);
45 }
46 
47 //----------------------------------------------------------------------------
48 template <class TVectorData, class TLabelMap>
49 void
51 ::SetSpacing(const SpacingType& spacing)
52 {
53  if (this->m_Spacing != spacing)
54  {
55  this->m_Spacing = spacing;
56  this->Modified();
57  }
58 }
59 
60 //----------------------------------------------------------------------------
61 template <class TVectorData, class TLabelMap>
62 void
64 ::SetSpacing(const double spacing[2])
65 {
66  SpacingType s(spacing);
67  this->SetSpacing(s);
68 }
69 
70 //----------------------------------------------------------------------------
71 template <class TVectorData, class TLabelMap>
72 void
74 ::SetSpacing(const float spacing[2])
75 {
76  itk::Vector<float, 2> sf(spacing);
77  SpacingType s;
78  s.CastFrom(sf);
79  this->SetSpacing(s);
80 }
81 
82 //----------------------------------------------------------------------------
83 template <class TVectorData, class TLabelMap>
84 void
86 ::SetOrigin(const double origin[2])
87 {
88  OriginType p(origin);
89  this->SetOrigin(p);
90 }
91 
92 //----------------------------------------------------------------------------
93 template <class TVectorData, class TLabelMap>
94 void
96 ::SetOrigin(const float origin[2])
97 {
98  itk::Point<float, 2> of(origin);
99  OriginType p;
100  p.CastFrom(of);
101  this->SetOrigin(p);
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  // Set the size of the output region
123  typename OutputLabelMapType::RegionType outputLargestPossibleRegion;
124  outputLargestPossibleRegion.SetSize(m_Size);
125  outputLargestPossibleRegion.SetIndex(m_StartIndex);
126  outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
127 
128  otbGenericMsgDebugMacro(<< "LargestPossibleRegion " << outputPtr->GetLargestPossibleRegion());
129 
130  // Set spacing and origin
131  outputPtr->SetSpacing(m_Spacing);
132  outputPtr->SetOrigin(m_Origin);
133  outputPtr->SetDirection(m_Direction);
134 
135  return;
136 }
137 /*
138 template <class TVectorData, class TLabelMap >
139 void
140  VectorDataToLabelMapFilter<TVectorData, TLabelMap >
141 ::GenerateInputRequestedRegion()
142 {
143 
144  //call the superclass' implementation of this method
145  Superclass::GenerateInputRequestedRegion();
146 
147  // We need all the input.
148  InputVectorDataPointer input = const_cast<InputVectorDataType *>(this->GetInput());
149  if( !input )
150  {
151  return;
152  }
153  input->SetRequestedRegionToLargestPossibleRegion ();
154 }
155 
156 
157 template <class TVectorData, class TLabelMap >
158 void
159 VectorDataToLabelMapFilter<TVectorData, TLabelMap >
160 ::EnlargeOutputRequestedRegion(itk::DataObject *)
161 {
162  this->GetOutput()
163  ->SetRequestedRegion( this->GetOutput()->GetLargestPossibleRegion() );
164 }
165 */
166 
167 template<class TVectorData, class TLabelMap>
168 void
171 {
172 // Process object is not const-correct so the const_cast is required here
174  const_cast<InputVectorDataType *>(input));
175 }
176 
177 template<class TVectorData, class TLabelMap>
178 void
180 ::SetInput(unsigned int idx, const InputVectorDataType *input)
181 {
182  // Process object is not const-correct so the const_cast is required here
184  const_cast<InputVectorDataType *>(input));
185 }
186 
187 template<class TVectorData, class TLabelMap>
191 {
192  if (this->GetNumberOfInputs() < 1)
193  {
194  return 0;
195  }
196 
197  return static_cast<const TVectorData *>
198  (this->itk::ProcessObject::GetInput(0));
199 }
200 
201 template<class TVectorData, class TLabelMap>
204 ::GetInput(unsigned int idx)
205 {
206  return static_cast<const TVectorData *>
207  (this->itk::ProcessObject::GetInput(idx));
208 }
209 
210 template <class TVectorData, class TLabelMap>
211 void
214 {
215  // Allocate the output
216  this->AllocateOutputs();
217 
218  OutputLabelMapType * output = this->GetOutput();
219 
220  //For each input
221  for (unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
222  {
223  if (this->GetInput(idx))
224  {
225 
226  InputVectorDataConstPointer input = this->GetInput(idx);
227  InternalTreeNodeType * inputRoot = const_cast<InternalTreeNodeType *>(input->GetDataTree()->GetRoot());
228  //Use our own value for the background
229  output->SetBackgroundValue(itk::NumericTraits<OutputLabelMapPixelType>::max());
230  //Set the value of the first label
232 // otbGenericMsgDebugMacro(<<"input " << idx);
233 
234  //The projection information
235  output->SetMetaDataDictionary(input->GetMetaDataDictionary());
236  ProcessNode(inputRoot);
237 
238  }
239  }
240 }
241 
242 template<class TVectorData, class TLabelMap>
243 void
246 {
247 
248  // Get the children list from the input node
249  ChildrenListType children = source->GetChildrenList();
250 
251  // For each child
252  for (typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
253  {
254  // Copy input DataNode info
255  DataNodePointerType dataNode = (*it)->Get();
256  otbGenericMsgDebugMacro(<< "Type of node " << dataNode->GetNodeType() << " id" << dataNode->GetNodeId());
257  switch (dataNode->GetNodeType())
258  {
259  case otb::ROOT:
260  {
261  ProcessNode((*it));
262  break;
263  }
264  case otb::DOCUMENT:
265  {
266  ProcessNode((*it));
267  break;
268  }
269  case otb::FOLDER:
270  {
271  ProcessNode((*it));
272  break;
273  }
274  case FEATURE_POINT:
275  {
276  otbGenericMsgDebugMacro(<< "Insert Point from vectorData");
277  IndexType index;
278  this->GetOutput()->TransformPhysicalPointToIndex(dataNode->GetPoint(), index);
279 
280  this->GetOutput()->SetPixel(index, m_lab);
281  m_lab += 10;
282  break;
283  }
284  case otb::FEATURE_LINE:
285  {
286  //TODO Bresenham
287  itkExceptionMacro(
288  << "This type (FEATURE_LINE) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
289  break;
290  }
291  case FEATURE_POLYGON:
292  {
293 
295  CorrectFunctorType correct;
296  PolygonPointerType correctPolygonExtRing = correct(dataNode->GetPolygonExteriorRing());
298 
299  typedef typename DataNodeType::PolygonType PolygonType;
300  typedef typename PolygonType::RegionType RegionType;
301  typedef typename PolygonType::VertexType VertexType;
302  typedef typename IndexType::IndexValueType IndexValueType;
303  typedef typename VertexType::ValueType VertexValueType;
304  RegionType polygonExtRingBoundReg = correctPolygonExtRing->GetBoundingRegion();
305 
306  VertexType vertex;
307  otbMsgDevMacro( "Polygon bounding region " << polygonExtRingBoundReg);
308  otbMsgDevMacro( "output origin " << this->GetOutput()->GetOrigin());
309  otbMsgDevMacro( "spacing " << this->GetOutput()->GetSpacing());
310  // For each position in the bounding region of the polygon
311 
312  for (double i = polygonExtRingBoundReg.GetOrigin(0);
313  i < polygonExtRingBoundReg.GetOrigin(0) + polygonExtRingBoundReg.GetSize(0);
314  i += this->GetOutput()->GetSpacing()[0])
315  {
316  vertex[0] = static_cast<VertexValueType>(i);
317  for (double j = polygonExtRingBoundReg.GetOrigin(1);
318  j < polygonExtRingBoundReg.GetOrigin(1) + polygonExtRingBoundReg.GetSize(1);
319  j += this->GetOutput()->GetSpacing()[1])
320  {
321  vertex[1] = static_cast<VertexValueType>(j);
322 
323  if (correctPolygonExtRing->IsInside(vertex) || correctPolygonExtRing->IsOnEdge (vertex))
324  {
325  IndexType index;
326  index[0] = static_cast<IndexValueType>(vertex[0] - polygonExtRingBoundReg.GetOrigin(0));
327  index[1] = static_cast<IndexValueType>(vertex[1] - polygonExtRingBoundReg.GetOrigin(1));
328 // index[0] += this->GetOutput()->GetOrigin()[0];
329 // index[1] += this->GetOutput()->GetOrigin()[1];
330 // std::cout << "index " << index << std::endl;
331  if (this->GetOutput()->HasLabel(m_lab))
332  {
333  if (!this->GetOutput()->GetLabelObject(m_lab)->HasIndex(index))
334  { //Add a pixel to the current labelObject
335  this->GetOutput()->SetPixel(index, m_lab);
336  }
337  }
338  else
339  {
340  //Add a pixel to the current labelObject
341  this->GetOutput()->SetPixel(index, m_lab);
342  }
343  }
344  }
345  }
346  //Modify the label for the next layer
347  m_lab += 10;
348  break;
349  }
350  case FEATURE_MULTIPOINT:
351  {
352  itkExceptionMacro(
353  <<
354  "This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
355  break;
356  }
357  case FEATURE_MULTILINE:
358  {
359  itkExceptionMacro(
360  << "This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
361  break;
362  }
364  {
365  itkExceptionMacro(
366  <<
367  "This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
368  break;
369  }
370  case FEATURE_COLLECTION:
371  {
372  itkExceptionMacro(
373  <<
374  "This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
375  break;
376  }
377  }
378  }
379 }
380 
381 template <class TVectorData, class TLabelMap>
382 void
384 ::PrintSelf(std::ostream& os, itk::Indent indent) const
385 {
386  Superclass::PrintSelf(os, indent);
387  os << indent << "BackgroundValue: " << static_cast<typename itk::NumericTraits<OutputLabelMapPixelType>::PrintType>(
388  m_BackgroundValue) << std::endl;
389 }
390 
391 } // end namespace otb
392 
393 #endif
InputVectorDataType::ConstPointer InputVectorDataConstPointer
InputVectorDataType::DataTreeType::TreeNodeType InternalTreeNodeType
signed long IndexValueType
virtual void SetInput(const InputVectorDataType *input)
#define otbGenericMsgDebugMacro(x)
Definition: otbMacro.h:70
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:98