MRPT  1.9.9
polygons_utils.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/math/geometry.h> // distance()
13 
14 namespace mrpt::math
15 {
16 /** Auxiliary functor class to compute polygon's center */
17 template <class T, int N>
18 class FAddPoint
19 {
20  public:
21  T& object;
22  FAddPoint(T& o) : object(o)
23  {
24  for (size_t i = 0; i < N; i++) object[i] = 0.0;
25  }
26  void operator()(const T& o)
27  {
28  for (size_t i = 0; i < N; i++) object[i] += o[i];
29  }
30 };
31 
32 template <class T>
33 inline void removeUnusedVertices(T& poly)
34 {
35  size_t N = poly.size();
36  if (N < 3) return;
37  std::vector<size_t> unused;
38  if (std::abs(
39  mrpt::math::distance(poly[N - 1], poly[0]) +
40  mrpt::math::distance(poly[0], poly[1]) -
41  mrpt::math::distance(poly[N - 1], poly[1])) <
43  unused.push_back(0);
44  for (size_t i = 1; i < N - 1; i++)
45  if (std::abs(
46  mrpt::math::distance(poly[i - 1], poly[i]) +
47  mrpt::math::distance(poly[i], poly[i + 1]) -
48  mrpt::math::distance(poly[i - 1], poly[i + 1])) <
50  unused.push_back(i);
51  if (std::abs(
52  mrpt::math::distance(poly[N - 2], poly[N - 1]) +
53  mrpt::math::distance(poly[N - 1], poly[0]) -
54  mrpt::math::distance(poly[N - 2], poly[0])) <
56  unused.push_back(N - 1);
57  unused.push_back(N);
58  size_t diff = 1;
59  for (size_t i = 0; i < unused.size() - 1; i++)
60  {
61  size_t last = unused[i + 1];
62  for (size_t j = unused[i] + 1 - diff; j < last - diff; j++)
63  poly[j] = poly[j + diff];
64  }
65  poly.resize(N + 1 - unused.size());
66 }
67 template <class T>
68 inline void removeRepVertices(T& poly)
69 {
70  size_t N = poly.size();
71  if (N < 3) return;
72  std::vector<size_t> rep;
73  for (size_t i = 0; i < N - 1; i++)
74  if (mrpt::math::distance(poly[i], poly[i + 1]) < getEpsilon())
75  rep.push_back(i);
76  if (mrpt::math::distance(poly[N - 1], poly[0]) < getEpsilon())
77  rep.push_back(N - 1);
78  rep.push_back(N);
79  size_t diff = 1;
80  for (size_t i = 0; i < rep.size() - 1; i++)
81  {
82  size_t last = rep[i + 1];
83  for (size_t j = rep[i] + 1 - diff; j < last - diff; j++)
84  poly[j] = poly[j + diff];
85  }
86  poly.resize(N + 1 - rep.size());
87 }
88 
89 } // namespace mrpt::math
Auxiliary functor class to compute polygon&#39;s center.
void removeRepVertices(T &poly)
This base provides a set of functions for maths stuff.
void removeUnusedVertices(T &poly)
double getEpsilon()
Gets the value of the geometric epsilon (default = 1e-5)
Definition: geometry.cpp:34
void operator()(const T &o)
double distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
Definition: geometry.cpp:1807



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020