Page MenuHome

Fix T75968: PBVH raycast returns wrong active vertex
ClosedPublic

Authored by Pablo Dobarro (pablodp606) on Apr 21 2020, 8:17 PM.

Details

Summary

nearest_vertex_co was not reset when a new triangle was intersected by
the ray, so it was always returning the closest vertex to the real
cursor position in any triangle, which was not always the triangle under
the cursor.

Diff Detail

Repository
rB Blender

Event Timeline

Pablo Dobarro (pablodp606) requested review of this revision.Apr 21 2020, 8:17 PM
Pablo Dobarro (pablodp606) created this revision.
Sergey Sharybin (sergey) requested changes to this revision.Apr 22 2020, 10:22 AM

I don't think this is correct. Treating coordinate (0, 0, 0) as something special is wrong.

I can think of two possible ways to make it right:

  1. Use boolean flag which denotes whether there is a known nearest coordinate or not. And then the check becomes if (!have_closest_co || len(..) < len(...)).
  2. Initialize nearest vertex to FLT_MAX coordinates. From quick thinking seems this will give correct behavior as well.
This revision now requires changes to proceed.Apr 22 2020, 10:22 AM

Had a quick discussion with Brecht in blender-coders.

Thing is: historically in Blender some find-extremum code is not numerically robust. However, in the new code is better to not rely on floating point comparison.

So here suggestion would be to actually use explicit check for whether value is initialized to something meaningful or not. Here you can easily do it by checking whether this is the first loop iteration.

For the readability you can do:

if (j == 0 || len_squared_v3v3(location, co[j]) < len_squared_v3v3(location, nearest_vertex_co)) {
This revision is now accepted and ready to land.Apr 29 2020, 11:07 AM