Note
Go to the endto download the full example code or to run this example in your browser via JupyterLite or Binder.
Collision#
Perform a collision detection between two meshes.
This example uses thecollision filter to detect the faces from one spherecolliding with another sphere.
Note
Due to the nature of thevtk.vtkCollisionDetectionFilter,repeated uses of this method will be slower that using thevtk.vtkCollisionDetectionFilter directly. The firstupdate of the filter creates two instances ofvtkOBBTree,which can be subsequently updated by modifying the transform ormatrix of the input meshes.
This method assumes no transform and is easier to use forsingle collision tests, but it is recommended to use acombination ofpyvista andvtk for rapidly computingrepeated collisions. See theCollision Detection Example
importnumpyasnpimportpyvistaaspvpv.set_plot_theme("document")
Create the main mesh and the secondary “moving” mesh.
Collision faces will be plotted on this sphere, and to do so weinitialize an initial"collisions" mask.
Setup the plotter open a movie, and write a frame after moving the sphere.
pl=pv.Plotter()pl.enable_hidden_line_removal()pl.add_mesh(sphere0,show_scalar_bar=False,cmap="bwr")pl.camera_position="xz"pl.add_mesh(sphere1,style="wireframe",color="green",line_width=5)# for this examplepl.open_gif("collision_movie.gif")# alternatively, to disable movie generation:# pl.show(auto_close=False, interactive=False)delta_x=0.05foriinrange(int(2/delta_x)):sphere1.translate([delta_x,0,0])col,n_contacts=sphere0.collision(sphere1)collision_mask=np.zeros(sphere0.n_cells,dtype=bool)ifn_contacts:collision_mask[col["ContactCells"]]=Truesphere0["collisions"]=collision_maskpl.write_frame()# alternatively, disable movie plotting and simply render the image# pl.render()pl.close()

Total running time of the script: (0 minutes 10.379 seconds)
Estimated memory usage: 493 MB