OTB  10.0.0
Orfeo Toolbox
otbVectorDataToLabelMapFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 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  // Use our own value for the background
203  output->SetBackgroundValue(itk::NumericTraits<OutputLabelMapPixelType>::max());
204  // Set the value of the first label
205  m_lab = itk::NumericTraits<LabelType>::Zero;
206  // otbGenericMsgDebugMacro(<<"input " << idx);
207 
208  // The projection information
209  output->SetMetaDataDictionary(input->GetMetaDataDictionary());
210  ProcessNode(input,input->GetRoot());
211  }
212  }
213 }
214 
215 template <class TVectorData, class TLabelMap>
217 {
218 
219  // Get the children list from the input node
220  ChildrenListType children = inputVd->GetChildrenList(source);
221 
222  // For each child
223  for (typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
224  {
225  // Copy input DataNode info
226  DataNodePointerType dataNode = (*it);
227  otbGenericMsgDebugMacro(<< "Type of node " << dataNode->GetNodeType() << " id" << dataNode->GetNodeId());
228  switch (dataNode->GetNodeType())
229  {
230  case otb::ROOT:
231  {
232  //ProcessNode(inputVd,dataNode);
233  break;
234  }
235  case otb::DOCUMENT:
236  {
237  ProcessNode(inputVd,dataNode);
238  break;
239  }
240  case otb::FOLDER:
241  {
242  ProcessNode(inputVd,dataNode);
243  break;
244  }
245  case FEATURE_POINT:
246  {
247  otbGenericMsgDebugMacro(<< "Insert Point from vectorData");
248  IndexType index;
249  this->GetOutput()->TransformPhysicalPointToIndex(dataNode->GetPoint(), index);
250 
251  this->GetOutput()->SetPixel(index, m_lab);
252  m_lab += 10;
253  break;
254  }
255  case otb::FEATURE_LINE:
256  {
257  // TODO Bresenham
258  itkExceptionMacro(<< "This type (FEATURE_LINE) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
259  break;
260  }
261  case FEATURE_POLYGON:
262  {
263 
265  CorrectFunctorType correct;
266  PolygonPointerType correctPolygonExtRing = correct(dataNode->GetPolygonExteriorRing());
268 
269  typedef typename PolygonType::RegionType RegionType;
270  typedef typename PolygonType::VertexType VertexType;
271  typedef typename IndexType::IndexValueType IndexValueType;
272  typedef typename VertexType::ValueType VertexValueType;
273  RegionType polygonExtRingBoundReg = correctPolygonExtRing->GetBoundingRegion();
274 
275  VertexType vertex;
276  otbMsgDevMacro("Polygon bounding region " << polygonExtRingBoundReg);
277  otbMsgDevMacro("output origin " << this->GetOutput()->GetOrigin());
278  otbMsgDevMacro("spacing " << this->GetOutput()->GetSpacing());
279  // For each position in the bounding region of the polygon
280 
281  for (double i = polygonExtRingBoundReg.GetOrigin(0); i < polygonExtRingBoundReg.GetOrigin(0) + polygonExtRingBoundReg.GetSize(0);
282  i += this->GetOutput()->GetSpacing()[0])
283  {
284  vertex[0] = static_cast<VertexValueType>(i);
285  for (double j = polygonExtRingBoundReg.GetOrigin(1); j < polygonExtRingBoundReg.GetOrigin(1) + polygonExtRingBoundReg.GetSize(1);
286  j += this->GetOutput()->GetSpacing()[1])
287  {
288  vertex[1] = static_cast<VertexValueType>(j);
289 
290  if (correctPolygonExtRing->IsInside(vertex) || correctPolygonExtRing->IsOnEdge(vertex))
291  {
292  IndexType index;
293  index[0] = static_cast<IndexValueType>(vertex[0] - polygonExtRingBoundReg.GetOrigin(0));
294  index[1] = static_cast<IndexValueType>(vertex[1] - polygonExtRingBoundReg.GetOrigin(1));
295  // index[0] += this->GetOutput()->GetOrigin()[0];
296  // index[1] += this->GetOutput()->GetOrigin()[1];
297  // std::cout << "index " << index << std::endl;
298  if (this->GetOutput()->HasLabel(m_lab))
299  {
300  if (!this->GetOutput()->GetLabelObject(m_lab)->HasIndex(index))
301  { // Add a pixel to the current labelObject
302  this->GetOutput()->SetPixel(index, m_lab);
303  }
304  }
305  else
306  {
307  // Add a pixel to the current labelObject
308  this->GetOutput()->SetPixel(index, m_lab);
309  }
310  }
311  }
312  }
313  // Modify the label for the next layer
314  m_lab += 10;
315  break;
316  }
317  case FEATURE_MULTIPOINT:
318  {
319  itkExceptionMacro(<< "This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
320  break;
321  }
322  case FEATURE_MULTILINE:
323  {
324  itkExceptionMacro(<< "This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
325  break;
326  }
328  {
329  itkExceptionMacro(<< "This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
330  break;
331  }
332  case FEATURE_COLLECTION:
333  {
334  itkExceptionMacro(<< "This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToLabelMapFilter(), please request for it");
335  break;
336  }
337  }
338  }
339 }
340 
341 template <class TVectorData, class TLabelMap>
342 void VectorDataToLabelMapFilter<TVectorData, TLabelMap>::PrintSelf(std::ostream& os, itk::Indent indent) const
343 {
344  Superclass::PrintSelf(os, indent);
345  os << indent << "BackgroundValue: " << static_cast<typename itk::NumericTraits<OutputLabelMapPixelType>::PrintType>(m_BackgroundValue) << std::endl;
346 }
347 
348 } // end namespace otb
349 
350 #endif
This filter simplify and close the input polygon, making the last point equal to the first one.
virtual void SetOrigin(OriginType _arg)
void PrintSelf(std::ostream &os, itk::Indent indent) const override
void ProcessNode(InputVectorDataConstPointer inputVdata, DataNodePointerType source)
InputVectorDataType::ConstPointer InputVectorDataConstPointer
virtual void SetSpacing(const SpacingType &spacing)
OutputLabelMapType::SpacingType SpacingType
OutputLabelMapType::PointType OriginType
const InputVectorDataType * GetInput(void)
OutputLabelMapType::IndexType IndexType
virtual void SetInput(const InputVectorDataType *input)
InputVectorDataType::ChildrenListType ChildrenListType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
@ FEATURE_POLYGON
Definition: otbDataNode.h:45
@ FEATURE_MULTIPOINT
Definition: otbDataNode.h:46
@ FEATURE_MULTIPOLYGON
Definition: otbDataNode.h:48
@ FOLDER
Definition: otbDataNode.h:42
@ FEATURE_MULTILINE
Definition: otbDataNode.h:47
@ DOCUMENT
Definition: otbDataNode.h:41
@ ROOT
Definition: otbDataNode.h:40
@ FEATURE_POINT
Definition: otbDataNode.h:43
@ FEATURE_COLLECTION
Definition: otbDataNode.h:49
@ FEATURE_LINE
Definition: otbDataNode.h:44
#define otbMsgDevMacro(x)
Definition: otbMacro.h:116
#define otbGenericMsgDebugMacro(x)
Definition: otbMacro.h:115