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

224 lines 8.43 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 { AcGeBox2d, AcGePoint2d, AcGePoint3d } from '../math'; import { AcGeCircArc2d } from './AcGeCircArc2d'; import { AcGeCurve2d } from './AcGeCurve2d'; /** * The class represents the polyline geometry. */ var AcGePolyline2d = /** @class */ (function (_super) { __extends(AcGePolyline2d, _super); function AcGePolyline2d(vertices, closed) { if (vertices === void 0) { vertices = null; } if (closed === void 0) { closed = false; } var _this = _super.call(this) || this; _this._vertices = vertices ? vertices : new Array(); _this._closed = closed; return _this; } Object.defineProperty(AcGePolyline2d.prototype, "numberOfVertices", { /** * The number of vertices in the polyline */ get: function () { return this._vertices.length; }, enumerable: false, configurable: true }); Object.defineProperty(AcGePolyline2d.prototype, "closed", { /** * @inheritdoc */ get: function () { return this._closed; }, /** * Set the polyline to be closed (that is, there is a segment drawn from the last vertex to the first) * if 'value' is true. Set the polyline to be open (no segment between the last and first vertices) if * 'value' is false. */ set: function (value) { this._closed = value; this._boundingBoxNeedsUpdate = true; }, enumerable: false, configurable: true }); Object.defineProperty(AcGePolyline2d.prototype, "startPoint", { /** * Start point of this polyline */ get: function () { if (this.numberOfVertices > 0) { var vertex = this._vertices[0]; return new AcGePoint2d(vertex.x, vertex.y); } throw new Error('Start point does not exist in an empty polyline.'); }, enumerable: false, configurable: true }); Object.defineProperty(AcGePolyline2d.prototype, "endPoint", { /** * End point of this polyline */ get: function () { var length = this.numberOfVertices; if (length > 0) { if (this.closed) { var vertex = this._vertices[0]; return new AcGePoint2d(vertex.x, vertex.y); } else { var vertex = this._vertices[length - 1]; return new AcGePoint2d(vertex.x, vertex.y); } } throw new Error('End point does not exist in an empty polyline.'); }, enumerable: false, configurable: true }); Object.defineProperty(AcGePolyline2d.prototype, "length", { /** * @inheritdoc */ get: function () { var length = 0; var vertexArraylength = this._vertices.length; for (var index = 0; index < vertexArraylength; ++index) { var vertex = this._vertices[index]; var nextVertex = null; if (index < vertexArraylength - 1) { nextVertex = this._vertices[index + 1]; } else if (index == vertexArraylength - 1 && this.closed) { nextVertex = this._vertices[0]; } if (nextVertex) { if (vertex.bulge) { var arc = new AcGeCircArc2d(vertex, nextVertex, vertex.bulge); length += arc.length; } else { length += new AcGePoint2d(vertex.x, vertex.y).distanceTo(nextVertex); } } } return length; }, enumerable: false, configurable: true }); /** * This function adds a vertex to the polyline. If index is 0, the vertex will become the first * vertex of the polyline. If index is the value returned by this.numberOfVertices, then the vertex * will become the last vertex of the polyline. Otherwise the vertex will be added just before the * index vertex. * * @param index Input index (0 based) before which to insert the vertex * @param vertex Input vertex location point */ AcGePolyline2d.prototype.addVertexAt = function (index, vertex) { if (index <= 0) { this._vertices.unshift(vertex); } else { this._vertices.splice(index, 0, vertex); } this._boundingBoxNeedsUpdate = true; }; /** * Get the 2d location of the vertex index in the polyline's own object coordinate system (OCS). * * @param index Input index (0 based) of the vertex */ AcGePolyline2d.prototype.getPointAt = function (index) { var vertex = this._vertices[index]; return new AcGePoint2d(vertex.x, vertex.y); }; /** * @inheritdoc */ AcGePolyline2d.prototype.calculateBoundingBox = function () { var points = this.getPoints(100); return new AcGeBox2d().setFromPoints(points); }; /** * @inheritdoc */ AcGePolyline2d.prototype.transform = function (_matrix) { // TODO: implement it this._boundingBoxNeedsUpdate = true; return this; }; /** * Return an array of points to draw this polyline. * @param numPoints Input the nubmer of points returned for arc segmentation * @param elevation Input z value of points returned * @returns Return an array of point */ AcGePolyline2d.prototype.getPoints3d = function (numPoints, elevation) { var points = []; var tmp = this.getPoints(numPoints); tmp.forEach(function (point) { return points.push(new AcGePoint3d().set(point.x, point.y, elevation)); }); return points; }; /** * Return an array of points to draw this polyline. * @param numPoints Input the nubmer of points returned for arc segmentation * @returns Return an array of point */ AcGePolyline2d.prototype.getPoints = function (numPoints) { var points = []; var length = this._vertices.length; for (var index = 0; index < length; ++index) { var vertex = this._vertices[index]; if (vertex.bulge) { var nextVertex = null; if (index < length - 1) { nextVertex = this._vertices[index + 1]; } else if (index == length - 1 && this.closed) { nextVertex = this._vertices[0]; } // In theory, nextVertex should be always not null if (nextVertex) { var arc = new AcGeCircArc2d(vertex, nextVertex, vertex.bulge); var arcPoints = arc.getPoints(numPoints); var length_1 = arcPoints.length; for (var i = 0; i < length_1; ++i) { var point = arcPoints[i]; points.push(new AcGePoint2d(point.x, point.y)); } } } else { points.push(new AcGePoint2d(vertex.x, vertex.y)); if (index == length - 1 && this.closed) { points.push(points[0]); } } } return points; }; return AcGePolyline2d; }(AcGeCurve2d)); export { AcGePolyline2d }; //# sourceMappingURL=AcGePolyline2d.js.map