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 | /** * Interface representing information about a rotation matrix * @interface */ interface RotationMatrixInformation { /** Whether the matrix represents a standard basis (identity matrix) */ isStandard: boolean; /** The rotation matrix as a flat array of 9 numbers [m11, m12, m13, m21, m22, m23, m31, m32, m33] */ rotationMatrix: number[]; } /** * Helper function to validate a 3x3 matrix input * @param matrix - The input matrix as a flat array * @throws {Error} If matrix is not a valid 3x3 matrix (array of 9 numbers) */ function validate3x3Matrix(matrix: number[]): void { if (!Array.isArray(matrix) || matrix.length !== 9) { throw new Error('Matrix must be an array of 9 numbers'); } if (!matrix.every((n) => typeof n === 'number' && !isNaN(n))) { throw new Error('Matrix must contain only valid numbers'); } } /** * Calculates the inverse of a 3x3 matrix * @param matrix - The input matrix as a flat array of 9 numbers [m11, m12, m13, m21, m22, m23, m31, m32, m33] * @returns The inverse matrix as a flat array of 9 numbers * @throws {Error} If matrix is not invertible or invalid */ export function inverse3x3Matrix(matrix: number[]): number[] { validate3x3Matrix(matrix); // First, convert the flat array into a 2D matrix for easier handling const mat = [ [matrix[0], matrix[1], matrix[2]], [matrix[3], matrix[4], matrix[5]], [matrix[6], matrix[7], matrix[8]], ]; // Calculate the determinant const determinant = mat[0][0] * (mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1]) - mat[0][1] * (mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]) + mat[0][2] * (mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0]); // Check if matrix is invertible if (Math.abs(determinant) < 1e-10) { throw new Error('Matrix is not invertible (determinant is zero)'); } // Calculate the adjugate matrix const adjugate = [ // First row [ mat[1][1] * mat[2][2] - mat[1][2] * mat[2][1], -(mat[0][1] * mat[2][2] - mat[0][2] * mat[2][1]), mat[0][1] * mat[1][2] - mat[0][2] * mat[1][1], ], // Second row [ -(mat[1][0] * mat[2][2] - mat[1][2] * mat[2][0]), mat[0][0] * mat[2][2] - mat[0][2] * mat[2][0], -(mat[0][0] * mat[1][2] - mat[0][2] * mat[1][0]), ], // Third row [ mat[1][0] * mat[2][1] - mat[1][1] * mat[2][0], -(mat[0][0] * mat[2][1] - mat[0][1] * mat[2][0]), mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0], ], ]; // Calculate inverse by dividing adjugate by determinant const inverse = []; for (let i = 0; i < 3; i++) { for (let j = 0; j < 3; j++) { inverse.push(adjugate[i][j] / determinant); } } return inverse; } /** * Normalizes a 3D vector * @param v - Array of 3 numbers representing a vector * @returns Normalized vector */ function normalizeVector(v: number[]): number[] { const magnitude = Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); return v.map((component) => component / magnitude); } /** * Checks if a set of direction vectors forms a standard basis * @param directions - Array of 9 numbers representing three 3D vectors [x1,x2,x3,y1,y2,y3,z1,z2,z3] * @returns Object containing whether the basis is standard and the corresponding rotation matrix * @throws {Error} If directions array is invalid */ export function checkStandardBasis( directions: number[] ): RotationMatrixInformation { validate3x3Matrix(directions); // Extract and normalize vectors const xVector = directions.slice(0, 3); const yVector = directions.slice(3, 6); const zVector = directions.slice(6, 9); const normalizedX = normalizeVector(xVector); const normalizedY = normalizeVector(yVector); const normalizedZ = normalizeVector(zVector); // Standard basis vectors for comparison const standardBasis = { x: [1, 0, 0], y: [0, 1, 0], z: [0, 0, 1], }; // Check if vectors match standard basis (allowing for small numerical errors) const epsilon = 1e-10; const isStandard = normalizedX.every( (val, i) => Math.abs(val - standardBasis.x[i]) < epsilon ) && normalizedY.every( (val, i) => Math.abs(val - standardBasis.y[i]) < epsilon ) && normalizedZ.every((val, i) => Math.abs(val - standardBasis.z[i]) < epsilon); const rotationMatrix = isStandard ? [...standardBasis.x, ...standardBasis.y, ...standardBasis.z] : inverse3x3Matrix([...normalizedX, ...normalizedY, ...normalizedZ]); return { isStandard, rotationMatrix, }; } /** * Rotates a single point around a given origin using a rotation matrix * @param point - Array of 3 numbers representing a point [x,y,z] * @param origin - Array of 3 numbers representing the rotation origin [x,y,z] * @param rotationMatrix - Array of 9 numbers representing the rotation matrix * @returns Rotated point as an array of 3 numbers */ function rotatePoint( point: number[], origin: number[], rotationMatrix: number[] ): number[] { const x = point[0] - origin[0]; const y = point[1] - origin[1]; const z = point[2] - origin[2]; return [ rotationMatrix[0] * x + rotationMatrix[1] * y + rotationMatrix[2] * z + origin[0], rotationMatrix[3] * x + rotationMatrix[4] * y + rotationMatrix[5] * z + origin[1], rotationMatrix[6] * x + rotationMatrix[7] * y + rotationMatrix[8] * z + origin[2], ]; } /** * Rotates an array of points around a given origin using a rotation matrix * @param rotationMatrix - Array of 9 numbers representing the rotation matrix * @param origin - Array of 3 numbers representing the rotation origin [x,y,z] * @param points - Array of points in format [x1,y1,z1,x2,y2,z2,...] * @returns Array of rotated points in the same format as input * @throws {Error} If any input array is invalid */ export function rotatePoints( rotationMatrix: number[], origin: number[], points: number[] ): number[] { const rotatedPoints: number[] = []; for (let i = 0; i < points.length; i += 3) { const point = points.slice(i, i + 3); const rotated = rotatePoint(point, origin, rotationMatrix); rotatedPoints.push(...rotated); } return rotatedPoints; } |