All files / services geometry-helper.ts

100% Statements 89/89
91.07% Branches 51/56
100% Functions 16/16
100% Lines 85/85

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                    250x   250x   619x   250x                 252x   252x 1x 1x     251x   252x             13x 10863x                   619x               619x 619x 3450x 3450x   619x                 13x 13x   38x 24x     13x                 13x 13x 11x 28x 28x 28x 15x   28x 11x     11x 3x 3x 3x 3x     2x 1x   13x                 74x 74x 1x   73x 73x 112x 38x 38x       73x               223x 223x                 13x 13x 9x 26x 26x 26x 22x   26x 9x     9x 3x 3x 3x 3x     4x 2x   13x               57x 57x 8x   4x   49x 47x     17x     57x       3450x 3450x 3450x 3450x 3450x   3450x 3450x 3450x 3450x        
import * as Inputs from "../inputs";
 
export class GeometryHelper {
 
    /**
     * Applies one or more 4×4 transformation matrices to a list of points sequentially.
     * Each transformation is applied in order (composition of transformations).
     * Example: points=[[0,0,0], [1,0,0]] with translation [5,0,0] → [[5,0,0], [6,0,0]]
     */
    transformControlPoints(transformation: number[][] | number[][][], transformedControlPoints: Inputs.Base.Point3[]): Inputs.Base.Point3[] {
        const transformationArrays = this.getFlatTransformations(transformation);
 
        transformationArrays.forEach(transform => {
 
            transformedControlPoints = this.transformPointsByMatrixArray(transformedControlPoints, transform);
        });
        return transformedControlPoints;
    }
 
    /**
     * Flattens nested transformation arrays into a single-level array of transformation matrices.
     * Handles both 2D arrays (single transform list) and 3D arrays (nested transform lists).
     * Example: [[[matrix1, matrix2]], [[matrix3]]] → [matrix1, matrix2, matrix3]
     */
    getFlatTransformations(transformation: number[][] | number[][][]): number[][] {
        let transformationArrays = [];
 
        if (this.getArrayDepth(transformation) === 3) {
            transformation.forEach(transform => {
                transformationArrays.push(...transform);
            });
        } else {
            transformationArrays = transformation;
        }
        return transformationArrays;
}
 
    /**
     * Calculates the nesting depth of an array recursively.
     * Example: [1,2,3] → 1, [[1,2],[3,4]] → 2, [[[1]]] → 3
     */
    getArrayDepth = (value): number => {
        return Array.isArray(value) ?
            1 + Math.max(...value.map(this.getArrayDepth)) :
            0;
    };
 
    /**
     * Applies a single 4×4 transformation matrix (as flat 16-element array) to multiple points.
     * Example: points=[[0,0,0], [1,0,0]] with translation matrix → transformed points
     */
    transformPointsByMatrixArray(points: Inputs.Base.Point3[], transform: number[]): Inputs.Base.Point3[] {
        return this.transformPointsCoordinates(points, transform);
    }
 
    /**
     * Transforms multiple points using a transformation matrix (maps each point through the matrix).
     * Example: points=[[1,0,0], [0,1,0]] with 90° rotation → [[0,1,0], [-1,0,0]]
     */
    transformPointsCoordinates(points: Inputs.Base.Point3[], transform: number[]): Inputs.Base.Point3[] {
        const transformedPoints = [];
        for (const pt of points) {
            const transformedVector = this.transformCoordinates(pt[0], pt[1], pt[2], transform);
            transformedPoints.push(transformedVector);
        }
        return transformedPoints;
    }
 
    /**
     * Removes all duplicate vectors from a list (works with arbitrary-length numeric vectors).
     * Compares vectors using tolerance for floating-point equality.
     * Example: [[1,2], [3,4], [1,2], [5,6]] with tolerance=1e-7 → [[1,2], [3,4], [5,6]]
     */
    removeAllDuplicateVectors(vectors: number[][], tolerance = 1e-7): number[][] {
        const cleanVectors: number[][] = [];
        vectors.forEach(vector => {
            // when there are no vectors in cleanVectors array that match the current vector, push it in.
            if (!cleanVectors.some(s => this.vectorsTheSame(vector, s, tolerance))) {
                cleanVectors.push(vector);
            }
        });
        return cleanVectors;
    }
 
    /**
     * Removes consecutive duplicate vectors from a list (keeps only first occurrence in each sequence).
     * Optionally checks and removes duplicate if first and last vectors match.
     * Example: [[1,2], [1,2], [3,4], [3,4], [5,6]] → [[1,2], [3,4], [5,6]]
     */
    removeConsecutiveVectorDuplicates(vectors: number[][], checkFirstAndLast = true, tolerance = 1e-7): number[][] {
        const vectorsRemaining: number[][] = [];
        if (vectors.length > 1) {
            for (let i = 1; i < vectors.length; i++) {
                const currentVector = vectors[i];
                const previousVector = vectors[i - 1];
                if (!this.vectorsTheSame(currentVector, previousVector, tolerance)) {
                    vectorsRemaining.push(previousVector);
                }
                if (i === vectors.length - 1) {
                    vectorsRemaining.push(currentVector);
                }
            }
            if (checkFirstAndLast) {
                const firstVector = vectorsRemaining[0];
                const lastVector = vectorsRemaining[vectorsRemaining.length - 1];
                Eif (this.vectorsTheSame(firstVector, lastVector, tolerance)) {
                    vectorsRemaining.pop();
                }
            }
        } else if (vectors.length === 1) {
            vectorsRemaining.push(...vectors);
        }
        return vectorsRemaining;
    }
 
    /**
     * Compares two vectors for approximate equality using tolerance (element-wise comparison).
     * Returns false if vectors have different lengths.
     * Example: [1.0000001, 2.0], [1.0, 2.0] with tolerance=1e-6 → true
     */
    vectorsTheSame(vec1: number[], vec2: number[], tolerance: number) {
        let result = false;
        if (vec1.length !== vec2.length) {
            return result;
        } else {
            result = true;
            for (let i = 0; i < vec1.length; i++) {
                if (!this.approxEq(vec1[i], vec2[i], tolerance)) {
                    result = false;
                    break;
                }
            }
        }
        return result;
    }
 
    /**
     * Checks if two numbers are approximately equal within a tolerance.
     * Example: 1.0000001, 1.0 with tolerance=1e-6 → true, 1.001, 1.0 with tolerance=1e-6 → false
     */
    approxEq(num1: number, num2: number, tolerance: number): boolean {
        const res = Math.abs(num1 - num2) < tolerance;
        return res;
    }
 
    /**
     * Removes consecutive duplicate points from a list (specialized for 3D/2D points).
     * Optionally checks and removes duplicate if first and last points match (for closed loops).
     * Example: [[0,0,0], [0,0,0], [1,0,0], [1,0,0]] → [[0,0,0], [1,0,0]]
     */
    removeConsecutivePointDuplicates(points: Inputs.Base.Point3[], checkFirstAndLast = true, tolerance = 1e-7): Inputs.Base.Point3[] {
        const pointsRemaining = [];
        if (points.length > 1) {
            for (let i = 1; i < points.length; i++) {
                const currentPoint = points[i];
                const previousPoint = points[i - 1];
                if (!this.arePointsTheSame(currentPoint, previousPoint, tolerance)) {
                    pointsRemaining.push(previousPoint);
                }
                if (i === points.length - 1) {
                    pointsRemaining.push(currentPoint);
                }
            }
            if (checkFirstAndLast) {
                const firstPoint = pointsRemaining[0];
                const lastPoint = pointsRemaining[pointsRemaining.length - 1];
                Eif (this.arePointsTheSame(firstPoint, lastPoint, tolerance)) {
                    pointsRemaining.pop();
                }
            }
        } else if (points.length === 1) {
            pointsRemaining.push(...points);
        }
        return pointsRemaining;
    }
 
    /**
     * Checks if two points are approximately equal using tolerance (supports 2D and 3D points).
     * Example: [1.0000001, 2.0, 3.0], [1.0, 2.0, 3.0] with tolerance=1e-6 → true
     */
    arePointsTheSame(pointA: Inputs.Base.Point3 | Inputs.Base.Point2, pointB: Inputs.Base.Point3 | Inputs.Base.Point2, tolerance: number): boolean {
        let result = false;
        if (pointA.length === 2 && pointB.length === 2) {
            if (this.approxEq(pointA[0], pointB[0], tolerance) &&
                this.approxEq(pointA[1], pointB[1], tolerance)) {
                result = true;
            }
        } else if (pointA.length === 3 && pointB.length === 3) {
            if (this.approxEq(pointA[0], pointB[0], tolerance) &&
                this.approxEq(pointA[1], pointB[1], tolerance) &&
                this.approxEq(pointA[2], pointB[2], tolerance)) {
                result = true;
            }
        }
        return result;
    }
 
    private transformCoordinates(x: number, y: number, z: number, transformation: number[]): Inputs.Base.Vector3 {
        const m = transformation;
        const rx = x * m[0] + y * m[4] + z * m[8] + m[12];
        const ry = x * m[1] + y * m[5] + z * m[9] + m[13];
        const rz = x * m[2] + y * m[6] + z * m[10] + m[14];
        const rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
 
        const newx = rx * rw;
        const newy = ry * rw;
        const newz = rz * rw;
        return [newx, newy, newz];
    }
 
}