K3D-tools / K3D-jupyter

K3D lets you create 3D plots backed by WebGL with high-level API (surfaces, isosurfaces, voxels, mesh, cloud points, vtk objects, volume renderer, colormaps, etc). The primary aim of K3D-jupyter is to be easy for use as stand alone package like matplotlib, but also to allow interoperation with existing libraries as VTK.
MIT License
941 stars 123 forks source link

Object Merging or hierarchy (feature request)? #430

Closed gpwood closed 3 months ago

gpwood commented 1 year ago

Hello,

I have a use case where plot objects exist in two parts (one a negative one positive). I'd like to "merge" each part into a single object so it appears as one item in the "objects" panel (rather than two). Is this possible?

So an example of this would be to take the six plots here: https://k3d-jupyter.org/reference/factory.marching_cubes.html#levels and merge all the hearts into one object and all the toruses into another, so that in the panel it displays only two objects rather than 6.

artur-trzesiok commented 1 year ago

Hi @gpwood ,

What kind of objects do you have? k3d.mesh? k3d.marching_cubes? Can you share a small code snippet with example data to recreate it on my machine?

gpwood commented 1 year ago

Hi @artur-trzesiok

This example is from the online documentation and gives 6 objects. One use case I have is to merge two or more of the plot objects together so it appears as only one object in the panel display. So for example, For each level below merge plt_heart and plt_torus together so you have 3 objects in the dropdown menu

import k3d
import numpy as np

t = np.linspace(-1.5, 1.5, 50, dtype=np.float32)
x, y, z = np.meshgrid(t, t, t, indexing='ij')
R = 1
r = 0.5

eq_heart = (x**2 + (9/4 * y**2) + z**2 - 1)**3 - (x**2 * z**3) - (9/200 * y**2 * z**3)
eq_torus = (x**2 + y**2 + z**2 + R**2 - r**2)**2 - 4 * R**2 * (x**2 + y**2)

plot = k3d.plot()

for i in range(3):
    level = 0 + i * 1.5

    plt_heart = k3d.marching_cubes(eq_heart, level=level,
                                   color=0xe31b23,
                                   xmin=-1.5, xmax=1.5,
                                   ymin=-1.5, ymax=1.5,
                                   zmin=-1.5, zmax=1.5,
                                   translation=[i * 3.5, 0, 0])
    plt_torus = k3d.marching_cubes(eq_torus, level=level,
                                   color=0x5aabac,
                                   xmin=-1.5, xmax=1.5,
                                   ymin=-1.5, ymax=1.5,
                                   zmin=-1.5, zmax=1.5,
                                   translation=[i * 3.5, 0, -3.5])

    plot += plt_heart
    plot += plt_torus

plot.display()
artur-trzesiok commented 9 months ago

Hi @gpwood !

Sorry for long delay with my response. We have something very simplified - one level of hierarchy in k3d. It's called "groups:

     plt_heart = k3d.marching_cubes(eq_heart, level=level,
                                   color=0xe31b23,
                                   xmin=-1.5, xmax=1.5,
                                   ymin=-1.5, ymax=1.5,
                                   zmin=-1.5, zmax=1.5,
                                   translation=[i * 3.5, 0, 0], group='heart', name='Heart {}'.format(i+1))
    plt_torus = k3d.marching_cubes(eq_torus, level=level,
                                   color=0x5aabac,
                                   xmin=-1.5, xmax=1.5,
                                   ymin=-1.5, ymax=1.5,
                                   zmin=-1.5, zmax=1.5,
                                   translation=[i * 3.5, 0, -3.5], group='torus', name='Torus {}'.format(i+1))

If you will add group parameter to objects they will be merged in panel: image

But still you will have control over each object: image

Is it usefull for you?