PK yX
7h plot_collisions.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Collision\nPerform a collision detection between two meshes.\n\nThis example uses the ``collision`` filter to detect the faces from one sphere\ncolliding with another sphere.\n\n
Note
Due to the nature of the [vtk.vtkCollisionDetectionFilter](https://vtk.org/doc/nightly/html/classvtkCollisionDetectionFilter.html),\n repeated uses of this method will be slower that using the\n ``vtk.vtkCollisionDetectionFilter`` directly. The first\n update of the filter creates two instances of [vtkOBBTree](https://vtk.org/doc/nightly/html/classvtkOBBTree.html),\n which can be subsequently updated by modifying the transform or\n matrix of the input meshes.\n\n This method assumes no transform and is easier to use for\n single collision tests, but it is recommended to use a\n combination of ``pyvista`` and ``vtk`` for rapidly computing\n repeated collisions. See the [Collision Detection Example](https://kitware.github.io/vtk-examples/site/Python/Visualization/CollisionDetection/)
\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\nimport pyvista as pv\n\npv.set_plot_theme(\"document\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create the main mesh and the secondary \"moving\" mesh.\n\nCollision faces will be plotted on this sphere, and to do so we\ninitialize an initial ``\"collisions\"`` mask.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sphere0 = pv.Sphere()\nsphere0[\"collisions\"] = np.zeros(sphere0.n_cells, dtype=bool)\n\n# This mesh will be the moving mesh\nsphere1 = pv.Sphere(radius=0.6, center=(-1, 0, 0))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setup the plotter open a movie, and write a frame after moving the sphere.\n\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pl = pv.Plotter()\npl.enable_hidden_line_removal()\npl.add_mesh(sphere0, show_scalar_bar=False, cmap=\"bwr\")\npl.camera_position = \"xz\"\npl.add_mesh(sphere1, style=\"wireframe\", color=\"green\", line_width=5)\n\n# for this example\npl.open_gif(\"collision_movie.gif\")\n\n# alternatively, to disable movie generation:\n# pl.show(auto_close=False, interactive=False)\n\ndelta_x = 0.05\nfor i in range(int(2 / delta_x)):\n sphere1.translate([delta_x, 0, 0])\n col, n_contacts = sphere0.collision(sphere1)\n\n collision_mask = np.zeros(sphere0.n_cells, dtype=bool)\n if n_contacts:\n collision_mask[col[\"ContactCells\"]] = True\n sphere0[\"collisions\"] = collision_mask\n pl.write_frame()\n\n # alternatively, disable movie plotting and simply render the image\n # pl.render()\n\npl.close()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK yXP plot_glyphs.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Plotting Glyphs (Vectors or PolyData)\n\nUse vectors in a dataset to plot and orient glyphs/geometric objects.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np\n\n# sphinx_gallery_thumbnail_number = 4\nimport pyvista as pv\nfrom pyvista import examples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Glyphying can be done via the :meth:`pyvista.DataSetFilters.glyph` filter\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh = examples.download_carotid().threshold(145, scalars=\"scalars\")\nmask = mesh[\"scalars\"] < 210\nmesh[\"scalars\"][mask] = 0 # null out smaller vectors\n\n# Make a geometric object to use as the glyph\ngeom = pv.Arrow() # This could be any dataset\n\n# Perform the glyph\nglyphs = mesh.glyph(orient=\"vectors\", scale=\"scalars\", factor=0.003, geom=geom)\n\n# plot using the plotting class\npl = pv.Plotter()\npl.add_mesh(glyphs, show_scalar_bar=False, lighting=False, cmap=\"coolwarm\")\npl.camera_position = [\n (146.53, 91.28, 21.70),\n (125.00, 94.45, 19.81),\n (-0.086, 0.007, 0.996),\n] # view only part of the vector field\ncpos = pl.show(return_cpos=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Another approach is to load the vectors directly to the mesh object and then\naccess the :attr:`pyvista.DataSet.arrows` property.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"sphere = pv.Sphere(radius=3.14)\n\n# make cool swirly pattern\nvectors = np.vstack(\n (\n np.sin(sphere.points[:, 0]),\n np.cos(sphere.points[:, 1]),\n np.cos(sphere.points[:, 2]),\n )\n).T\n\n# add and scale\nsphere[\"vectors\"] = vectors * 0.3\nsphere.set_active_vectors(\"vectors\")\n\n# plot just the arrows\nsphere.arrows.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the arrows and the sphere.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = pv.Plotter()\np.add_mesh(sphere.arrows, lighting=False, scalar_bar_args={\"title\": \"Vector Magnitude\"})\np.add_mesh(sphere, color=\"grey\", ambient=0.6, opacity=0.5, show_edges=False)\np.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Subset of Glyphs\n\nSometimes you might not want glyphs for every node in the input dataset. In\nthis case, you can choose to build glyphs for a subset of the input dataset\nby using a merging tolerance. Here we specify a merging tolerance of five\npercent which equates to five percent of the bounding box's length.\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Example dataset with normals\nmesh = examples.load_random_hills()\n\n# create a subset of arrows using the glyph filter\narrows = mesh.glyph(scale=\"Normals\", orient=\"Normals\", tolerance=0.05)\n\np = pv.Plotter()\np.add_mesh(arrows, color=\"black\")\np.add_mesh(mesh, scalars=\"Elevation\", cmap=\"terrain\", smooth_shading=True)\np.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK yX=uXm m plot_lighting.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Lighting Properties\n\nControl aspects of the rendered mesh's lighting such as Ambient, Diffuse,\nand Specular. These options only work if the ``lighting`` argument to\n``add_mesh`` is ``True`` (it's ``True`` by default).\n\nYou can turn off all lighting for the given mesh by passing ``lighting=False``\nto ``add_mesh``.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# sphinx_gallery_thumbnail_number = 4\nimport pyvista as pv\nfrom pyvista import examples\n\nmesh = examples.download_st_helens().warp_by_scalar()\n\ncpos = [\n (575848.0, 5128459.0, 22289.0),\n (562835.0, 5114981.5, 2294.5),\n (-0.5, -0.5, 0.7),\n]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First, lets take a look at the mesh with default lighting conditions\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh.plot(cpos=cpos, show_scalar_bar=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What about with no lighting\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh.plot(lighting=False, cpos=cpos, show_scalar_bar=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Demonstration of the specular property\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"p = pv.Plotter(shape=(1, 2), window_size=[1500, 500])\n\np.subplot(0, 0)\np.add_mesh(mesh, show_scalar_bar=False)\np.add_text(\"No Specular\")\n\np.subplot(0, 1)\ns = 1.0\np.add_mesh(mesh, specular=s, show_scalar_bar=False)\np.add_text(f\"Specular of {s}\")\n\np.link_views()\np.view_isometric()\np.show(cpos=cpos)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just specular\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh.plot(specular=0.5, cpos=cpos, show_scalar_bar=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Specular power\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh.plot(specular=0.5, specular_power=15, cpos=cpos, show_scalar_bar=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Demonstration of all three in use\n\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mesh.plot(diffuse=0.5, specular=0.5, ambient=0.5, cpos=cpos, show_scalar_bar=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK yX?+. plot_ray_trace.ipynb{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n\n# Ray Tracing\n\nSingle line segment ray tracing for PolyData objects.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import pyvista as pv\n\n# Create source to ray trace\nsphere = pv.Sphere(radius=0.85)\n\n# Define line segment\nstart = [0, 0, 0]\nstop = [0.25, 1, 0.5]\n\n# Perform ray trace\npoints, ind = sphere.ray_trace(start, stop)\n\n# Create geometry to represent ray trace\nray = pv.Line(start, stop)\nintersection = pv.PolyData(points)\n\n# Render the result\np = pv.Plotter()\np.add_mesh(\n sphere, show_edges=True, opacity=0.5, color=\"w\", lighting=False, label=\"Test Mesh\"\n)\np.add_mesh(ray, color=\"blue\", line_width=5, label=\"Ray Segment\")\np.add_mesh(intersection, color=\"maroon\", point_size=25, label=\"Intersection Points\")\np.add_legend()\np.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 0
}PK yX
7h plot_collisions.ipynbPK yXP plot_glyphs.ipynbPK yX=uXm m plot_lighting.ipynbPK yX?+. / plot_ray_trace.ipynbPK 5