geometry_discrete_primitives.h
Go to the documentation of this file.
1 #ifndef SLAM_CTOR_CORE_GEOMETRY_DISCRETE_PRIMITIVES_H
2 #define SLAM_CTOR_CORE_GEOMETRY_DISCRETE_PRIMITIVES_H
3 
4 #include <cmath>
5 #include <vector>
6 #include <utility>
7 #include <ostream>
8 #include <tuple>
9 
11 public: // fields
12  int x, y;
13 public: // functions
14  constexpr DiscretePoint2D(int x_coord = 0, int y_coord = 0)
15  : x{x_coord}, y{y_coord} {}
16 
18  x += p.x;
19  y += p.y;
20  return *this;
21  }
22 
24  return DiscretePoint2D{*this} += p;
25  }
26 
28  return {x - p.x, y - p.y};
29  }
30 
31  bool operator==(const DiscretePoint2D &p) const {
32  return x == p.x && y == p.y;
33  }
34 
35  bool operator!=(const DiscretePoint2D &p) const {
36  return !(*this == p);
37  }
38 
40  return {-x, -y};
41  }
42 
43  double dist_sq(const DiscretePoint2D &pt) const {
44  return std::pow(x - pt.x, 2) + std::pow(y - pt.y, 2);
45  }
46 };
47 
48 inline std::ostream &operator<<(std::ostream &stream,
49  const DiscretePoint2D &pnt) {
50  return stream << "(" << pnt.x << ", " << pnt.y << ")";
51 }
52 
53 //------------------
54 
57 public: // methods
58  DiscreteSegment2D(const DPoint &beg, const DPoint &end) {
59  // pricise pts nm: max(abs(d_x), abs(d_y)) + 1
60  _points.reserve(std::fabs(beg.x - end.x) + std::fabs(beg.y - end.y) + 1);
61  gen_points_with_bresenham(beg, end);
62  }
63  operator auto() const { return _points; } // to vector of discrete points
64 
65 private: // methods
66 
67  void gen_points_with_bresenham(const DPoint &beg, const DPoint &end) {
68  // 1. Setup vars according to line direction (d_x, d_y)
69  DPoint delta = end - beg;
70  bool y_is_primary = std::abs(delta.x) < std::abs(delta.y);
71 
72  int limit, primary, d_primary, secondary, d_secondary;
73  std::tie(limit, primary, d_primary, secondary, d_secondary) = y_is_primary ?
74  std::make_tuple(end.y, beg.y, delta.y, beg.x, delta.x) :
75  std::make_tuple(end.x, beg.x, delta.x, beg.y, delta.y);
76 
77  int inc_primary = 0 < d_primary ? 1 : -1;
78  int inc_secondary = 0 < d_secondary ? 1 : -1;
79  int *x = nullptr, *y = nullptr;
80  std::tie(x, y) = y_is_primary ? std::make_tuple(&secondary, &primary) :
81  std::make_tuple(&primary, &secondary);
82 
83  // 2. Generate points. Driver: pnt = err_min(next_pnt, next_diag_pnt)
84  int error = 0; // e = actual e * d_primary
85  while (1) {
86  _points.emplace_back(*x, *y);
87  if (primary == limit) { break; }
88 
89  int err_inc_primary = error + inc_primary * d_secondary; // e' = e + slope
90  int err_inc_both = err_inc_primary - inc_secondary * d_primary; // e' - 1
91 
92  primary += inc_primary;
93  if (std::abs(err_inc_primary) < std::abs(err_inc_both)) {
94  error = err_inc_primary;
95  } else {
96  secondary += inc_secondary;
97  error = err_inc_both;
98  }
99  }
100  }
101 
102 private: // fields
103  std::vector<DPoint> _points;
104 };
105 
106 #endif
DiscretePoint2D operator+(const DiscretePoint2D &p) const
double dist_sq(const DiscretePoint2D &pt) const
void gen_points_with_bresenham(const DPoint &beg, const DPoint &end)
DiscretePoint2D operator-() const
DiscretePoint2D operator-(const DiscretePoint2D &p) const
std::ostream & operator<<(std::ostream &stream, const DiscretePoint2D &pnt)
DiscreteSegment2D(const DPoint &beg, const DPoint &end)
constexpr DiscretePoint2D(int x_coord=0, int y_coord=0)
DiscretePoint2D & operator+=(const DiscretePoint2D &p)
std::vector< DPoint > _points
bool operator!=(const DiscretePoint2D &p) const
bool operator==(const DiscretePoint2D &p) const


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25