Skip to content

Commit b701652

Browse files
QiMingZhenFanMa Ruikai
andauthored
Fix Bug in NormalSpaceSampling::findBin() (#5936)
* Fix Bug in NormalSpaceSampling::findBin() * Avoid imprecise values for the normal[] variables --------- Co-authored-by: Ma Ruikai <maruikai@trunk.tech>
1 parent c295be9 commit b701652

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

filters/include/pcl/filters/impl/normal_space.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ pcl::NormalSpaceSampling<PointT, NormalT>::isEntireBinSampled (boost::dynamic_bi
8080
template<typename PointT, typename NormalT> unsigned int
8181
pcl::NormalSpaceSampling<PointT, NormalT>::findBin (const float *normal)
8282
{
83-
const auto ix = static_cast<unsigned> (std::round (0.5f * (binsx_ - 1.f) * (normal[0] + 1.f)));
84-
const auto iy = static_cast<unsigned> (std::round (0.5f * (binsy_ - 1.f) * (normal[1] + 1.f)));
85-
const auto iz = static_cast<unsigned> (std::round (0.5f * (binsz_ - 1.f) * (normal[2] + 1.f)));
83+
// in the case where normal[0] == 1.0f, ix will be binsx_, which is out of range.
84+
// thus, use std::min to avoid this situation.
85+
const auto ix = std::min (binsx_ - 1, static_cast<unsigned> (std::max (0.0f, std::floor (0.5f * (binsx_) * (normal[0] + 1.f)))));
86+
const auto iy = std::min (binsy_ - 1, static_cast<unsigned> (std::max (0.0f, std::floor (0.5f * (binsy_) * (normal[1] + 1.f)))));
87+
const auto iz = std::min (binsz_ - 1, static_cast<unsigned> (std::max (0.0f, std::floor (0.5f * (binsz_) * (normal[2] + 1.f)))));
8688
return ix * (binsy_*binsz_) + iy * binsz_ + iz;
8789
}
8890

0 commit comments

Comments
 (0)