forked from dimforge/rapier.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.ts
263 lines (218 loc) · 4.94 KB
/
math.ts
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {RawVector, RawRotation} from "./raw";
// #if DIM3
import {RawSdpMatrix3} from "./raw";
// #endif
// #if DIM2
export interface Vector {
x: number;
y: number;
}
/**
* A 2D vector.
*/
export class Vector2 implements Vector {
x: number;
y: number;
constructor(x: number, y: number) {
this.x = x;
this.y = y;
}
}
export class VectorOps {
public static new(x: number, y: number): Vector {
return new Vector2(x, y);
}
public static zeros(): Vector {
return VectorOps.new(0.0, 0.0);
}
// FIXME: type ram: RawVector?
public static fromRaw(raw: RawVector): Vector {
if (!raw) return null;
let res = VectorOps.new(raw.x, raw.y);
raw.free();
return res;
}
public static intoRaw(v: Vector): RawVector {
return new RawVector(v.x, v.y);
}
public static copy(out: Vector, input: Vector) {
out.x = input.x;
out.y = input.y;
}
}
/**
* A rotation angle in radians.
*/
export type Rotation = number;
export class RotationOps {
public static identity(): number {
return 0.0;
}
public static fromRaw(raw: RawRotation): Rotation {
if (!raw) return null;
let res = raw.angle;
raw.free();
return res;
}
public static intoRaw(angle: Rotation): RawRotation {
return RawRotation.fromAngle(angle);
}
}
// #endif
// #if DIM3
export interface Vector {
x: number;
y: number;
z: number;
}
/**
* A 3D vector.
*/
export class Vector3 implements Vector {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
}
export class VectorOps {
public static new(x: number, y: number, z: number): Vector {
return new Vector3(x, y, z);
}
public static intoRaw(v: Vector): RawVector {
return new RawVector(v.x, v.y, v.z);
}
public static zeros(): Vector {
return VectorOps.new(0.0, 0.0, 0.0);
}
// FIXME: type ram: RawVector?
public static fromRaw(raw: RawVector): Vector {
if (!raw) return null;
let res = VectorOps.new(raw.x, raw.y, raw.z);
raw.free();
return res;
}
public static copy(out: Vector, input: Vector) {
out.x = input.x;
out.y = input.y;
out.z = input.z;
}
}
export interface Rotation {
x: number;
y: number;
z: number;
w: number;
}
/**
* A quaternion.
*/
export class Quaternion implements Rotation {
x: number;
y: number;
z: number;
w: number;
constructor(x: number, y: number, z: number, w: number) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
}
export class RotationOps {
public static identity(): Rotation {
return new Quaternion(0.0, 0.0, 0.0, 1.0);
}
public static fromRaw(raw: RawRotation): Rotation {
if (!raw) return null;
let res = new Quaternion(raw.x, raw.y, raw.z, raw.w);
raw.free();
return res;
}
public static intoRaw(rot: Rotation): RawRotation {
return new RawRotation(rot.x, rot.y, rot.z, rot.w);
}
public static copy(out: Rotation, input: Rotation) {
out.x = input.x;
out.y = input.y;
out.z = input.z;
out.w = input.w;
}
}
/**
* A 3D symmetric-positive-definite matrix.
*/
export class SdpMatrix3 {
/**
* Row major list of the upper-triangular part of the symmetric matrix.
*/
elements: Float32Array;
/**
* Matrix element at row 1, column 1.
*/
public get m11(): number {
return this.elements[0];
}
/**
* Matrix element at row 1, column 2.
*/
public get m12(): number {
return this.elements[1];
}
/**
* Matrix element at row 2, column 1.
*/
public get m21(): number {
return this.m12;
}
/**
* Matrix element at row 1, column 3.
*/
public get m13(): number {
return this.elements[2];
}
/**
* Matrix element at row 3, column 1.
*/
public get m31(): number {
return this.m13;
}
/**
* Matrix element at row 2, column 2.
*/
public get m22(): number {
return this.elements[3];
}
/**
* Matrix element at row 2, column 3.
*/
public get m23(): number {
return this.elements[4];
}
/**
* Matrix element at row 3, column 2.
*/
public get m32(): number {
return this.m23;
}
/**
* Matrix element at row 3, column 3.
*/
public get m33(): number {
return this.elements[5];
}
constructor(elements: Float32Array) {
this.elements = elements;
}
}
export class SdpMatrix3Ops {
public static fromRaw(raw: RawSdpMatrix3): SdpMatrix3 {
const sdpMatrix3 = new SdpMatrix3(raw.elements());
raw.free();
return sdpMatrix3;
}
}
// #endif