21 #ifndef otbVectorDataToLabelMapWithAttributesFilter_hxx
22 #define otbVectorDataToLabelMapWithAttributesFilter_hxx
25 #include "itkBinaryImageToLabelMapFilter.h"
26 #include "itkNumericTraits.h"
33 template <
class TVectorData,
class TLabelMap>
36 m_BackgroundValue = itk::NumericTraits<OutputLabelMapPixelType>::max();
37 this->SetNumberOfRequiredInputs(1);
40 m_Direction.SetIdentity();
43 m_InitialLabel = itk::NumericTraits<LabelType>::Zero;
45 m_AutomaticSizeComputation =
true;
46 m_VectorDataProperties = VectorDataPropertiesType::New();
50 template <
class TVectorData,
class TLabelMap>
53 if (this->m_Spacing != spacing)
55 this->m_Spacing = spacing;
61 template <
class TVectorData,
class TLabelMap>
69 template <
class TVectorData,
class TLabelMap>
72 itk::Vector<float, 2> sf(spacing);
79 template <
class TVectorData,
class TLabelMap>
87 template <
class TVectorData,
class TLabelMap>
90 itk::Point<float, 2> of(origin);
101 template <
class TVectorData,
class TLabelMap>
115 if (m_AutomaticSizeComputation ==
false)
118 outputLargestPossibleRegion.SetSize(m_Size);
119 outputLargestPossibleRegion.SetIndex(m_StartIndex);
120 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
125 outputPtr->SetSpacing(m_Spacing);
126 outputPtr->SetOrigin(m_Origin);
127 outputPtr->SetDirection(m_Direction);
132 m_VectorDataProperties->SetVectorDataObject(this->GetInput());
135 m_VectorDataProperties->ComputeBoundingRegion();
139 SpacingType spacing = this->GetInput()->GetSpacing();
140 OriginType origin = m_VectorDataProperties->GetBoundingRegion().GetOrigin();
141 for (
unsigned int i = 0; i < 2; ++i)
143 if (spacing[i] < 0.0)
145 origin[i] += m_VectorDataProperties->GetBoundingRegion().GetSize(i);
147 origin[i] += (0.5 - m_StartIndex[i]) * spacing[i];
148 size[i] =
static_cast<unsigned long>(std::ceil(std::abs(m_VectorDataProperties->GetBoundingRegion().GetSize(i) / spacing[i])));
151 outputLargestPossibleRegion.SetSize(size);
152 outputLargestPossibleRegion.SetIndex(m_StartIndex);
154 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
155 outputPtr->SetSpacing(spacing);
156 outputPtr->SetOrigin(origin);
157 outputPtr->SetDirection(m_Direction);
191 template <
class TVectorData,
class TLabelMap>
198 template <
class TVectorData,
class TLabelMap>
205 template <
class TVectorData,
class TLabelMap>
209 if (this->GetNumberOfInputs() < 1)
214 return static_cast<const TVectorData*
>(this->itk::ProcessObject::GetInput(0));
217 template <
class TVectorData,
class TLabelMap>
221 return static_cast<const TVectorData*
>(this->itk::ProcessObject::GetInput(idx));
224 template <
class TVectorData,
class TLabelMap>
228 this->AllocateOutputs();
233 for (
unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
235 if (this->GetInput(idx))
241 output->SetBackgroundValue(m_BackgroundValue);
243 m_lab = m_InitialLabel;
247 output->SetMetaDataDictionary(input->GetMetaDataDictionary());
248 ProcessNode(inputRoot);
253 template <
class TVectorData,
class TLabelMap>
261 for (
typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
266 switch (dataNode->GetNodeType())
287 this->GetOutput()->TransformPhysicalPointToIndex(dataNode->GetPoint(), index);
288 if (this->GetOutput()->GetLargestPossibleRegion().IsInside(index))
290 this->GetOutput()->SetPixel(index, m_lab);
298 itkExceptionMacro(<<
"This type (FEATURE_LINE) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
306 PolygonPointerType correctPolygonExtRing = correct(dataNode->GetPolygonExteriorRing());
310 typedef typename PolygonType::RegionType RSRegionType;
311 typedef typename PolygonType::VertexType VertexType;
312 typedef typename IndexType::IndexValueType IndexValueType;
313 RSRegionType polygonExtRingBoundReg = correctPolygonExtRing->GetBoundingRegion();
317 physCorners[0][0] = polygonExtRingBoundReg.GetOrigin(0);
318 physCorners[0][1] = polygonExtRingBoundReg.GetOrigin(1);
319 physCorners[1] = physCorners[0];
320 physCorners[2] = physCorners[0];
321 physCorners[3] = physCorners[0];
323 physCorners[1][1] += polygonExtRingBoundReg.GetSize(1);
324 physCorners[2][1] += polygonExtRingBoundReg.GetSize(1);
325 physCorners[2][0] += polygonExtRingBoundReg.GetSize(0);
326 physCorners[3][0] += polygonExtRingBoundReg.GetSize(0);
330 startIdx.Fill(itk::NumericTraits<IndexValueType>::max());
331 endIdx.Fill(itk::NumericTraits<IndexValueType>::NonpositiveMin());
333 for (
unsigned int k = 0; k < 4; ++k)
335 this->GetOutput()->TransformPhysicalPointToIndex(physCorners[k], tmpIdx);
337 startIdx[0] = std::min(startIdx[0], tmpIdx[0]);
338 startIdx[1] = std::min(startIdx[1], tmpIdx[1]);
339 endIdx[0] = std::max(endIdx[0], tmpIdx[0]);
340 endIdx[1] = std::max(endIdx[1], tmpIdx[1]);
344 polyRegion.SetIndex(startIdx);
345 polyRegion.SetSize(0, endIdx[0] - startIdx[0] + 1);
346 polyRegion.SetSize(1, endIdx[1] - startIdx[1] + 1);
347 if (polyRegion.Crop(this->GetOutput()->GetLargestPossibleRegion()))
349 startIdx = polyRegion.GetIndex();
350 endIdx[0] = startIdx[0] - 1 + polyRegion.GetSize(0);
351 endIdx[1] = startIdx[1] - 1 + polyRegion.GetSize(1);
361 for (IndexValueType j = startIdx[1]; j <= endIdx[1]; ++j)
363 for (IndexValueType i = startIdx[0]; i <= endIdx[0]; ++i)
367 this->GetOutput()->TransformIndexToPhysicalPoint(tmpIdx, tmpPoint);
368 vertex[0] = tmpPoint[0];
369 vertex[1] = tmpPoint[1];
370 if (correctPolygonExtRing->IsInside(vertex) || correctPolygonExtRing->IsOnEdge(vertex))
373 if (this->GetOutput()->HasLabel(m_lab))
375 if (!this->GetOutput()->GetLabelObject(m_lab)->HasIndex(tmpIdx))
377 this->GetOutput()->SetPixel(tmpIdx, m_lab);
383 this->GetOutput()->SetPixel(tmpIdx, m_lab);
386 for (
unsigned int ii = 0; ii < dataNode->GetFieldList().size(); ii++)
388 fieldValue =
static_cast<AttributesValueType>(dataNode->GetFieldAsString(dataNode->GetFieldList()[ii]));
389 this->GetOutput()->GetLabelObject(m_lab)->SetAttribute(dataNode->GetFieldList()[ii].c_str(), fieldValue);
401 itkExceptionMacro(<<
"This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
406 itkExceptionMacro(<<
"This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
411 itkExceptionMacro(<<
"This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
416 itkExceptionMacro(<<
"This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToLabelMapWithAttributesFilter(), please request for it");
423 template <
class TVectorData,
class TLabelMap>
426 Superclass::PrintSelf(os, indent);
427 os << indent <<
"BackgroundValue: " <<
static_cast<typename itk::NumericTraits<OutputLabelMapPixelType>::PrintType
>(m_BackgroundValue) << std::endl;