norlab-ulaval / libpointmatcher

An Iterative Closest Point (ICP) library for 2D and 3D mapping in Robotics
BSD 3-Clause "New" or "Revised" License
1.61k stars 544 forks source link

Fix octree sampling #540

Closed boxanm closed 9 months ago

boxanm commented 10 months ago

I accidentally noticed this bug when unit testing an NDT-inspired octree sampling method. The current octree sampling implementation takes into account the situation when the indexed point was already processed; see this example:

std::size_t j = d;
//retrieve index from lookup table if sampling in already switched element
if(std::size_t(d)<idx)
   j = mapidx[d];

However, we don't know whether mapidx[d] doesn't again point to an index that was also processed. What actually needs to be done, is to propagate all the changes that were made on all previously used indexes of mapidx. So in this PR, I replaced the above code example and similar by

std::size_t j = d;
//retrieve index from lookup table if sampling in already switched element
while(j < idx)
   j = mapidx[j];

I haven't had time to check other Octree implementations and see how they deal with this situation. Naturally, the use of the while cycle slows down the execution. There's probably a more clever solution using pointers?

simonpierredeschenes commented 9 months ago

@boxanm Could you please confirm that this is the same issue as #454?

boxanm commented 9 months ago

The bug I described shouldn't make random parts of the cloud disappear. Instead, the bug causes incorrect sampling inside individual cells. So points, that should be sampled are preserved and the output contains more points, than the number of cells in the octree. Therefore I don't think #454 is related.