14 namespace signed_distance_field {
30 std::vector<DistanceLowerBound>::iterator
fillLowerBounds(
const Eigen::Ref<Eigen::VectorXf>& squareDistance1d,
31 std::vector<DistanceLowerBound>& lowerBounds, Eigen::Index start) {
32 const auto n = squareDistance1d.size();
33 const auto nFloat =
static_cast<float>(n);
34 const auto startFloat =
static_cast<float>(start);
37 auto rhsBoundIt = lowerBounds.begin();
39 auto firstBoundIt = lowerBounds.begin();
42 float qFloat = rhsBoundIt->v + 1.0F;
43 for (Eigen::Index q = start + 1; q < n; ++q) {
45 const float fq = squareDistance1d[q];
50 while (s < rhsBoundIt->
z_lhs) {
55 rhsBoundIt->z_rhs = s;
60 firstBoundIt = rhsBoundIt;
71 void extractSquareDistances(Eigen::Ref<Eigen::VectorXf> squareDistance1d, std::vector<DistanceLowerBound>::const_iterator lowerBoundIt,
73 const auto n = squareDistance1d.size();
76 auto lastz = lowerBoundIt->z_rhs;
78 auto qFloat =
static_cast<float>(start);
79 for (Eigen::Index q = start; q < n; ++q) {
80 if (squareDistance1d[q] > 0.0F) {
85 }
while (lowerBoundIt->z_rhs < qFloat);
86 lastz = lowerBoundIt->z_rhs;
101 std::vector<DistanceLowerBound>::const_iterator lowerBoundIt, Eigen::Index start) {
102 const auto n = squareDistance1d.size();
105 auto lastz = lowerBoundIt->z_rhs;
107 auto qFloat =
static_cast<float>(start);
108 for (Eigen::Index q = start; q < n; ++q) {
109 if (squareDistance1d[q] > 0.0F) {
111 if (qFloat > lastz) {
114 }
while (lowerBoundIt->z_rhs < qFloat);
115 lastz = lowerBoundIt->z_rhs;
129 const auto n = squareDistance1d.size();
131 for (Eigen::Index q = 0; q < n; ++q) {
132 if (squareDistance1d[q] > 0.0F) {
144 std::vector<DistanceLowerBound>& lowerBounds) {
148 if (start < squareDistance1d.size()) {
163 if (start < squareDistance1d.size()) {
170 const auto n = input.rows();
171 const auto m = input.cols();
174 std::vector<DistanceLowerBound> lowerBounds(std::max(n, m));
177 for (Eigen::Index i = 0; i < m; ++i) {
182 distanceTranspose = input.transpose();
183 for (Eigen::Index i = 0; i < n; ++i) {
199 result = ((1.0F / resolution) * (height - elevationMap.array()).cwiseMax(0.0F)).square();
212 result = ((1.0F / resolution) * (height - elevationMap.array()).cwiseMin(0.0F)).square();
229 Matrix init = occupancyGrid.unaryExpr([=](
bool val) {
return (val) ? 0.0F :
INF; });
234 init = occupancyGrid.unaryExpr([=](
bool val) {
return (val) ?
INF : 0.0F; });
237 return resolution * (sdfObstacle - sdfObstacleFree);
243 float resolution,
float minHeight,
float maxHeight) {
244 const bool allPixelsAreObstacles = height < minHeight;
245 const bool allPixelsAreFreeSpace = height > maxHeight;
247 if (allPixelsAreObstacles) {
250 sdfTranspose *= -resolution;
251 }
else if (allPixelsAreFreeSpace) {
254 sdfTranspose *= resolution;
259 sdfTranspose = resolution * (sdfTranspose - tmpTranspose);
269 return sdfTranspose.transpose();
273 auto obstacleCount = occupancyGrid.count();
274 bool hasObstacles = obstacleCount > 0;
276 bool hasFreeSpace = obstacleCount < occupancyGrid.size();
281 return Matrix::Constant(occupancyGrid.rows(), occupancyGrid.cols(), -
INF);
285 return Matrix::Constant(occupancyGrid.rows(), occupancyGrid.cols(),
INF);
Matrix signedDistanceFromOccupancyTranspose(const Eigen::Matrix< bool, -1, -1 > &occupancyGrid, float resolution)
float squarePixelBorderDistance(float i, float j, float f)
Eigen::Index lastZeroFromFront(const Eigen::Ref< Eigen::VectorXf > &squareDistance1d)
void distanceTransform_1d_inplace(Eigen::Ref< Eigen::VectorXf > squareDistance1d, std::vector< DistanceLowerBound > &lowerBounds)
void signedDistanceAtHeightTranspose(const Matrix &elevationMap, Matrix &sdfTranspose, Matrix &tmp, Matrix &tmpTranspose, float height, float resolution, float minHeight, float maxHeight)
void extractSquareDistances(Eigen::Ref< Eigen::VectorXf > squareDistance1d, std::vector< DistanceLowerBound >::const_iterator lowerBoundIt, Eigen::Index start)
Matrix signedDistanceAtHeight(const Matrix &elevationMap, float height, float resolution, float minHeight, float maxHeight)
void squaredDistanceTransform_1d_inplace(Eigen::Ref< Eigen::VectorXf > squareDistance1d, std::vector< DistanceLowerBound > &lowerBounds)
void initializeObstacleDistance(const Matrix &elevationMap, Matrix &result, float height, float resolution)
Matrix signedDistanceFromOccupancy(const Eigen::Matrix< bool, -1, -1 > &occupancyGrid, float resolution)
float equidistancePoint(float q, float fq, float p, float fp)
void initializeObstacleFreeDistance(const Matrix &elevationMap, Matrix &result, float height, float resolution)
void extractDistances(Eigen::Ref< Eigen::VectorXf > squareDistance1d, std::vector< DistanceLowerBound >::const_iterator lowerBoundIt, Eigen::Index start)
void pixelDistanceToFreeSpaceTranspose(const Matrix &elevationMap, Matrix &sdfObstacleFree, Matrix &tmp, float height, float resolution)
void pixelDistanceToObstacleTranspose(const Matrix &elevationMap, Matrix &sdfObstacleTranspose, Matrix &tmp, float height, float resolution)
std::vector< DistanceLowerBound >::iterator fillLowerBounds(const Eigen::Ref< Eigen::VectorXf > &squareDistance1d, std::vector< DistanceLowerBound > &lowerBounds, Eigen::Index start)
constexpr float INF
Distance value that is considered infinite.
void computePixelDistance2dTranspose(Matrix &input, Matrix &distanceTranspose)