* rotate this matrix (counter-clockwise) by the specified angle (in radians). * @param angle - Rotation angle in radians. * @param [v] - the axis to rotate around (defaults to Z axis) * @returns Reference to this object for method chaining
(angle: number, v?: Vector3d)
| 672 | * @returns Reference to this object for method chaining |
| 673 | */ |
| 674 | rotate(angle: number, v?: Vector3d) { |
| 675 | if (angle !== 0) { |
| 676 | const a = this.val; |
| 677 | let x = v ? v.x : 0; |
| 678 | let y = v ? v.y : 0; |
| 679 | let z = v ? v.z : 1; |
| 680 | |
| 681 | let len = Math.sqrt(x * x + y * y + z * z); |
| 682 | |
| 683 | if (len < EPSILON) { |
| 684 | return this; |
| 685 | } |
| 686 | |
| 687 | len = 1 / len; |
| 688 | x *= len; |
| 689 | y *= len; |
| 690 | z *= len; |
| 691 | |
| 692 | const s = Math.sin(angle); |
| 693 | const c = Math.cos(angle); |
| 694 | const t = 1 - c; |
| 695 | |
| 696 | const a00 = a[0]; |
| 697 | const a01 = a[1]; |
| 698 | const a02 = a[2]; |
| 699 | const a03 = a[3]; |
| 700 | const a10 = a[4]; |
| 701 | const a11 = a[5]; |
| 702 | const a12 = a[6]; |
| 703 | const a13 = a[7]; |
| 704 | const a20 = a[8]; |
| 705 | const a21 = a[9]; |
| 706 | const a22 = a[10]; |
| 707 | const a23 = a[11]; |
| 708 | |
| 709 | // Construct the elements of the rotation matrix |
| 710 | const b00 = x * x * t + c; |
| 711 | const b01 = y * x * t + z * s; |
| 712 | const b02 = z * x * t - y * s; |
| 713 | const b10 = x * y * t - z * s; |
| 714 | const b11 = y * y * t + c; |
| 715 | const b12 = z * y * t + x * s; |
| 716 | const b20 = x * z * t + y * s; |
| 717 | const b21 = y * z * t - x * s; |
| 718 | const b22 = z * z * t + c; |
| 719 | |
| 720 | // Perform rotation-specific matrix multiplication |
| 721 | a[0] = a00 * b00 + a10 * b01 + a20 * b02; |
| 722 | a[1] = a01 * b00 + a11 * b01 + a21 * b02; |
| 723 | a[2] = a02 * b00 + a12 * b01 + a22 * b02; |
| 724 | a[3] = a03 * b00 + a13 * b01 + a23 * b02; |
| 725 | a[4] = a00 * b10 + a10 * b11 + a20 * b12; |
| 726 | a[5] = a01 * b10 + a11 * b11 + a21 * b12; |
| 727 | a[6] = a02 * b10 + a12 * b11 + a22 * b12; |
| 728 | a[7] = a03 * b10 + a13 * b11 + a23 * b12; |
| 729 | a[8] = a00 * b20 + a10 * b21 + a20 * b22; |
| 730 | a[9] = a01 * b20 + a11 * b21 + a21 * b22; |
| 731 | a[10] = a02 * b20 + a12 * b21 + a22 * b22; |
no outgoing calls