geometry_utils.h
Go to the documentation of this file.
1 
7 #ifndef __GEOMETRY_UTILS_H
8 #define __GEOMETRY_UTILS_H
9 
10 #include <vector>
11 #include <cmath>
12 
13 
14 #define EQ_DOUBLE(a, b) \
15  (std::abs((a) - (b)) < 0.0001)
16 
20 struct Rectangle {
21 
23  Rectangle() : Rectangle(0, 0, 0, 0) {}
24 
33  Rectangle(double b, double t, double l, double r) :
34  bot(b), top(t), left(l), right(r) {}
35 
41  bool does_contain(double x, double y) const {
42  return ((bot < y) && (y < top)) && ((left < x) && (x < right));
43  }
44 
49  double area() const {
50  return (top - bot)*(right - left);
51  }
52 
53  double bot,
54  top,
55  left,
56  right;
57 };
58 
63 public:
64 
69  DiscretePoint2D(int x_coord, int y_coord):
70  x{x_coord}, y{y_coord} {}
71  // TODO: mv (!!), cpy ctors
72  int x, y;
73 
76  return DiscretePoint2D(x + p.x, y + p.y);
77  }
78 
81  return DiscretePoint2D(-x, -y);
82  }
83 
89  double dist_sq(const DiscretePoint2D &pt) const {
90  return std::pow(x - pt.x, 2) + std::pow(y - pt.y, 2);
91  }
92 };
93 
99 public: // methods
100 
106  DiscreteLine2D(const Point &start, const Point &end) {
107  generatePointsWithBresenham(start.x, start.y, end.x, end.y);
108  }
110  const std::vector<Point>& points() const { return _points; }
111 private:
112 
121  void generatePointsWithBresenham(int x1, int y1, int x2, int y2) {
122  // TODO: copypasted from
123  // http://www.roguebasin.com/index.php?title=Bresenham%27s_Line_Algorithm
124  // review and simplification are required
125 
126  int delta_x(x2 - x1);
127  // if x1 == x2, then it does not matter what we set here
128  signed char const ix((delta_x > 0) - (delta_x < 0));
129  delta_x = std::abs(delta_x) * 2;
130 
131  int delta_y(y2 - y1);
132  // if y1 == y2, then it does not matter what we set here
133  signed char const iy((delta_y > 0) - (delta_y < 0));
134  delta_y = std::abs(delta_y) * 2;
135 
136  _points.push_back(Point(x1, y1));
137 
138  if (delta_x >= delta_y) {
139  // error may go below zero
140  int error(delta_y - (delta_x >> 1));
141  while (x1 != x2) {
142  if ((0 <= error) && (error || (0 < ix))) {
143  error -= delta_x;
144  y1 += iy;
145  }
146  // else do nothing
147  error += delta_y;
148  x1 += ix;
149  _points.push_back(Point(x1, y1));
150  }
151  }
152  else {
153  // error may go below zero
154  int error(delta_x - (delta_y >> 1));
155 
156  while (y1 != y2) {
157  if ((0 <= error) && (error || (0 < iy))) {
158  error -= delta_y;
159  x1 += ix;
160  }
161  // else do nothing
162  error += delta_x;
163  y1 += iy;
164  _points.push_back(Point(x1, y1));
165  }
166  }
167  }
168 private: // fields
169  std::vector<Point> _points;
170 };
171 
172 #endif
double top
The top of a rectangle.
double bot
The bottom of a rectangle.
double right
The right side of a rectangle.
Rectangle(double b, double t, double l, double r)
void generatePointsWithBresenham(int x1, int y1, int x2, int y2)
DiscretePoint2D operator+(const DiscretePoint2D &p) const
Operator of points summation by element-wise addition.
TFSIMD_FORCE_INLINE const tfScalar & y() const
double dist_sq(const DiscretePoint2D &pt) const
double area() const
DiscretePoint2D operator-() const
Returns additive inverse of the point.
int y
Coordinates of point.
bool does_contain(double x, double y) const
const std::vector< Point > & points() const
Returns the line&#39;s component points.
double left
The left side of a rectangle.
TFSIMD_FORCE_INLINE const tfScalar & x() const
Defines an axis-aligned rectangle.
Rectangle()
Creates a rectangle with zero area in point (0,0).
DiscretePoint2D(int x_coord, int y_coord)
Defines a line segment on a plane.
std::vector< Point > _points
DiscreteLine2D(const Point &start, const Point &end)
Defines a point with integer coordinates on a plane.


tiny_slam
Author(s):
autogenerated on Mon Jun 10 2019 15:30:57