Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_
00018 #define CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_
00019
00020 #include <algorithm>
00021 #include <cmath>
00022 #include <iostream>
00023 #include <iterator>
00024
00025 #include "Eigen/Core"
00026 #include "cartographer/common/math.h"
00027 #include "cartographer/common/port.h"
00028 #include "cartographer/mapping/proto/2d/cell_limits.pb.h"
00029 #include "glog/logging.h"
00030
00031 namespace cartographer {
00032 namespace mapping {
00033
00034 struct CellLimits {
00035 CellLimits() = default;
00036 CellLimits(int init_num_x_cells, int init_num_y_cells)
00037 : num_x_cells(init_num_x_cells), num_y_cells(init_num_y_cells) {}
00038
00039 explicit CellLimits(const proto::CellLimits& cell_limits)
00040 : num_x_cells(cell_limits.num_x_cells()),
00041 num_y_cells(cell_limits.num_y_cells()) {}
00042
00043 int num_x_cells = 0;
00044 int num_y_cells = 0;
00045 };
00046
00047 inline proto::CellLimits ToProto(const CellLimits& cell_limits) {
00048 proto::CellLimits result;
00049 result.set_num_x_cells(cell_limits.num_x_cells);
00050 result.set_num_y_cells(cell_limits.num_y_cells);
00051 return result;
00052 }
00053
00054
00055 class XYIndexRangeIterator
00056 : public std::iterator<std::input_iterator_tag, Eigen::Array2i> {
00057 public:
00058
00059 XYIndexRangeIterator(const Eigen::Array2i& min_xy_index,
00060 const Eigen::Array2i& max_xy_index)
00061 : min_xy_index_(min_xy_index),
00062 max_xy_index_(max_xy_index),
00063 xy_index_(min_xy_index) {}
00064
00065
00066 explicit XYIndexRangeIterator(const CellLimits& cell_limits)
00067 : XYIndexRangeIterator(Eigen::Array2i::Zero(),
00068 Eigen::Array2i(cell_limits.num_x_cells - 1,
00069 cell_limits.num_y_cells - 1)) {}
00070
00071 XYIndexRangeIterator& operator++() {
00072
00073
00074
00075 DCHECK(*this != end());
00076 if (xy_index_.x() < max_xy_index_.x()) {
00077 ++xy_index_.x();
00078 } else {
00079 xy_index_.x() = min_xy_index_.x();
00080 ++xy_index_.y();
00081 }
00082 return *this;
00083 }
00084
00085 Eigen::Array2i& operator*() { return xy_index_; }
00086
00087 bool operator==(const XYIndexRangeIterator& other) const {
00088 return (xy_index_ == other.xy_index_).all();
00089 }
00090
00091 bool operator!=(const XYIndexRangeIterator& other) const {
00092 return !operator==(other);
00093 }
00094
00095 XYIndexRangeIterator begin() {
00096 return XYIndexRangeIterator(min_xy_index_, max_xy_index_);
00097 }
00098
00099 XYIndexRangeIterator end() {
00100 XYIndexRangeIterator it = begin();
00101 it.xy_index_ = Eigen::Array2i(min_xy_index_.x(), max_xy_index_.y() + 1);
00102 return it;
00103 }
00104
00105 private:
00106 Eigen::Array2i min_xy_index_;
00107 Eigen::Array2i max_xy_index_;
00108 Eigen::Array2i xy_index_;
00109 };
00110
00111 }
00112 }
00113
00114 #endif // CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_