Bitbybit Docs
    Preparing search index...

    Class MeshBitByBit

    Geometry on plain triangle meshes: a mesh is a list of triangles, each three points. The methods here work out the plane of a triangle, the distance from a point to a plane, and where two meshes cut through each other, as segments, as polylines or as point lists, measure the volume a closed mesh holds, and build simple meshes (an icosphere, a frustum). Indexed meshes, which store each point once, convert to and from triangle lists and take smooth or creased normals and consistent winding. They need no CAD kernel, so they run on any triangulated data.

    Index

    Constructors

    base

    • Measures how far a point is from a plane, with a sign: positive on the side the normal points to, negative on the other.

      Example: point [0,5,0] and the XZ plane with normal [0,1,0] -> 5

      Parameters

      Returns number

      The signed distance in model units

      const above = bitbybit.mesh.signedDistanceToPlane({ point: [0, 5, 0], plane: { normal: [0, 1, 0], d: 0 } });
      

    indexed

    • Turns a list of triangles into an indexed mesh, which stores each point once.

      Corners that agree within tolerance in every coordinate become one point, kept where it first appears, so triangles that meet share their corners and the mesh knows its neighbours. Example: two triangles that share an edge -> 4 points and 6 indices

      Parameters

      Returns IndexedMesh

      The indexed mesh

      const indexed = bitbybit.mesh.toIndexed({ mesh: bitbybit.mesh.icosphere({ radius: 1, detail: 1 }) });
      
    • Turns an indexed mesh back into a list of triangles, each written out as its three points.

      Normals the mesh carries are left out, since a triangle list has no place for them. Example: 4 points and indices [0, 1, 2, 1, 3, 2] -> 2 triangles

      Parameters

      Returns Mesh3

      The triangles

      const triangles = bitbybit.mesh.fromIndexed({ mesh: { positions: [0, 0, 0, 1, 0, 0, 0, 1, 0], indices: [0, 1, 2] } });
      
    • Gives an indexed mesh smooth normals, one per point, so it shades without visible facets.

      Each point's normal sums those of the triangles around it, a large triangle counting for more than a sliver. The points and triangles stay as they are. Example: a welded cube -> 2 corner normals on diagonals, 6 tipped 19.47 degrees

      Parameters

      Returns IndexedMesh

      The mesh with normals filled in

      const smooth = bitbybit.mesh.smoothNormals({ mesh: bitbybit.mesh.toIndexed({ mesh: bitbybit.mesh.icosphere({ radius: 1, detail: 2 }) }) });
      
    • Gives an indexed mesh normals that keep its sharp edges sharp and smooth the rest.

      Where triangles turn from each other by more than angle, the point between them is split, one copy per side, so a cube keeps hard edges while a cylinder's side shades smoothly. Example: a welded cube, angle 30 -> 24 points, 4 per face

      Parameters

      Returns IndexedMesh

      The mesh with its points split at sharp edges and normals filled in

      const shaded = bitbybit.mesh.creasedNormals({ mesh: indexed, angle: 30 });
      
    • Splits an indexed mesh into its connected pieces and finds the triangles that wind against the first triangle of their piece.

      Triangles that share an edge belong to one piece; an edge more than two share is not followed. Example: two separate triangles -> count 2, nothing to turn over

      Parameters

      Returns MeshWindingPieces

      Per triangle, whether to turn it over and its piece, and the number of pieces

      const { count, pieces } = bitbybit.mesh.windingPieces({ mesh: indexed });
      
    • Turns over the triangles of an indexed mesh that wind against the rest of their piece.

      Each connected piece keeps the winding most of its area has, so a few triangles written inside out are mended and a piece wound inward on purpose stays. Example: a cube with one face inside out -> every face outward

      Parameters

      Returns IndexedMesh

      The mesh with those triangles turned over; normals are left out

      const mended = bitbybit.mesh.orientConsistently({ mesh: indexed });
      

    measure

    • 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.

      A mesh that is not closed has no inside, and the number it gives means nothing. Example: a unit cube facing outwards -> 1

      Parameters

      Returns number

      The enclosed volume in cubic model units

      const volume = bitbybit.mesh.signedVolume({ mesh: [[[0, 0, 0], [0, 1, 0], [1, 0, 0]], [[0, 0, 0], [1, 0, 0], [0, 0, 1]], [[0, 0, 0], [0, 0, 1], [0, 1, 0]], [[1, 0, 0], [0, 1, 0], [0, 0, 1]]] });
      
    • Tells whether two triangle meshes come within a distance of each other, touching and crossing included, as a quick collision test.

      It stops at the first triangles close enough; a mesh, or any separate piece of one, wholly inside a closed one counts too. Example: two unit cubes half a unit apart, distance 0.6 -> true; distance 0.4 -> false

      Parameters

      Returns boolean

      True when the meshes come within the distance

      const close = bitbybit.mesh.meshesWithin({ mesh1: bracket, mesh2: bolt, distance: 0.5 });
      
    • Tells whether a point lies inside a closed triangle mesh.

      Rays from the point in three directions count the triangles they cross; an odd count for most of them means inside. A mesh that is not closed has no inside, and the answer means nothing. Example: [0.5, 0.5, 0.5] in a unit cube -> true

      Parameters

      Returns boolean

      True when the point is inside the mesh

      const inside = bitbybit.mesh.isPointInside({ mesh: housing, point: [0, 0, 5] });
      

    mesh

    • Finds every segment where the surfaces of two meshes cut through each other, testing each triangle of one against each triangle of the other.

      Example: a cube mesh and a sphere mesh -> the segments that together trace their intersection curve

      Parameters

      Returns Segment3[]

      The crossing segments, in no particular order

      const segments = bitbybit.mesh.meshMeshIntersectionSegments({ mesh1: cubeTriangles, mesh2: sphereTriangles, tolerance: 1e-7 });
      
    • Finds where the surfaces of two meshes cut through each other and joins the pieces into polylines, closed where the curve loops.

      Example: a cube mesh and a sphere mesh -> closed polylines where the two surfaces meet

      Parameters

      Returns Polyline3[]

      The intersection curves as polylines

      const curves = bitbybit.mesh.meshMeshIntersectionPolylines({ mesh1: cubeTriangles, mesh2: sphereTriangles, tolerance: 1e-7 });
      
    • Finds where the surfaces of two meshes cut through each other, as one list of points per curve.

      A closed curve repeats its first point at the end so the loop is explicit. Example: a cube mesh and a sphere mesh -> point lists tracing where the two surfaces meet

      Parameters

      Returns Bit.Inputs.Base.Point3[][]

      One point list per intersection curve

      const curves = bitbybit.mesh.meshMeshIntersectionPoints({ mesh1: cubeTriangles, mesh2: sphereTriangles, tolerance: 1e-7 });
      

    polygon

    • Cuts a flat polygon, with any holes through it, into triangles.

      Every corner stays a corner of a triangle, so neighbouring polygons give a mesh closed along the edges they share. The triangles face the way the outline turns, by the right-hand rule; holes may run either way. Example: a 4 by 4 square -> two triangles

      Parameters

      Returns Mesh3

      The triangles, each three points

      const triangles = bitbybit.mesh.triangulatePolygon({ points: [[0, 0, 0], [4, 0, 0], [4, 4, 0], [0, 4, 0]], holes: [[[1, 1, 0], [3, 1, 0], [3, 3, 0], [1, 3, 0]]] });
      

    primitives

    • Makes a sphere as an evenly triangulated mesh, the icosahedron split detail times.

      Its triangles are close to equal in size everywhere, unlike a sphere made of latitude and longitude bands, and each faces outward. Each level of detail has four times the triangles. Example: detail 0 -> 20 triangles, detail 2 -> 320

      Parameters

      Returns Mesh3

      The sphere's triangles

      const sphere = bitbybit.mesh.icosphere({ center: [0, 0, 0], radius: 5, detail: 3 });
      
    • Makes a cone, a cylinder or a cone with its tip cut off, as a closed triangulated mesh.

      It stands on center and rises along direction by height, with flat ends and sides flat faces around. Equal radii make a cylinder. Example: top radius 0, height 2 -> a cone 2 units tall

      Parameters

      • inputs: FrustumDto

        The bottom center, the direction, the radii, the height and the sides

      Returns Mesh3

      The shape's triangles

      const cone = bitbybit.mesh.frustum({ center: [0, 0, 0], bottomRadius: 2, topRadius: 0, height: 4, sides: 32 });
      

    traingle

    • Finds the plane a triangle lies in: its unit normal and its distance from the origin along that normal.

      The normal follows the right-hand rule around the triangle's points. A triangle with no area, whose points are on one line, has no plane and gives undefined. Example: [[0,0,0], [1,0,0], [0,1,0]] -> { normal: [0,0,1], d: 0 }

      Parameters

      Returns TrianglePlane3

      The plane, or undefined for a flat triangle

      const plane = bitbybit.mesh.calculateTrianglePlane({ triangle: [[0, 0, 0], [1, 0, 0], [0, 1, 0]], tolerance: 1e-7 });
      
    • Finds the segment where two triangles cut through each other.

      Triangles that do not touch, are parallel, or lie in the same plane give undefined. Example: a triangle in the XY plane and one standing across it -> the segment where they cross

      Parameters

      Returns Segment3

      The crossing segment, or undefined when there is none

      const cut = bitbybit.mesh.triangleTriangleIntersection({
      triangle1: [[0, 0, 0], [2, 0, 0], [1, 2, 0]],
      triangle2: [[1, -1, 1], [1, 1, 1], [1, 1, -1]],
      tolerance: 1e-7,
      });