зеркало из https://github.com/microsoft/scenepic.git
137 строки
6.0 KiB
Plaintext
137 строки
6.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import numpy as np\r\n",
|
|
"import scenepic as sp\r\n",
|
|
"\r\n",
|
|
"\r\n",
|
|
"# when we build a ScenePic we are essentially building a web\r\n",
|
|
"# page, and the ScenePic will automatically populate parts of\r\n",
|
|
"# that webpage. \r\n",
|
|
"\r\n",
|
|
"# the scene object acts as the root of the entire ScenePic environment\r\n",
|
|
"scene = sp.Scene()\r\n",
|
|
"\r\n",
|
|
"# you can use it to create one or more canvases to display 3D or 2D\r\n",
|
|
"# objects. Canvas objects display Frames. For a static ScenePic, there\r\n",
|
|
"# will only be a single Frame, but you can use multiple frames to create\r\n",
|
|
"# an animation or to display a range of visualizations in the same visual\r\n",
|
|
"# space. We will create one 3D canvas to display the full scene, and then\r\n",
|
|
"# some 2D canvases which will show projections of the scene.\r\n",
|
|
"main = scene.create_canvas_3d(width=600, height=600)\r\n",
|
|
"projx = scene.create_canvas_2d(\"projx\", width=200, height=200)\r\n",
|
|
"projy = scene.create_canvas_2d(\"projy\", width=200, height=200)\r\n",
|
|
"projz = scene.create_canvas_2d(\"projz\", width=200, height=200)\r\n",
|
|
"\r\n",
|
|
"# the scene object is also used to create Mesh objects that will be added\r\n",
|
|
"# to frames. We are going to create an animation of some spheres orbiting\r\n",
|
|
"# a fixed cube, so let's create a default unit cube to start.\r\n",
|
|
"cube = scene.create_mesh(\"cube\")\r\n",
|
|
"\r\n",
|
|
"# the Mesh object has a variety of methods for adding primitive objects\r\n",
|
|
"# or loading arbitrary mesh geometry. In this example, we will just\r\n",
|
|
"# be using primitives, but the python tutorial shows all the various\r\n",
|
|
"# methods for adding geometry to a mesh.\r\n",
|
|
"cube.add_cube(color=sp.Colors.White)\r\n",
|
|
"\r\n",
|
|
"# let's create our spheres as well, using some different colors\r\n",
|
|
"sphere_names = [\"red\", \"green\", \"blue\"]\r\n",
|
|
"sphere_colors = [sp.Colors.Red, sp.Colors.Green, sp.Colors.Blue]\r\n",
|
|
"spheres = []\r\n",
|
|
"for name, color in zip(sphere_names, sphere_colors):\r\n",
|
|
" # by placing each sphere on a different layer, we can toggle them on and off\r\n",
|
|
" sphere = scene.create_mesh(\"{}_sphere\".format(name), layer_id=name)\r\n",
|
|
" sphere.add_sphere(color=color, transform=sp.Transforms.scale(0.5))\r\n",
|
|
" spheres.append(sphere)\r\n",
|
|
"\r\n",
|
|
"# now we will iteratively create each frame of the animation.\r\n",
|
|
"for i in range(180):\r\n",
|
|
" # first we create a frame object. This will be used to populate\r\n",
|
|
" # the 3D canvas.\r\n",
|
|
" main_frame = main.create_frame()\r\n",
|
|
"\r\n",
|
|
" # Now that we have a frame, we can add the cube mesh to the frame\r\n",
|
|
" main_frame.add_mesh(cube)\r\n",
|
|
"\r\n",
|
|
" # Next, we add the spheres. ScenePic has a variety of useful tools\r\n",
|
|
" # for operating on 3D data. Some of the most useful enable us to\r\n",
|
|
" # create transforms to move meshes around. Let's create the\r\n",
|
|
" # transforms for our three rotating spheres and add them to the frame.\r\n",
|
|
" # NB The Python interface uses numpy for matrices and vectors.\r\n",
|
|
" positions = np.concatenate([np.eye(3, dtype=np.float32) * 1.3,\r\n",
|
|
" np.ones((3, 1), dtype=np.float32)], axis=-1)\r\n",
|
|
" inc = 2 * np.pi / 180\r\n",
|
|
" positions[0] = sp.Transforms.RotationAboutYAxis(inc * i) @ positions[0].T\r\n",
|
|
" positions[1] = sp.Transforms.RotationAboutZAxis(2 * inc * i) @ positions[1].T\r\n",
|
|
" positions[2] = sp.Transforms.RotationAboutXAxis(3 * inc * i) @ positions[2].T\r\n",
|
|
" positions = positions[:, :3]\r\n",
|
|
" for sphere, position in zip(spheres, positions):\r\n",
|
|
" transform = sp.Transforms.translate(position)\r\n",
|
|
" main_frame.add_mesh(sphere, transform=transform)\r\n",
|
|
"\r\n",
|
|
" # now we'll populate our projections\r\n",
|
|
" for j, proj in enumerate([projx, projy, projz]):\r\n",
|
|
" proj_frame = proj.create_frame()\r\n",
|
|
"\r\n",
|
|
" # 2D frames work in pixels (as oppose to world units) so we need\r\n",
|
|
" # to convert positions to pixels.\r\n",
|
|
" proj_frame.add_rectangle(75, 75, 50, 50, fill_color=sp.Colors.White)\r\n",
|
|
" points = np.roll(positions, j, axis=1)[:, 1:]\r\n",
|
|
" points[:, 1] *= -1\r\n",
|
|
" points = points * 50 + 100\r\n",
|
|
"\r\n",
|
|
" for point, color in zip(points, sphere_colors):\r\n",
|
|
" proj_frame.add_circle(point[0], point[1], 12.5, fill_color=color)\r\n",
|
|
"\r\n",
|
|
" proj_frame.add_text(proj.canvas_id, 10, 190, size_in_pixels=16)\r\n",
|
|
" \r\n",
|
|
"# this will make user interactions happen to all canvases simultaneously\r\n",
|
|
"scene.link_canvas_events(main, projx, projy, projz)\r\n",
|
|
"\r\n",
|
|
"# ScenePic provides some useful layout controls by exposing CSS grid commands\r\n",
|
|
"scene.grid(width=\"800px\", grid_template_rows=\"200px 200px 200px\", grid_template_columns=\"600px 200px\")\r\n",
|
|
"scene.place(main.canvas_id, \"1 / span 3\", \"1\")\r\n",
|
|
"scene.place(projx.canvas_id, \"1\", \"2\")\r\n",
|
|
"scene.place(projy.canvas_id, \"2\", \"2\")\r\n",
|
|
"scene.place(projz.canvas_id, \"3\", \"2\")\r\n",
|
|
"\r\n",
|
|
"# display the scene in Jupyter\r\n",
|
|
"scene\r\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.9.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|