OTB  9.0.0
Orfeo Toolbox
otbVectorDataToRandomLineGenerator.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 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()->GetDataTree()->GetRoot()->Get();
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)->GetDataTree()->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  TreeIteratorType itVector(vectorData->GetDataTree());
119  itVector.GoToBegin();
120  while (!itVector.IsAtEnd())
121  {
122  if (itVector.Get()->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(itVector.Get());
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)->GetDataTree()->Add(currentGeometry, document);
141  }
142  }
143  ++itVector;
144  }
145 }
146 
147 } // end namespace otb
148 
149 #endif
otb::VectorDataToRandomLineGenerator::RegionType
DataNodeType::PolygonType::RegionType RegionType
Definition: otbVectorDataToRandomLineGenerator.h:67
otb::DOCUMENT
@ DOCUMENT
Definition: otbDataNode.h:41
otbMath.h
otb::VectorDataToRandomLineGenerator::DataNodeType
VectorDataType::DataNodeType DataNodeType
Definition: otbVectorDataToRandomLineGenerator.h:66
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::FEATURE_LINE
@ FEATURE_LINE
Definition: otbDataNode.h:44
otb::VectorDataToRandomLineGenerator::TreeIteratorType
itk::PreOrderTreeIterator< typename VectorDataType::DataTreeType > TreeIteratorType
Definition: otbVectorDataToRandomLineGenerator.h:73
otb::VectorDataToRandomLineGenerator::SetInput
void SetInput(const VectorDataType *)
Definition: otbVectorDataToRandomLineGenerator.hxx:48
otb::VectorDataToRandomLineGenerator::GetInput
const VectorDataType * GetInput() const
Definition: otbVectorDataToRandomLineGenerator.hxx:54
otb::VectorDataToRandomLineGenerator::VectorDataType
TVectorData VectorDataType
Definition: otbVectorDataToRandomLineGenerator.h:62
otb::VectorDataToRandomLineGenerator::PointVectorType
std::vector< PointType > PointVectorType
Definition: otbVectorDataToRandomLineGenerator.h:71
otb::VectorDataToRandomLineGenerator::RandomPointsGenerator
PointVectorType RandomPointsGenerator(DataNodeType *node)
Definition: otbVectorDataToRandomLineGenerator.hxx:60
otb::VectorDataToRandomLineGenerator::m_RandomSizeGenerator
RandomGeneratorType::Pointer m_RandomSizeGenerator
Definition: otbVectorDataToRandomLineGenerator.h:124
otb::VectorDataToRandomLineGenerator::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbVectorDataToRandomLineGenerator.hxx:41
otb::VectorDataToRandomLineGenerator::VertexType
DataNodeType::LineType::VertexType VertexType
Definition: otbVectorDataToRandomLineGenerator.h:70
otb::VectorDataToRandomLineGenerator::PointType
DataNodeType::PointType PointType
Definition: otbVectorDataToRandomLineGenerator.h:68
otbVectorDataToRandomLineGenerator.h
otb::VectorDataToRandomLineGenerator::VectorDataToRandomLineGenerator
VectorDataToRandomLineGenerator()
Definition: otbVectorDataToRandomLineGenerator.hxx:31
otb::VectorDataToRandomLineGenerator::m_RandomGenerator
RandomGeneratorType::Pointer m_RandomGenerator
Definition: otbVectorDataToRandomLineGenerator.h:123
otb::VectorDataToRandomLineGenerator::GenerateData
void GenerateData(void) override
Definition: otbVectorDataToRandomLineGenerator.hxx:103