Working with tetrahedral meshes in Pybullet
Loading OBJ files for deformables is great for objects that are "floppy" or "thin" in nature, such as a tote bag, but not so good for objects that are more "squishy", like a sponge. This is because files such as .OBJ
or .STL
are surface (triangular) meshes, so they have no volumetric information to them.
So, instead we'll need to use a tetrahedral mesh in .VTK
format
Using GMSH
- Open the GMSH GUI (
./gmsh
within thebin/
directory of your GMSH install) - File -> Open -> Click on the STL file to load. Note: In theory GMSH should be able to open an OBJ, but OBJs tend to lead to import errors. If this is the case, use a STL of the same mesh instead.
- Geometry -> Elementary entities -> Add -> Volume
- When prompted with "Select volume boundary", click on the mesh
- If a popup mentions "A scripting command is going to be appended to a non-geo file", click on "Create new .geo file"
- When prompted with "Select hole boundaries", press 'e' since there are no holes in the cargo bag
- Click on Mesh -> 3D to view and enable the tetrahedral mesh
- Save the file
- File -> Export -> Specify the filename with a .VTK extension. If a VTK options window shows up, keep it in ASCII format. The "save all elements" checkbox does not seem to make a difference
This .VTK
file will look something like:
# vtk DataFile Version 2.0
bag_remesh, Created by Gmsh 4.11.1
ASCII
DATASET UNSTRUCTURED_GRID
POINTS 518 double
0.125 -0.125 -0.0250000003725
0.107393302023 -0.139757603407 -0.0250000003725
...
0.06279948249067006 -0.07503616573439448 0.09226952087427626
CELLS 2577 12107
3 0 1 2
3 0 37 1
...
3 384 385 382
4 308 427 121 442
4 188 405 186 422
...
4 424 468 357 481
CELL_TYPES 2577
5
5
...
5
10
10
...
10
Modifying the VTK
Pybullet will not be able to load this file right now, because it is a combination of surface (triangle) meshes and volume (tetrahedral) meshes. But, it is not too difficult to modify this file to remove the surface mesh and just leave behind the tet mesh.
Before this, we need to understand the file format:
- The line starting with
CELLS
has two values corresponding to NUM_CELLS
: The total number of (surface + volume) cells in the meshLIST_SIZE
: The number of values in theCELLS
list. This is equal to4*NUM_TRIS + 5*NUM_TETS
- Each line in the
CELLS
list starting with3
corresponds to a triangular cell, and any line starting with4
is a tetrahedral cell. - The
CELL_TYPES
line contains the same value (NUM_CELLS
) as was seen inCELLS
- Each line in
CELL_TYPES
is a5
for a triangular cell and10
for a tetrahedral cell.
To delete the surface (triangular) mesh,
- Count the number of triangular cells (
NUM_TRIS
) - Update the counts:
NUM_CELLS -= NUM_TRIS
LIST_SIZE -= 4*NUM_TRIS
- Delete the lines in
CELLS
starting with3
, and all5
s in theCELL_TYPES
- Update the
CELLS
andCELL_TYPES
lines with the new counts
After these modifications, Pybullet should be able to successfully import the mesh.
Texturing
To texture a tetrahedral mesh, there are two options:
- Generate the tetrahedral mesh from an OBJ which already has the correct UV mapping for a specified texture file, then apply the texture with
loadTexture
/changeVisualShape
- This should work given some preliminary tests with a VTK with a random UV mapping. But, this hasn't been fully tested yet (as of 4/10/23)
- Use the texture from an OBJ by loading both the OBJ and VTK together
- The OBJ and the VTK should have the exact same geometry
- Load the OBJ file through the main
filename
parameter, but pass the VTK file in throughsimFileName
. The resulting softbody will have the physical properties you'd expect from a tetrahedral mesh, but the same texture as the OBJ - This has been tested and works. Note: if you
removeBody
using the ID returned fromloadSoftBody
, it will leave a "ghost" visual of the OBJ. There may be a separate ID attached to the visual, but it is unclear what it is right now
Future TODOs
Apparently, there is a way to export just the tetrahedral mesh elements via Physical groups -- see this thread for info. For now though, this manual modification process works.