CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jscad--modeling

Constructive Solid Geometry (CSG) Library for 2D and 3D geometries with boolean operations, transformations, and mathematical utilities

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

measurements.mddocs/

Measurements

All shapes (primitives or the results of operations) can be measured to calculate various geometric properties like area, volume, bounding boxes, centers, and dimensions. These functions are essential for analysis, positioning, and manufacturing preparation.

Capabilities

Area Measurements

Calculate the area of 2D shapes or surface area of 3D shapes.

/**
 * Measure the area of a 2D shape or surface area of a 3D shape
 * @param {...Object} geometries - Geometries to measure (geom2 or geom3)
 * @returns {Number|Array} Area value, or array of areas for multiple geometries
 */
function measureArea(...geometries: (geom2 | geom3)[]): number | number[];

/**
 * Measure the total area of multiple geometries
 * @param {Array} objects - Array of geometries to measure
 * @returns {Number} Total area of all objects
 */
function measureAggregateArea(objects: any[]): number;

Usage Examples:

const { circle, rectangle, cube, sphere, measureArea, measureAggregateArea, union } = require('@jscad/modeling');

// Measure 2D area
const myCircle = circle({ radius: 5 });
const circleArea = measureArea(myCircle); // ≈ 78.54

const myRect = rectangle({ size: [10, 6] });
const rectArea = measureArea(myRect); // = 60

// Measure 3D surface area
const myCube = cube({ size: 10 });
const cubeArea = measureArea(myCube); // = 600 (6 faces × 100)

const mySphere = sphere({ radius: 4 });
const sphereArea = measureArea(mySphere); // ≈ 201.06

// Measure total area of multiple shapes
const shapes = [myCircle, myRect];
const totalArea = measureAggregateArea(shapes);

Volume Measurements

Calculate the volume of 3D shapes.

/**
 * Measure the volume of a 3D shape
 * @param {...Object} geometries - Geometries to measure (accepts any geometry type, but only geom3 has volume)
 * @returns {Number|Array} Volume value, or array of volumes for multiple geometries
 */
function measureVolume(...geometries: any[]): number | number[];

/**
 * Measure the total volume of multiple 3D geometries
 * @param {Array} objects - Array of 3D geometries to measure
 * @returns {Number} Total volume of all objects
 */
function measureAggregateVolume(objects: geom3[]): number;

Usage Examples:

const { cube, sphere, cylinder, measureVolume, measureAggregateVolume, subtract } = require('@jscad/modeling');

// Measure basic volumes
const myCube = cube({ size: 10 });
const cubeVolume = measureVolume(myCube); // = 1000

const mySphere = sphere({ radius: 3 });
const sphereVolume = measureVolume(mySphere); // ≈ 113.10

const myCylinder = cylinder({ height: 8, radius: 2 });
const cylinderVolume = measureVolume(myCylinder); // ≈ 100.53

// Measure volume of complex shape
const cubeWithHole = subtract(myCube, mySphere);
const complexVolume = measureVolume(cubeWithHole); // ≈ 886.90

// Measure total volume
const solids = [myCube, mySphere, myCylinder];
const totalVolume = measureAggregateVolume(solids);

Bounding Box Measurements

Calculate bounding boxes that completely enclose geometries.

/**
 * Measure the bounding box of a geometry
 * @param {Object} geometry - Geometry to measure
 * @returns {Array} Bounding box as [[minX, minY, minZ], [maxX, maxY, maxZ]]
 */
function measureBoundingBox(geometry: any): [[number, number, number], [number, number, number]];

/**
 * Measure bounding box that contains all given geometries
 * @param {Array} objects - Array of geometries
 * @returns {Array} Aggregate bounding box containing all objects
 */
function measureAggregateBoundingBox(objects: any[]): [[number, number, number], [number, number, number]];

Usage Examples:

const { cube, sphere, translate, measureBoundingBox, measureAggregateBoundingBox } = require('@jscad/modeling');

// Measure single object bounding box
const myCube = cube({ size: 10, center: [5, 0, 0] });
const cubeBounds = measureBoundingBox(myCube);
// Returns [[0, -5, -5], [10, 5, 5]]

// Measure aggregate bounding box
const sphere1 = sphere({ radius: 3, center: [-8, 0, 0] });
const sphere2 = sphere({ radius: 2, center: [12, 5, 3] });
const objects = [myCube, sphere1, sphere2];
const totalBounds = measureAggregateBoundingBox(objects);
// Returns [[-11, -5, -5], [14, 7, 5]]

// Extract bounding box dimensions
const [[minX, minY, minZ], [maxX, maxY, maxZ]] = cubeBounds;
const width = maxX - minX;   // 10
const height = maxY - minY;  // 10  
const depth = maxZ - minZ;   // 10

Bounding Sphere Measurements

Calculate the smallest sphere that completely encloses a geometry.

/**
 * Measure the bounding sphere of a geometry
 * @param {Object} geometry - Geometry to measure
 * @returns {Array} Bounding sphere as [centerX, centerY, centerZ, radius]
 */
function measureBoundingSphere(geometry: any): [number, number, number, number];

Usage Examples:

const { cube, cylinder, measureBoundingSphere } = require('@jscad/modeling');

// Measure bounding sphere
const myCube = cube({ size: 10 });
const [centerX, centerY, centerZ, radius] = measureBoundingSphere(myCube);
// Center at [0, 0, 0], radius ≈ 8.66 (half diagonal of cube)

const myCylinder = cylinder({ height: 10, radius: 3 });
const cylinderSphere = measureBoundingSphere(myCylinder);
// Sphere that encloses the entire cylinder

Center and Centroid Measurements

Calculate geometric centers and centers of mass.

/**
 * Measure the geometric center (centroid) of a geometry
 * @param {Object} geometry - Geometry to measure
 * @returns {Array} Center point as [x, y] for 2D or [x, y, z] for 3D
 */
function measureCenter(geometry: any): [number, number] | [number, number, number];

/**
 * Measure the center of mass of a geometry (considering uniform density)
 * @param {Object} geometry - Geometry to measure  
 * @returns {Array} Center of mass as [x, y] for 2D or [x, y, z] for 3D
 */
function measureCenterOfMass(geometry: any): [number, number] | [number, number, number];

Usage Examples:

const { rectangle, cube, sphere, union, measureCenter, measureCenterOfMass, translate } = require('@jscad/modeling');

// Measure center of simple shapes
const rect = rectangle({ size: [10, 6], center: [5, 3] });
const rectCenter = measureCenter(rect); // [5, 3]

const myCube = cube({ size: 10, center: [0, 0, 0] });
const cubeCenter = measureCenter(myCube); // [0, 0, 0]

// Measure center of complex shape
const offsetSphere = translate([8, 0, 0], sphere({ radius: 3 }));
const combined = union(myCube, offsetSphere);
const combinedCenter = measureCenter(combined); // Weighted toward sphere

// Center of mass (useful for balancing)
const centerOfMass = measureCenterOfMass(combined);

Dimension Measurements

Calculate the overall dimensions (width, height, depth) of geometries.

/**
 * Measure the dimensions of a geometry
 * @param {Object} geometry - Geometry to measure
 * @returns {Array} Dimensions as [width, height] for 2D or [width, height, depth] for 3D
 */
function measureDimensions(geometry: any): [number, number] | [number, number, number];

Usage Examples:

const { rectangle, cube, sphere, union, measureDimensions } = require('@jscad/modeling');

// Measure 2D dimensions
const rect = rectangle({ size: [15, 8] });
const [width, height] = measureDimensions(rect); // [15, 8]

// Measure 3D dimensions  
const myCube = cube({ size: 12 });
const [cubeWidth, cubeHeight, cubeDepth] = measureDimensions(myCube); // [12, 12, 12]

// Measure complex shape dimensions
const complex = union(myCube, sphere({ radius: 8, center: [10, 0, 0] }));
const complexDims = measureDimensions(complex); // Overall envelope dimensions

Epsilon Measurements

Calculate epsilon values for numerical precision in operations.

/**
 * Measure the epsilon value of a geometry (precision tolerance)
 * @param {Object} geometry - Geometry to measure
 * @returns {Number} Epsilon value for the geometry
 */
function measureEpsilon(geometry: any): number;

/**
 * Measure aggregate epsilon for multiple geometries
 * @param {Array} objects - Array of geometries
 * @returns {Number} Aggregate epsilon value
 */
function measureAggregateEpsilon(objects: any[]): number;

Usage Examples:

const { cube, sphere, measureEpsilon, measureAggregateEpsilon } = require('@jscad/modeling');

// Measure precision tolerance
const myCube = cube({ size: 10 });
const cubeEpsilon = measureEpsilon(myCube);

// Use for precision-sensitive operations
const objects = [myCube, sphere({ radius: 5 })];
const tolerance = measureAggregateEpsilon(objects);

Advanced Measurement Techniques

Manufacturing Analysis

Use measurements for manufacturing preparation:

const { cube, cylinder, subtract, measureVolume, measureArea, measureDimensions } = require('@jscad/modeling');

// Analyze a machined part
const stock = cube({ size: [50, 30, 20] });
const hole = cylinder({ height: 25, radius: 8 });
const part = subtract(stock, hole);

// Calculate material usage
const stockVolume = measureVolume(stock);
const partVolume = measureVolume(part);
const materialRemoved = stockVolume - partVolume;
const removalPercentage = (materialRemoved / stockVolume) * 100;

// Calculate surface area for coating
const surfaceArea = measureArea(part);

// Check dimensions for tolerances
const [width, height, depth] = measureDimensions(part);
console.log(`Part dimensions: ${width} × ${height} × ${depth}`);

Assembly Analysis

Analyze assemblies and part relationships:

const { cube, sphere, translateX, measureCenter, measureBoundingBox, measureAggregateBoundingBox } = require('@jscad/modeling');

// Assembly components
const base = cube({ size: [30, 20, 5] });
const pillar1 = translateX(-10, cube({ size: [4, 4, 15] }));
const pillar2 = translateX(10, cube({ size: [4, 4, 15] }));
const top = translateZ(15, cube({ size: [25, 15, 3] }));

const assembly = [base, pillar1, pillar2, top];

// Analyze assembly
const assemblyBounds = measureAggregateBoundingBox(assembly);
const assemblyCOG = measureCenter(union(...assembly));

// Check component alignment
const baseCOG = measureCenter(base);
const topCOG = measureCenter(top);
const alignment = [topCOG[0] - baseCOG[0], topCOG[1] - baseCOG[1]];

Quality Control

Use measurements for quality validation:

const { sphere, measureBoundingSphere, measureVolume } = require('@jscad/modeling');

// Check if sphere meets specifications
const testSphere = sphere({ radius: 10, segments: 32 });
const [cx, cy, cz, boundingRadius] = measureBoundingSphere(testSphere);
const volume = measureVolume(testSphere);

// Theoretical values for comparison
const theoreticalVolume = (4/3) * Math.PI * Math.pow(10, 3);
const volumeError = Math.abs(volume - theoreticalVolume) / theoreticalVolume;

// Quality check
const isWithinTolerance = volumeError < 0.01; // 1% tolerance

Performance Optimization

Use measurements to optimize geometry complexity:

const { sphere, measureArea, retessellate } = require('@jscad/modeling');

// Create spheres with different detail levels
const lowDetail = sphere({ radius: 5, segments: 8 });
const medDetail = sphere({ radius: 5, segments: 16 });
const highDetail = sphere({ radius: 5, segments: 32 });

// Compare surface areas (should be similar)
const areas = [lowDetail, medDetail, highDetail].map(measureArea);
const surfaceAreaVariation = Math.max(...areas) - Math.min(...areas);

// Choose appropriate detail level based on requirements
const targetArea = 4 * Math.PI * 25; // Theoretical sphere area
const errors = areas.map(area => Math.abs(area - targetArea) / targetArea);

docs

booleans.md

colors.md

curves.md

expansions-modifiers.md

extrusions.md

geometries.md

hulls.md

index.md

maths.md

measurements.md

primitives.md

text-utils.md

transforms.md

tile.json