Closed rubengrandia closed 2 years ago
Old code:
New code:
Isn't the round
the problem here? I think flooring with just int i = int((x - center) / resolution);
should also be correct, no?
Seems slightly different for even numbered maps: http://cpp.sh/6c3bd But maybe I didn't fully understand what you proposed
But you are right that it should be possible to exploit the flooring of the int, instead of doing round manually. I think this one should work:
return static_cast<int>((x - center) / resolution + static_cast<float>(width) / 2);
Basically removing the half shift again and then taking the floor instead of the round. I do think it is necessary to not divide width as an integer to be correct for both even and odd sized maps.
I meant more the problem with the original implementation. This is what I meant. So
int get_x_idx_floor(float x, float center, float resolution, int width) {
return static_cast<int>((x - center) / resolution + width*0.5);
}
Ok, we came to the same conclusion :P
I don't have a strong preference which one to use, I could imagine that integer casting is slightly faster.
In any case, I would replace that / 2
with a * 0.5
:D
I tested on the robot yesterday, looks fine. Let's wait for @mktk1117 's opinion. He can also run it on some larger offline dataset to double check.
Tested from my side and it looks good.
trick is to move the half shift (width / 2) into the rounding operation to work for both even and odd numbered maps.
We also need to correct for the fact that the center of a cell is shifted by half the resolution when viewed from the center. That is where you get the (width - 1.0) / 2 from