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
953 stars 121 forks source link

Marching Cubes and Implicit Surfaces #340

Closed Davide-sd closed 2 years ago

Davide-sd commented 2 years ago

I'm trying to plot implicit surfaces. Also, I'm comparing K3D-Jupyter against Plotly: I think I found a major difference.

import k3d
import numpy as np

r = 2
zmin, zmax = -r, r
xmin, xmax = -r, r
ymin, ymax = -r, r
Nx, Ny, Nz = 100, 100, 100

x = np.linspace(xmin, xmax, Nx, dtype=np.float32)
y = np.linspace(ymin, ymax, Ny, dtype=np.float32)
z = np.linspace(zmin, zmax, Nz, dtype=np.float32)
x, y, z = np.meshgrid(x, y, z, indexing='ij')

p = x**2 + y**3 - z**2

plot = k3d.plot()
plt_iso = k3d.marching_cubes(p, compression_level=9, xmin=xmin, xmax=xmax,
                             ymin=ymin, ymax=ymax,
                             zmin=zmin, zmax=zmax, level=0.0,
                             flat_shading=False)
plot += plt_iso
plot.display()

K3D-1651527411281

import plotly.graph_objects as go
import numpy as np

X, Y, Z = np.mgrid[-2:2:40j, -2:2:40j, -2:2:40j]
values = X**2 + Y**3 - Z**2

col = "#2f77aa"
colorscale = [[0, col], [1, col]]
fig = go.Figure(data=go.Isosurface(
    x=X.flatten(),
    y=Y.flatten(),
    z=Z.flatten(),
    value=values.flatten(),
    isomin=0,
    isomax=0,
    showscale=False,
    colorscale=colorscale
    ))
fig.show()

newplot(1)

If I look at those two pictures, one mesh appears to be rotated. Which one of the two visualization is correct? I tried to look at Wolfram Alpha, sadly it doesn't show axis label...

Davide-sd commented 2 years ago

I have checked against a few Sage's examples: it appears that K3D is swapping a couple of axis.