Examples | TS
Core Functionality
Self-contained examples demonstrating primary features of trueform.
These examples demonstrate fundamental trueform workflows, from NDArray operations to spatial acceleration and boolean operations.
NDArray
Demonstrates creating and manipulating WASM-resident typed arrays.
Features Showcased
- Creating arrays with
tf.ndarray,tf.linspace,tf.arange,tf.random - Stacking and reshaping with
tf.stack - Reductions:
tf.sum,tf.mean,tf.min,tf.max,tf.norm - Element-wise math:
tf.sqrt,.mul,.add - Boolean indexing with
.gtand.booleanIndex
Example Code
import * as tf from "@polydera/trueform";
// Create arrays
const a = tf.ndarray([1, 2, 3, 4, 5, 6], [2, 3]);
console.log("shape:", a.shape, "data:", a.data);
const b = tf.linspace(0, 1, 5);
const c = tf.arange("float32", 0, 10, 2);
// Reductions
const vals = tf.ndarray([1, 2, 3, 4, 5]);
console.log("sum:", tf.sum(vals), "mean:", tf.mean(vals));
// Element-wise math
const x = tf.ndarray([1, 4, 9]);
console.log("sqrt:", tf.sqrt(x).data);
// Stack
const v1 = tf.ndarray([1, 2, 3]);
const v2 = tf.ndarray([4, 5, 6]);
const stacked = tf.stack([v1, v2], 0);
console.log("stacked:", stacked.shape, stacked.data);
// Boolean indexing
const arr = tf.ndarray([10, 20, 30, 40, 50]);
const mask = arr.gt(25);
console.log("filtered:", arr.booleanIndex(mask).data);
Queries on Primitives
Demonstrates fundamental geometric queries between individual primitives.
Features Showcased
- Creating primitives (
tf.triangle,tf.segment,tf.ray,tf.point) - Boolean intersection tests with
tf.intersects - Finding closest points with
tf.closestPointPair - Ray casting with
tf.rayCast
Example Code
import * as tf from "@polydera/trueform";
// Create primitives
const triangle = tf.triangle([0, 0, 0], [1, 0, 0], [0, 1, 0]);
const segment = tf.segment([2, 2, 0], [3, 3, 0]);
// Intersection test — segment is outside triangle
if (!tf.intersects(triangle, segment)) {
const pair = tf.closestPointPair(triangle, segment);
console.log(`Distance: ${Math.sqrt(pair.distance2)}`);
}
// Ray casting
const ray = tf.ray([0.2, 0.2, -1], [0, 0, 1]);
const hit = tf.rayCast(ray, triangle);
if (hit.hit) {
console.log(`Hit at t=${hit.t}`);
}
Spatial Queries on Meshes
Demonstrates building spatial acceleration structures and querying meshes.
Features Showcased
- Generating meshes with
tf.sphereMesh - Building spatial tree with
mesh.buildTree() - Single nearest neighbor search with
tf.neighborSearch - k-NN search with
tf.neighborSearch(mesh, query, { k }) - Collision detection with
tf.intersects
Example Code
import * as tf from "@polydera/trueform";
// Create a sphere mesh
const mesh = tf.sphereMesh(1.0, 32, 32);
mesh.buildTree();
// Single nearest neighbor search
const query = tf.point(0.5, 0.5, 0.5);
const nn = tf.neighborSearch(mesh, query);
console.log(`Nearest face: ${nn.elementId}, distance: ${Math.sqrt(nn.distance2)}`);
// k-NN search
const knn = tf.neighborSearch(mesh, query, { k: 3 });
console.log(`k-NN: ${knn.elementIds.data.length} results`);
// Collision detection between meshes
const other = tf.sphereMesh(1.0, 16, 16);
other.transformation = tf.makeTranslation(0.5, 0, 0);
other.buildTree();
console.log("Intersects:", tf.intersects(mesh, other));
Boolean Operations
Demonstrates mesh boolean operations (union, intersection, difference).
Features Showcased
- Generating box meshes with
tf.boxMesh - Computing boolean union, intersection, difference
- Extracting intersection curves with
returnCurves
Example Code
import * as tf from "@polydera/trueform";
// Create two overlapping boxes
const box0 = tf.boxMesh(1, 1, 1);
const box1 = tf.boxMesh(1, 1, 1);
box1.transformation = tf.makeTranslation(0.5, 0, 0);
// Union
const union = tf.booleanUnion(box0, box1);
console.log(`Union: ${union.mesh.numberOfFaces} faces`);
// Intersection
const inter = tf.booleanIntersection(box0, box1);
console.log(`Intersection: ${inter.mesh.numberOfFaces} faces`);
// Difference with intersection curves
const diff = tf.booleanDifference(box0, box1, { returnCurves: true });
console.log(`Difference: ${diff.mesh.numberOfFaces} faces, ${diff.curves.length} curves`);
Required topology structures are built automatically on first use. The spatial tree, face membership, and manifold edge link are computed lazily.
Isocontours
Demonstrates extracting isocontours from scalar fields on meshes.
Features Showcased
- Generating plane meshes with
tf.planeMesh - Creating scalar fields from point coordinates
- Extracting isocontours with
tf.isocontours - Extracting isobands with
tf.isobands
Example Code
import * as tf from "@polydera/trueform";
// Create a plane mesh with scalar field
const plane = tf.planeMesh(2, 2, 20, 20);
// Scalar field: X coordinates of each vertex
const scalars = plane.points.take(null, 0);
// Extract isocontours at multiple thresholds
const thresholds = new Float32Array([-0.5, 0.0, 0.5]);
const curves = tf.isocontours(plane, scalars, thresholds);
console.log(`Isocontours: ${curves.length} curves`);
// Isobands — regions between thresholds
const bands = tf.isobands(plane, scalars, thresholds);
console.log(`Isobands: ${bands.mesh.numberOfFaces} faces`);
Transformations
Demonstrates applying transformations without rebuilding spatial trees.
Features Showcased
- Attaching transformation to mesh via
mesh.transformation - Creating translations with
tf.makeTranslation - Queries use transformed geometry without tree rebuild
Example Code
import * as tf from "@polydera/trueform";
// Create two meshes
const meshA = tf.boxMesh(1, 1, 1);
const meshB = tf.boxMesh(1, 1, 1);
meshB.transformation = tf.makeTranslation(3, 0, 0);
meshA.buildTree();
meshB.buildTree();
// No collision initially (3 units apart)
console.log("Initial:", tf.intersects(meshA, meshB)); // false
// Move meshA close to meshB (tree is reused)
meshA.transformation = tf.makeTranslation(2.5, 0, 0);
console.log("After translation:", tf.intersects(meshA, meshB)); // true
Transformations enable real-time collision detection in interactive applications without the cost of rebuilding spatial trees on every frame.
