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

Scaling error #375

Closed modaresimr closed 1 year ago

modaresimr commented 1 year ago

Description

It seems that scaling parameter is not working correctly. It should be sx,sy,sz but it is sz,sy,sx

What I Did

import numpy as np
import matplotlib.pyplot as plt
from skimage import draw
siz=200

img2 = np.zeros((siz, siz//2,1))
rr, cc = draw.ellipse(siz/2, siz/4, siz*.4, siz*.2, img2.shape[:2])
img2[rr, cc,:] = 1

def myshow(img,spacing):      
        import k3d        
        plot = k3d.plot()
        plot+= k3d.voxels(img, opacity=0.1,scaling=spacing)
        plot.display()
myshow(img2,(1,2,1))
myshow(img2,(100,2,1)) # it should scale x but actually it scales z
myshow(img2,(1,1,2)) # it should scale y but actually it scales x

outputs

image image image

artur-trzesiok commented 1 year ago

Hi! Thanks for sharing

myshow(img2,(100,2,1)) # it should scale x but actually it scales z

but second images show scaling in x. Look at: image

myshow(img2,(1,1,2)) # it should scale y but actually it scales x

No, it should scale in z axis (1,1,2) -> (x,y,z). And that is exactly what is done:

image

modaresimr commented 1 year ago

I think there is a mis-interpretation between me and you: I explain my perspective: When we say voxel size it means that each voxel will have a bigger size, for example, if we have a voxel at position (0,0,0) to (1,1,1), for any voxel size, for all axis, in the axis labels we have (1,1,1) but from the visualization point of view, we will have a bigger voxel.

Therefore, First: In all of the above cases, the axis labels should be the same since we only change the voxel size

second: let's see img2(1,2,1): image

If you compare case myshow(img2,(1,2,1)) and myshow(img2,(1,1,2)), you can see that the only difference is that scale down y and scale up z.

what is your expectation? it should have more depth (z), and it should only scale down y by 2 and x with no change:

artur-trzesiok commented 1 year ago

@modaresimr I'm not sure what exactly do you want to get as outcome :(. Can you share some example data or sketch in paint?

Please make sure that we understand two topic similary:

First: image

in k3d z axis is directed to "up"

Second: In k3d when we talk about numpy dense data we have convention that shape is mapped as (L, H, W). So

img2.shape
(200, 100, 1)
z_size, y_size, x_size = img2.shape

That's exactly why our ellipse is displaying on YZ plane

artur-trzesiok commented 1 year ago

@modaresimr did that make sense to you?