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

Add mesh 3d #376

Merged
merged 14 commits into from
Sep 19, 2024
Merged

Add mesh 3d #376

merged 14 commits into from
Sep 19, 2024

Conversation

nvaytet
Copy link
Member

@nvaytet nvaytet commented Sep 17, 2024

This adds a plot that can take in a list of vertices and faces and construct a 3D mesh.

This opens the way for representing full instrument geometries in 3D.

Notes:

For now we only support vertex colors, we could add face colors in the future.
This also supports showing only a single mesh at a time, unlike the scatter3d plot who can take in a dict of data arrays.

Supersedes #346

@nvaytet nvaytet marked this pull request as ready for review September 18, 2024 10:11
@nvaytet
Copy link
Member Author

nvaytet commented Sep 18, 2024

@g5t would be great if you could also give this a go, to see if it does what you want.

docs/user-guide/plot-types/mesh3d-plot.ipynb Outdated Show resolved Hide resolved
"source": [
"## Creating a mesh plot\n",
"\n",
"We can now send the data to the `mesh3d` function for rendering (we color the mesh according to z position):"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make mesh3d a link to the function?

src/plopp/backends/pythreejs/mesh3d.py Outdated Show resolved Hide resolved
edgecolor: str | None = None,
artist_number: int = 0,
):
import pythreejs as p3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if pythreejs is not installed or python is not running in Jupyter? Does the backend manager raise an appropriate exception or does this need to happen here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea. It's the same in scatter3d.
I don't think it's an issue not running in Jupyter, as the unit tests pass fine.

If it's not installed, you will just get ImportError on pythreejs?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not installed, you will just get ImportError on pythreejs?

I would assume so. Maybe this is something that the backend manager can catch so we don't have to handle it in everywhere. But that is unrelated to this PR.

self._data.coords["y"].values.astype('float32', copy=False),
self._data.coords["z"].values.astype('float32', copy=False),
]
).T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't know what size of mesh this can or should support. But you could avoid extra copies here by not taking x,y,z apart into separate variables. Instead, you could use

p3.BufferAttribute(positions.values.astype('float32', copy=False))

(copy=False doesn't do anything here because the positions vector is always float64.)

Copy link
Member Author

@nvaytet nvaytet Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split up the coordinates because I wanted the mesh3dfigure to behave a bit like scatter3dfigure, with checking the units of the x,y,z coordinates and setting the axis labels.

I guess I could keep the vector vertices also in the data and use that directly if they are present (fallback to the x,y,z if a vector coord is not found)? That would maybe only make 1 copy instead of 2? It would be a slightly strange interface/convention, but maybe the reduced cost is worth it?

# TODO: for now we only update the colors of the mesh. Updating the positions
# of the vertices is doable but is made more complicated by the edges geometry,
# whose positions cannot just be updated. A new geometry and edge lines would
# have to be created, the old one removed from the scene and the new one added.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't looks like update changes the colours to me. That is what set_colors does. So could update simply raise an exception instead.
As it stands, this could hide errors.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's true that the colors are not changed. However, the values of the ._data should be updated, because this is what the colormapper uses to determine the colorbar range. So it should not raise, but I will update the comment.

"source": [
"## Loading mesh data\n",
"\n",
"We load a file containing the data to construct the [Utah teapot](https://en.wikipedia.org/wiki/Utah_teapot)."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain the data format? It is not obvious to me how to encode vertices and faces.

@nvaytet nvaytet merged commit db3bbf6 into main Sep 19, 2024
4 checks passed
@nvaytet nvaytet deleted the new-mesh-3d branch September 19, 2024 08:22
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

Successfully merging this pull request may close these issues.

2 participants