jupyter-widgets-contrib / ipygany

3-D Scientific Visualization in the Jupyter Notebook
BSD 3-Clause "New" or "Revised" License
487 stars 53 forks source link

Set colormap on init #99

Closed akaszynski closed 3 years ago

akaszynski commented 3 years ago

This PR allows you to set the default colormap on the initialization of IsoColor.

import numpy as np
from ipygany import Scene, PolyMesh, Component, IsoColor

# Create triangle indices
Nr = 100
Nc = 100

triangle_indices = np.empty((Nr - 1, Nc - 1, 2, 3), dtype=int)

r = np.arange(Nr * Nc).reshape(Nr, Nc)

triangle_indices[:, :, 0, 0] = r[:-1, :-1]
triangle_indices[:, :, 1, 0] = r[:-1, 1:]
triangle_indices[:, :, 0, 1] = r[:-1, 1:]

triangle_indices[:, :, 1, 1] = r[1:, 1:]
triangle_indices[:, :, :, 2] = r[1:, :-1, None]

triangle_indices.shape = (-1, 3)

# Create vertices
x = np.arange(-5, 5, 0.1)
y = np.arange(-5, 5, 0.1)

xx, yy = np.meshgrid(x, y, sparse=True)

z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)

vertices = np.empty((100, 100, 3))
vertices[:, :, 0] = xx
vertices[:, :, 1] = yy
vertices[:, :, 2] = z
vertices = vertices.reshape(10000, 3)

height_component = Component(name='value', array=z)

mesh = PolyMesh(
    vertices=vertices,
    triangle_indices=triangle_indices,
    data={'height': [height_component]}
)

# Colorize by curvature
colored_mesh = IsoColor(mesh, input=('height', 'value'), min=np.min(z), max=np.max(z),
                        colormap='Turbo')

scene = Scene([colored_mesh])
scene

jup

akaszynski commented 3 years ago

While I'm on this project, do you know if there's any way to enable smooth shading or custom lighting? I really like this library and want to see if I can map some of the features from vtk over to here. I'm working on adding this as a jupyter backend and want it to have similar functionality as our Plotter class.

martinRenou commented 3 years ago

do you know if there's any way to enable smooth shading or custom lighting?

Smooth shading is not currently possible. I guess we could allow this as an option.

Consider custom lighting, what are you thinking of? Do you have pyvista/vtk examples in mind?

I'm working on adding this as a jupyter backend and want it to have similar functionality as our Plotter class.

This is great. Do you think such back-end should live in ipygany directly?

akaszynski commented 3 years ago

Do you think such back-end should live in ipygany directly?

It could. Right now I'm writing something that wraps the Plotter class to allow us to generate a plot using the standard pyvista APIs and then plot it using ipygany: jup

This way, users can use the exact same scripts regardless of which backend they're using. I invested quite a bit of time in itkwidgets, and it's not working with jupyterlab 3.x, so I want to be able to switch backends without recreating a new api every time. This seems like to works well.

Perhaps you can also wrap a Plotting class, but since the details of the API are somewhat complex, it might be easier to put the wrapping on pyvista's side.

martinRenou commented 3 years ago

Perhaps you can also wrap a Plotting class, but since the details of the API are somewhat complex, it might be easier to put the wrapping on pyvista's side.

Well if most of the work is already done in PyVista, it might make sense to just rely on it. I have no problem in having PyVista as a hard dependency if that helps a bit.

akaszynski commented 3 years ago

Well if most of the work is already done in PyVista, it might make sense to just rely on it. I have no problem in having PyVista as a hard dependency if that helps a bit.

Not necessary. I would encourage you keeping your module as light as possible as VTK itself is a heavy dependency to have (they don't tend to update the wheels too often, and you can't always rely on conda).

Really appreciate the work you did here!

martinRenou commented 3 years ago

Thanks!

This work is a reboot of something I've done a few years ago. You can find a demo of it here: https://demo.logilab.fr/SciviJS. Not everything is reimplemented yet, we are still missing the ClipPlane, Slice, Vector glyph etc. But I am hoping to find time to do that.

Also you might see in this demo why I created ipytree ;) I am planning on having this kind of graphical interface in the Notebook.