1 #ifndef SLAM_CTOR_CORE_REGULAR_SQUARES_GRID_H 2 #define SLAM_CTOR_CORE_REGULAR_SQUARES_GRID_H 10 #include "../math_utils.h" 11 #include "../geometry_utils.h" 16 static constexpr
double Dbl_Inf = std::numeric_limits<double>::infinity();
42 assert(x != Dbl_Inf && y != Dbl_Inf);
44 auto curr_scale =
scale();
45 return {int(std::floor(x / curr_scale)), int(std::floor(y / curr_scale))};
50 assert(x != Dbl_Inf && y != Dbl_Inf);
52 return {int(std::floor(x / scale)), int(std::floor(y / scale))};
61 int inc_x = 0 < d_x ? 1 : -1, inc_y = 0 < d_y ? 1 : -1;
65 size_t cells_nm = std::abs(end.x - pnt.
x) + std::abs(end.y - pnt.
y) + 1;
66 std::vector<Coord> cells;
67 cells.reserve(cells_nm);
69 double m_per_cell =
scale();
70 Point2D mid_cell{(pnt.
x + 0.5) * m_per_cell, (pnt.
y + 0.5) * m_per_cell};
72 auto mid_cell_seg_y = d_x * s.
beg().
y + (mid_cell.x - s.
beg().
x) * d_y;
73 auto e = mid_cell_seg_y - mid_cell.y * d_x;
74 auto e_x_inc = inc_x * m_per_cell * d_y;
75 auto e_y_inc = -inc_y * m_per_cell * d_x;
79 if (pnt == end) {
break; }
80 if (cells_nm < cells.size()) {
84 double e_x = e + e_x_inc, e_y = e + e_y_inc;
85 double abs_err_diff = std::abs(e_y) - std::abs(e_x);
88 if (pnt.
x == end.x) { pnt.
y += inc_y; }
89 else if (pnt.
y == end.y) { pnt.
x += inc_x; }
90 else { pnt.
x += inc_x; pnt.
y += inc_y; }
92 }
else if (0 < abs_err_diff) {
104 return {
scale() * (cell.
x + 0.5),
scale() * (cell.
y + 0.5)};
109 auto m_per_cell =
scale();
110 if (m_per_cell == Dbl_Inf) {
114 return {m_per_cell * coord.
y,
115 m_per_cell * (coord.
y + 1),
116 m_per_cell * coord.
x,
117 m_per_cell * (coord.
x + 1)};
Coord external2internal(const Coord &coord) const
static constexpr double Dbl_Inf
Coord world_to_cell(const Point2D &pt) const
Rectangle world_cell_bounds(const Coord &coord) const
CONSTEXPR bool are_equal(const T &a, const T &b, const T &eps)
Point2D cell_to_world(const Coord &cell) const
virtual bool has_cell(const Coord &c) const
void set_width(unsigned w)
virtual DiscretePoint2D origin() const
bool has_internal_cell(const Coord &c) const
virtual ~RegularSquaresGrid()=default
Coord world_to_cell(double x, double y, double scale) const
const Point2D & end() const
void set_height(unsigned h)
RegularSquaresGrid & operator=(const RegularSquaresGrid &grid)=default
Coord internal2external(const Coord &coord) const
std::vector< Coord > world_to_cells(const Segment2D &s) const
virtual int height() const
const Point2D & beg() const
Coord world_to_cell(double x, double y) const
virtual void rescale(double)
RegularSquaresGrid(int w, int h, double scale)
virtual double scale() const
virtual int width() const