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

expansions-modifiers.mddocs/

Expansions and Modifications

All shapes (primitives or the results of operations) can be expanded, contracted, or modified to correct issues and improve geometry quality. In all cases, these functions return new results and never change the original geometry.

Expansions

Expand

Expand shapes by adding material uniformly around their perimeter or surface.

/**
 * Expand geometries by adding material around the perimeter/surface
 * @param {Object} options - Expansion options
 * @param {Number} options.delta - Distance to expand (positive) or contract (negative)
 * @param {String} [options.corners='round'] - Corner style: 'round', 'chamfer', or 'square'
 * @param {Number} [options.segments=16] - Number of segments for rounded corners
 * @param {...Object} objects - Objects to expand
 * @returns {Object|Array} Expanded object(s)
 */
function expand(options: {
  delta: number,
  corners?: 'round' | 'chamfer' | 'square',
  segments?: number
}, ...objects: any[]): any;

Usage Examples:

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

// Expand 2D shape with rounded corners
const rect = rectangle({ size: [10, 6] });
const expandedRect = expand({ delta: 2, corners: 'round' }, rect);

// Contract 3D shape (negative delta)
const myCube = cube({ size: 10 });
const contractedCube = expand({ delta: -1 }, myCube);

// Expand with chamfered corners
const chamferedExpansion = expand({ 
  delta: 1.5, 
  corners: 'chamfer',
  segments: 8 
}, rect);

Offset

Create 2D offset curves from 2D shapes, useful for creating tool paths and margin operations.

/**
 * Create offset curves from 2D shapes
 * @param {Object} options - Offset options
 * @param {Number} options.delta - Distance to offset (positive for outward, negative for inward)
 * @param {String} [options.corners='round'] - Corner style: 'round', 'chamfer', or 'square'
 * @param {Number} [options.segments=16] - Number of segments for rounded corners
 * @param {...Object} objects - 2D objects to offset (geom2)
 * @returns {geom2|Array} Offset 2D geometries
 */
function offset(options: {
  delta: number,
  corners?: 'round' | 'chamfer' | 'square',
  segments?: number
}, ...objects: geom2[]): geom2 | geom2[];

Usage Examples:

const { circle, polygon, offset, union } = require('@jscad/modeling');

// Create tool path offset
const partOutline = polygon({
  points: [[0, 0], [10, 0], [10, 8], [5, 12], [0, 8]]
});
const toolPath = offset({ delta: 3, corners: 'round' }, partOutline);

// Create multiple offset rings
const baseCircle = circle({ radius: 5 });
const offsets = [];
for (let i = 1; i <= 5; i++) {
  offsets.push(offset({ delta: i * 2 }, baseCircle));
}
const concentricRings = union(...offsets);

Modifiers

Snap

Snap vertices to a grid or custom function to clean up geometry and reduce precision errors.

/**
 * Snap vertices of geometries to a grid or custom function
 * @param {Object} options - Snap options
 * @param {Function} options.snapFunction - Function that snaps a vertex: (vertex) => snappedVertex
 * @param {...Object} objects - Objects to snap
 * @returns {Object|Array} Objects with snapped vertices
 */
function snap(options: {
  snapFunction: (vertex: [number, number, number]) => [number, number, number]
}, ...objects: any[]): any;

Usage Examples:

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

// Snap to 0.1 unit grid
const gridSnap = (vertex) => [
  Math.round(vertex[0] * 10) / 10,
  Math.round(vertex[1] * 10) / 10,
  Math.round(vertex[2] * 10) / 10
];

const roughShape = union(
  cube({ size: 10.0123 }),
  sphere({ radius: 4.9876 })
);
const cleanShape = snap({ snapFunction: gridSnap }, roughShape);

// Snap to custom positions
const customSnap = (vertex) => {
  // Snap to nearest multiple of 5
  return [
    Math.round(vertex[0] / 5) * 5,
    Math.round(vertex[1] / 5) * 5,
    Math.round(vertex[2] / 5) * 5
  ];
};
const customSnapped = snap({ snapFunction: customSnap }, roughShape);

Retessellate

Retessellate meshes to improve triangle quality and reduce complexity.

/**
 * Retessellate geometries to improve mesh quality
 * @param {Object} options - Retessellation options
 * @param {Number} [options.maxAngle=Math.PI] - Maximum angle between face normals for coplanar detection
 * @param {...Object} objects - Objects to retessellate
 * @returns {Object|Array} Retessellated objects
 */
function retessellate(options?: {
  maxAngle?: number
}, ...objects: any[]): any;

Usage Examples:

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

// Retessellate complex boolean result
const complex = union(
  sphere({ radius: 5, segments: 64 }),
  cube({ size: 8 }),
  sphere({ radius: 3, center: [4, 4, 4] })
);
const optimized = retessellate({}, complex);

// Retessellate with custom angle threshold
const strictRetessellation = retessellate({
  maxAngle: Math.PI / 6  // 30 degrees
}, complex);

Generalize

Convert specific geometry types to more general forms for compatibility and processing.

/**
 * Generalize geometries to more general forms
 * @param {Object} options - Generalization options
 * @param {Boolean} [options.snap=true] - Whether to snap vertices during generalization
 * @param {Number} [options.snapFunction] - Custom snap function
 * @param {...Object} objects - Objects to generalize
 * @returns {Object|Array} Generalized objects
 */
function generalize(options?: {
  snap?: boolean,
  snapFunction?: (vertex: [number, number, number]) => [number, number, number]
}, ...objects: any[]): any;

Usage Examples:

const { path2, polygon, generalize } = require('@jscad/modeling');

// Convert path2 to geom2 for boolean operations
const myPath = path2.fromPoints([[0, 0], [5, 0], [5, 5], [0, 5], [0, 0]]);
const generalizedShape = generalize({}, myPath);

// Generalize with custom snapping
const preciseGeneralize = generalize({
  snap: true,
  snapFunction: (v) => [Math.round(v[0]), Math.round(v[1]), Math.round(v[2])]
}, myPath);

Hull Operations

Hull operations compute convex boundaries around sets of shapes or points.

Hull

Create the convex hull of multiple shapes, enclosing all input geometry.

/**
 * Create convex hull of shapes
 * @param {...Object} objects - Objects to hull
 * @returns {Object} Convex hull containing all input objects
 */
function hull(...objects: any[]): any;

Hull Chain

Create a chain of hulls between consecutive shapes, useful for creating smooth transitions.

/**
 * Create chain of hulls between consecutive shapes
 * @param {...Object} objects - Objects to chain with hulls
 * @returns {Object} Union of hulls between consecutive pairs
 */
function hullChain(...objects: any[]): any;

Point-based Hulls

Create hulls directly from point sets.

/**
 * Create 2D convex hull from points
 * @param {Array} points - Array of [x,y] points
 * @returns {geom2} 2D convex hull geometry
 */
function hullPoints2(points: Array<[number, number]>): geom2;

/**
 * Create 3D convex hull from points  
 * @param {Array} points - Array of [x,y,z] points
 * @returns {geom3} 3D convex hull geometry
 */
function hullPoints3(points: Array<[number, number, number]>): geom3;

Usage Examples:

const { cube, sphere, cylinder, hull, hullChain, hullPoints3, translate } = require('@jscad/modeling');

// Hull multiple shapes
const shape1 = cube({ size: 5 });
const shape2 = translate([10, 0, 0], sphere({ radius: 3 }));
const shape3 = translate([5, 8, 0], cylinder({ height: 6, radius: 2 }));
const hullResult = hull(shape1, shape2, shape3);

// Create smooth chain between shapes
const smoothTransition = hullChain(shape1, shape2, shape3);

// Hull from point cloud
const points = [
  [0, 0, 0], [5, 0, 0], [2.5, 4, 0], [2.5, 2, 6], [1, 1, 8]
];
const pointHull = hullPoints3(points);

Advanced Techniques

Combining Operations

Complex modifications often combine multiple operations:

const { rectangle, expand, offset, snap, union } = require('@jscad/modeling');

// Create a part with precise dimensions and clean geometry
const baseRect = rectangle({ size: [20, 15] });

// Add material and create offsets
const expanded = expand({ delta: 2, corners: 'round' }, baseRect);
const innerOffset = offset({ delta: -1 }, baseRect);

// Combine and clean up
const combined = union(expanded, innerOffset);
const cleanFinal = snap({
  snapFunction: (v) => [Math.round(v[0] * 100) / 100, Math.round(v[1] * 100) / 100]
}, combined);

Manufacturing Considerations

Use expansions and modifications for manufacturing preparation:

const { cube, expand, retessellate } = require('@jscad/modeling');

// Add material allowance for machining
const rawPart = expand({ delta: 0.5 }, cube({ size: 10 }));

// Optimize mesh for 3D printing
const printReady = retessellate({ maxAngle: Math.PI / 4 }, rawPart);

// Create tool clearance
const toolClearance = expand({ delta: 1.5, corners: 'round' }, printReady);

Performance Tips

  • Use retessellate after complex boolean operations to improve mesh quality
  • Apply snap operations to reduce floating-point precision issues
  • Consider expand with negative delta as an alternative to complex subtract operations
  • Hull operations can be computationally expensive with many input shapes

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