tex = gfx.Texture(im, dim=2).get_view(filter="linear") material = gfx.MeshBasicMaterial(map=tex, side="front") geometry = gfx.box_geometry(100, 100, 100) cubes = [gfx.Mesh(geometry, material) for i in range(8)] for i, cube in enumerate(cubes): cube.position.set(350 - i * 100, 150, 0) scene.add(cube) background = gfx.Background(None, gfx.BackgroundMaterial((0, 1, 0, 1), (0, 1, 1, 1))) scene.add(background) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.position.set(0, 0, 500) controls = gfx.OrbitControls(camera.position.clone()) controls.add_default_event_handlers(canvas, camera) def animate(): for i, cube in enumerate(cubes): rot = gfx.linalg.Quaternion().set_from_euler( gfx.linalg.Euler(0.005 * i, 0.01 * i)) cube.rotation.multiply(rot) controls.update_camera(camera) renderer.render(scene, camera) canvas.request_draw()
voldata = imageio.volread("imageio:stent.npz").astype(np.float32) tex = gfx.Texture(voldata, dim=3) vol = gfx.Volume(gfx.Geometry(grid=tex), gfx.VolumeRayMaterial(clim=(0, 2000))) slice = gfx.Volume( gfx.Geometry(grid=tex), gfx.VolumeSliceMaterial(clim=(0, 2000), plane=(0, 0, 1, 0)) ) scene.add(vol, slice) for ob in (slice, vol): ob.position.set(*(-0.5 * i for i in voldata.shape[::-1])) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.position.z = 500 controls = gfx.OrbitControls(camera.position.clone(), up=gfx.linalg.Vector3(0, 0, 1)) controls.rotate(-0.5, -0.5) @canvas.add_event_handler("pointer_down", "pointer_up", "pointer_move", "wheel") def handle_event(event): if event["event_type"] == "pointer_down" and "Shift" in event["modifiers"]: info = renderer.get_pick_info((event["x"], event["y"])) if "index" in info: x, y, z = (max(1, int(i)) for i in info["index"]) print("Picking", x, y, z) tex.data[z - 1 : z + 1, y - 1 : y + 1, x - 1 : x + 1] = 2000 tex.update_range((x - 1, y - 1, z - 1), (3, 3, 3)) else: controls.handle_event(event, canvas, camera)
planes = [] for dim in [0, 1, 2]: # xyz abcd = [0, 0, 0, 0] abcd[dim] = -1 abcd[-1] = vol.shape[2 - dim] / 2 material = gfx.VolumeSliceMaterial(clim=(0, 2000), plane=abcd) plane = gfx.Volume(gfx.Geometry(grid=tex), material) planes.append(plane) scene.add(plane) # camera = gfx.PerspectiveCamera(70, 16 / 9) camera = gfx.OrthographicCamera(200, 200) camera.position.set(170, 170, 170) controls = gfx.OrbitControls( camera.position.clone(), gfx.linalg.Vector3(64, 64, 128), up=gfx.linalg.Vector3(0, 0, 1), zoom_changes_distance=False, ) controls.add_default_event_handlers(canvas, camera) # Add a slight tilt. This is to show that the slices are still orthogonal # to the world coordinates. for ob in planes + [mesh]: ob.rotation.set_from_axis_angle(gfx.linalg.Vector3(1, 0, 0), 0.1) def animate(): t = np.cos(time() / 2) * 0.5 + 0.5 # 0..1 planes[2].material.plane = 0, 0, -1, t * vol.shape[0] mesh.material.plane = 0, 0, -1, (1 - t) * vol.shape[0]