PICCANTE  0.4
The hottest HDR imaging library!
polyline.hpp
Go to the documentation of this file.
1 /*
2 
3 PICCANTE
4 The hottest HDR imaging library!
5 http://vcg.isti.cnr.it/piccante
6 
7 Copyright (C) 2014
8 Visual Computing Laboratory - ISTI CNR
9 http://vcg.isti.cnr.it
10 First author: Francesco Banterle
11 
12 This Source Code Form is subject to the terms of the Mozilla Public
13 License, v. 2.0. If a copy of the MPL was not distributed with this
14 file, You can obtain one at http://mozilla.org/MPL/2.0/.
15 
16 */
17 
18 #ifndef PIC_UTIL_POLYLINE_HPP
19 #define PIC_UTIL_POLYLINE_HPP
20 
21 #include <vector>
22 
23 #include "../base.hpp"
24 
25 #include "../util/vec.hpp"
26 
27 #ifndef PIC_DISABLE_EIGEN
28 #ifndef PIC_EIGEN_NOT_BUNDLED
29  #include "../externals/Eigen/Dense"
30 #else
31  #include <Eigen/Dense>
32 #endif
33 #endif
34 
35 namespace pic {
36 
40 template<unsigned int N, class T>
41 class Polyline
42 {
43 public:
44 
45  std::vector< Vec<N, T> > points;
46 
48  {
49  points.clear();
50  }
51 
52  Polyline(std::vector< Vec<N, T> > &points)
53  {
54  this->points.assign(points.begin(), points.end());
55  }
56 
61  void add(Vec<N, T> &point)
62  {
63  points.push_back(point);
64  }
65 
70  void simplify(int target_n_points)
71  {
72  int n = int(points.size());
73 
74  if(n <= target_n_points) {
75  return;
76  }
77 
78  while(n > target_n_points) {
79  float area_min = FLT_MAX;
80  int index = -1;
81  float a, b, c, area_tmp;
82  for(int i = 0; i < (n - 2); i++) {
83 
84  auto p0 = points.at(i );
85  auto p1 = points.at(i + 1);
86  auto p2 = points.at(i + 2);
87 
88  a = sqrtf(float(p0.distanceSq(p1)));
89  b = sqrtf(float(p1.distanceSq(p2)));
90  c = sqrtf(float(p2.distanceSq(p0)));
91 
92  float s = (a + b + c) / 2.0f ;
93 
94  float area_sq = s * (s - a) * (s - b) * (s - c);
95  area_tmp = sqrtf(area_sq);
96 
97  if(area_tmp < area_min) {
98  area_min = area_tmp;
99  index = i;
100  }
101  }
102 
103  if(index > -1) {
104  points.erase(points.begin() + index + 1);
105  } else {
106 
107  break;
108  }
109 
110  n = int(points.size());
111  }
112  }
113 };
114 
119 
120 #ifndef PIC_DISABLE_EIGEN
121 
126 PIC_INLINE void convertFromEigenToPolyLine(std::vector< Eigen::Vector2i > &in, Polyline2i &out)
127 {
128  for(unsigned int i = 0; i < in.size(); i++) {
129  auto tmp = Vec2i(in[i][0], in[i][1]);
130  out.add(tmp);
131  }
132 }
133 #endif
134 
135 } // end namespace pic
136 
137 #endif //PIC_UTIL_POLYLINE_HPP
Polyline< 2, int > Polyline2i
Polyline2i.
Definition: polyline.hpp:118
void add(Vec< N, T > &point)
add
Definition: polyline.hpp:61
std::vector< Vec< N, T > > points
Definition: polyline.hpp:45
Polyline()
Definition: polyline.hpp:47
Vec< 2, int > Vec2i
Vec2i.
Definition: vec.hpp:829
#define PIC_INLINE
Definition: base.hpp:33
void simplify(int target_n_points)
simplify
Definition: polyline.hpp:70
PIC_INLINE void convertFromEigenToPolyLine(std::vector< Eigen::Vector2i > &in, Polyline2i &out)
convertFromEigenToPolyLine
Definition: polyline.hpp:126
Definition: bilateral_separation.hpp:25
Polyline(std::vector< Vec< N, T > > &points)
Definition: polyline.hpp:52
The Vec class.
Definition: vec.hpp:35
The Polyline class.
Definition: polyline.hpp:41