xy_index.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_
18 #define CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_
19 
20 #include <algorithm>
21 #include <cmath>
22 #include <iostream>
23 #include <iterator>
24 
25 #include "Eigen/Core"
28 #include "cartographer/mapping/proto/2d/cell_limits.pb.h"
29 #include "glog/logging.h"
30 
31 namespace cartographer {
32 namespace mapping {
33 
34 struct CellLimits {
35  CellLimits() = default;
36  CellLimits(int init_num_x_cells, int init_num_y_cells)
37  : num_x_cells(init_num_x_cells), num_y_cells(init_num_y_cells) {}
38 
39  explicit CellLimits(const proto::CellLimits& cell_limits)
40  : num_x_cells(cell_limits.num_x_cells()),
41  num_y_cells(cell_limits.num_y_cells()) {}
42 
43  int num_x_cells = 0;
44  int num_y_cells = 0;
45 };
46 
47 inline proto::CellLimits ToProto(const CellLimits& cell_limits) {
48  proto::CellLimits result;
49  result.set_num_x_cells(cell_limits.num_x_cells);
50  result.set_num_y_cells(cell_limits.num_y_cells);
51  return result;
52 }
53 
54 // Iterates in row-major order through a range of xy-indices.
56  : public std::iterator<std::input_iterator_tag, Eigen::Array2i> {
57  public:
58  // Constructs a new iterator for the specified range.
59  XYIndexRangeIterator(const Eigen::Array2i& min_xy_index,
60  const Eigen::Array2i& max_xy_index)
61  : min_xy_index_(min_xy_index),
62  max_xy_index_(max_xy_index),
63  xy_index_(min_xy_index) {}
64 
65  // Constructs a new iterator for everything contained in 'cell_limits'.
66  explicit XYIndexRangeIterator(const CellLimits& cell_limits)
67  : XYIndexRangeIterator(Eigen::Array2i::Zero(),
68  Eigen::Array2i(cell_limits.num_x_cells - 1,
69  cell_limits.num_y_cells - 1)) {}
70 
72  // This is a necessary evil. Bounds checking is very expensive and needs to
73  // be avoided in production. We have unit tests that exercise this check
74  // in debug mode.
75  DCHECK(*this != end());
76  if (xy_index_.x() < max_xy_index_.x()) {
77  ++xy_index_.x();
78  } else {
79  xy_index_.x() = min_xy_index_.x();
80  ++xy_index_.y();
81  }
82  return *this;
83  }
84 
85  Eigen::Array2i& operator*() { return xy_index_; }
86 
87  bool operator==(const XYIndexRangeIterator& other) const {
88  return (xy_index_ == other.xy_index_).all();
89  }
90 
91  bool operator!=(const XYIndexRangeIterator& other) const {
92  return !operator==(other);
93  }
94 
96  return XYIndexRangeIterator(min_xy_index_, max_xy_index_);
97  }
98 
100  XYIndexRangeIterator it = begin();
101  it.xy_index_ = Eigen::Array2i(min_xy_index_.x(), max_xy_index_.y() + 1);
102  return it;
103  }
104 
105  private:
106  Eigen::Array2i min_xy_index_;
107  Eigen::Array2i max_xy_index_;
108  Eigen::Array2i xy_index_;
109 };
110 
111 } // namespace mapping
112 } // namespace cartographer
113 
114 #endif // CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_
bool operator!=(const XYIndexRangeIterator &other) const
Definition: xy_index.h:91
bool operator==(const XYIndexRangeIterator &other) const
Definition: xy_index.h:87
CellLimits(int init_num_x_cells, int init_num_y_cells)
Definition: xy_index.h:36
XYIndexRangeIterator & operator++()
Definition: xy_index.h:71
CellLimits(const proto::CellLimits &cell_limits)
Definition: xy_index.h:39
XYIndexRangeIterator(const Eigen::Array2i &min_xy_index, const Eigen::Array2i &max_xy_index)
Definition: xy_index.h:59
XYIndexRangeIterator(const CellLimits &cell_limits)
Definition: xy_index.h:66
proto::MapLimits ToProto(const MapLimits &map_limits)
Definition: map_limits.h:92


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:59