transformEntities moves, rotates or scales corners, edges and faces of a
mesh by an affine map, as one undo step. planTransform answers what that
step would make without touching the mesh, so a host can draw a live
preview while the user drags and commit on release.
import { Matrix4 } from 'three';
import { kernelTessellation, meshVerbContext, planTransform, transformEntities } from '@commonplace-technologies/kernel';
const ctx = meshVerbContext(mesh);
const start = { ids: [cornerId], matrix: new Matrix4().makeTranslation(0.3, 0.2, 0) };
// Every pointer move: plan from the mesh as it was when the drag began.
const plan = planTransform(mesh, start);
if ('preview' in plan) {
const frame = kernelTessellation(plan.preview).frame({ curvature: false, faceDebug: false });
// draw `frame`; show `plan.note` at the cursor
} else {
// plan.code, plan.message: why the commit would refuse
}
// On release:
const done = await transformEntities(ctx, start);
done.ok; // true
done.removed; // the ids the step removed: drop them from the selection
matrix is any Mat4Like (a three.js Matrix4, or { elements } with
16 numbers in column-major order): a translation, a rotation, a scale (uniform or not), or a product
of them, with a positive determinant. An edge id moves both its ends; a face
id moves every corner of the face, its holes' too.
A transform takes each selected corner where the matrix says. Faces with
every corner selected go along rigidly. A flat face with only some of its
corners selected would bend, and options.bend says how that is answered:
bend |
What happens to a face that would bend |
|---|---|
twist (default) |
It follows the corners and becomes a ruled surface. |
flat |
Its neighbors re-solve so every face stays flat. A corner where more than three planes meet and cannot stay one point splits into two, joined by a new edge. |
fold |
It is creased along a new soft edge so each piece stays flat. foldVariant picks which crease when there are several (plan.readings.foldVariants). |
Whatever bend is, an edge the transform shrinks to nothing welds: its
two ends become one corner. A rotation or scale that tilts a flat face on a
cylinder holds the cylinder where it is (curved: 'hold', the default; the
rim becomes an ellipse) or moves it with the face (curved: 'follow'). An
arc with one end moved keeps its swept angle (arcRule: 'angle'), its
radius, or its tangent at the end that stayed. After a translation,
collinear edges and coplanar faces around the moved corners merge, keeping
subdivisions that were drawn on purpose.
plan.readings says which of these choices the gesture actually meets, so
a host offers only the ones that change something: bends (flat faces
bend: twist, flat and fold differ), holds (a cylinder could be held
or moved).
| Field | Meaning |
|---|---|
unchanged |
The matrix moves nothing (an identity). Everything else is empty. |
positions |
Where every vertex the step moves goes, re-solved neighbors included, by id. |
bend |
How a bend was answered: none, twist, flat, fold, or hold. |
welds |
{ keepId, dropId, edgeId } for each edge that closes up. |
splits |
{ vertexId, faceIds, position } for each corner that splits. |
creases |
The two corners of each crease a fold makes. |
reshapedFaceIds |
Faces whose surface kind changes (a twist's ruled faces, faces made flat again). |
removed |
The existing vertex, half-edge and face ids the step removes. |
createdCounts |
How many vertices, half-edges and faces the step makes. |
preview |
A detached mesh to draw: the step's result with every weld left open. |
note |
A sentence for the cursor when a corner splits or an edge welds. |
A plan is a pure function of the mesh and the operands. It reads the mesh and never writes it, and planning twice answers the same. Plan from the gesture's start state every frame rather than from the last preview; the commit re-plans from the mesh it is given and makes exactly what the plan said for that mesh.
Every vertex, edge and face keeps its id across the step unless the plan's
removed lists it. In particular:
keepId survives and dropId is removed, with the two
half-edges of the edge that closed up (edgeId and its twin). The kept
corner is the end that moves when only one end moves, so a dragged corner
keeps its id. The other edges that ended at dropId keep their ids and
now end at keepId. Both faces on the welded edge keep their ids, each
with one corner fewer.positions.
The new corner and the new edge between them are new.removed.Ids of new entities are made when the step commits, so a plan gives their
counts, not their ids: the commit's created lists them.
The commit then imprints what moved where it now crosses other faces, as
every edit does, which can split faces and edges the plan did not touch.
Its created and removed include that. Pass imprint: false beside
ids and matrix in transformEntities's operands to skip it; the
commit's removed then equals the plan's exactly.
plan.preview leaves every weld open: the closing edge is drawn squeezed to
zero length and both its corners stay, so the corner under the cursor stays
put and a host's snapping stays stable while the drag runs. The step welds
when it commits. Everything else the commit makes (splits, creases, ruled
faces, the merge) is in the preview as it will be.
preview is a fresh mesh each time, so its tessellation is a full one: a
host that previews every frame keeps the selection small or throttles.
A refusal leaves the mesh untouched. planTransform returns it as
{ code, message }; transformEntities as its result's error. The codes
are in TRANSFORM_REFUSAL_CODES:
| Code | When |
|---|---|
nothing-to-transform |
ids is empty. |
unknown-entity |
An id is not a vertex, edge or face of the mesh. |
invalid-operands |
The matrix has a coordinate that is not a finite number. |
not-affine |
The matrix's last row is not 0 0 0 1. |
reflects |
The determinant is negative: a mirror turns faces over and is not this verb. |
degenerate-matrix |
The determinant is about zero: the selection would collapse. |
too-far |
A corner would move, or the map would scale, by more than 10⁷. |
no-change |
The matrix moves nothing (commit only; a plan says unchanged). |
would-stretch |
A scale would flatten a circle or arc of the selection. |
face-inverts |
A face would turn over or lose its area. |
edge-collapses |
An edge would shrink to nothing and cannot weld, or its ends would pass each other. |
needs-new-edge |
More than three planes meet at a corner and no split answers it. |
ill-conditioned |
Planes at a corner are too nearly parallel to meet in a point. |
over-constrained |
The planes the move asks for contradict each other. |
curved-neighbor |
A curved face beside the move cannot hold a plane. |
holed-face |
A face that would change plane has a hole that cannot follow it. |
cannot-fold |
Under fold, a bent face has no crease that keeps its pieces flat. |
face-kind-invariant |
A developable curved wall would twist into a surface that is not developable. twistCurved: true allows it. |
The step itself can also be refused by the history's postcondition gate
(postcondition-refused) or because the mesh is read-only
(read-only-context).
To move the whole mesh rigidly, use move; it carries every curve and
surface exactly and takes no ids.