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
916 stars 123 forks source link

Timeseries mesh interpolation issues #418

Closed bsekura closed 6 months ago

bsekura commented 1 year ago

However, the problem exists on different combinations of Python and OS: e.g. Python 3.10, Linux

Description

I am animating triangle mesh using timeseries. Basically, the keyframes are built first, as a list of np.array(num_verts, 3) and then packaged as dictionary:

frames = [np.array(num_verts, 3)] # pseudo code
verts = { str(t * 0.016): frames[t] for t in range(len(frames)) } # 60 FPS
# plot
mesh = k3d.mesh(verts, indices, color=0xb9aebc)

process_transform_arguments(mesh,
                            translation=[0, 0, 0],
                            rotation=[1.5, np.pi, 0, 0],
                            scaling=[1,1,1])

plot = k3d.plot()
plot += mesh
plot.background_color = 0x444455
plot.display()

Problem

Occasionally, as the animation plays, some frames seem to contain garbled vertices, nans or infinities, which manifests itself as flickering and triangles exploding all over the place. It seems random: often it's perfect and then upon re-running the cell the animation has errors.

I have relatively large number of frames, in the range of 100-300, and at first I thought the problem was in a dictionary because of timestep being converted to string. However, I moved the dictionary creation to a preceding cell, so that it's created once. When I re-run the next cell that contains k3d plotting, sometimes results are perfect, sometimes garbled, so it seems to point to interpolation code on k3d side.

What I Did

I'm running jupyter-notebook locally, set up in a virtual env like this:

pip install jupyterlab numpy k3d
artur-trzesiok commented 1 year ago

Hi @bsekura !

I did some test. First for all it is hard to reproduce your case without data. Many unexpected behaviour will happen if number of vertices will change during some keyframes. That is not allowed in fact (because how to interpolate values then?). If I fit to the same amount of vertices everything looks good:

My example:

import k3d
import numpy as np

N = 100

theta = np.linspace(0, 2.0 * np.pi, N)
phi = np.linspace(0, 2.0 * np.pi, N)
theta, phi = np.meshgrid(theta, phi)

def get_vertices():
    global theta, phi

    c, a = 2, 1

    x = (c + a * np.cos(theta)) * np.cos(phi)
    y = (c + a * np.cos(theta)) * np.sin(phi)
    z = a * np.sin(theta)

    return np.dstack([x, y, z]).astype(np.float32)

vertices1 = get_vertices()
vertices2 = get_vertices()

indices = (np.stack([
    np.arange(N*N - N - 1) + 0, np.arange(N*N - N - 1) + N, np.arange(N*N - N - 1) + N + 1,
    np.arange(N*N - N - 1) + 0, np.arange(N*N - N - 1) + N + 1, np.arange(N*N - N - 1) + 1
]).T).astype(np.uint32)

plot = k3d.plot()

vertices2 = vertices2 + (np.sin(vertices2 * 5) * 0.15) * np.array([0,1,0]) \
                      + (np.cos(vertices2 * 3) * 0.15) * np.array([1,0,0])

vertices = {
    "0.0": vertices1,
    "1.0": vertices2,
    "2.0": vertices1
}

mesh = k3d.mesh(vertices, indices, flat_shading=False, 
                 shader='mesh',
                 attribute=phi, 
                 color_map=k3d.matplotlib_color_maps.twilight,
                 color=0x00ff00)

plot += mesh

plot.colorbar_object_id = 0
plot.display()

Can you run that on your machine and tell me if you observed some problems?

artur-trzesiok commented 10 months ago

@bsekura any updates?