20 namespace mapping_2d {
25 constexpr
int kSubpixelScale = 1000;
30 void CastRay(
const Eigen::Array2i& begin,
const Eigen::Array2i& end,
31 const std::function<
void(
const Eigen::Array2i&)>& visitor) {
33 if (begin.x() > end.x()) {
34 CastRay(end, begin, visitor);
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()) {
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;
90 while (sub_y > denominator) {
96 if (sub_y == denominator) {
100 if (current.x() == end_x) {
104 sub_y += dy * 2 * kSubpixelScale;
107 sub_y += dy * last_pixel;
109 while (sub_y > denominator) {
110 sub_y -= denominator;
114 CHECK_NE(sub_y, denominator);
115 CHECK_EQ(current.y(), end.y() / kSubpixelScale);
123 sub_y += denominator;
129 sub_y += denominator;
132 if (current.x() == end_x) {
135 sub_y += dy * 2 * kSubpixelScale;
137 sub_y += dy * last_pixel;
140 sub_y += denominator;
145 CHECK_EQ(current.y(), end.y() / kSubpixelScale);
151 const std::function<
void(
const Eigen::Array2i&)>& hit_visitor,
152 const std::function<
void(
const Eigen::Array2i&)>& miss_visitor) {
153 const double superscaled_resolution = limits.
resolution() / kSubpixelScale;
155 superscaled_resolution, limits.
max(),
158 const Eigen::Array2i begin =
163 std::vector<Eigen::Array2i> ends;
164 ends.reserve(range_data.
returns.size());
165 for (
const Eigen::Vector3f&
hit : range_data.
returns) {
168 hit_visitor(ends.back() / kSubpixelScale);
172 for (
const Eigen::Array2i& end : ends) {
173 CastRay(begin, end, miss_visitor);
177 for (
const Eigen::Vector3f& missing_echo : range_data.
misses) {
180 missing_echo.x(), missing_echo.y()),
const Eigen::Vector2d & max() const
Eigen::Array2i GetXYIndexOfCellContainingPoint(const double x, const double y) const
double resolution() const
const CellLimits & cell_limits() const
void CastRays(const sensor::RangeData &range_data, const MapLimits &limits, const std::function< void(const Eigen::Array2i &)> &hit_visitor, const std::function< void(const Eigen::Array2i &)> &miss_visitor)