VIPRA Documentation
Loading...
Searching...
No Matches
density_grid.hpp
1#pragma once
2
3#include <array>
4#include <limits>
5#include <queue>
6
7#include "vipra/geometry/circle.hpp"
8#include "vipra/geometry/f3d.hpp"
9
10#include "vipra/types/float.hpp"
11#include "vipra/types/idx.hpp"
12#include "vipra/types/size.hpp"
13
14#include "vipra/logging/logging.hpp"
15
16// TODO: Generalize Grid and inherit Grid for this class.
17
18namespace VIPRA::Goals {
19
20class DensityGrid {
21 public:
22 struct GridPoint {
23 VIPRA::f3d direction;
24 VIPRA::f3d end{VIPRA::_emptyf3d_};
25 VIPRA::f_pnt distance{std::numeric_limits<VIPRA::f_pnt>::max()};
26 int pedCount{0};
27 };
28
29 void intialize(auto const& map, VIPRA::f_pnt gridSize)
30 {
31 _gridSize = gridSize;
32 construct_grid(map);
33 }
34
35 void clear_grid()
36 {
37 for ( GridPoint& gridPoint : _grid ) {
38 gridPoint.pedCount = 0;
39 }
40 }
41
42 [[nodiscard]] auto get_grid(VIPRA::f3d pos) -> GridPoint&
43 {
44 return _grid[get_closest_grid_idx(pos)];
45 }
46 [[nodiscard]] auto get_grid(VIPRA::f3d pos) const -> GridPoint const&
47 {
48 return _grid[get_closest_grid_idx(pos)];
49 }
50
51 [[nodiscard]] auto get_x_count() const -> size_t { return _xCount; }
52 [[nodiscard]] auto get_y_count() const -> size_t { return _xCount; }
53
54 [[nodiscard]] auto get_ped_count_at_idx(VIPRA::idx idx) const -> int
55 {
56 return _grid[idx].pedCount;
57 }
58
59 [[nodiscard]] auto begin() -> std::vector<GridPoint>::iterator { return _grid.begin(); }
60 [[nodiscard]] auto end() -> std::vector<GridPoint>::iterator { return _grid.end(); }
61
68 void incr_gridpoint(VIPRA::f3d const& pos)
69 {
70 const auto currentPedcount = _grid[get_closest_grid_idx(pos)].pedCount;
71 _grid[get_closest_grid_idx(pos)].pedCount++;
72 }
73
80 [[nodiscard]] auto get_closest_grid_idx(VIPRA::f3d pos) const -> VIPRA::idx
81 {
82 auto gridX = static_cast<VIPRA::idx>(std::floor(pos.x / _gridSize));
83 auto gridY = static_cast<VIPRA::idx>(std::floor(pos.y / _gridSize));
84
85 auto const idx = get_index(gridX, gridY, _xCount);
86
87 if ( out_of_bounds(gridX, gridY) ) {
88 VIPRA::Log::error("Grid index is out of bounds Pos: ({}, {})", pos.x, pos.y);
89 throw std::runtime_error("Grid index is out of bounds");
90 }
91
92 return idx;
93 }
94
95 [[nodiscard]] auto grid_center(VIPRA::f3d pos) const -> VIPRA::f3d
96 {
97 auto gridX = static_cast<VIPRA::idx>(std::floor(pos.x / _gridSize));
98 auto gridY = static_cast<VIPRA::idx>(std::floor(pos.y / _gridSize));
99
100 return VIPRA::f3d{_gridSize * static_cast<VIPRA::f_pnt>(gridX),
101 _gridSize * static_cast<VIPRA::f_pnt>(gridY)};
102 }
103
104 private:
105 VIPRA::size _xCount{};
106 VIPRA::size _yCount{};
107 VIPRA::f_pnt _gridSize{};
108
109 std::vector<GridPoint> _grid;
110
116 void set_grid_counts(auto const& map)
117 {
118 const VIPRA::f3d dimensions = map.get_dimensions();
119
120 assert(dimensions.x > 0 && dimensions.y > 0);
121 assert(_gridSize > 0);
122
123 _xCount = static_cast<VIPRA::idx>(std::ceil(dimensions.x / _gridSize) + 1);
124 _yCount = static_cast<VIPRA::idx>(std::ceil(dimensions.y / _gridSize) + 1);
125 }
126
135 [[nodiscard]] static auto get_index(VIPRA::size gridX, VIPRA::size gridY,
136 VIPRA::size xCount) noexcept -> VIPRA::idx
137 {
138 return gridX + (gridY * xCount);
139 }
140
146 void construct_grid(auto const& map)
147 {
148 set_grid_counts(map);
149
150 assert(_xCount > 0 && _yCount > 0);
151
152 _grid = std::vector<GridPoint>(_xCount * _yCount);
153 }
154
155 [[nodiscard]] VIPRA_INLINE auto out_of_bounds(VIPRA::f_pnt gridX,
156 VIPRA::f_pnt gridY) const -> bool
157 {
158 return gridX < 0 || gridX >= static_cast<VIPRA::f_pnt>(_xCount) * _gridSize ||
159 gridY < 0 || gridY >= static_cast<VIPRA::f_pnt>(_yCount) * _gridSize;
160 }
161
162 [[nodiscard]] VIPRA_INLINE auto out_of_bounds(size_t gridX, size_t gridY) const -> bool
163 {
164 return gridX < 0 || gridX >= _xCount || gridY < 0 || gridY >= _yCount;
165 }
166
167 public:
168 DensityGrid() = default;
169 DensityGrid(const DensityGrid&) = default;
170 DensityGrid(DensityGrid&&) noexcept = default;
171 auto operator=(const DensityGrid&) -> DensityGrid& = default;
172 auto operator=(DensityGrid&&) noexcept -> DensityGrid& = default;
173 ~DensityGrid() = default;
174};
175} // namespace VIPRA::Goals
void incr_gridpoint(VIPRA::f3d const &pos)
Increments the pedestrian count at the grid index that contains the point.
Definition density_grid.hpp:68
auto get_closest_grid_idx(VIPRA::f3d pos) const -> VIPRA::idx
Gets the closest grid index to the coordinate.
Definition density_grid.hpp:80
static VIPRA_INLINE void error(fmt::format_string< param_ts... > message, param_ts &&... params)
Calls the provided Logger with Level DEBUG.
Definition logging.hpp:102
Definition density_grid.hpp:22
Definition pathing_graph.hpp:14
Definition f3d.hpp:27