OTB  10.0.0
Orfeo Toolbox
otbLabelObjectToPolygonFunctor.h
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 otbLabelObjectToPolygonFunctor_h
22 #define otbLabelObjectToPolygonFunctor_h
23 
24 #include <vector>
25 
26 // TODO change this include have to define from what inherate this class
27 #include "otbPolyLineParametricPathWithValue.h" //for std::abs
28 
29 #include "itkPoint.h"
30 #include "itkIndex.h"
31 
32 namespace otb
33 {
34 
35 namespace Functor
36 {
37 
61 template <class TLabelObject, class TPolygon>
63 {
64 public:
66  typedef TLabelObject LabelObjectType;
67 
69  typedef typename LabelObjectType::ConstLineIterator ConstLineIteratorType;
70 
71  typedef typename LabelObjectType::LineType LineType;
72  typedef typename LineType::IndexType IndexType;
73  typedef TPolygon PolygonType;
74  typedef typename PolygonType::Pointer PolygonPointerType;
75  typedef typename PolygonType::VertexType VertexType;
76  typedef itk::Point<double, 2> PointType;
77  typedef itk::Vector<double, 2> SpacingType;
78  typedef itk::Index<2> RegionIndexType;
79 
84  inline PolygonType* operator()(LabelObjectType* labelObject);
85 
87  const char* GetNameOfClass()
88  {
89  return "LabelObjectToPolygonFunctor";
90  }
91 
93  void SetStartIndex(const RegionIndexType& index)
94  {
95  m_StartIndex = index;
96  }
97 
100  {
101  return m_StartIndex;
102  }
103 
105  void SetOrigin(const PointType& origin)
106  {
107  m_Origin = origin;
108  }
109 
111  const PointType& GetOrigin() const
112  {
113  return m_Origin;
114  }
115 
117  void SetSpacing(const SpacingType& spacing)
118  {
119  m_Spacing = spacing;
120  }
121 
123  const SpacingType& GetSpacing() const
124  {
125  return m_Spacing;
126  }
127 
130  : m_Polygon(nullptr),
133  m_StartingPoint(),
134  m_CurrentPoint(),
135  m_CurrentRun(),
136  m_CurrentLine(0),
137  m_Solution(),
138  m_LineOffset(0),
139  m_StartIndex(),
140  m_Origin(),
141  m_Spacing(1.)
142  {
143  m_StartingPoint.Fill(0);
144  m_CurrentPoint.Fill(0);
145  m_CurrentRun.Fill(0);
146  m_StartIndex.Fill(0);
147  m_Origin.Fill(0);
148  }
150 
153  {
154  }
155 
156 private:
158  typedef std::vector<LineType> RunsPerLineType;
159  typedef std::vector<RunsPerLineType> RunsPerLineVectorType;
160  typedef std::vector<IndexType> IndexVectorType;
161 
164  {
168  DOWN_RIGHT
169  };
171  {
173  RIGHT_END
174  };
175 
177  static bool LexicographicalLineCompare(const LineType& l1, const LineType& l2);
178 
180  inline bool IsRunIndexValid(const IndexType& index) const;
181 
183  inline IndexType Within(const IndexType& point, unsigned int line) const;
184 
186  inline IndexType LeftEnd(const IndexType& runIndex) const;
187 
189  inline IndexType RightEnd(const IndexType& runIndex) const;
190 
194  inline IndexType RightMostLeftEndInside(unsigned int line, const IndexType& point, const IndexType& run) const;
195 
199  inline IndexType LeftMostRightEndInside(unsigned int line, const IndexType& point, const IndexType& run) const;
200 
202  inline void WalkLeft(unsigned int line, const IndexType& startPoint, const IndexType& endPoint, PolygonType* polygon, const StateType state);
203 
205  inline void WalkRight(unsigned int line, const IndexType& startPoint, const IndexType& endPoint, PolygonType* polygon, const StateType state);
206 
207  // Apply origin and spacing
208  VertexType IndexToPoint(const VertexType& index) const;
209 
211 
212  // Internal structure to store runs
214 
219 
220  // The starting point for vectorization
222 
225 
228 
231 
234 
236  unsigned int m_LineOffset;
237 
238  // The following will be used for coordinate transform
242 
243 }; // end class LabelObjectToPolygonFunctor
244 
245 } // end namespace Functor
246 
247 } // end namespace otb
248 
249 #ifndef OTB_MANUAL_INSTANTIATION
251 #endif
252 
253 #endif
This class vectorizes a LabelObject to a Polygon.
IndexType LeftEnd(const IndexType &runIndex) const
Return the left-end of the run.
std::vector< IndexType > IndexVectorType
Internal enums.
std::vector< LineType > RunsPerLineType
Internal structures.
IndexType RightMostLeftEndInside(unsigned int line, const IndexType &point, const IndexType &run) const
RunsPerLineVectorType m_InternalDataSet
Internal enums.
bool IsRunIndexValid(const IndexType &index) const
Check if the given run index (index in line, line) is valid.
void WalkRight(unsigned int line, const IndexType &startPoint, const IndexType &endPoint, PolygonType *polygon, const StateType state)
Walk right to update the finite states machine.
IndexType RightEnd(const IndexType &runIndex) const
Return the right-end of the run.
PolygonType * operator()(LabelObjectType *labelObject)
IndexType LeftMostRightEndInside(unsigned int line, const IndexType &point, const IndexType &run) const
IndexType m_CurrentPoint
The current point for vectorization.
void WalkLeft(unsigned int line, const IndexType &startPoint, const IndexType &endPoint, PolygonType *polygon, const StateType state)
Walk left to update the finite states machine.
int m_CurrentLine
The current line for vectorization.
static bool LexicographicalLineCompare(const LineType &l1, const LineType &l2)
Compare two line in the lexicographical order.
LabelObjectType::ConstLineIterator ConstLineIteratorType
unsigned int m_LineOffset
The line offset from start of the region.
VertexType IndexToPoint(const VertexType &index) const
Internal enums.
IndexVectorType m_Solution
The vector of vectorized boundaries.
std::vector< RunsPerLineType > RunsPerLineVectorType
Internal enums.
IndexType m_CurrentRun
The current run for vectorization.
IndexType Within(const IndexType &point, unsigned int line) const
Check if the point lies within the range of the line.
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.