Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for polygon with 0 vertices #229

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions grid_map_core/include/grid_map_core/Polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* Institute: ETH Zurich, ANYbotics
*/


#define _USE_MATH_DEFINES
#include <cmath>

#pragma once

#include <grid_map_core/TypeDefs.hpp>
Expand Down Expand Up @@ -210,6 +214,8 @@ class Polygon
*/
static Polygon monotoneChainConvexHullOfPoints(const std::vector<Position>& points);

const std::vector<Position> getInterpolatedConvexHull(const double stepSize);

protected:

/*!
Expand Down
29 changes: 29 additions & 0 deletions grid_map_core/src/Polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,33 @@ double Polygon::vectorsMakeClockwiseTurn(const Eigen::Vector2d &pointOrigin,
return computeCrossProduct2D(pointA - pointOrigin, pointB - pointOrigin) <= 0;
}

const std::vector<Position> Polygon::getInterpolatedConvexHull(const double stepSize) {
std::vector<Position> result;
Polygon hull = monotoneChainConvexHullOfPoints(this->getVertices());

for (int curIdx = 0; curIdx < hull.nVertices(); curIdx++){
int nextIdx = curIdx < hull.nVertices() -1 ? curIdx + 1 : 0;

Position curPos = hull.getVertex(curIdx);
Position nextPos = hull.getVertex(nextIdx);

Position diff = nextPos - curPos;
double diffDistance = diff.norm();
int steps = std::max(1, (int) ceil(diffDistance / stepSize));
Position direction(
diff[0] / steps,
diff[1] / steps
);

result.push_back(curPos);
for (int i = 0; i < steps; i++) {
double factor = ((double) i) / steps;
Position tmp = curPos + factor * diff;
result.push_back(tmp);
}
}

return result;
}

} /* namespace grid_map */
27 changes: 16 additions & 11 deletions grid_map_core/src/iterators/PolygonIterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,23 @@ bool PolygonIterator::isInside() const

void PolygonIterator::findSubmapParameters(const grid_map::Polygon& polygon, Index& startIndex, Size& bufferSize) const
{
Position topLeft = polygon_.getVertices()[0];
Position bottomRight = topLeft;
for (const auto& vertex : polygon_.getVertices()) {
topLeft = topLeft.array().max(vertex.array());
bottomRight = bottomRight.array().min(vertex.array());
startIndex = Index(0, 0);
bufferSize = Size(0, 0);

if (polygon_.getVertices().size() > 0) {
Position topLeft = polygon_.getVertices()[0];
Position bottomRight = topLeft;
for (const auto& vertex : polygon_.getVertices()) {
topLeft = topLeft.array().max(vertex.array());
bottomRight = bottomRight.array().min(vertex.array());
}
boundPositionToRange(topLeft, mapLength_, mapPosition_);
boundPositionToRange(bottomRight, mapLength_, mapPosition_);
getIndexFromPosition(startIndex, topLeft, mapLength_, mapPosition_, resolution_, bufferSize_, bufferStartIndex_);
Index endIndex;
getIndexFromPosition(endIndex, bottomRight, mapLength_, mapPosition_, resolution_, bufferSize_, bufferStartIndex_);
bufferSize = getSubmapSizeFromCornerIndeces(startIndex, endIndex, bufferSize_, bufferStartIndex_);
}
boundPositionToRange(topLeft, mapLength_, mapPosition_);
boundPositionToRange(bottomRight, mapLength_, mapPosition_);
getIndexFromPosition(startIndex, topLeft, mapLength_, mapPosition_, resolution_, bufferSize_, bufferStartIndex_);
Index endIndex;
getIndexFromPosition(endIndex, bottomRight, mapLength_, mapPosition_, resolution_, bufferSize_, bufferStartIndex_);
bufferSize = getSubmapSizeFromCornerIndeces(startIndex, endIndex, bufferSize_, bufferStartIndex_);
}

} /* namespace grid_map */
Expand Down