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

curves.mddocs/

Curves

The curves module provides Bézier curve functionality for mathematical paths, animation easing functions, and parametric curve operations. It supports arbitrary dimensional control points and degrees with efficient arc length parameterization.

Capabilities

Bézier Curve Creation

Create Bézier curves with arbitrary number of control points and dimensions.

/**
 * Create a Bézier curve object with precomputed coefficients
 * @param {Array} points - Control points array (minimum 2 points)
 *   - For 1D: [n1, n2, n3, ...] or [[n1], [n2], [n3], ...]
 *   - For 2D+: [[x1,y1], [x2,y2], [x3,y3], ...]
 * @returns {Object} Bezier curve object with mathematical coefficients
 */
function create(points: Array<number> | Array<Array<number>>): Bezier;

/**
 * Calculate position along the curve at parameter t
 * @param {Number} t - Parameter value between 0 and 1 (inclusive)
 * @param {Object} bezier - Bezier curve object from create()
 * @returns {Number|Array} Position value(s) at parameter t
 */
function valueAt(t: number, bezier: Bezier): Array<number> | number;

/**
 * Calculate the first derivative (tangent/slope) at parameter t
 * @param {Number} t - Parameter value between 0 and 1 (inclusive)
 * @param {Object} bezier - Bezier curve object from create()
 * @returns {Number|Array} Tangent value(s) at parameter t
 */
function tangentAt(t: number, bezier: Bezier): Array<number> | number;

Usage Examples:

const { curves } = require('@jscad/modeling');
const { bezier } = curves;

// Linear 1D curve for scaling
const scaleEasing = bezier.create([1, 2]);
const scaleFactor = bezier.valueAt(0.5, scaleEasing); // 1.5

// Quadratic 2D curve for animation paths
const animPath = bezier.create([[0, 0], [50, 100], [100, 0]]);
const position = bezier.valueAt(0.5, animPath); // [50, 50]
const direction = bezier.tangentAt(0.5, animPath); // [50, -100]

// Cubic 3D curve for complex paths
const spatialPath = bezier.create([
  [0, 0, 0],     // start point
  [0, 10, 5],    // control point 1
  [10, 0, -5],   // control point 2
  [10, 10, 0]    // end point
]);

Arc Length Operations

Measure and parameterize curves by arc length for equal spacing and distance-based operations.

/**
 * Calculate cumulative arc lengths along the curve
 * @param {Number} segments - Number of segments for approximation accuracy
 * @param {Object} bezier - Bezier curve object from create()
 * @returns {Array} Array of cumulative arc lengths (length = segments + 1)
 */
function lengths(segments: number, bezier: Bezier): Array<number>;

/**
 * Calculate total arc length of the curve
 * @param {Number} segments - Number of segments for approximation accuracy
 * @param {Object} bezier - Bezier curve object from create()
 * @returns {Number} Approximate total arc length
 */
function length(segments: number, bezier: Bezier): number;

/**
 * Convert arc length distance to parameter t value
 * @param {Object} options - Configuration object
 * @param {Number} [options.distance=0] - Arc length distance along curve
 * @param {Number} [options.segments=100] - Accuracy segments for approximation
 * @param {Object} bezier - Bezier curve object from create()
 * @returns {Number} Parameter t value (0-1) corresponding to arc length
 */
function arcLengthToT(options: {
  distance?: number,
  segments?: number
}, bezier: Bezier): number;

Usage Examples:

const { curves } = require('@jscad/modeling');
const { bezier } = curves;

const curve = bezier.create([[0, 0], [10, 5], [20, 0]]);

// Measure curve length
const totalLength = bezier.length(100, curve); // High accuracy with 100 segments

// Create equally spaced points along curve
const segments = 10;
const increment = totalLength / segments;
const equalPoints = [];

for (let i = 0; i <= segments; i++) {
  const distance = i * increment;
  const t = bezier.arcLengthToT({ distance, segments: 100 }, curve);
  const point = bezier.valueAt(t, curve);
  equalPoints.push(point);
}

// Get cumulative lengths for analysis
const cumLengths = bezier.lengths(50, curve);
const halfwayLength = cumLengths[25]; // Length at middle segment

Type Definitions

// Bézier curve object structure
type Bezier = {
  points: Array<number> | Array<Array<number>>;  // Control points
  pointType: string;                             // Point type identifier  
  dimensions: number;                            // Number of dimensions
  permutations: Array<number>;                   // Binomial coefficients
  tangentPermutations: Array<number>;            // Tangent coefficients
};

// Point type identifiers
type PointTypes = 
  | 'float_single'  // 1D curves: [n1, n2, n3, ...]
  | 'float_1'       // 1D curves: [[n1], [n2], [n3], ...]  
  | 'float_2'       // 2D curves: [[x,y], [x,y], ...]
  | 'float_3'       // 3D curves: [[x,y,z], [x,y,z], ...]
  | 'float_n';      // n-dimensional curves

// Arc length parameterization options
type ArcLengthToTOptions = {
  distance?: number;   // Arc length distance (default: 0)
  segments?: number;   // Accuracy segments (default: 100)
};

Mathematical Foundation

The module implements the standard Bézier curve formula:

Position: B(t) = Σ(C(n,i) * (1-t)^(n-i) * t^i * P_i) where:

  • C(n,i) = binomial coefficient n!/(i!(n-i)!)
  • n = curve degree (number of control points - 1)
  • P_i = control point i
  • t = parameter (0 to 1)

Tangent: First derivative reduces degree by 1, using control point differences n * (P_{i+1} - P_i)

Arc Length: Euclidean distance approximation between evenly spaced parameter samples, with binary search for inverse parameterization.

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