Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
williamstein
GitHub Repository: williamstein/scratch
Path: blob/main/k3d.ipynb
Views: 39
Kernel: Python 3 (system-wide)
import k3d plot = k3d.plot() vertices = [[0, 0, 0], [1, 0, 0], [0, 0, 1]] indices = [[0, 1, 2]] mesh = k3d.mesh(vertices, indices) plot += mesh plot.display()
plot += k3d.mesh([0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 1, 1], [0, 1, 2, 3, 4, 5], color=0x00ff00) plot
import k3d import numpy as np x = np.random.randn(100000,3).astype(np.float32) point_size = 0.2 plot = k3d.plot(name='points') plt_points = k3d.points(positions=x, point_size=0.2) plot += plt_points plt_points.shader='3d' plot.display()
f = (np.sum(x ** 3 - .1 * x ** 2, axis=1)) colormap = k3d.colormaps.basic_color_maps.WarmCool colors = k3d.helpers.map_colors(f, colormap, [-2, .1]) colors = colors.astype(np.uint32) plt_points.colors = colors
import numpy as np import k3d plot = k3d.plot(name='Wiener process') N = 1000 traj = np.cumsum(np.random.randn(N, 3).astype(np.float32), axis=0) plt_line = k3d.line(traj, shader='mesh', width=0.5) plt_line2 = k3d.line([traj[0], traj[-1]], shader='mesh', width=.5, color=0xff0000) plot += plt_line plot += plt_line2 plot.display()
import k3d import numpy as np plot = k3d.plot() Nx, Ny = 50, 60 xmin, xmax, ymin, ymax = -3, 3, 0, 3 x = np.linspace(xmin, xmax, Nx, dtype=np.float32) y = np.linspace(ymin, ymax, Ny, dtype=np.float32) x, y = np.meshgrid(x, y) f = np.sin(x ** 2 + y ** 2) plt_surface = k3d.surface(f, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) plot += plt_surface plot.display()
# this code is a part of matplotlib trisurf3d_demo import numpy as np import k3d from matplotlib.tri import Triangulation plot = k3d.plot() n_radii = 8 n_angles = 36 radii = np.linspace(0.125, 1.0, n_radii, dtype=np.float32) angles = np.linspace(0, 2 * np.pi, n_angles, endpoint=False, dtype=np.float32)[..., np.newaxis] x = np.append(np.float32(0), (radii * np.cos(angles)).flatten()) y = np.append(np.float32(0), (radii * np.sin(angles)).flatten()) z = np.sin(-x * y) indices = Triangulation(x, y).triangles.astype(np.uint32) plt_mesh = k3d.mesh(np.vstack([x, y, z]).T, indices, color_map=k3d.colormaps.basic_color_maps.Jet, attribute=z, color_range=[-1.1, 2.01] ) plot += plt_mesh plot.display()
plt_mesh.attribute = {str(t): 3 * t * x ** 2 + y ** 2 for t in np.linspace(0, 2, 20)}
import k3d import numpy as np from numpy import sin,cos,pi plot = k3d.plot() T = 1.6 r = 6 zmin, zmax = -r, r xmin, xmax = -r, r ymin, ymax = -r, r Nx, Ny, Nz = 37, 37, 37 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 = 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)) 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=True) plot += plt_iso plot += plt_iso plot.display()
import k3d import numpy as np g = 9.81 v0 = 24 plot = k3d.plot() for alpha_deg in [10, 30, 45, 60, 85]: alpha = np.radians(alpha_deg) t_end = 2 * v0 * np.sin(alpha) / g t = np.linspace(0, t_end, 100) # note .T at the end, k3d takes data (x1,y1,z1),(x2,y2,z2)... traj3d = np.stack([v0 * t * np.cos(alpha), \ 20 * alpha + np.zeros_like(t), \ v0 * t * np.sin(alpha) - g * t ** 2 / 2]).T.astype(np.float32) plt_traj = k3d.line(traj3d) plt_text = k3d.text('h_{max}', position=[float(np.cos(alpha) * t_end * v0 / 2), float(20 * alpha), float((v0 * np.sin(alpha)) ** 2 / (2 * g))], color=0xff0000, size=1) plt_text2d = k3d.text2d( r'\text{ballistic trajectory: }\; h=v_0 t \sin \alpha - \frac{g t^2}{2}', position=[0.0, 0.0], color=0x0000ff, size=1) plt_texture_text = k3d.texture_text('START', position=[0, 0, 0], font_face='Calibri', color=255, size=5) if alpha_deg == 45: plt_label = k3d.label('Optimal angle', traj3d[-1].tolist(), mode='dynamic', is_html=True, color=0xff00f0) plot += plt_label plot += plt_text plot += plt_text2d plot += plt_texture_text plot += plt_traj plot.display()
import k3d import k3d.platonic as platonic import math plot = k3d.plot() meshes = [ platonic.Dodecahedron().mesh, platonic.Cube().mesh, platonic.Icosahedron().mesh, platonic.Octahedron().mesh, platonic.Tetrahedron().mesh ] colors = [0xff0000, 0x00ff00, 0x0000ff, 0xffff00, 0xff00ff] for i, obj in enumerate(meshes): rad = math.radians(i / len(meshes) * 360) radius = 3.5 obj.transform.translation = [math.sin(rad) * radius, math.cos(rad) * radius, 0] obj.color = colors[i] plot += obj plot.display()
import k3d import numpy as np x = np.random.randn(100,3).astype(np.float32) plot = k3d.plot(name='points') plt_points = k3d.points(positions=x, point_size=0.2, shader='3d') plot += plt_points plot.display()
from time import sleep for t in range(10): plt_points.positions = x - t/10*x/np.linalg.norm(x,axis=-1)[:,np.newaxis] sleep(0.5)
import numpy as np import k3d plot = k3d.plot() iteration = 4 size = 3 ** iteration voxels = np.ones((size, size, size)) def iterate(length, x, y, z): nl = length // 3 if nl < 1: return margin = (nl - 1) // 2 voxels[z - margin:z + margin + 1, y - margin:y + margin + 1, :] = 0 voxels[z - margin:z + margin + 1, :, x - margin:x + margin + 1] = 0 voxels[:, y - margin:y + margin + 1, x - margin:x + margin + 1] = 0 for ix, iy, iz in np.ndindex((3, 3, 3)): if (1 if ix != 1 else 0) + (1 if iy != 1 else 0) + (1 if iz != 1 else 0) != 2: iterate(nl, x + (ix - 1) * nl, y + (iy - 1) * nl, z + (iz - 1) * nl) iterate(size, size // 2, size // 2, size // 2) plot += k3d.voxels(voxels.astype(np.uint8), color_map=(0xffff00)) plot.display()