Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mesh picking - Barycentric coordinates not normalized #639

Open
smfloery opened this issue Feb 6, 2024 · 1 comment
Open

Mesh picking - Barycentric coordinates not normalized #639

smfloery opened this issue Feb 6, 2024 · 1 comment

Comments

@smfloery
Copy link

smfloery commented Feb 6, 2024

Dear all,

the "face_coords" in the event.pick_info are not normalized (their sum does not equal 1). If one wants to use those coordinates to calculate the coordinates of a picked point on a triangle mesh in world coordinates, this leads to erroneous calculations. While it is not a big problem, as it's enough to divide by their sum, I somehow automatically assumed them to be normalized and it did cost me some time to realize that this might be the problem!

Is this an intended behavior? If so, then I would suggest adding this information somewhere in the documentation. Or is it already mentioned and I didn't find it?

Sebastian

@almarklein
Copy link
Collaborator

Hi Sebastian,

You are quite right. The picking info is stored in a limited amount of memory, leaving just 6 bits for each of the coords, so the deviation from 1 is due to rounding errors.

I agree that it would be good to normalize the values so that they add up to one again. That code is here:

def _wgpu_get_pick_info(self, pick_value):
values = unpack_bitfield(
pick_value, wobject_id=20, index=26, coord1=6, coord2=6, coord3=6
)
face_index = values["index"]
face_coord = [
values["coord1"] / 63,
values["coord2"] / 63,
values["coord3"] / 63,
]
if (
self.geometry.indices.data is not None
and self.geometry.indices.data.shape[-1] == 4
):
triangle_index = face_index % 2
face_index = face_index // 2
if triangle_index == 0:
# The sub indices are 0, 1, 2, so we just add the zero for index 3.
face_coord.append(0.0)
else:
# The sub indices are 0, 2, 3. The index 3 uses the
# face_coord slot of index 1, (see meshshader.py), so
# we put that at the end and put a zero in its place.
face_coord = face_coord[0], 0.0, face_coord[2], face_coord[1]
return {"face_index": face_index, "face_coord": tuple(face_coord)}

Would you perhaps be interested in contributing a PR for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants