Let's say that I generate a volume mesh using the following command:

Icosahedron[] // BoundaryDiscretizeGraphics // TriangulateMesh

which gives the following: enter image description here

As you can see from the images this is a volume mesh. When I export it as vtk, it just prints the boundary mesh only. Is there a way to export the volume mesh (the actual tetrahedrons)? I would like to visualise the mesh in ParaView.

Thanks in advance.

  • $\begingroup$ does Icosahedron[] // DiscretizeGraphics // TriangulateMesh give what you need? $\endgroup$ – kglr Apr 27 '20 at 9:51
  • $\begingroup$ No. It's the same thing. $\endgroup$ – Fotos Stylianou Apr 27 '20 at 9:58

It appears that Mathematica only exports surface data to VTK (i.e., Polygons) and the format that it exports in an old format and not the new XML format. The following bullet point from the Background & Context section of the VTK documentation describes the limitation.

  • Stores a single 3D object as a collection of line, point, and polygon primitives and their properties.

I looked up the structure of a VTK version 3.0 unstructured mesh format here.

Here is simple example of an export of a single unstructured quad element:

# vtk DataFile Version 3.0 vtk output by Tim Laska ASCII DATASET UNSTRUCTURED_GRID POINTS 4 double 0 0 0 1 0 0 0 1 0 1.1 1.1 0 CELLS 1 5 4 0 1 3 2 CELL_TYPES 1 10

It is relatively straightforward to output tetrahedra using Mathematica's StringTemplate functionality as shown in the following workflow that will write a tet mesh in the NotebookDirectory that I wrapped in a module.

m = Icosahedron[] // BoundaryDiscretizeGraphics // TriangulateMesh;
mcrd = MeshCoordinates[m];
minc = Delete[0] /@ MeshCells[m, 3];
(* Set Directory To Notebook *)
vtk[c_, i_, fn_ : "test.vtk"] := Module[
  {vtkTemplate, nPoints, nInc, nTotal,
   crdtmp, inctmp, typetmp, crd, inc, types, assoc, file
  vtkTemplate = 
  nPoints = Length@mcrd;
  nInc = Length@minc;
  nTotal = 5*nInc;
  crdtmp = StringTemplate["`1` `2` `3`\n"];
  inctmp = StringTemplate["4 `1` `2` `3` `4`\n"];
  typetmp = StringTemplate["`1`\n"];
  crd = StringJoin[crdtmp @@@ c];
  inc = StringJoin[inctmp @@@ (i - 1)];
  types = StringJoin[typetmp /@ ConstantArray[10, nInc]];
  assoc = <|"nPoints" -> nPoints, "nInc" -> nInc,
    "crd" -> crd, "inc" -> inc, "nTotal" -> nTotal,
    "types" -> types|>;
  (* Write stream *)
  file = OpenWrite[fn];
  WriteString[file, vtkTemplate[assoc]];
vtk[mcrd, minc, "myTest.vtk"];

Now, we can view a "crinkly" clip plane in Paraview, which shows that we have exported the tetrahedra and not just the surface mesh.

tetrahedral mesh

  • $\begingroup$ You are the boss. Thank you. $\endgroup$ – Fotos Stylianou Apr 28 '20 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.