# %% [markdown] # # Visualization in notebooks # %% [markdown] # It is possible to visualize pyddg objects in jupyter notebooks. # For this we use the python package [k3d](https://k3d-jupyter.org/). # Objects in this package can be displayed as interactive widgets # in a jupyter notebook # or in even in a static html page. # On the top right of the widgets there is a `k3d` panel that # allows to take screenshots, show the widget in fullscreen # or modify objects. # %% import ddg import numpy as np # %% [markdown] # ## Converting pyddg objects to drawable objects # %% [markdown] # ### Default conversion # %% quadric = ddg.geometry.Quadric(np.diag((1, 1, -1, -1))) # %% [markdown] # To visualize this quadric as a smooth surface, # we first need to convert it to an instance of `k3d.objects.Drawable`. # %% hyperboloid = ddg.jupyter.convert(quadric) type(hyperboloid) # %% [markdown] # `k3d.objects.Drawable` objects can be previewed direclty # when outputed from a jupyter cell: # %% hyperboloid # %% [markdown] # ### Special conversions # %% [markdown] # To convert to a `k3d.objects.Drawable` containing only the edges of an object, run: # %% hyperboloid_edges = ddg.jupyter.edges(quadric) hyperboloid_edges # %% [markdown] # To get only the points of an object, run: # %% hyperboloid_points = ddg.jupyter.vertices(quadric) hyperboloid_points # %% [markdown] # ## Visualizing a 3D scene # %% [markdown] # To create a 3D scene from `k3d.objects.Drawable` objects, # one must add them to a `k3d.plot.Plot`. We provide a utility function # to render them in a clean and higher quality render setting. # One can combine objects to create more complex scenes: # %% ddg.jupyter.show_3d(hyperboloid, hyperboloid_points, hyperboloid_edges) # %% [markdown] # ## Visualizing a 2D scene # %% [markdown] # To create a 2D scene, we provide a similar function: # %% I = (-1, 0, 1) conics = [ddg.geometry.Quadric(np.diag((i, j, k))) for i in I for j in I for k in I] kobjs = [ddg.jupyter.convert(c) for c in conics] ddg.jupyter.show_2d(*kobjs) # %% [markdown] # Compared to 3D scenes, 2D scenes just have flat colors # and the camera looks from the top by default. # %% [markdown] # ## Interactive visualization # %% [markdown] # One can make interactive visualizations using widgets, # like the ones from `ipywigets`. # Let's make an empty plot for the visualization: # %% pencil = ddg.geometry.Pencil(quadric, ddg.geometry.Quadric(np.diag((1, 1, 1, -1)))) plot = ddg.jupyter.show_3d() plot # %% [markdown] # Now let's create a slider widgets and define a callback function # that places a quadric in the scene for each value of the slider. # %% from ipywidgets import FloatSlider, interact @interact(i=FloatSlider(value=0, min=-2, max=2)) def callback(i): global plot ddg.jupyter.clear_plot(plot) quadric = pencil.quadric(i) plot += ddg.jupyter.convert(quadric) # %% [markdown] # (The slider will take effect in the jupyter notebook, but not here in the docs.)