13 #include "../include/cost_map_core/submap_geometry.hpp" 15 #include <Eigen/Dense> 30 data_.insert(std::pair<std::string, Matrix>(layer,
Matrix()));
46 assert(length(0) > 0.0);
47 assert(length(1) > 0.0);
48 assert(resolution > 0.0);
51 size(0) =
static_cast<int>(round(length(0) / resolution));
52 size(1) =
static_cast<int>(round(length(1) / resolution));
81 for (
const auto& layer :
layers_) {
82 if (!other.
exists(layer))
return false;
94 assert(
size_(0) == data.rows());
95 assert(
size_(1) == data.cols());
99 data_.at(layer) = data;
102 data_.insert(std::pair<std::string, Matrix>(layer, data));
115 return data_.at(layer);
116 }
catch (
const std::out_of_range& exception) {
117 throw std::out_of_range(
"CostMap::get(...) : No map layer '" + layer +
"' available.");
124 return data_.at(layer);
125 }
catch (
const std::out_of_range& exception) {
126 throw std::out_of_range(
"CostMap::get(...) : No map layer of type '" + layer +
"' available.");
142 const auto dataIterator =
data_.find(layer);
143 if (dataIterator ==
data_.end())
return false;
144 data_.erase(dataIterator);
146 const auto layerIterator = std::find(
layers_.begin(),
layers_.end(), layer);
147 if (layerIterator ==
layers_.end())
return false;
163 Eigen::Array2i index;
165 return at(layer, index);
167 throw std::out_of_range(
"CostMap::atPosition(...) : Position is out of range.");
187 return at(layer, index);
189 throw std::out_of_range(
"CostMap::atPosition(...) : position is out of range.");
193 throw std::runtime_error(
"CostMap::atPosition(...) : specified interpolation method not implemented.");
199 return data_.at(layer)(index(0), index(1));
200 }
catch (
const std::out_of_range& exception) {
201 throw std::out_of_range(
"CostMap::at(...) : No map layer '" + layer +
"' available.");
208 return data_.at(layer)(index(0), index(1));
209 }
catch (
const std::out_of_range& exception) {
210 throw std::out_of_range(
"CostMap::at(...) : No map layer '" + layer +
"' available.");
241 if (layers.empty())
return false;
242 for (
auto& layer : layers) {
251 if (!
isValid(index, layer))
return false;
254 position.head(2) = position2d;
255 position.z() =
at(layer, index);
260 Eigen::Vector3d& vector)
const 262 std::vector<std::string> layers;
263 layers.push_back(layerPrefix +
"x");
264 layers.push_back(layerPrefix +
"y");
265 layers.push_back(layerPrefix +
"z");
266 if (!
isValid(index, layers))
return false;
267 for (
size_t i = 0; i < 3; ++i) {
268 vector(i) =
at(layers[i], index);
274 bool& isSuccess)
const 277 return getSubmap(position, length, index, isSuccess);
290 SubmapGeometry submapInformation(*
this, position, length, isSuccess);
296 std::vector<BufferRegion> bufferRegions;
300 std::cout <<
"Cannot access submap of this size." << std::endl;
305 for (
auto& data :
data_) {
306 for (
const auto& bufferRegion : bufferRegions) {
307 Index index = bufferRegion.getStartIndex();
308 Size size = bufferRegion.getSize();
311 submap.
data_[data.first].topLeftCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
313 submap.
data_[data.first].topRightCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
315 submap.
data_[data.first].bottomLeftCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
317 submap.
data_[data.first].bottomRightCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
336 for (
int i = 0; i < indexShift.size(); i++) {
337 if (indexShift(i) != 0) {
338 if (abs(indexShift(i)) >=
getSize()(i)) {
344 int sign = (indexShift(i) > 0 ? 1 : -1);
345 int startIndex =
startIndex_(i) - (sign < 0 ? 1 : 0);
346 int endIndex = startIndex - sign + indexShift(i);
347 int nCells = abs(indexShift(i));
348 int index = (sign > 0 ? startIndex : endIndex);
351 if (index + nCells <=
getSize()(i)) {
362 int firstIndex = index;
363 int firstNCells =
getSize()(i) - firstIndex;
373 int secondNCells = nCells - firstNCells;
389 position_ += alignedPositionShift;
392 return (indexShift.any() != 0);
397 std::vector<BufferRegion> newRegions;
398 return move(position, newRegions);
402 bool copyAllLayers, std::vector<std::string> layers)
405 if (copyAllLayers) layers = other.
getLayers();
411 for (
const auto& layer : layers) {
418 if (
isValid(*iterator) && !overwriteData)
continue;
422 if (!other.
isInside(position))
continue;
424 for (
const auto& layer : layers) {
425 if (!other.
isValid(index, layer))
continue;
426 at(layer, *iterator) = other.
at(layer, index);
441 bool resizeMap =
false;
444 if (topLeftCornerOther.x() > topLeftCorner.x()) {
445 extendedMapPosition.x() += (topLeftCornerOther.x() - topLeftCorner.x()) / 2.0;
446 extendedMapLength.x() += topLeftCornerOther.x() - topLeftCorner.x();
449 if (topLeftCornerOther.y() > topLeftCorner.y()) {
450 extendedMapPosition.y() += (topLeftCornerOther.y() - topLeftCorner.y()) / 2.0;
451 extendedMapLength.y() += topLeftCornerOther.y() - topLeftCorner.y();
454 if (bottomRightCornerOther.x() < bottomRightCorner.x()) {
455 extendedMapPosition.x() -= (bottomRightCorner.x() - bottomRightCornerOther.x()) / 2.0;
456 extendedMapLength.x() += bottomRightCorner.x() - bottomRightCornerOther.x();
459 if (bottomRightCornerOther.y() < bottomRightCorner.y()) {
460 extendedMapPosition.y() -= (bottomRightCorner.y() - bottomRightCornerOther.y()) / 2.0;
461 extendedMapLength.y() += bottomRightCorner.y() - bottomRightCornerOther.y();
491 if (
isValid(*iterator))
continue;
495 if (!mapCopy.
isInside(position))
continue;
497 for (
const auto& layer :
layers_) {
498 at(layer, *iterator) = mapCopy.
at(layer, index);
563 }
catch (
const std::out_of_range& exception) {
564 throw std::out_of_range(
"CostMap::clear(...) : No map layer '" + layer +
"' available.");
577 for (
auto& data :
data_) {
584 std::vector<std::string> layersToClear;
587 for (
auto& layer : layersToClear) {
594 std::vector<std::string> layersToClear;
597 for (
auto& layer : layersToClear) {
605 std::vector<Position> points(4);
606 std::vector<Index> indices(4);
610 if (position.x() >= points[0].x()) {
612 indices[1] = indices[0] +
Index(-1, 0);
613 if (!
getPosition(indices[1], points[1]))
return false;
615 indices[1] = indices[0] +
Index(1, 0);
616 if (!
getPosition(indices[1], points[1]))
return false;
619 if (position.y() >= points[0].y()) {
621 indices[2] = indices[0] +
Index(0, -1);
622 if (!
getPosition(indices[2], points[2]))
return false;
624 indices[2] = indices[0] +
Index(0, 1);
625 if (!
getPosition(indices[2], points[2]))
return false;
628 indices[3].x() = indices[1].x();
629 indices[3].y() = indices[2].y();
630 if (!
getPosition(indices[3], points[3]))
return false;
635 for (
unsigned int i = 0; i < points.size(); ++i) {
636 b(i) =
at(layer, indices[i]);
637 A.row(i) << 1, points[i].x(), points[i].y(), points[i].x() * points[i].y();
640 Eigen::Vector4d x = A.colPivHouseholderQr().solve(b);
643 value = x(0) + x(1) * position.x() + x(2) * position.y() + x(3) * position.x() * position.y();
650 for (
auto& data :
data_) {
Time timestamp_
Timestamp of the grid map (nanoseconds).
Eigen::Matrix< unsigned char, Eigen::Dynamic, Eigen::Dynamic > Matrix
const unsigned char NO_INFORMATION
const Length & getLength() const
const Position & getPosition() const
const Matrix & operator[](const std::string &layer) const
double getResolution() const
DataType & at(const std::string &layer, const Index &index)
bool isValid(const Index &index) const
const Index & getStartIndex() const
bool getBufferRegionsForSubmap(std::vector< BufferRegion > &submapBufferRegions, const Index &submapIndex, const Size &submapBufferSize, const Size &bufferSize, const Index &bufferStartIndex=Index::Zero())
const std::vector< std::string > & getBasicLayers() const
const Size & getSize() const
void clearCols(unsigned int index, unsigned int nCols)
double getResolution() const
void setStartIndex(const Index &startIndex)
void add(const std::string &layer, const DataType value=NO_INFORMATION)
Size size_
Size of the buffer (rows and cols of the data structure).
void setFrameId(const std::string &frameId)
const Matrix & get(const std::string &layer) const
bool addDataFrom(const CostMap &other, bool extendMap, bool overwriteData, bool copyAllLayers, std::vector< std::string > layers=std::vector< std::string >())
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
bool getPosition3(const std::string &layer, const Index &index, Position3 &position) const
Index startIndex_
Circular buffer start indeces.
bool getIndexFromPosition(Index &index, const Position &position, const Length &mapLength, const Position &mapPosition, const double &resolution, const Size &bufferSize, const Index &bufferStartIndex=Index::Zero())
bool hasSameLayers(const CostMap &other) const
void clear(const std::string &layer)
const std::string & getFrameId() const
bool checkIfPositionWithinMap(const Position &position, const Length &mapLength, const Position &mapPosition)
void setBasicLayers(const std::vector< std::string > &basicLayers)
Time getTimestamp() const
DataType & atPosition(const std::string &layer, const Position &position)
bool getPositionShiftFromIndexShift(Vector &positionShift, const Index &indexShift, const double &resolution)
std::unordered_map< std::string, Matrix > data_
Grid map data stored as layers of matrices.
void setTimestamp(const Time timestamp)
bool getVector(const std::string &layerPrefix, const Index &index, Eigen::Vector3d &vector) const
const Length & getLength() const
bool getPosition(const Index &index, Position &position) const
const Position & getPosition() const
Length length_
Side length of the map in x- and y-direction [m].
void clearRows(unsigned int index, unsigned int nRows)
bool erase(const std::string &layer)
bool move(const Position &position, std::vector< BufferRegion > &newRegions)
Position position_
Map position in the grid map frame [m].
double resolution_
Map resolution in xy plane [m/cell].
void boundIndexToRange(Index &index, const Size &bufferSize)
bool exists(const std::string &layer) const
CostMap getSubmap(const Position &position, const Length &length, bool &isSuccess) const
grid_map::Position Position
bool getPositionFromIndex(Position &position, const Index &index, const Length &mapLength, const Position &mapPosition, const double &resolution, const Size &bufferSize, const Index &bufferStartIndex=Index::Zero())
grid_map::Position3 Position3
const Index & getStartIndex() const
std::string frameId_
Frame id of the grid map.
const std::vector< std::string > & getLayers() const
bool getIndexShiftFromPositionShift(Index &indexShift, const Vector &positionShift, const double &resolution)
void resize(const Eigen::Array2i &bufferSize)
bool extendToInclude(const CostMap &other)
cost_map::Matrix::Scalar DataType
std::vector< std::string > basicLayers_
grid_map::BufferRegion BufferRegion
bool isInside(const Position &position) const
bool getIndex(const Position &position, Index &index) const
bool atPositionLinearInterpolated(const std::string &layer, const Position &position, float &value) const
std::vector< std::string > layers_
Names of the data layers.