1.5.1 • Published 8 months ago

msh-parser v1.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

msh-parser

msh-parser main image

NPM Package Build Size NPM Downloads License npm.io

Finite element .msh format loader and parser, works on binary and ascii .msh files – unit tested and written in TypeScript.

Live demo: apps.amandaghassaei.com/msh-parser/demo/

.msh File Format

The MSH file format is used for storing 3D finite element mesh information for simulation purposes. This library does not generate or render .msh files, but it will allow you to parse them in a browser or Nodejs environment. You can generate tetrahedral .msh files from boundary meshes (.stl, .obj) using TetWild. You can view .msh files with Gmsh or by dragging them into the demo app. Example .msh files can be found in test/msh/.

Installation

Install via npm

npm install msh-parser

and import into your project:

import { parseMSH, loadMSH, loadMSHAsync } from 'msh-parser';

Import into HTML

Import bundle/msh-parser.min.js directly into your html:

<html>
  <head>
    <script src="msh-parser.min.js"></script>
  </head>
  <body>
  </body>
</html>

MSHParserLib will be accessible globally:

const { parseMSH, loadMSH, loadMSHAsync } = MSHParserLib;

Use

Full API documentation in docs.

// Load and parse the .msh file using the specified file path.
loadMSH('./bunny.msh', (mesh) => {
  const {
    nodes,
    elementIndices,
    edgesIndices,
    exteriorEdgesIndices,
    exteriorFacesIndices,
    elementVolumes,
    nodalVolumes,
    isTetMesh,
    numExteriorNodes,
    boundingBox,
  } = mesh;
});
// Also try:
// const mesh = loadMSHAsync('./bunny.msh');

// Or parse synchronously from file buffer.
const mesh = parseMSH(fs.readFileSync('./bunny.msh'));
  • nodes is a Float32Array or Float64Array of length 3 * numNodes containing a flat list of node positions in the following order [x0, y0, z0, x1, y1, z1, ...].
  • elementIndices is a 2 dimensional array of node indices corresponding to the finite elements of the mesh. For a tetrahedral mesh, all elements will contain four node indices, but element length may vary for other types of meshes. elementIndices has the following structure:
[
  [e0a, e0b, e0c, e0d], // Element 0
  [e1a, e1b, e1c, e1d], // Element 1
  ...
]
  • edgesIndices (tet-meshes only for now) is a Uint32Array containing all pairs of edgesIndices in the mesh. Node indices are in the form: [e01, e02, e11, e12, ...]. edgesIndices is calculated when queried and then cached.
  • exteriorEdgesIndices (tet-meshes only for now) is a Uint32Array containing all pairs of exterior edgesIndices in the mesh. Node indices are in the form: [e01, e02, e11, e12, ...]. exteriorEdgesIndices is calculated when queried and then cached.
  • exteriorFacesIndices (tet-meshes only for now) is a 2 dimensional array of node indices corresponding to the exterior faces of the mesh. For a tetrahedral mesh, all exterior faces will be triangles, but face shape may vary for other types of meshes. Triangular exterior faces have CC winding order. exteriorFacesArray has the following structure:
[
  [f0a, f0b, f0c], // Face 0
  [f1a, f1b, f1c], // Face 1
  ...
]
  • elementVolumes (tet-meshes only for now) is a Float32Array containing the volume of all elements in the mesh. elementVolumes is calculated when queried and then cached.
  • nodalVolumes (tet-meshes only for now) is a Float32Array containing the approximate volume of all nodes in the mesh. This is calculated by evenly distributing element volume across adjacent nodes. nodalVolumes is calculated when queried and then cached.
  • isTetMesh is a boolean that indicates all mesh elements are tetrahedra.
  • numExteriorNodes (tet-meshes only for now) is the number of nodes that lie on the exterior of the mesh. nodes has been ordered so that the nodes in the range 0, numExteriorNodes - 1 correspond to the nodes referenced by exteriorFacesIndices.
  • boundingBox is the min and max of the mesh's bounding box in the form: { min: [x, y, z], max: [x, y, z] }. boundingBox is calculated when queried and then cached.

The mesh object returned by parseMSH, loadMSH, and loadMSHAsync also exposes methods for modifying its geometry:

mesh.scaleNodesToUnitBoundingBox();
  • MSHMesh.scaleNodesToUnitBoundingBox() scales the nodes values (in place) to fit inside a unit box and centered around the origin.

Build Notes

This library imports fs in Nodejs environments to load files from a url string. This code is never hit in the browser environment (the browser uses a XMLHttpRequest instead), but you may see build warnings due to the import('fs') statement in the code. (I'm wondering if there is a better way to handle this that is compatible for both Nodejs and browser environments, please let me know if you have ideas.)

To fix this warning, you need to set fs as an external dependency in your build settings:

// rollup.config.js
export default {
  ...
  external: ['fs'],
};
// vite.config.js
export default {
  build: {
    rollupOptions: {
      external: ['fs'],
    },
  }
  ...
}

Limitations

  • This package has mainly been tested on binary tetrahedral meshes generated by TetWild with double precision. If you are having trouble parsing a .msh with this library, please submit an issue and attach a sample file for testing or submit a pull request with a fix.
  • This package does not currently parse field data or tags, though it could be added by implementing more of the following class: https://github.com/PyMesh/PyMesh/blob/main/src/IO/MshLoader.cpp. Pull requests welcome.

Acknowledgements

License

This work is licensed under an MIT License. It depends on the following:

Related Libraries

Development

I don't have any plans to continue developing this package, but I'm happy to review pull requests if you would like to add a new feature.

To install dev dependencies:

npm install

To compile src to dist:

npm run build

Testing

To run tests:

npm run test

TODO:

1.5.1

8 months ago

1.5.0

10 months ago

1.4.0

10 months ago

1.3.0

10 months ago

1.2.10

10 months ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.1.2

1 year ago

1.2.9

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago