OTB  6.7.0
Orfeo Toolbox
otbSimplifyPathFunctor.h
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 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 
58  typedef typename TInput::VertexListType::ConstIterator VertexListConstIteratorType;
59  typedef typename TInput::VertexListType::ConstPointer VertexListConstPointerType;
60  typedef TOutput OutputPathType;
61  typedef typename OutputPathType::Pointer OutputPathPointerType;
62 
63  void SetTolerance(double Tolerance)
64  {
65  m_Tolerance = Tolerance;
66  }
67  double GetTolerance(void) const
68  {
69  return (m_Tolerance);
70  }
71 
73  {
74  m_Tolerance = 1.0;
75  }
77 
78  inline OutputPathPointerType operator ()(const TInput * input)
79  {
80 
81  OutputPathPointerType newPath = OutputPathType::New();
82  newPath->Initialize();
83  // Getting the verices list of the current input paths
84  VertexListConstPointerType vertexList = input->GetVertexList();
85  if(vertexList->Size()>0)
86  {
87  VertexListConstIteratorType beginIt = vertexList->Begin();
88  VertexListConstIteratorType beforeTheEndIt = --(vertexList->End());
89 
90  // Add the first vertex
91  newPath->AddVertex(beginIt.Value());
92 
93  while (beginIt != beforeTheEndIt)
94  {
95  VertexListConstIteratorType endIt = beforeTheEndIt;
96  // while the segment is not consistent, decrement endIt
97  while (!this->TestPathConsistency(beginIt, endIt))
98  {
99  --endIt;
100  }
101  // Add the final vertex
102  newPath->AddVertex(endIt.Value());
103  beginIt = endIt;
104  }
105  }
106 
107  newPath->SetMetaDataDictionary(input->GetMetaDataDictionary());
108  return newPath;
109 
110  }
111 
112 private:
113  double m_Tolerance;
114 
116  VertexListConstIteratorType end) const
117  {
118  VertexListConstIteratorType segmentIt = begin;
119  ++segmentIt;
120  //Compute the distance of a point to a segment based on the cross product
121  while (segmentIt != end)
122  {
123  double crossProduct = (end.Value()[0] - begin.Value()[0]) * (segmentIt.Value()[1] - begin.Value()[1])
124  - (end.Value()[1] - begin.Value()[1]) * (segmentIt.Value()[0] - begin.Value()[0]);
125  double lengthSeg = (end.Value()[0] - begin.Value()[0]) * (end.Value()[0] - begin.Value()[0])
126  + (end.Value()[1] - begin.Value()[1]) * (end.Value()[1] - begin.Value()[1]);
127  if (lengthSeg == 0) 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 }
141 
142 #endif
void SetTolerance(double Tolerance)
TInput::VertexListType::ConstPointer VertexListConstPointerType
double GetTolerance(void) const
OutputPathType::Pointer OutputPathPointerType
bool TestPathConsistency(VertexListConstIteratorType begin, VertexListConstIteratorType end) const
TInput::VertexListType::ConstIterator VertexListConstIteratorType
This filter performs a simplification of the input path.
OutputPathPointerType operator()(const TInput *input)