Commonplace kernel API
    Preparing search index...

    Paper surfaces

    A paper surface bends from flat stock without stretching: a plane, a cylinder, a cone, or any developable face. Every straight line on it (a ruling) stays straight when it lies flat, and every length on it is the length on the page. This kernel makes such faces exactly, knows which of its faces are paper, and lays them flat with nothing approximated. When a face cannot lie flat exactly, it says so.

    This page walks the workflow: make paper faces, lay them flat, thicken and cut them, fold sheets on creases, turn solids into nets, and print a model to cut out. The same ideas without code are in How the kernel works, in plain language. Every snippet here runs as src/core/integration/kernel-paper-guide.test.ts. Each section's snippets run on an empty mesh made like this:

    import {
    HalfEdgeMesh, checkFlatFold, develop, draw, fold, foldCurvedCrease, foldUpNet, kernelQuery, layOutNet, loadKernel,
    markCrease, meshVerbContext, offset, paperModel, paperPDF, paperPageSVG, regrain, revolve, skin,
    subtract, unfold,
    } from '@commonplace-technologies/kernel';

    await loadKernel();
    const mesh = new HalfEdgeMesh();
    const ctx = meshVerbContext(mesh);
    const q = kernelQuery(mesh);

    develop makes one developable face from a rail, a cubic Bézier's four control points. Its family says how the rulings run: parallel to one vector (a general cylinder), or through one apex (a general cone). With a directrix instead, it makes the tangent developable of that curve: the surface its tangent lines sweep.

    const sail = await develop(ctx, {
    rail: [[0, 0, 0], [1, 1, 0], [2, 1, 0], [3, 0, 0]],
    family: { kind: 'cylindrical', offset: [0, 0, 2] },
    });
    const fan = await develop(ctx, {
    rail: [[5, 0, 0], [6, 1, 0], [7, 1, 0], [8, 0, 0]],
    family: { kind: 'conic', apex: [6.5, -3, 0], scale: 0.5 },
    });
    const twist = await develop(ctx, {
    directrix: [[10, 0, 0], [11, 1, 0], [12, 1, 0.5], [13, 0, 1]],
    sMax: 0.5,
    });
    sail.value!.faceId; // the new face

    Other verbs make paper faces too. A sector base raised by draw.cuboid is a cylinder; a profile of straight lines turned by revolve is made of cylinders, cones and flat rings; offset and the Booleans keep a paper face on its own surface (Verbs, by example).

    unfold lays faces flat (every face by default). Each piece says whether it is exact and its strain: 0 for an exact piece. With exactOnly, a face that would have to stretch is refused, never approximated.

    const panel = await develop(ctx, {
    rail: [[0, 0, 0], [1, 1, 0], [2, 1, 0], [3, 0, 0]],
    family: { kind: 'cylindrical', offset: [0, 0, 2] },
    });
    const pattern = unfold(mesh, [panel.value!.faceId], { exactOnly: true });
    const piece = pattern.islands[0].pieces[0];
    piece.exact; // true
    piece.strain; // 0
    piece.loops; // the outline (and any holes) on the page
    piece.rulings; // the straight lines the sheet holds, on the page

    // One face at a time, from the query.
    const one = q.develop(panel.value!.faceId);
    one?.ok; // true: this face lies flat exactly

    // A ruled face between two lines that are not in one plane is twisted:
    // it cannot lie flat without stretching.
    const a = await draw.line(ctx, { points: [[5, 0, 0], [8, 0, 1]] });
    const b = await draw.line(ctx, { points: [[5, 2, 1], [8, 2, 0]] });
    const band = await skin(ctx, { railAId: a.value!.halfEdgeIds[0], railBId: b.value!.halfEdgeIds[0] });
    unfold(mesh, [band.value!.faceId], { exactOnly: true }).refused[0].code; // 'not-exact'
    unfold(mesh, [band.value!.faceId]).islands[0].pieces[0].method; // 'strips': an approximation, marked so

    Without exactOnly, a face with no exact development is laid out as strips with score lines, and the piece says exact: false with its strain. connect: false gives one island per face; by default faces that share a straight edge stay hinged in one net.

    A paper sheet pushed to a thickness stays paper: its far side is the exact offset of the sheet, and the walls between them are ruled. A window cut through the panel leaves faces on the panel's own surfaces, so every face still lies flat.

    const sheet = await develop(ctx, {
    rail: [[0, 0, 0], [1, 1, 0], [2, 1, 0], [3, 0, 0]],
    family: { kind: 'cylindrical', offset: [0, 0, 2] },
    });
    await offset(ctx, { mode: 'push', faceIds: [sheet.value!.faceId], distance: 0.05 });

    const cutter = new HalfEdgeMesh();
    await draw.cuboid(meshVerbContext(cutter), {
    base: { shape: 'rectangle', corners: [[1.2, -1, 0.6], [1.8, -1, 0.6], [1.8, 2, 0.6], [1.2, 2, 0.6]] },
    height: 0.8,
    });
    await subtract(ctx, { other: cutter });

    const parts = unfold(mesh, undefined, { exactOnly: true, connect: false });
    parts.refused.length; // 0: every face of the cut panel lies flat exactly

    A four-sided ruled face has a grain: the direction its rulings run, from one rail to the other. regrain flips it (faceIds), carries one face's grain across its neighbors (faceId), or makes the whole mesh agree with the faces held (heldFaceIds).

    const railA = await draw.line(ctx, { points: [[0, 0, 0], [3, 0, 1]] });
    const railB = await draw.line(ctx, { points: [[0, 2, 1], [3, 2, 0]] });
    const ruled = await skin(ctx, { railAId: railA.value!.halfEdgeIds[0], railBId: railB.value!.halfEdgeIds[0] });
    await regrain(ctx, { faceIds: [ruled.value!.faceId] }); // { flippedFaceCount: 1 }

    Lines drawn across a flat sheet divide it into panels. markCrease records which way each crease should fold, mountain or valley, and how far; it is intent, not geometry. checkFlatFold says whether the crease pattern can fold flat. fold then moves the panels rigidly: every marked crease part of the way to its mark (progress), or named creases to angles (angles), with the rest following as the mechanism makes them.

    await draw.rectangle(ctx, { corners: [[0, 0, 0], [3, 0, 0], [3, 2, 0], [0, 2, 0]] });
    const left = await draw.line(ctx, { points: [[1, 0, 0], [1, 2, 0]] });
    const right = await draw.line(ctx, { points: [[2, 0, 0], [2, 2, 0]] });
    const valley = left.value!.halfEdgeIds[0];
    const mountain = right.value!.halfEdgeIds[0];

    await markCrease(ctx, { edgeIds: [valley], kind: 'valley', degrees: 90 });
    await markCrease(ctx, { edgeIds: [mountain], kind: 'mountain', degrees: 90 });
    checkFlatFold(mesh, q.faceIds()).ok; // true

    const folded = await fold(ctx, { faceIds: q.faceIds(), progress: 1 });
    folded.value!.outcome; // 'reached'
    folded.value!.angles; // each crease's angle after the fold, degrees

    await fold(ctx, { faceIds: q.faceIds(), angles: [{ edgeId: valley, degrees: -45 }] });

    A fold stops short where panels would pass through each other (contact: 'stop', the default) and commits where it stopped, with outcome: 'contact'. bend: true lets a panel of four corners or more bend along a diagonal. material folds a sheet of a given thickness, with bends of a given radius, and tests contact on the plates.

    A sheet of two panels joined by one curved crease folds by reflection. foldCurvedCrease folds an arc crease to two cones, or a cubic crease drawn across a rectangle to two mirrored cylinders. The panels become ruled faces between exact rails, and the folded crease unfolds to the drawn one (deviation, 0 for an arc). degrees is the fold from flat, positive a mountain; folding a folded crease again starts from flat, so 0 lays it flat.

    await draw.rectangle(ctx, { corners: [[0, -1, 0], [2, -1, 0], [2, 1, 0], [0, 1, 0]] });
    const crease = await draw.curve(ctx, { controls: [[0, 0, 0], [0.7, 0.45, 0], [1.3, -0.45, 0], [2, 0, 0]] });
    const curved = await foldCurvedCrease(ctx, { creaseEdgeId: crease.value!.edgeIds[0], degrees: 60 });
    curved.value!.family; // 'cylinders'

    For now each panel must be a band across the crease: straight sides along one direction and a straight edge across them, or, for an arc, a ring sector about its center. Other outlines are refused with panel-shape.

    layOutNet unfolds flat faces into one net drawn in the mesh beside them: a face per face, a crease wherever the net is hinged, each crease marked with the angle it had. foldUpNet folds a net back up and joins its seams.

    const box = await draw.cuboid(ctx, {
    base: { shape: 'rectangle', corners: [[0, 0, 0], [2, 0, 0], [2, 1, 0], [0, 1, 0]] },
    height: 1.5,
    });
    const net = await layOutNet(ctx, { faceIds: box.created.faces, at: [4, 0, 0] });
    net.value!.faceIds; // the six faces of the net, flat at z = 0
    net.value!.creases; // each with the angle it folds back to

    const upAgain = await foldUpNet(ctx, { faceIds: net.value!.faceIds });
    upAgain.value!.closed; // true: the net folded back into a closed box

    layOutNet with material lays out for a plate of a given thickness: each fold gives up its bend deduction, so the folded plate's outside is the part.

    paperModel lays faces out on pages to print, cut and glue: numbered parts, numbered seams, glue flaps, fold lines with their angles. Sizes on the page are millimeters; at scale: 1 a model unit is a meter, and the default 'fit' picks the largest standard scale at which every part fits a page. paperPageSVG writes one page as SVG, with each line type on its own layer; paperPDF writes every page into one PDF.

    const block = await draw.cuboid(ctx, {
    base: { shape: 'rectangle', corners: [[0, 0, 0], [2, 0, 0], [2, 1, 0], [0, 1, 0]] },
    height: 1.5,
    });
    const model = paperModel(mesh, block.created.faces, { paper: { size: 'A4', orientation: 'portrait' } });
    model.scale; // 0.04: 1:25
    const svg = paperPageSVG(model.pages[0]); // '<svg … width="210mm" …'
    const pdf = paperPDF(model, 'Block'); // '%PDF-1.4 …'

    Masons cut the stones of a conical vault from templates developed off the cone. A cone here is a straight profile turned about an axis, and its template is its unrolled face.

    // A right triangle in the XZ plane, turned about Z: a solid cone.
    const profile = await draw.line(ctx, { points: [[0, 0, 0], [2, 0, 0], [0, 0, 3]], closed: true });
    const cone = await revolve(ctx, {
    profile: { kind: 'face', faceId: profile.created.faces[0] },
    axis: { from: [0, 0, 0], to: [0, 0, 1] },
    degrees: 360,
    });
    const templates = unfold(mesh, cone.created.faces, { exactOnly: true, connect: false });
    templates.refused.length; // 0: the side unrolls to sectors of a disc, the base to a disc

    The testbed runs scripts like these in the browser and draws the result (npm run testbed:kernel), with showFlat() drawing a pattern beside the model. Open a preset at http://localhost:3310/?example=<id>: panel (a curved panel made, thickened and laid flat), fold (markCrease, checkFlatFold, fold), strip (a conic sheet and its annular sector), net (paperModel, paperPageSVG, paperPDF, layOutNet, foldUpNet) and opening (a window cut through a curved wall, laid flat with the hole in place). Under Showcase, the paper forms are worked end to end: durer-nets (eight convex solids, each unfolded in one piece), melencolia (the Melencolia solid and its net), trompe (a trompe after de l'Orme and its masons' templates), screen (a folded screen), lamp (a turned floor lamp), chair (one ribbon of board) and canopy (a blade from a curve's tangents). The presets are in examples/kernel/testbed/examples.ts; any snippet on this page also runs there as written, with kernel in place of the import.