24 constexpr
int kSubpixelScale = 1000;
29 void CastRay(
const Eigen::Array2i& begin,
const Eigen::Array2i& end,
30 const std::vector<uint16>& miss_table,
31 ProbabilityGrid*
const probability_grid) {
33 if (begin.x() > end.x()) {
34 CastRay(end, begin, miss_table, probability_grid);
38 CHECK_GE(begin.x(), 0);
39 CHECK_GE(begin.y(), 0);
44 if (begin.x() / kSubpixelScale == end.x() / kSubpixelScale) {
45 Eigen::Array2i current(begin.x() / kSubpixelScale,
46 std::min(begin.y(), end.y()) / kSubpixelScale);
47 const int end_y = std::max(begin.y(), end.y()) / kSubpixelScale;
48 for (; current.y() <= end_y; ++current.y()) {
49 probability_grid->ApplyLookupTable(current, miss_table);
54 const int64 dx = end.x() - begin.x();
55 const int64 dy = end.y() - begin.y();
56 const int64 denominator = 2 * kSubpixelScale * dx;
59 Eigen::Array2i current = begin / kSubpixelScale;
73 int64 sub_y = (2 * (begin.y() % kSubpixelScale) + 1) * dx;
77 const int first_pixel =
78 2 * kSubpixelScale - 2 * (begin.x() % kSubpixelScale) - 1;
80 const int last_pixel = 2 * (end.x() % kSubpixelScale) + 1;
83 const int end_x = std::max(begin.x(), end.x()) / kSubpixelScale;
86 sub_y += dy * first_pixel;
89 probability_grid->ApplyLookupTable(current, miss_table);
90 while (sub_y > denominator) {
93 probability_grid->ApplyLookupTable(current, miss_table);
96 if (sub_y == denominator) {
100 if (current.x() == end_x) {
104 sub_y += dy * 2 * kSubpixelScale;
107 sub_y += dy * last_pixel;
108 probability_grid->ApplyLookupTable(current, miss_table);
109 while (sub_y > denominator) {
110 sub_y -= denominator;
112 probability_grid->ApplyLookupTable(current, miss_table);
114 CHECK_NE(sub_y, denominator);
115 CHECK_EQ(current.y(), end.y() / kSubpixelScale);
121 probability_grid->ApplyLookupTable(current, miss_table);
123 sub_y += denominator;
125 probability_grid->ApplyLookupTable(current, miss_table);
129 sub_y += denominator;
132 if (current.x() == end_x) {
135 sub_y += dy * 2 * kSubpixelScale;
137 sub_y += dy * last_pixel;
138 probability_grid->ApplyLookupTable(current, miss_table);
140 sub_y += denominator;
142 probability_grid->ApplyLookupTable(current, miss_table);
145 CHECK_EQ(current.y(), end.y() / kSubpixelScale);
148 void GrowAsNeeded(
const sensor::RangeData& range_data,
149 ProbabilityGrid*
const probability_grid) {
150 Eigen::AlignedBox2f bounding_box(range_data.origin.head<2>());
151 constexpr
float kPadding = 1e-6f;
152 for (
const Eigen::Vector3f& hit : range_data.returns) {
153 bounding_box.extend(hit.head<2>());
155 for (
const Eigen::Vector3f& miss : range_data.misses) {
156 bounding_box.extend(miss.head<2>());
158 probability_grid->GrowLimits(bounding_box.min() -
159 kPadding * Eigen::Vector2f::Ones());
160 probability_grid->GrowLimits(bounding_box.max() +
161 kPadding * Eigen::Vector2f::Ones());
167 const std::vector<uint16>& hit_table,
168 const std::vector<uint16>& miss_table,
169 const bool insert_free_space,
171 GrowAsNeeded(range_data, probability_grid);
174 const double superscaled_resolution = limits.
resolution() / kSubpixelScale;
176 superscaled_resolution, limits.
max(),
179 const Eigen::Array2i begin =
182 std::vector<Eigen::Array2i> ends;
183 ends.reserve(range_data.
returns.size());
184 for (
const Eigen::Vector3f& hit : range_data.
returns) {
185 ends.push_back(superscaled_limits.
GetCellIndex(hit.head<2>()));
186 probability_grid->
ApplyLookupTable(ends.back() / kSubpixelScale, hit_table);
189 if (!insert_free_space) {
194 for (
const Eigen::Array2i& end : ends) {
195 CastRay(begin, end, miss_table, probability_grid);
199 for (
const Eigen::Vector3f& missing_echo : range_data.
misses) {
200 CastRay(begin, superscaled_limits.
GetCellIndex(missing_echo.head<2>()),
201 miss_table, probability_grid);
const Eigen::Vector2d & max() const
void CastRays(const sensor::RangeData &range_data, const std::vector< uint16 > &hit_table, const std::vector< uint16 > &miss_table, const bool insert_free_space, ProbabilityGrid *const probability_grid)
double resolution() const
Eigen::Array2i GetCellIndex(const Eigen::Vector2f &point) const
const MapLimits & limits() const
bool ApplyLookupTable(const Eigen::Array2i &cell_index, const std::vector< uint16 > &table)
const CellLimits & cell_limits() const