npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

msh-parser

v1.5.1

Published

Finite element .msh format parser, written in TypeScript.

Downloads

23

Readme

msh-parser

msh-parser main image

NPM Package Build Size NPM Downloads License

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:

  • Test with sample files from https://people.sc.fsu.edu/~jburkardt/data/msh/msh.html
  • Add double precision msh file in tests