T = 1.618033988749895
from numpy import sin,cos,pi
r = 4.77
zmin,zmax = -r,r
xmin,xmax = -r,r
ymin,ymax = -r,r
Nx,Ny,Nz = 80,80,80

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

%time p = 2 - (cos(x + T*y) + cos(x - T*y) + cos(y + T*z) \
               + cos(y - T*z) + cos(z - T*x) + cos(z + T*x))

p3d_1 = k3d.marching_cubes(p,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax,\
                           level=0.0)
plot += p3d_1

plot.display()


p3d_1.color = 0x114455

"""
Explanation: let us avaluate a function of 3 variables on relatively large mesh
End of explanation
"""


p3d_1.level=-0.1

from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets

@interact(l=widgets.FloatSlider(value=-.1,min=-3,max=1.1))
def g(l):
    p3d_1.level=-l


"""
Explanation: isolevel can be changed from Python side:
End of explanation
"""


plot.camera_auto_fit = False
plot.grid_auto_fit = False

"""
Explanation: to avoid recentering one can disable camera auto fit:
End of explanation
"""


%%time
p =(x**2+y**2+z**2+2*y-1)*((x**2+y**2+z**2-2*y-1)**2-8*z**2)+16*x*z*(x**2+y**2+z**2-2*y-1)
plot += k3d.marching_cubes(p,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax, level=0.0,color=0xff0000)

%%time 
p = x**2 + y**2 - z**2 -0.
plot += k3d.marching_cubes(p,xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax, zmin=zmin, zmax=zmax, level=0.0,color=0x00ff00)

"""
Explanation: one can add other plots to the same scene:
End of explanation
"""


