One short, working snippet for each verb family. Every snippet here runs,
as src/core/integration/kernel-verbs-guide.test.ts. For
the loop around them (display, picking, undo, saving), read
Build your own modeler first. For the paper
workflow (developable faces, unfolding, creases, folding, printing), read
Paper surfaces. For the ideas without the code, read
How the kernel works, in plain language.
Each section's snippets run on an empty mesh made like this:
import {
HalfEdgeMesh, draw, erase, fillet, intersect, kernelQuery, loadKernel, loft,
meshVerbContext, offset, patch, revolve, skin, subtract, sweep, unite,
} from '@commonplace-technologies/kernel';
await loadKernel();
const mesh = new HalfEdgeMesh();
const ctx = meshVerbContext(mesh);
const q = kernelQuery(mesh);
[x, y, z] tuples in the mesh's frame, Z up, in the
host's units.created and removed list every vertex, half-edge and face a
step made or took away. Many verbs also return the ids that matter in
value: draw.line returns its halfEdgeIds in order, develop and
skin return the new faceId, offset returns the selection a tool
would leave in ids. An edge is named by either of its half-edge ids.degrees (revolve, markCrease,
fold) take degrees. A sweep (draw.arcCenterRadius, a sector
base) takes radians.VerbResult: ok, value,
created, removed, notices, or ok: false with error.code and a
sentence in error.message, and the mesh unchanged
(Refusals). The snippets below skip the ok checks to
stay short; a host should not.await draw.line(ctx, { points: [[0, 0, 0], [2, 0, 0], [2, 1, 0]] }); // an open polyline: wires
await draw.line(ctx, { points: [[0, 2, 0], [1, 2, 0], [1, 3, 0]], closed: true }); // closed and flat: a face
await draw.rectangle(ctx, { corners: [[3, 0, 0], [5, 0, 0], [5, 1, 0], [3, 1, 0]] });
await draw.circle(ctx, { center: [7, 0, 0], radius: 0.5 }); // in the plane square to +Z
await draw.ellipse(ctx, { center: [9, 0, 0], axis: [0.8, 0, 0], secondRadius: 0.4 });
await draw.arc(ctx, { start: [0, 5, 0], through: [1, 6, 0], end: [2, 5, 0] });
await draw.arcCenterRadius(ctx, { center: [4, 5, 0], start: [5, 5, 0], sweep: Math.PI / 2 });
await draw.curve(ctx, { controls: [[6, 5, 0], [7, 6, 0], [8, 4, 0], [9, 5, 0]] }); // a cubic Bézier
await draw.points(ctx, { points: [[0, 8, 0], [1, 8, 0]] });
A point that lands on an existing vertex welds to it, and a point on an
edge splits it, so lines drawn across a face divide it. draw.arcTwoPoint
takes the chord's ends and a bulge point.
draw.cuboid raises a base by a signed height: a rectangle base makes a
box, a sector base a cylinder (sweep: 2π) or a wedge of one.
await draw.cuboid(ctx, {
base: { shape: 'rectangle', corners: [[0, 10, 0], [4, 10, 0], [4, 13, 0], [0, 13, 0]] },
height: 1,
});
await draw.cuboid(ctx, {
base: { shape: 'sector', center: [8, 11, 0], rim: [9, 11, 0], normal: [0, 0, 1], sweep: 2 * Math.PI },
height: 2,
});
The most common edit in a direct modeler: draw a shape on a face, then
push the new face in or out. A shape drawn on a face divides it, and the
new face is in created.faces.
const box = await draw.cuboid(ctx, {
base: { shape: 'rectangle', corners: [[0, 0, 0], [4, 0, 0], [4, 3, 0], [0, 3, 0]] },
height: 1,
});
const top = q.pickFace([2, 1.5, 10], [0, 0, -1])!.faceId;
// A circle on the top face, pushed down through the box: a hole.
const disc = await draw.circle(ctx, { center: [3, 1.5, 1], radius: 0.4, hostFaceId: top });
await offset(ctx, { mode: 'push', faceIds: [disc.created.faces[0]], distance: -1 });
// A square drawn in the top face's plane, pulled up: a block on the box.
const square = await draw.rectangle(ctx, { corners: [[0.5, 0.5, 1], [1.5, 0.5, 1], [1.5, 1.5, 1], [0.5, 1.5, 1]] });
await offset(ctx, { mode: 'push', faceIds: [square.created.faces[0]], distance: 0.5 });
A push that reaches the far side of the solid cuts through it; one short
of it makes a pocket. draw.circle's hostFaceId also takes a curved
wall, and draw.line's surfaceHostFaceId draws on one, as a window is
drawn on a curved wall.
offset is one verb with seven modes. Each takes the ids in the field the
table names; push, move, wall, inset and chain take a
distance, group and ladder a vector.
mode |
Ids | What it does |
|---|---|---|
push |
faceIds: one face |
Moves the face along its normal: out extends the solid, in carves it, through cuts it. shear slants the walls. |
move |
faceIds: one or more |
Moves the faces along their normals, with their neighbors following. |
wall |
faceIds: one or more |
Gives sheet faces a wall of one thickness (negative builds it behind them). |
inset |
faceIds: one flat face |
Draws the face's outline again inside it (negative: outside, on the face's plane). |
chain |
edgeIds: an open chain |
Draws the offset of a flat chain of lines and arcs, joined to the chain at both ends. |
group |
edgeIds or vertexIds: loose ones |
Moves loose edges by a vector, each sweeping a face to its copy; loose vertices each draw an edge. |
ladder |
one id in edgeIds or vertexIds |
One edge sweeps a face to its copy moved by vector; one vertex draws an edge. |
const block = await draw.cuboid(ctx, {
base: { shape: 'rectangle', corners: [[0, 0, 0], [2, 0, 0], [2, 2, 0], [0, 2, 0]] },
height: 2,
});
const lid = q.pickFace([1, 1, 10], [0, 0, -1])!.faceId;
await offset(ctx, { mode: 'inset', faceIds: [lid], distance: 0.2 }); // a frame round the top
const sheet = await draw.rectangle(ctx, { corners: [[4, 0, 0], [7, 0, 0], [7, 2, 0], [4, 2, 0]] });
await offset(ctx, { mode: 'wall', faceIds: sheet.created.faces, distance: 0.1 }); // a 0.1 slab
const path = await draw.line(ctx, { points: [[0, 5, 0], [2, 5, 0], [2, 6, 0]] });
await offset(ctx, { mode: 'chain', edgeIds: path.value!.halfEdgeIds, distance: 0.2 }); // a strip beside it
const rung = await draw.line(ctx, { points: [[4, 5, 0], [6, 5, 0]] });
await offset(ctx, { mode: 'ladder', edgeIds: [rung.value!.halfEdgeIds[0]], vector: [0, 0, 1] }); // a fin
// Revolve a closed profile about an axis: a ring, 360 degrees.
const profile = await draw.line(ctx, { points: [[2, 0, 0], [3, 0, 0], [3, 0, 2], [2, 0, 2]], closed: true });
await revolve(ctx, { profile: { kind: 'face', faceId: profile.created.faces[0] }, axis: { from: [0, 0, 0], to: [0, 0, 1] }, degrees: 360 });
// Loft from one profile to another.
const base = await draw.rectangle(ctx, { corners: [[10, 0, 0], [12, 0, 0], [12, 2, 0], [10, 2, 0]] });
const head = await draw.rectangle(ctx, { corners: [[10.5, 0.5, 3], [11.5, 0.5, 3], [11.5, 1.5, 3], [10.5, 1.5, 3]] });
await loft(ctx, { sourceFaceId: base.created.faces[0], targetFaceIds: [head.created.faces[0]] });
// Sweep a profile along a path of loose edges.
const ring = await draw.circle(ctx, { center: [0, 10, 0], radius: 0.2, normal: [1, 0, 0] });
const rail = await draw.line(ctx, { points: [[0, 10, 0], [3, 10, 0], [3, 13, 0]] });
await sweep(ctx, { profileFaceId: ring.created.faces[0], pathEdgeIds: rail.value!.halfEdgeIds });
// Skin: the ruled face between two edges.
const a = await draw.curve(ctx, { controls: [[6, 10, 0], [7, 10, 1], [8, 10, 1], [9, 10, 0]] });
const b = await draw.curve(ctx, { controls: [[6, 12, 0], [7, 12, 1], [8, 12, 1], [9, 12, 0]] });
await skin(ctx, { railAId: a.created.halfEdges[0], railBId: b.created.halfEdges[0] });
revolve also takes an open chain of loose edges (kind: 'wire') for a
dome or a shell. loft takes several targets, and guide rails in
railEdgeIds. sweep with orientation: 'keep' moves the profile
without turning it.
const cube = await draw.cuboid(ctx, {
base: { shape: 'rectangle', corners: [[0, 20, 0], [2, 20, 0], [2, 22, 0], [0, 22, 0]] },
height: 1,
});
// Round one edge of a solid: the edge nearest a ray.
const edge = q.pickEdge([1, 15, 1], [0, 1, 0], 0.01)!.edgeId;
await fillet(ctx, { edgeId: edge, radius: 0.2 });
// Erase a face, then fill the opening it left.
const roof = q.pickFace([1, 21, 10], [0, 0, -1])!.faceId;
const frame = q.face(roof)!.edgeIds;
await erase(ctx, { ids: [roof] });
await patch(ctx, { edgeIds: frame });
fillet with firstEdgeId, secondEdgeId and radius rounds the corner
between two lines instead. patch with controls and weights makes a
free bicubic patch. Erase says what each kind of id takes with
it.
The Booleans combine the edited mesh with another mesh:
const tower = new HalfEdgeMesh();
await draw.cuboid(meshVerbContext(tower), {
base: { shape: 'sector', center: [2, 22, 0], rim: [2.8, 22, 0], normal: [0, 0, 1], sweep: 2 * Math.PI },
height: 3,
});
const joined = await unite(ctx, { other: tower });
joined.value!.engine; // 'exact', or 'bsp' where the exact engine refused
subtract and intersect take the same operands. The other mesh is read,
not changed. imprint cuts two faces of one mesh along the curve where
they cross; merge joins triangles into one face where they lie flat.
transformEntities moves, rotates or scales corners, edges and faces,
and planTransform previews it: Move, rotate and scale.
move moves the whole mesh by a matrix.
The testbed runs scripts like these in the browser and draws the
result (npm run testbed:kernel). Its presets are in
examples/kernel/testbed/examples.ts.
Open one at http://localhost:3310/?example=<id>: offset-drag (offset
a face, drag a corner with planTransform and transformEntities, undo),
opening (subtract a window from a curved wall), refusal (canRun,
refusalCodes and a refused verb), save-load (saveMesh and
loadMesh), step (write STEP and read it back). The paper verbs have
their own presets: see Paper.