xy_index.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2016 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
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 // Iterates in row-major order through a range of xy-indices.
00055 class XYIndexRangeIterator
00056     : public std::iterator<std::input_iterator_tag, Eigen::Array2i> {
00057  public:
00058   // Constructs a new iterator for the specified range.
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   // Constructs a new iterator for everything contained in 'cell_limits'.
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     // This is a necessary evil. Bounds checking is very expensive and needs to
00073     // be avoided in production. We have unit tests that exercise this check
00074     // in debug mode.
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 }  // namespace mapping
00112 }  // namespace cartographer
00113 
00114 #endif  // CARTOGRAPHER_MAPPING_2D_XY_INDEX_H_


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:36