GridMap.cpp
Go to the documentation of this file.
00001 /*
00002  * GridMap.cpp
00003  *
00004  *  Created on: Jul 14, 2014
00005  *      Author: Péter Fankhauser
00006  *       Institute: ETH Zurich, Autonomous Systems Lab
00007  */
00008 
00009 #include "grid_map_core/GridMap.hpp"
00010 #include "grid_map_core/GridMapMath.hpp"
00011 #include "grid_map_core/SubmapGeometry.hpp"
00012 #include "grid_map_core/iterators/GridMapIterator.hpp"
00013 
00014 #include <Eigen/Dense>
00015 
00016 #include <iostream>
00017 #include <cassert>
00018 #include <math.h>
00019 #include <algorithm>
00020 #include <stdexcept>
00021 
00022 using namespace std;
00023 using namespace grid_map;
00024 
00025 namespace grid_map {
00026 
00027 GridMap::GridMap(const std::vector<std::string>& layers)
00028 {
00029   position_.setZero();
00030   length_.setZero();
00031   resolution_ = 0.0;
00032   size_.setZero();
00033   startIndex_.setZero();
00034   timestamp_ = 0;
00035   layers_ = layers;
00036 
00037   for (auto& layer : layers_) {
00038     data_.insert(std::pair<std::string, Matrix>(layer, Matrix()));
00039   }
00040 }
00041 
00042 GridMap::GridMap() :
00043     GridMap(std::vector<std::string>())
00044 {
00045 }
00046 
00047 GridMap::~GridMap()
00048 {
00049 }
00050 
00051 void GridMap::setGeometry(const Length& length, const double resolution,
00052                           const Position& position)
00053 {
00054   assert(length(0) > 0.0);
00055   assert(length(1) > 0.0);
00056   assert(resolution > 0.0);
00057 
00058   Size size;
00059   size(0) = static_cast<int>(round(length(0) / resolution)); // There is no round() function in Eigen.
00060   size(1) = static_cast<int>(round(length(1) / resolution));
00061   resize(size);
00062   clearAll();
00063 
00064   resolution_ = resolution;
00065   length_ = (size_.cast<double>() * resolution_).matrix();
00066   position_ = position;
00067   startIndex_.setZero();
00068 
00069   return;
00070 }
00071 
00072 void GridMap::setGeometry(const SubmapGeometry& geometry)
00073 {
00074   setGeometry(geometry.getLength(), geometry.getResolution(), geometry.getPosition());
00075 }
00076 
00077 void GridMap::setBasicLayers(const std::vector<std::string>& basicLayers)
00078 {
00079   basicLayers_ = basicLayers;
00080 }
00081 
00082 const std::vector<std::string>& GridMap::getBasicLayers() const
00083 {
00084   return basicLayers_;
00085 }
00086 
00087 bool GridMap::hasBasicLayers() const
00088 {
00089   return basicLayers_.size() > 0;
00090 }
00091 
00092 bool GridMap::hasSameLayers(const GridMap& other) const
00093 {
00094   for (const auto& layer : layers_) {
00095     if (!other.exists(layer)) return false;
00096   }
00097   return true;
00098 }
00099 
00100 void GridMap::add(const std::string& layer, const double value)
00101 {
00102   add(layer, Matrix::Constant(size_(0), size_(1), value));
00103 }
00104 
00105 void GridMap::add(const std::string& layer, const Matrix& data)
00106 {
00107   assert(size_(0) == data.rows());
00108   assert(size_(1) == data.cols());
00109 
00110   if (exists(layer)) {
00111     // Type exists already, overwrite its data.
00112     data_.at(layer) = data;
00113   } else {
00114     // Type does not exist yet, add type and data.
00115     data_.insert(std::pair<std::string, Matrix>(layer, data));
00116     layers_.push_back(layer);
00117   }
00118 }
00119 
00120 bool GridMap::exists(const std::string& layer) const
00121 {
00122   return !(data_.find(layer) == data_.end());
00123 }
00124 
00125 const Matrix& GridMap::get(const std::string& layer) const
00126 {
00127   try {
00128     return data_.at(layer);
00129   } catch (const std::out_of_range& exception) {
00130     throw std::out_of_range("GridMap::get(...) : No map layer '" + layer + "' available.");
00131   }
00132 }
00133 
00134 Matrix& GridMap::get(const std::string& layer)
00135 {
00136   try {
00137     return data_.at(layer);
00138   } catch (const std::out_of_range& exception) {
00139     throw std::out_of_range("GridMap::get(...) : No map layer of type '" + layer + "' available.");
00140   }
00141 }
00142 
00143 const Matrix& GridMap::operator [](const std::string& layer) const
00144 {
00145   return get(layer);
00146 }
00147 
00148 Matrix& GridMap::operator [](const std::string& layer)
00149 {
00150   return get(layer);
00151 }
00152 
00153 bool GridMap::erase(const std::string& layer)
00154 {
00155   const auto dataIterator = data_.find(layer);
00156   if (dataIterator == data_.end()) return false;
00157   data_.erase(dataIterator);
00158 
00159   const auto layerIterator = std::find(layers_.begin(), layers_.end(), layer);
00160   if (layerIterator == layers_.end()) return false;
00161   layers_.erase(layerIterator);
00162 
00163   const auto basicLayerIterator = std::find(basicLayers_.begin(), basicLayers_.end(), layer);
00164   if (basicLayerIterator != basicLayers_.end()) basicLayers_.erase(basicLayerIterator);
00165 
00166   return true;
00167 }
00168 
00169 const std::vector<std::string>& GridMap::getLayers() const
00170 {
00171   return layers_;
00172 }
00173 
00174 float& GridMap::atPosition(const std::string& layer, const Position& position)
00175 {
00176   Index index;
00177   if (getIndex(position, index)) {
00178     return at(layer, index);
00179   }
00180   throw std::out_of_range("GridMap::atPosition(...) : Position is out of range.");
00181 }
00182 
00183 float GridMap::atPosition(const std::string& layer, const Position& position, InterpolationMethods interpolationMethod) const
00184 {
00185   switch (interpolationMethod) {
00186       case InterpolationMethods::INTER_LINEAR:
00187       {
00188         float value;
00189         if (atPositionLinearInterpolated(layer, position, value))
00190           return value;
00191         else
00192             interpolationMethod = InterpolationMethods::INTER_NEAREST;
00193       }
00194       case  InterpolationMethods::INTER_NEAREST:
00195       {
00196         Index index;
00197         if (getIndex(position, index)) {
00198         return at(layer, index);
00199         }
00200         else
00201         throw std::out_of_range("GridMap::atPosition(...) : Position is out of range.");
00202         break;
00203       }
00204       default:
00205         throw std::runtime_error("GridMap::atPosition(...) : Specified interpolation method not implemented.");
00206   }
00207 }
00208 
00209 float& GridMap::at(const std::string& layer, const Index& index)
00210 {
00211   try {
00212     return data_.at(layer)(index(0), index(1));
00213   } catch (const std::out_of_range& exception) {
00214     throw std::out_of_range("GridMap::at(...) : No map layer '" + layer + "' available.");
00215   }
00216 }
00217 
00218 float GridMap::at(const std::string& layer, const Index& index) const
00219 {
00220   try {
00221     return data_.at(layer)(index(0), index(1));
00222   } catch (const std::out_of_range& exception) {
00223     throw std::out_of_range("GridMap::at(...) : No map layer '" + layer + "' available.");
00224   }
00225 }
00226 
00227 bool GridMap::getIndex(const Position& position, Index& index) const
00228 {
00229   return getIndexFromPosition(index, position, length_, position_, resolution_, size_, startIndex_);
00230 }
00231 
00232 bool GridMap::getPosition(const Index& index, Position& position) const
00233 {
00234   return getPositionFromIndex(position, index, length_, position_, resolution_, size_, startIndex_);
00235 }
00236 
00237 bool GridMap::isInside(const Position& position) const
00238 {
00239   return checkIfPositionWithinMap(position, length_, position_);
00240 }
00241 
00242 bool GridMap::isValid(const Index& index) const
00243 {
00244   return isValid(index, basicLayers_);
00245 }
00246 
00247 bool GridMap::isValid(const Index& index, const std::string& layer) const
00248 {
00249   if (!isfinite(at(layer, index))) return false;
00250   return true;
00251 }
00252 
00253 bool GridMap::isValid(const Index& index, const std::vector<std::string>& layers) const
00254 {
00255   if (layers.empty()) return false;
00256   for (auto& layer : layers) {
00257     if (!isfinite(at(layer, index))) return false;
00258   }
00259   return true;
00260 }
00261 
00262 bool GridMap::getPosition3(const std::string& layer, const Index& index,
00263                            Position3& position) const
00264 {
00265   if (!isValid(index, layer)) return false;
00266   Position position2d;
00267   getPosition(index, position2d);
00268   position.head(2) = position2d;
00269   position.z() = at(layer, index);
00270   return true;
00271 }
00272 
00273 bool GridMap::getVector(const std::string& layerPrefix, const Index& index,
00274                         Eigen::Vector3d& vector) const
00275 {
00276   std::vector<std::string> layers;
00277   layers.push_back(layerPrefix + "x");
00278   layers.push_back(layerPrefix + "y");
00279   layers.push_back(layerPrefix + "z");
00280   if (!isValid(index, layers)) return false;
00281   for (size_t i = 0; i < 3; ++i) {
00282     vector(i) = at(layers[i], index);
00283   }
00284   return true;
00285 }
00286 
00287 GridMap GridMap::getSubmap(const Position& position, const Length& length,
00288                            bool& isSuccess) const
00289 {
00290   Index index;
00291   return getSubmap(position, length, index, isSuccess);
00292 }
00293 
00294 GridMap GridMap::getSubmap(const Position& position, const Length& length,
00295                            Index& indexInSubmap, bool& isSuccess) const
00296 {
00297   // Submap the generate.
00298   GridMap submap(layers_);
00299   submap.setBasicLayers(basicLayers_);
00300   submap.setTimestamp(timestamp_);
00301   submap.setFrameId(frameId_);
00302 
00303   // Get submap geometric information.
00304   SubmapGeometry submapInformation(*this, position, length, isSuccess);
00305   if (isSuccess == false) return GridMap(layers_);
00306   submap.setGeometry(submapInformation);
00307   submap.startIndex_.setZero(); // Because of the way we copy the data below.
00308 
00309   // Copy data.
00310   std::vector<BufferRegion> bufferRegions;
00311 
00312   if (!getBufferRegionsForSubmap(bufferRegions, submapInformation.getStartIndex(),
00313                                  submap.getSize(), size_, startIndex_)) {
00314     cout << "Cannot access submap of this size." << endl;
00315     isSuccess = false;
00316     return GridMap(layers_);
00317   }
00318 
00319   for (const auto& data : data_) {
00320     for (const auto& bufferRegion : bufferRegions) {
00321       Index index = bufferRegion.getStartIndex();
00322       Size size = bufferRegion.getSize();
00323 
00324       if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::TopLeft) {
00325         submap.data_[data.first].topLeftCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00326       } else if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::TopRight) {
00327         submap.data_[data.first].topRightCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00328       } else if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::BottomLeft) {
00329         submap.data_[data.first].bottomLeftCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00330       } else if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::BottomRight) {
00331         submap.data_[data.first].bottomRightCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00332       }
00333 
00334     }
00335   }
00336 
00337   isSuccess = true;
00338   return submap;
00339 }
00340 
00341 void GridMap::setPosition(const Position& position)
00342 {
00343   position_ = position;
00344 }
00345 
00346 bool GridMap::move(const Position& position, std::vector<BufferRegion>& newRegions)
00347 {
00348   Index indexShift;
00349   Position positionShift = position - position_;
00350   getIndexShiftFromPositionShift(indexShift, positionShift, resolution_);
00351   Position alignedPositionShift;
00352   getPositionShiftFromIndexShift(alignedPositionShift, indexShift, resolution_);
00353 
00354   // Delete fields that fall out of map (and become empty cells).
00355   for (int i = 0; i < indexShift.size(); i++) {
00356     if (indexShift(i) != 0) {
00357       if (abs(indexShift(i)) >= getSize()(i)) {
00358         // Entire map is dropped.
00359         clearAll();
00360         newRegions.push_back(BufferRegion(Index(0, 0), getSize(), BufferRegion::Quadrant::Undefined));
00361       } else {
00362         // Drop cells out of map.
00363         int sign = (indexShift(i) > 0 ? 1 : -1);
00364         int startIndex = startIndex_(i) - (sign < 0 ? 1 : 0);
00365         int endIndex = startIndex - sign + indexShift(i);
00366         int nCells = abs(indexShift(i));
00367         int index = (sign > 0 ? startIndex : endIndex);
00368         wrapIndexToRange(index, getSize()(i));
00369 
00370         if (index + nCells <= getSize()(i)) {
00371           // One region to drop.
00372           if (i == 0) {
00373             clearRows(index, nCells);
00374             newRegions.push_back(BufferRegion(Index(index, 0), Size(nCells, getSize()(1)), BufferRegion::Quadrant::Undefined));
00375           } else if (i == 1) {
00376             clearCols(index, nCells);
00377             newRegions.push_back(BufferRegion(Index(0, index), Size(getSize()(0), nCells), BufferRegion::Quadrant::Undefined));
00378           }
00379         } else {
00380           // Two regions to drop.
00381           int firstIndex = index;
00382           int firstNCells = getSize()(i) - firstIndex;
00383           if (i == 0) {
00384             clearRows(firstIndex, firstNCells);
00385             newRegions.push_back(BufferRegion(Index(firstIndex, 0), Size(firstNCells, getSize()(1)), BufferRegion::Quadrant::Undefined));
00386           } else if (i == 1) {
00387             clearCols(firstIndex, firstNCells);
00388             newRegions.push_back(BufferRegion(Index(0, firstIndex), Size(getSize()(0), firstNCells), BufferRegion::Quadrant::Undefined));
00389           }
00390 
00391           int secondIndex = 0;
00392           int secondNCells = nCells - firstNCells;
00393           if (i == 0) {
00394             clearRows(secondIndex, secondNCells);
00395             newRegions.push_back(BufferRegion(Index(secondIndex, 0), Size(secondNCells, getSize()(1)), BufferRegion::Quadrant::Undefined));
00396           } else if (i == 1) {
00397             clearCols(secondIndex, secondNCells);
00398             newRegions.push_back(BufferRegion(Index(0, secondIndex), Size(getSize()(0), secondNCells), BufferRegion::Quadrant::Undefined));
00399           }
00400         }
00401       }
00402     }
00403   }
00404 
00405   // Update information.
00406   startIndex_ += indexShift;
00407   wrapIndexToRange(startIndex_, getSize());
00408   position_ += alignedPositionShift;
00409 
00410   // Check if map has been moved at all.
00411   return (indexShift.any() != 0);
00412 }
00413 
00414 bool GridMap::move(const Position& position)
00415 {
00416   std::vector<BufferRegion> newRegions;
00417   return move(position, newRegions);
00418 }
00419 
00420 bool GridMap::addDataFrom(const GridMap& other, bool extendMap, bool overwriteData,
00421                           bool copyAllLayers, std::vector<std::string> layers)
00422 {
00423   // Set the layers to copy.
00424   if (copyAllLayers) layers = other.getLayers();
00425 
00426   // Resize map.
00427   if (extendMap) extendToInclude(other);
00428 
00429   // Check if all layers to copy exist and add missing layers.
00430   for (const auto& layer : layers) {
00431     if (std::find(layers_.begin(), layers_.end(), layer) == layers_.end()) {
00432       add(layer);
00433     }
00434   }
00435   // Copy data.
00436   for (GridMapIterator iterator(*this); !iterator.isPastEnd(); ++iterator) {
00437     if (isValid(*iterator) && !overwriteData) continue;
00438     Position position;
00439     getPosition(*iterator, position);
00440     Index index;
00441     if (!other.isInside(position)) continue;
00442     other.getIndex(position, index);
00443     for (const auto& layer : layers) {
00444       if (!other.isValid(index, layer)) continue;
00445       at(layer, *iterator) = other.at(layer, index);
00446     }
00447   }
00448 
00449   return true;
00450 }
00451 
00452 bool GridMap::extendToInclude(const GridMap& other)
00453 {
00454   // Get dimension of maps.
00455   Position topLeftCorner(position_.x() + length_.x() / 2.0, position_.y() + length_.y() / 2.0);
00456   Position bottomRightCorner(position_.x() - length_.x() / 2.0, position_.y() - length_.y() / 2.0);
00457   Position topLeftCornerOther(other.getPosition().x() + other.getLength().x() / 2.0, other.getPosition().y() + other.getLength().y() / 2.0);
00458   Position bottomRightCornerOther(other.getPosition().x() - other.getLength().x() / 2.0, other.getPosition().y() - other.getLength().y() / 2.0);
00459   // Check if map needs to be resized.
00460   bool resizeMap = false;
00461   Position extendedMapPosition = position_;
00462   Length extendedMapLength = length_;
00463   if (topLeftCornerOther.x() > topLeftCorner.x()) {
00464     extendedMapPosition.x() += (topLeftCornerOther.x() - topLeftCorner.x()) / 2.0;
00465     extendedMapLength.x() += topLeftCornerOther.x() - topLeftCorner.x();
00466     resizeMap = true;
00467   }
00468   if (topLeftCornerOther.y() > topLeftCorner.y()) {
00469     extendedMapPosition.y() += (topLeftCornerOther.y() - topLeftCorner.y()) / 2.0;
00470     extendedMapLength.y() += topLeftCornerOther.y() - topLeftCorner.y();
00471     resizeMap = true;
00472   }
00473   if (bottomRightCornerOther.x() < bottomRightCorner.x()) {
00474     extendedMapPosition.x() -= (bottomRightCorner.x() - bottomRightCornerOther.x()) / 2.0;
00475     extendedMapLength.x() += bottomRightCorner.x() - bottomRightCornerOther.x();
00476     resizeMap = true;
00477   }
00478   if (bottomRightCornerOther.y() < bottomRightCorner.y()) {
00479     extendedMapPosition.y() -= (bottomRightCorner.y() - bottomRightCornerOther.y()) / 2.0;
00480     extendedMapLength.y() += bottomRightCorner.y() - bottomRightCornerOther.y();
00481     resizeMap = true;
00482   }
00483   // Resize map and copy data to new map.
00484   if (resizeMap) {
00485     GridMap mapCopy = *this;
00486     setGeometry(extendedMapLength, resolution_, extendedMapPosition);
00487     // Align new map with old one.
00488     Vector shift = position_ - mapCopy.getPosition();
00489     shift.x() = std::fmod(shift.x(), resolution_);
00490     shift.y() = std::fmod(shift.y(), resolution_);
00491     if (std::abs(shift.x()) < resolution_ / 2.0) {
00492       position_.x() -= shift.x();
00493     } else {
00494       position_.x() += resolution_ - shift.x();
00495     }
00496     if (size_.x() % 2 != mapCopy.getSize().x() % 2) {
00497       position_.x() += -std::copysign(resolution_ / 2.0, shift.x());
00498     }
00499     if (std::abs(shift.y()) < resolution_ / 2.0) {
00500       position_.y() -= shift.y();
00501     } else {
00502       position_.y() += resolution_ - shift.y();
00503     }
00504     if (size_.y() % 2 != mapCopy.getSize().y() % 2) {
00505       position_.y() += -std::copysign(resolution_ / 2.0, shift.y());
00506     }
00507     // Copy data.
00508     for (GridMapIterator iterator(*this); !iterator.isPastEnd(); ++iterator) {
00509       if (isValid(*iterator)) continue;
00510       Position position;
00511       getPosition(*iterator, position);
00512       Index index;
00513       if (!mapCopy.isInside(position)) continue;
00514       mapCopy.getIndex(position, index);
00515       for (const auto& layer : layers_) {
00516         at(layer, *iterator) = mapCopy.at(layer, index);
00517       }
00518     }
00519   }
00520   return true;
00521 }
00522 
00523 void GridMap::setTimestamp(const Time timestamp)
00524 {
00525   timestamp_ = timestamp;
00526 }
00527 
00528 Time GridMap::getTimestamp() const
00529 {
00530   return timestamp_;
00531 }
00532 
00533 void GridMap::resetTimestamp()
00534 {
00535   timestamp_ = 0.0;
00536 }
00537 
00538 void GridMap::setFrameId(const std::string& frameId)
00539 {
00540   frameId_ = frameId;
00541 }
00542 
00543 const std::string& GridMap::getFrameId() const
00544 {
00545   return frameId_;
00546 }
00547 
00548 const Length& GridMap::getLength() const
00549 {
00550   return length_;
00551 }
00552 
00553 const Position& GridMap::getPosition() const
00554 {
00555   return position_;
00556 }
00557 
00558 double GridMap::getResolution() const
00559 {
00560   return resolution_;
00561 }
00562 
00563 const Size& GridMap::getSize() const
00564 {
00565   return size_;
00566 }
00567 
00568 void GridMap::setStartIndex(const Index& startIndex) {
00569   startIndex_ = startIndex;
00570 }
00571 
00572 const Index& GridMap::getStartIndex() const
00573 {
00574   return startIndex_;
00575 }
00576 
00577 bool GridMap::isDefaultStartIndex() const
00578 {
00579   return (startIndex_ == 0).all();
00580 }
00581 
00582 void GridMap::convertToDefaultStartIndex()
00583 {
00584   if (isDefaultStartIndex()) return;
00585 
00586   std::vector<BufferRegion> bufferRegions;
00587   if (!getBufferRegionsForSubmap(bufferRegions, startIndex_, size_, size_, startIndex_)) {
00588     throw std::out_of_range("Cannot access submap of this size.");
00589   }
00590 
00591   for (auto& data : data_) {
00592     auto tempData(data.second);
00593     for (const auto& bufferRegion : bufferRegions) {
00594       Index index = bufferRegion.getStartIndex();
00595       Size size = bufferRegion.getSize();
00596 
00597       if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::TopLeft) {
00598         tempData.topLeftCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00599       } else if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::TopRight) {
00600         tempData.topRightCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00601       } else if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::BottomLeft) {
00602         tempData.bottomLeftCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00603       } else if (bufferRegion.getQuadrant() == BufferRegion::Quadrant::BottomRight) {
00604         tempData.bottomRightCorner(size(0), size(1)) = data.second.block(index(0), index(1), size(0), size(1));
00605       }
00606     }
00607     data.second = tempData;
00608   }
00609 
00610   startIndex_.setZero();
00611 }
00612 
00613 void GridMap::clear(const std::string& layer)
00614 {
00615   try {
00616     data_.at(layer).setConstant(NAN);
00617   } catch (const std::out_of_range& exception) {
00618     throw std::out_of_range("GridMap::clear(...) : No map layer '" + layer + "' available.");
00619   }
00620 }
00621 
00622 void GridMap::clearBasic()
00623 {
00624   for (auto& layer : basicLayers_) {
00625     clear(layer);
00626   }
00627 }
00628 
00629 void GridMap::clearAll()
00630 {
00631   for (auto& data : data_) {
00632     data.second.setConstant(NAN);
00633   }
00634 }
00635 
00636 void GridMap::clearRows(unsigned int index, unsigned int nRows)
00637 {
00638   std::vector<std::string> layersToClear;
00639   if (basicLayers_.size() > 0) layersToClear = basicLayers_;
00640   else layersToClear = layers_;
00641   for (auto& layer : layersToClear) {
00642     data_.at(layer).block(index, 0, nRows, getSize()(1)).setConstant(NAN);
00643   }
00644 }
00645 
00646 void GridMap::clearCols(unsigned int index, unsigned int nCols)
00647 {
00648   std::vector<std::string> layersToClear;
00649   if (basicLayers_.size() > 0) layersToClear = basicLayers_;
00650   else layersToClear = layers_;
00651   for (auto& layer : layersToClear) {
00652     data_.at(layer).block(0, index, getSize()(0), nCols).setConstant(NAN);
00653   }
00654 }
00655 
00656 bool GridMap::atPositionLinearInterpolated(const std::string& layer, const Position& position,
00657                                            float& value) const
00658 {
00659   Position point;
00660   Index indices[4];
00661   bool idxTempDir;
00662   size_t idxShift[4];
00663   
00664   getIndex(position, indices[0]);
00665   getPosition(indices[0], point);
00666   
00667   if (position.x() >= point.x()) {
00668     indices[1] = indices[0] + Index(-1, 0); // Second point is above first point.
00669     idxTempDir = true;
00670   } else {
00671     indices[1] = indices[0] + Index(+1, 0);
00672     idxTempDir = false;
00673   }
00674   if (position.y() >= point.y()) {
00675       indices[2] = indices[0] + Index(0, -1); // Third point is right of first point.
00676       if(idxTempDir){ idxShift[0]=0; idxShift[1]=1; idxShift[2]=2; idxShift[3]=3; }
00677       else          { idxShift[0]=1; idxShift[1]=0; idxShift[2]=3; idxShift[3]=2; }
00678       
00679       
00680   } else { 
00681       indices[2] = indices[0] + Index(0, +1); 
00682       if(idxTempDir){ idxShift[0]=2; idxShift[1]=3; idxShift[2]=0; idxShift[3]=1; }
00683       else          { idxShift[0]=3; idxShift[1]=2; idxShift[2]=1; idxShift[3]=0; }
00684   }
00685   indices[3].x() = indices[1].x();
00686   indices[3].y() = indices[2].y();
00687   
00688   const Size& mapSize = getSize();
00689   const size_t bufferSize = mapSize(0) * mapSize(1);
00690   const size_t startIndexLin = getLinearIndexFromIndex(startIndex_, mapSize);
00691   const size_t endIndexLin = startIndexLin + bufferSize;
00692   const auto& layerMat = operator[](layer);
00693   float         f[4];
00694 
00695   for (size_t i = 0; i < 4; ++i) {
00696     const size_t indexLin = getLinearIndexFromIndex(indices[idxShift[i]], mapSize);
00697     if ((indexLin < startIndexLin) || (indexLin > endIndexLin)) return false;
00698     f[i] = layerMat(indexLin);
00699   }
00700 
00701   getPosition(indices[idxShift[0]], point);
00702   const Position positionRed     = ( position - point ) / resolution_;
00703   const Position positionRedFlip = Position(1.,1.) - positionRed;
00704   
00705   value = f[0] * positionRedFlip.x() * positionRedFlip.y() + 
00706           f[1] *     positionRed.x() * positionRedFlip.y() +
00707           f[2] * positionRedFlip.x() *     positionRed.y() +
00708           f[3] *     positionRed.x() *     positionRed.y();
00709   return true;
00710 }
00711 
00712 void GridMap::resize(const Index& size)
00713 {
00714   size_ = size;
00715   for (auto& data : data_) {
00716     data.second.resize(size_(0), size_(1));
00717   }
00718 }
00719 
00720 } /* namespace */
00721 


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Mon Oct 9 2017 03:09:16