OTB  10.0.0
Orfeo Toolbox
otbVectorDataToRandomLineGenerator.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 otbVectorDataToRandomLineGenerator_hxx
22 #define otbVectorDataToRandomLineGenerator_hxx
23 
25 
26 #include "otbMath.h"
27 
28 namespace otb
29 {
30 template <class TVectorData>
31 VectorDataToRandomLineGenerator<TVectorData>::VectorDataToRandomLineGenerator() : m_NumberOfOutputLine(100), m_MinLineSize(2), m_MaxLineSize(10), m_CurrentID(0)
32 {
33  this->SetNumberOfRequiredInputs(1);
34  this->SetNumberOfRequiredOutputs(1);
35 
36  m_RandomGenerator = RandomGeneratorType::GetInstance();
37  m_RandomSizeGenerator = RandomGeneratorType::GetInstance();
38 }
39 
40 template <class TVectorData>
41 void VectorDataToRandomLineGenerator<TVectorData>::PrintSelf(std::ostream& os, itk::Indent indent) const
42 {
43  this->Superclass::PrintSelf(os, indent);
44  os << indent << "Required Number of Output Line: " << m_NumberOfOutputLine << std::endl;
45 }
46 
47 template <class TVectorData>
49 {
50  this->Superclass::SetNthInput(0, const_cast<VectorDataType*>(vectorData));
51 }
52 
53 template <class TVectorData>
55 {
56  return static_cast<const VectorDataType*>(this->Superclass::GetInput(0));
57 }
58 
59 template <class TVectorData>
61 {
62  // Output
63  PointVectorType vPoint;
64 
65  // Gathering Information
66  RegionType generatorRegion = node->GetPolygonExteriorRing()->GetBoundingRegion();
67  typename RegionType::SizeType generatorRegionSize = generatorRegion.GetSize();
68  typename RegionType::IndexType generatorRegionIndex = generatorRegion.GetIndex();
69  // typename RegionType::IndexType generatorRegionOrigin = generatorRegion.GetOrigin();
70 
71  // Generation
72  PointType rangeMin, rangeMax;
73 
74  for (unsigned int dim = 0; dim < 2; ++dim)
75  {
76  rangeMin[dim] = generatorRegionIndex[dim];
77  rangeMax[dim] = generatorRegionIndex[dim] + generatorRegionSize[dim];
78  }
79 
80  unsigned int nbPoint = this->m_RandomSizeGenerator->GetUniformVariate(this->GetMinLineSize(), this->GetMaxLineSize());
81 
82  while (nbPoint > 0)
83  {
84  VertexType candidate;
85  for (unsigned int dim = 0; dim < 2; ++dim)
86  {
87  candidate[dim] = this->m_RandomGenerator->GetUniformVariate(rangeMin[dim], rangeMax[dim]);
88  }
89 
90  if (node->GetPolygonExteriorRing()->IsInside(candidate))
91  {
92  PointType point;
93  point[0] = candidate[0];
94  point[1] = candidate[1];
95  vPoint.push_back(point);
96  nbPoint--;
97  }
98  }
99  return vPoint;
100 }
101 
102 template <class TVectorData>
104 {
105  this->GetOutput()->SetMetaDataDictionary(this->GetInput()->GetMetaDataDictionary());
106 
107  // Retrieving root node
108  typename DataNodeType::Pointer root = this->GetOutput()->GetRoot();
109  // Create the document node
110  typename DataNodeType::Pointer document = DataNodeType::New();
111  document->SetNodeType(otb::DOCUMENT);
112  // Adding the layer to the data tree
113  this->GetOutput(0)->Add(document, root);
114 
115  // Iterates through the polygon features and generates random Lines inside the polygon
116  typename VectorDataType::ConstPointer vectorData = static_cast<const VectorDataType*>(this->GetInput());
117 
118  auto itVectorPair = vectorData->GetIteratorPair();
119  auto currentIt = itVectorPair.first;
120  while (currentIt != itVectorPair.second)
121  {
122  if (vectorData->Get(currentIt)->IsPolygonFeature())
123  {
124 
125  for (unsigned int i = 0; i < this->GetNumberOfOutputLine(); ++i)
126  {
127  typename DataNodeType::Pointer currentGeometry = DataNodeType::New();
128  currentGeometry->SetNodeId(this->GetNextID());
129  currentGeometry->SetNodeType(otb::FEATURE_LINE);
130  typename LineType::Pointer line = LineType::New();
131  currentGeometry->SetLine(line);
132  PointVectorType vPoints = RandomPointsGenerator(vectorData->Get(currentIt));
133  for (typename PointVectorType::const_iterator it = vPoints.begin(); it != vPoints.end(); ++it)
134  {
135  VertexType vertex;
136  vertex[0] = (*it)[0];
137  vertex[1] = (*it)[1];
138  currentGeometry->GetLine()->AddVertex(vertex);
139  }
140  this->GetOutput(0)->Add(currentGeometry, document);
141  }
142  }
143  ++currentIt;
144  }
145 }
146 
147 } // end namespace otb
148 
149 #endif
PointVectorType RandomPointsGenerator(DataNodeType *node)
DataNodeType::PolygonType::RegionType RegionType
void PrintSelf(std::ostream &os, itk::Indent indent) const override
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
@ DOCUMENT
Definition: otbDataNode.h:41
@ FEATURE_LINE
Definition: otbDataNode.h:44