Mesh Utilities
View Full Source & Details on GitHub
bitbybit.mesh works on plain triangle meshes. It needs no CAD kernel, so it runs on any triangulated data: a mesh you built point by point, or one taken from a shape.
How a mesh is written
- A triangle is a list of three points:
[[x1,y1,z1], [x2,y2,z2], [x3,y3,z3]]. - A mesh is a list of triangles.
- A plane is
{ normal: [nx,ny,nz], d }: a unit normal and the plane's distance from the origin along that normal. A pointXlies on the plane whennormal · X = d.
Planes and distances
calculateTrianglePlanegives the plane a triangle lies in. The normal follows the right-hand rule around the triangle's points, so[[0,0,0], [1,0,0], [0,1,0]]gives{ normal: [0,0,1], d: 0 }. A triangle with no area, whose points are on one line, has no plane and givesundefined;tolerance(default1e-7) sets how short the normal may be before the triangle counts as having no area.signedDistanceToPlanemeasures how far a point is from a plane, asnormal · point - d: positive on the side the normal points to, negative on the other.
Where triangles and meshes cut each other
triangleTriangleIntersectiongives the segment where two triangles cut through each other. Triangles that do not touch, are parallel, or lie in the same plane giveundefined, so two coplanar triangles that overlap over an area give no result.meshMeshIntersectionSegmentstests every triangle of one mesh against every triangle of the other and lists each segment found.meshMeshIntersectionPolylinesjoins those segments end to end into polylines, closed where the curve loops.meshMeshIntersectionPointsgives the same curves as lists of points. A closed curve repeats its first point at the end, so the loop is explicit when you draw it.
Each of these takes a tolerance (default 1e-7, in model units): distances below it count as zero when deciding whether triangles touch and when joining segment ends. Comparing every triangle with every other takes time on large meshes.
Volume and triangulation
signedVolumemeasures the volume a closed mesh encloses. It is positive when the triangles face outwards by the right-hand rule and negative when they all face inwards. A mesh that is not closed has no inside, and the number means nothing.triangulatePolygoncuts a flat polygon, with any holes through it, into triangles. Every corner of the outline stays a corner of a triangle, so neighbouring polygons that share an edge give a mesh closed along it. The triangles face the way the outline turns; holes may run either way.
Every function
| Function | What it does |
|---|---|
calculateTrianglePlane | Finds the plane a triangle lies in: its unit normal and its distance from the origin along that normal. |
meshMeshIntersectionPoints | Finds where the surfaces of two meshes cut through each other, as one list of points per curve. |
meshMeshIntersectionPolylines | Finds where the surfaces of two meshes cut through each other and joins the pieces into polylines, closed where the curve loops. |
meshMeshIntersectionSegments | Finds every segment where the surfaces of two meshes cut through each other, testing each triangle of one against each triangle of the other. |
signedDistanceToPlane | Measures how far a point is from a plane, with a sign: positive on the side the normal points to, negative on the other. |
signedVolume | Measures the volume a closed triangle mesh encloses, with a sign: positive when its triangles face outwards by the right-hand rule, negative when they all face inwards. |
triangleTriangleIntersection | Finds the segment where two triangles cut through each other. |
triangulatePolygon | Cuts a flat polygon, with any holes through it, into triangles. |