UNPKG

ts-scikit

Version:

A scientific toolkit written in Typescript

101 lines 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Segment = void 0; const point3_1 = require("./point3"); /** * A segment of a line. */ class Segment { constructor(a, b) { this._a = a; this._b = b; this._d = b.minus(a); } /** * Copies a segment. * @param s the segment. */ static FromSegment(s) { return new Segment(s.a, s.b); } /** * Endpoint A. */ get a() { return this._a; } /** * Endpoint B. */ get b() { return this._b; } /** * The vector from endpoint A to B. */ get d() { return this._d; } /** * The length of this segment. */ get length() { return this.a.distanceTo(this.b); } /** * Returns a copy of this segment. */ clone() { return Segment.FromSegment(this); } /** * Transforms this segment using matrix M. * @param m the matrix. */ transform(m) { this._a = m.times(this._a); this._b = m.times(this._b); this._d = this._b.minus(this._a); } /** * Tests this segment for intersection with the specified triangle. * * If such an intersection exists, this method returns the intersection * point. * @param xa x-coordinate of triangle vertex a. * @param ya y-coordinate of triangle vertex a. * @param za z-coordinate of triangle vertex a. * @param xb x-coordinate of triangle vertex b. * @param yb y-coordinate of triangle vertex b. * @param zb z-coordinate of triangle vertex b. * @param xc x-coordinate of triangle vertex c. * @param yc y-coordinate of triangle vertex c. * @param zc z-coordinate of triangle vertex c. * @returns the point of intersection; null, if none. */ intersectWithTriangle(xa, ya, za, xb, yb, zb, xc, yc, zc) { const xd = this._d.x, yd = this._d.y, zd = this._d.z; const xba = xb - xa, yba = yb - ya, zba = zb - za; const xca = xc - xa, yca = yc - ya, zca = zc - za; const xp = yd * zca - zd * yca, yp = zd * xca - xd * zca, zp = xd * yca - yd * xca; const a = xba * xp + yba * yp + zba * zp; if (-Segment.TINY < a && a < Segment.TINY) { return null; } const f = 1.0 / a; const xaa = this._a.x - xa, yaa = this._a.y - ya, zaa = this._a.z - za; const u = f * (xaa * xp + yaa * yp + zaa * zp); if (u < 0.0 || u > 1.0) { return null; } const xq = yaa * zba - zaa * yba, yq = zaa * xba - xaa * zba, zq = xaa * yba - yaa * xba; const v = f * (xd * xq + yd * yq + zd * zq); if (v < 0.0 || (u + v) > 1.0) { return null; } const t = f * (xca * xq + yca * yq + zca * zq); if (t < 0.0 || 1.0 < t) { return null; } const w = 1.0 - u - v; const xi = w * xa + u * xb + v * xc, yi = w * ya + u * yb + v * yc, zi = w * za + u * zb + v * zc; return new point3_1.Point3(xi, yi, zi); } } exports.Segment = Segment; Segment.TINY = 1000.0 * Number.EPSILON; //# sourceMappingURL=segment.js.map