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