OTB  6.7.0
Orfeo Toolbox
otbVectorDataToRandomLineGenerator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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>
33  m_NumberOfOutputLine(100),
34  m_MinLineSize(2),
35  m_MaxLineSize(10),
36  m_CurrentID(0)
37 {
38  this->SetNumberOfRequiredInputs(1);
39  this->SetNumberOfRequiredOutputs(1);
40 
41  m_RandomGenerator = RandomGeneratorType::GetInstance();
42  m_RandomSizeGenerator = RandomGeneratorType::GetInstance();
43 }
44 
45 template <class TVectorData>
46 void
48 ::PrintSelf(std::ostream& os, itk::Indent indent) const
49 {
50  this->Superclass::PrintSelf(os, indent);
51  os << indent << "Required Number of Output Line: " << m_NumberOfOutputLine << std::endl;
52 }
53 
54 template <class TVectorData>
55 void
57 ::SetInput(const VectorDataType * vectorData)
58 {
59  this->Superclass::SetNthInput(0, const_cast<VectorDataType *>(vectorData));
60 }
61 
62 template <class TVectorData>
63 const TVectorData *
65 ::GetInput() const
66 {
67  return static_cast<const VectorDataType *>(this->Superclass::GetInput(0));
68 }
69 
70 template <class TVectorData>
72 ::PointVectorType
75 {
76  // Output
77  PointVectorType vPoint;
78 
79  // Gathering Information
80  RegionType generatorRegion = node->GetPolygonExteriorRing()->GetBoundingRegion();
81  typename RegionType::SizeType generatorRegionSize = generatorRegion.GetSize();
82  typename RegionType::IndexType generatorRegionIndex = generatorRegion.GetIndex();
83  //typename RegionType::IndexType generatorRegionOrigin = generatorRegion.GetOrigin();
84 
85  // Generation
86  PointType rangeMin, rangeMax;
87 
88  for(unsigned int dim = 0; dim < 2; ++dim)
89  {
90  rangeMin[dim] = generatorRegionIndex[dim];
91  rangeMax[dim] = generatorRegionIndex[dim] + generatorRegionSize[dim];
92  }
93 
94  unsigned int nbPoint = this->m_RandomSizeGenerator->GetUniformVariate(this->GetMinLineSize(), this->GetMaxLineSize());
95 
96  while(nbPoint > 0)
97  {
98  VertexType candidate;
99  for(unsigned int dim = 0; dim < 2; ++dim)
100  {
101  candidate[dim] = this->m_RandomGenerator->GetUniformVariate(rangeMin[dim], rangeMax[dim]);
102  }
103 
104  if(node->GetPolygonExteriorRing()->IsInside(candidate))
105  {
106  PointType point;
107  point[0] = candidate[0];
108  point[1] = candidate[1];
109  vPoint.push_back(point);
110  nbPoint --;
111  }
112  }
113  return vPoint;
114 }
115 
116 template <class TVectorData>
117 void
120 {
121  this->GetOutput()->SetMetaDataDictionary(this->GetInput()->GetMetaDataDictionary());
122 
123  // Retrieving root node
124  typename DataNodeType::Pointer root = this->GetOutput()->GetDataTree()->GetRoot()->Get();
125  // Create the document node
126  typename DataNodeType::Pointer document = DataNodeType::New();
127  document->SetNodeType(otb::DOCUMENT);
128  // Adding the layer to the data tree
129  this->GetOutput(0)->GetDataTree()->Add(document, root);
130 
131  // Iterates through the polygon features and generates random Lines inside the polygon
132  typename VectorDataType::ConstPointer vectorData = static_cast<const VectorDataType *>(this->GetInput());
133 
134  TreeIteratorType itVector(vectorData->GetDataTree());
135  itVector.GoToBegin();
136  while (!itVector.IsAtEnd())
137  {
138  if (itVector.Get()->IsPolygonFeature())
139  {
140 
141  for(unsigned int i=0; i<this->GetNumberOfOutputLine(); ++i)
142  {
143  typename DataNodeType::Pointer currentGeometry = DataNodeType::New();
144  currentGeometry->SetNodeId(this->GetNextID());
145  currentGeometry->SetNodeType(otb::FEATURE_LINE);
146  typename LineType::Pointer line = LineType::New();
147  currentGeometry->SetLine(line);
148  PointVectorType vPoints = RandomPointsGenerator(itVector.Get());
149  for (typename PointVectorType::const_iterator it = vPoints.begin(); it != vPoints.end(); ++it)
150  {
151  VertexType vertex;
152  vertex[0] = (*it)[0];
153  vertex[1] = (*it)[1];
154  currentGeometry->GetLine()->AddVertex(vertex);
155  }
156  this->GetOutput(0)->GetDataTree()->Add(currentGeometry, document);
157  }
158  }
159  ++itVector;
160  }
161 }
162 
163 } // end namespace otb
164 
165 #endif
166 
167 
itk::Size< Monteverdi_DIMENSION > SizeType
Definition: mvdTypes.h:137
void PrintSelf(std::ostream &os, itk::Indent indent) const override
itk::Index< Monteverdi_DIMENSION > IndexType
Definition: mvdTypes.h:133
PointVectorType RandomPointsGenerator(DataNodeType *node)
Produces a VectorData Containing Random Lines.
DataNodeType::PolygonType::RegionType RegionType
const SizeValueType * GetSize() const