UNPKG

@mlightcad/geometry-engine

Version:

The geometry-engine package provides comprehensive geometric entities, mathematical operations, and transformations for 2D and 3D space. This package mimics AutoCAD ObjectARX's AcGe (Geometry) classes and provides the mathematical foundation for CAD opera

358 lines 12.7 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { AcGeBox3d, AcGePoint3d, AcGeVector3d } from '../math'; import { AcGeCurve3d } from './AcGeCurve3d'; /** * Internal class for computing cubic polynomial coefficients * Based on an optimized c++ solution in * - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/ * - http://ideone.com/NoEbVM */ var CubicPoly = /** @class */ (function () { function CubicPoly() { this.c0 = 0; this.c1 = 0; this.c2 = 0; this.c3 = 0; } /** * Compute coefficients for a cubic polynomial * p(s) = c0 + c1*s + c2*s^2 + c3*s^3 * such that * p(0) = x0, p(1) = x1 * and * p'(0) = t0, p'(1) = t1. */ CubicPoly.prototype.init = function (x0, x1, t0, t1) { this.c0 = x0; this.c1 = t0; this.c2 = -3 * x0 + 3 * x1 - 2 * t0 - t1; this.c3 = 2 * x0 - 2 * x1 + t0 + t1; }; /** * Initialize for Catmull-Rom interpolation */ CubicPoly.prototype.initCatmullRom = function (x0, x1, x2, x3, tension) { this.init(x1, x2, tension * (x2 - x0), tension * (x3 - x1)); }; /** * Initialize for non-uniform Catmull-Rom interpolation */ CubicPoly.prototype.initNonuniformCatmullRom = function (x0, x1, x2, x3, dt0, dt1, dt2) { // compute tangents when parameterized in [t1,t2] var t1 = (x1 - x0) / dt0 - (x2 - x0) / (dt0 + dt1) + (x2 - x1) / dt1; var t2 = (x2 - x1) / dt1 - (x3 - x1) / (dt1 + dt2) + (x3 - x2) / dt2; // rescale tangents for parametrization in [0,1] t1 *= dt1; t2 *= dt1; this.init(x1, x2, t1, t2); }; /** * Calculate the polynomial value at parameter t */ CubicPoly.prototype.calc = function (t) { var t2 = t * t; var t3 = t2 * t; return this.c0 + this.c1 * t + this.c2 * t2 + this.c3 * t3; }; return CubicPoly; }()); /** * A curve representing a Catmull-Rom spline. * * ```js * //Create a closed wavey loop * const curve = new AcGeCatmullRomCurve3d( [ * new AcGePoint3d( -10, 0, 10 ), * new AcGePoint3d( -5, 5, 5 ), * new AcGePoint3d( 0, 0, 0 ), * new AcGePoint3d( 5, -5, 5 ), * new AcGePoint3d( 10, 0, 10 ) * ], true ); // true for closed curve * * const points = curve.getPoints( 50 ); * * // Convert to NURBS curve * const nurbsCurve = curve.toNurbsCurve(); * ``` */ var AcGeCatmullRomCurve3d = /** @class */ (function (_super) { __extends(AcGeCatmullRomCurve3d, _super); /** * Constructs a new Catmull-Rom curve. * * @param points - An array of 3D points defining the curve. * @param closed - Whether the curve is closed or not. * @param curveType - The curve type. * @param tension - Tension of the curve. */ function AcGeCatmullRomCurve3d(points, closed, curveType, tension) { if (points === void 0) { points = []; } if (closed === void 0) { closed = false; } if (curveType === void 0) { curveType = 'centripetal'; } if (tension === void 0) { tension = 0.5; } var _this = _super.call(this) || this; /** * This flag can be used for type testing. */ _this.isCatmullRomCurve3d = true; /** * The curve type identifier */ _this.type = 'CatmullRomCurve3d'; // Internal computation objects _this._tmp = new AcGeVector3d(); _this._px = new CubicPoly(); _this._py = new CubicPoly(); _this._pz = new CubicPoly(); _this._points = points.map(function (p) { return new AcGePoint3d(p); }); _this._closed = closed; _this._curveType = curveType; _this._tension = tension; return _this; } Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "points", { /** * An array of 3D points defining the curve. */ get: function () { return this._points; }, enumerable: false, configurable: true }); Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "closed", { /** * Whether the curve is closed or not. */ get: function () { return this._closed; }, enumerable: false, configurable: true }); Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "curveType", { /** * The curve type. */ get: function () { return this._curveType; }, enumerable: false, configurable: true }); Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "tension", { /** * Tension of the curve. */ get: function () { return this._tension; }, enumerable: false, configurable: true }); Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "startPoint", { /** * Start point of this curve. */ get: function () { return this._points.length > 0 ? this._points[0] : new AcGePoint3d(); }, enumerable: false, configurable: true }); Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "endPoint", { /** * End point of this curve. */ get: function () { return this._points.length > 0 ? this._points[this._points.length - 1] : new AcGePoint3d(); }, enumerable: false, configurable: true }); Object.defineProperty(AcGeCatmullRomCurve3d.prototype, "length", { /** * Length of this curve (approximated). */ get: function () { if (this._points.length < 2) return 0; var totalLength = 0; for (var i = 1; i < this._points.length; i++) { totalLength += this._points[i - 1].distanceTo(this._points[i]); } if (this._closed && this._points.length > 2) { totalLength += this._points[this._points.length - 1].distanceTo(this._points[0]); } return totalLength; }, enumerable: false, configurable: true }); /** * Returns a point on the curve. * * @param t - A interpolation factor representing a position on the curve. Must be in the range `[0,1]`. * @param optionalTarget - The optional target vector the result is written to. * @return The position on the curve. */ AcGeCatmullRomCurve3d.prototype.getPoint = function (t, optionalTarget) { if (optionalTarget === void 0) { optionalTarget = new AcGePoint3d(); } var point = optionalTarget; var points = this._points; var l = points.length; if (l === 0) { return point.set(0, 0, 0); } if (l === 1) { return point.copy(points[0]); } var p = (l - (this._closed ? 0 : 1)) * t; var intPoint = Math.floor(p); var weight = p - intPoint; if (this._closed) { intPoint += intPoint > 0 ? 0 : (Math.floor(Math.abs(intPoint) / l) + 1) * l; } else if (weight === 0 && intPoint === l - 1) { intPoint = l - 2; weight = 1; } var p0, p3; // 4 points (p1 & p2 defined below) if (this._closed || intPoint > 0) { p0 = points[(intPoint - 1) % l]; } else { // extrapolate first point this._tmp.subVectors(points[0], points[1]).add(points[0]); p0 = new AcGePoint3d(this._tmp.x, this._tmp.y, this._tmp.z); } var p1 = points[intPoint % l]; var p2 = points[(intPoint + 1) % l]; if (this._closed || intPoint + 2 < l) { p3 = points[(intPoint + 2) % l]; } else { // extrapolate last point this._tmp.subVectors(points[l - 1], points[l - 2]).add(points[l - 1]); p3 = new AcGePoint3d(this._tmp.x, this._tmp.y, this._tmp.z); } if (this._curveType === 'centripetal' || this._curveType === 'chordal') { // init Centripetal / Chordal Catmull-Rom var pow = this._curveType === 'chordal' ? 0.5 : 0.25; var dt0 = Math.pow(p0.distanceToSquared(p1), pow); var dt1 = Math.pow(p1.distanceToSquared(p2), pow); var dt2 = Math.pow(p2.distanceToSquared(p3), pow); // safety check for repeated points if (dt1 < 1e-4) dt1 = 1.0; if (dt0 < 1e-4) dt0 = dt1; if (dt2 < 1e-4) dt2 = dt1; this._px.initNonuniformCatmullRom(p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2); this._py.initNonuniformCatmullRom(p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2); this._pz.initNonuniformCatmullRom(p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2); } else if (this._curveType === 'catmullrom') { this._px.initCatmullRom(p0.x, p1.x, p2.x, p3.x, this._tension); this._py.initCatmullRom(p0.y, p1.y, p2.y, p3.y, this._tension); this._pz.initCatmullRom(p0.z, p1.z, p2.z, p3.z, this._tension); } point.set(this._px.calc(weight), this._py.calc(weight), this._pz.calc(weight)); return point; }; /** * Get an array of points along the curve * @param divisions - Number of divisions to create * @returns Array of points along the curve */ AcGeCatmullRomCurve3d.prototype.getPoints = function (divisions) { var points = []; for (var d = 0; d <= divisions; d++) { points.push(this.getPoint(d / divisions)); } return points; }; /** * Set the points defining the curve * @param points - Array of points */ AcGeCatmullRomCurve3d.prototype.setPoints = function (points) { this._points = points.map(function (p) { return new AcGePoint3d(p); }); this._boundingBoxNeedsUpdate = true; }; /** * Set whether the curve is closed * @param closed - Whether the curve should be closed */ AcGeCatmullRomCurve3d.prototype.setClosed = function (closed) { if (this._closed === closed) { return; } this._closed = closed; this._boundingBoxNeedsUpdate = true; }; /** * Set the curve type * @param curveType - The curve type */ AcGeCatmullRomCurve3d.prototype.setCurveType = function (curveType) { this._curveType = curveType; }; /** * Set the tension of the curve * @param tension - The tension value */ AcGeCatmullRomCurve3d.prototype.setTension = function (tension) { this._tension = tension; }; /** * Transforms the curve by applying the input matrix. * @param matrix Input transformation matrix * @return Return this curve */ AcGeCatmullRomCurve3d.prototype.transform = function (matrix) { this._points = this._points.map(function (point) { var transformedPoint = new AcGePoint3d(); transformedPoint.copy(point); transformedPoint.applyMatrix4(matrix); return transformedPoint; }); this._boundingBoxNeedsUpdate = true; return this; }; /** * Calculate the bounding box of this curve. * @return The bounding box */ AcGeCatmullRomCurve3d.prototype.calculateBoundingBox = function () { if (this._points.length === 0) { return new AcGeBox3d(); } var box = new AcGeBox3d(); this._points.forEach(function (point) { box.expandByPoint(point); }); return box; }; return AcGeCatmullRomCurve3d; }(AcGeCurve3d)); export { AcGeCatmullRomCurve3d }; //# sourceMappingURL=AcGeCatmullRomCurve3d.js.map