2
2
* @fileOverview
3
3
* @author David Gossow - [email protected]
4
4
*/
5
+ import { type PartialNullable } from '../types/interface-types.js' ;
6
+
7
+ export interface IQuaternion {
8
+ /**
9
+ * The x value.
10
+ */
11
+ x : number ;
12
+
13
+ /**
14
+ * The y value.
15
+ */
16
+ y : number ;
17
+
18
+ /**
19
+ * The z value.
20
+ */
21
+ z : number ;
22
+
23
+ /**
24
+ * The w value.
25
+ */
26
+ w : number ;
27
+ }
5
28
6
29
/**
7
30
* A Quaternion.
8
31
*/
9
- export default class Quaternion {
10
- /**
11
- * @param {Object } [options]
12
- * @param {number|null } [options.x=0] - The x value.
13
- * @param {number|null } [options.y=0] - The y value.
14
- * @param {number|null } [options.z=0] - The z value.
15
- * @param {number|null } [options.w=1] - The w value.
16
- */
17
- constructor ( options ) {
18
- options = options || { } ;
19
- this . x = options . x || 0 ;
20
- this . y = options . y || 0 ;
21
- this . z = options . z || 0 ;
22
- this . w = typeof options . w === 'number' ? options . w : 1 ;
32
+ export default class Quaternion implements IQuaternion {
33
+ x : number ;
34
+ y : number ;
35
+ z : number ;
36
+ w : number ;
37
+
38
+ constructor ( options ?: PartialNullable < IQuaternion > | null ) {
39
+ this . x = options ?. x ?? 0 ;
40
+ this . y = options ?. y ?? 0 ;
41
+ this . z = options ?. z ?? 0 ;
42
+ this . w = typeof options ?. w === 'number' ? options . w : 1 ;
23
43
}
44
+
24
45
/**
25
46
* Perform a conjugation on this quaternion.
26
47
*/
27
- conjugate ( ) {
48
+ conjugate ( ) : void {
28
49
this . x *= - 1 ;
29
50
this . y *= - 1 ;
30
51
this . z *= - 1 ;
31
52
}
53
+
32
54
/**
33
55
* Return the norm of this quaternion.
34
56
*/
35
- norm ( ) {
57
+ norm ( ) : number {
36
58
return Math . sqrt (
37
59
this . x * this . x + this . y * this . y + this . z * this . z + this . w * this . w
38
60
) ;
39
61
}
62
+
40
63
/**
41
64
* Perform a normalization on this quaternion.
42
65
*/
43
- normalize ( ) {
44
- var l = Math . sqrt (
66
+ normalize ( ) : void {
67
+ let l = Math . sqrt (
45
68
this . x * this . x + this . y * this . y + this . z * this . z + this . w * this . w
46
69
) ;
47
70
if ( l === 0 ) {
@@ -67,24 +90,25 @@ export default class Quaternion {
67
90
/**
68
91
* Set the values of this quaternion to the product of itself and the given quaternion.
69
92
*
70
- * @param {Quaternion } q - The quaternion to multiply with.
93
+ * @param {IQuaternion } q - The quaternion to multiply with.
71
94
*/
72
- multiply ( q ) {
73
- var newX = this . x * q . w + this . y * q . z - this . z * q . y + this . w * q . x ;
74
- var newY = - this . x * q . z + this . y * q . w + this . z * q . x + this . w * q . y ;
75
- var newZ = this . x * q . y - this . y * q . x + this . z * q . w + this . w * q . z ;
76
- var newW = - this . x * q . x - this . y * q . y - this . z * q . z + this . w * q . w ;
95
+ multiply ( q : IQuaternion ) : void {
96
+ const newX = this . x * q . w + this . y * q . z - this . z * q . y + this . w * q . x ;
97
+ const newY = - this . x * q . z + this . y * q . w + this . z * q . x + this . w * q . y ;
98
+ const newZ = this . x * q . y - this . y * q . x + this . z * q . w + this . w * q . z ;
99
+ const newW = - this . x * q . x - this . y * q . y - this . z * q . z + this . w * q . w ;
77
100
this . x = newX ;
78
101
this . y = newY ;
79
102
this . z = newZ ;
80
103
this . w = newW ;
81
104
}
105
+
82
106
/**
83
107
* Clone a copy of this quaternion.
84
108
*
85
109
* @returns {Quaternion } The cloned quaternion.
86
110
*/
87
- clone ( ) {
111
+ clone ( ) : Quaternion {
88
112
return new Quaternion ( this ) ;
89
113
}
90
114
}
0 commit comments