OTB  7.2.0
Orfeo Toolbox
otbSimplifyPathFunctor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2020 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 otbSimplifyPathFunctor_h
22 #define otbSimplifyPathFunctor_h
23 
24 #include "otbMath.h"
25 
26 namespace otb
27 {
28 
53 template <class TInput, class TOutput>
55 {
56 public:
57  typedef typename TInput::VertexListType::ConstIterator VertexListConstIteratorType;
58  typedef typename TInput::VertexListType::ConstPointer VertexListConstPointerType;
59  typedef TOutput OutputPathType;
60  typedef typename OutputPathType::Pointer OutputPathPointerType;
61 
62  void SetTolerance(double Tolerance)
63  {
64  m_Tolerance = Tolerance;
65  }
66  double GetTolerance(void) const
67  {
68  return (m_Tolerance);
69  }
70 
72  {
73  m_Tolerance = 1.0;
74  }
76  {
77  }
78 
79  inline OutputPathPointerType operator()(const TInput* input)
80  {
81 
82  OutputPathPointerType newPath = OutputPathType::New();
83  newPath->Initialize();
84  // Getting the verices list of the current input paths
85  VertexListConstPointerType vertexList = input->GetVertexList();
86  if (vertexList->Size() > 0)
87  {
88  VertexListConstIteratorType beginIt = vertexList->Begin();
89  VertexListConstIteratorType beforeTheEndIt = --(vertexList->End());
90 
91  // Add the first vertex
92  newPath->AddVertex(beginIt.Value());
93 
94  while (beginIt != beforeTheEndIt)
95  {
96  VertexListConstIteratorType endIt = beforeTheEndIt;
97  // while the segment is not consistent, decrement endIt
98  while (!this->TestPathConsistency(beginIt, endIt))
99  {
100  --endIt;
101  }
102  // Add the final vertex
103  newPath->AddVertex(endIt.Value());
104  beginIt = endIt;
105  }
106  }
107 
108  newPath->SetMetaDataDictionary(input->GetMetaDataDictionary());
109  return newPath;
110  }
111 
112 private:
113  double m_Tolerance;
114 
115  bool TestPathConsistency(VertexListConstIteratorType begin, VertexListConstIteratorType end) const
116  {
117  VertexListConstIteratorType segmentIt = begin;
118  ++segmentIt;
119  // Compute the distance of a point to a segment based on the cross product
120  while (segmentIt != end)
121  {
122  double crossProduct = (end.Value()[0] - begin.Value()[0]) * (segmentIt.Value()[1] - begin.Value()[1]) -
123  (end.Value()[1] - begin.Value()[1]) * (segmentIt.Value()[0] - begin.Value()[0]);
124  double lengthSeg =
125  (end.Value()[0] - begin.Value()[0]) * (end.Value()[0] - begin.Value()[0]) + (end.Value()[1] - begin.Value()[1]) * (end.Value()[1] - begin.Value()[1]);
126  if (lengthSeg == 0)
127  return false;
128  double distsq = crossProduct * crossProduct / lengthSeg;
129  if (distsq > static_cast<double>(m_Tolerance))
130  {
131  return false;
132  }
133  ++segmentIt;
134  }
135  return true;
136  }
137 };
138 }
139 
140 #endif
void SetTolerance(double Tolerance)
TInput::VertexListType::ConstPointer VertexListConstPointerType
OutputPathType::Pointer OutputPathPointerType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
TInput::VertexListType::ConstIterator VertexListConstIteratorType
This filter performs a simplification of the input path.
OutputPathPointerType operator()(const TInput *input)
bool TestPathConsistency(VertexListConstIteratorType begin, VertexListConstIteratorType end) const