@pawells/math-extended 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +319 -0
- package/build/angles.d.ts +31 -0
- package/build/angles.d.ts.map +1 -0
- package/build/angles.js +85 -0
- package/build/angles.js.map +1 -0
- package/build/angles.spec.d.ts +2 -0
- package/build/angles.spec.d.ts.map +1 -0
- package/build/angles.spec.js +147 -0
- package/build/angles.spec.js.map +1 -0
- package/build/clamp.d.ts +17 -0
- package/build/clamp.d.ts.map +1 -0
- package/build/clamp.js +19 -0
- package/build/clamp.js.map +1 -0
- package/build/clamp.spec.d.ts +2 -0
- package/build/clamp.spec.d.ts.map +1 -0
- package/build/clamp.spec.js +19 -0
- package/build/clamp.spec.js.map +1 -0
- package/build/documentation-validation.spec.d.ts +11 -0
- package/build/documentation-validation.spec.d.ts.map +1 -0
- package/build/documentation-validation.spec.js +401 -0
- package/build/documentation-validation.spec.js.map +1 -0
- package/build/index.d.ts +8 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +8 -0
- package/build/index.js.map +1 -0
- package/build/interpolation.d.ts +175 -0
- package/build/interpolation.d.ts.map +1 -0
- package/build/interpolation.js +369 -0
- package/build/interpolation.js.map +1 -0
- package/build/interpolation.spec.d.ts +2 -0
- package/build/interpolation.spec.d.ts.map +1 -0
- package/build/interpolation.spec.js +480 -0
- package/build/interpolation.spec.js.map +1 -0
- package/build/matrices/arithmetic.d.ts +411 -0
- package/build/matrices/arithmetic.d.ts.map +1 -0
- package/build/matrices/arithmetic.js +954 -0
- package/build/matrices/arithmetic.js.map +1 -0
- package/build/matrices/arithmetic.spec.d.ts +2 -0
- package/build/matrices/arithmetic.spec.d.ts.map +1 -0
- package/build/matrices/arithmetic.spec.js +915 -0
- package/build/matrices/arithmetic.spec.js.map +1 -0
- package/build/matrices/asserts.d.ts +306 -0
- package/build/matrices/asserts.d.ts.map +1 -0
- package/build/matrices/asserts.js +396 -0
- package/build/matrices/asserts.js.map +1 -0
- package/build/matrices/asserts.spec.d.ts +2 -0
- package/build/matrices/asserts.spec.d.ts.map +1 -0
- package/build/matrices/asserts.spec.js +565 -0
- package/build/matrices/asserts.spec.js.map +1 -0
- package/build/matrices/core.d.ts +168 -0
- package/build/matrices/core.d.ts.map +1 -0
- package/build/matrices/core.js +457 -0
- package/build/matrices/core.js.map +1 -0
- package/build/matrices/core.spec.d.ts +2 -0
- package/build/matrices/core.spec.d.ts.map +1 -0
- package/build/matrices/core.spec.js +634 -0
- package/build/matrices/core.spec.js.map +1 -0
- package/build/matrices/decompositions.d.ts +326 -0
- package/build/matrices/decompositions.d.ts.map +1 -0
- package/build/matrices/decompositions.js +816 -0
- package/build/matrices/decompositions.js.map +1 -0
- package/build/matrices/decompositions.spec.d.ts +2 -0
- package/build/matrices/decompositions.spec.d.ts.map +1 -0
- package/build/matrices/decompositions.spec.js +195 -0
- package/build/matrices/decompositions.spec.js.map +1 -0
- package/build/matrices/index.d.ts +9 -0
- package/build/matrices/index.d.ts.map +1 -0
- package/build/matrices/index.js +9 -0
- package/build/matrices/index.js.map +1 -0
- package/build/matrices/linear-algebra.d.ts +64 -0
- package/build/matrices/linear-algebra.d.ts.map +1 -0
- package/build/matrices/linear-algebra.js +253 -0
- package/build/matrices/linear-algebra.js.map +1 -0
- package/build/matrices/linear-algebra.spec.d.ts +2 -0
- package/build/matrices/linear-algebra.spec.d.ts.map +1 -0
- package/build/matrices/linear-algebra.spec.js +355 -0
- package/build/matrices/linear-algebra.spec.js.map +1 -0
- package/build/matrices/normalization.d.ts +62 -0
- package/build/matrices/normalization.d.ts.map +1 -0
- package/build/matrices/normalization.js +167 -0
- package/build/matrices/normalization.js.map +1 -0
- package/build/matrices/normalization.spec.d.ts +2 -0
- package/build/matrices/normalization.spec.d.ts.map +1 -0
- package/build/matrices/normalization.spec.js +335 -0
- package/build/matrices/normalization.spec.js.map +1 -0
- package/build/matrices/transformations.d.ts +484 -0
- package/build/matrices/transformations.d.ts.map +1 -0
- package/build/matrices/transformations.js +592 -0
- package/build/matrices/transformations.js.map +1 -0
- package/build/matrices/transformations.spec.d.ts +2 -0
- package/build/matrices/transformations.spec.d.ts.map +1 -0
- package/build/matrices/transformations.spec.js +755 -0
- package/build/matrices/transformations.spec.js.map +1 -0
- package/build/matrices/types.d.ts +134 -0
- package/build/matrices/types.d.ts.map +1 -0
- package/build/matrices/types.js +6 -0
- package/build/matrices/types.js.map +1 -0
- package/build/quaternions/asserts.d.ts +77 -0
- package/build/quaternions/asserts.d.ts.map +1 -0
- package/build/quaternions/asserts.js +175 -0
- package/build/quaternions/asserts.js.map +1 -0
- package/build/quaternions/asserts.spec.d.ts +2 -0
- package/build/quaternions/asserts.spec.d.ts.map +1 -0
- package/build/quaternions/asserts.spec.js +320 -0
- package/build/quaternions/asserts.spec.js.map +1 -0
- package/build/quaternions/conversions.d.ts +73 -0
- package/build/quaternions/conversions.d.ts.map +1 -0
- package/build/quaternions/conversions.js +179 -0
- package/build/quaternions/conversions.js.map +1 -0
- package/build/quaternions/conversions.spec.d.ts +2 -0
- package/build/quaternions/conversions.spec.d.ts.map +1 -0
- package/build/quaternions/conversions.spec.js +344 -0
- package/build/quaternions/conversions.spec.js.map +1 -0
- package/build/quaternions/core.d.ts +203 -0
- package/build/quaternions/core.d.ts.map +1 -0
- package/build/quaternions/core.js +374 -0
- package/build/quaternions/core.js.map +1 -0
- package/build/quaternions/core.spec.d.ts +2 -0
- package/build/quaternions/core.spec.d.ts.map +1 -0
- package/build/quaternions/core.spec.js +294 -0
- package/build/quaternions/core.spec.js.map +1 -0
- package/build/quaternions/index.d.ts +7 -0
- package/build/quaternions/index.d.ts.map +1 -0
- package/build/quaternions/index.js +7 -0
- package/build/quaternions/index.js.map +1 -0
- package/build/quaternions/interpolation.d.ts +54 -0
- package/build/quaternions/interpolation.d.ts.map +1 -0
- package/build/quaternions/interpolation.js +201 -0
- package/build/quaternions/interpolation.js.map +1 -0
- package/build/quaternions/interpolation.spec.d.ts +2 -0
- package/build/quaternions/interpolation.spec.d.ts.map +1 -0
- package/build/quaternions/interpolation.spec.js +64 -0
- package/build/quaternions/interpolation.spec.js.map +1 -0
- package/build/quaternions/predefined.d.ts +36 -0
- package/build/quaternions/predefined.d.ts.map +1 -0
- package/build/quaternions/predefined.js +42 -0
- package/build/quaternions/predefined.js.map +1 -0
- package/build/quaternions/predefined.spec.d.ts +2 -0
- package/build/quaternions/predefined.spec.d.ts.map +1 -0
- package/build/quaternions/predefined.spec.js +35 -0
- package/build/quaternions/predefined.spec.js.map +1 -0
- package/build/quaternions/types.d.ts +55 -0
- package/build/quaternions/types.d.ts.map +1 -0
- package/build/quaternions/types.js +7 -0
- package/build/quaternions/types.js.map +1 -0
- package/build/random.d.ts +66 -0
- package/build/random.d.ts.map +1 -0
- package/build/random.js +115 -0
- package/build/random.js.map +1 -0
- package/build/random.spec.d.ts +2 -0
- package/build/random.spec.d.ts.map +1 -0
- package/build/random.spec.js +267 -0
- package/build/random.spec.js.map +1 -0
- package/build/vectors/asserts.d.ts +182 -0
- package/build/vectors/asserts.d.ts.map +1 -0
- package/build/vectors/asserts.js +285 -0
- package/build/vectors/asserts.js.map +1 -0
- package/build/vectors/asserts.spec.d.ts +2 -0
- package/build/vectors/asserts.spec.d.ts.map +1 -0
- package/build/vectors/asserts.spec.js +260 -0
- package/build/vectors/asserts.spec.js.map +1 -0
- package/build/vectors/core.d.ts +507 -0
- package/build/vectors/core.d.ts.map +1 -0
- package/build/vectors/core.js +825 -0
- package/build/vectors/core.js.map +1 -0
- package/build/vectors/core.spec.d.ts +2 -0
- package/build/vectors/core.spec.d.ts.map +1 -0
- package/build/vectors/core.spec.js +343 -0
- package/build/vectors/core.spec.js.map +1 -0
- package/build/vectors/index.d.ts +6 -0
- package/build/vectors/index.d.ts.map +1 -0
- package/build/vectors/index.js +6 -0
- package/build/vectors/index.js.map +1 -0
- package/build/vectors/interpolation.d.ts +404 -0
- package/build/vectors/interpolation.d.ts.map +1 -0
- package/build/vectors/interpolation.js +585 -0
- package/build/vectors/interpolation.js.map +1 -0
- package/build/vectors/interpolation.spec.d.ts +2 -0
- package/build/vectors/interpolation.spec.d.ts.map +1 -0
- package/build/vectors/interpolation.spec.js +378 -0
- package/build/vectors/interpolation.spec.js.map +1 -0
- package/build/vectors/predefined.d.ts +191 -0
- package/build/vectors/predefined.d.ts.map +1 -0
- package/build/vectors/predefined.js +191 -0
- package/build/vectors/predefined.js.map +1 -0
- package/build/vectors/predefined.spec.d.ts +2 -0
- package/build/vectors/predefined.spec.d.ts.map +1 -0
- package/build/vectors/predefined.spec.js +333 -0
- package/build/vectors/predefined.spec.js.map +1 -0
- package/build/vectors/types.d.ts +62 -0
- package/build/vectors/types.d.ts.map +1 -0
- package/build/vectors/types.js +6 -0
- package/build/vectors/types.js.map +1 -0
- package/package.json +75 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { QuaternionNLERP, QuaternionSQUAD, QuaternionCreatePath } from './interpolation.js';
|
|
2
|
+
import { QuaternionIdentity, QuaternionFromAxisAngle, QuaternionEquals } from './core.js';
|
|
3
|
+
const TOLERANCE = 1e-6;
|
|
4
|
+
describe('Quaternion Interpolation', () => {
|
|
5
|
+
test('QuaternionNLERP: returns start and end for t=0 and t=1', () => {
|
|
6
|
+
const a = QuaternionIdentity();
|
|
7
|
+
const b = QuaternionFromAxisAngle([0, 0, 1], Math.PI / 2);
|
|
8
|
+
expect(QuaternionEquals(QuaternionNLERP(a, b, 0), a, TOLERANCE, true)).toBe(true);
|
|
9
|
+
expect(QuaternionEquals(QuaternionNLERP(a, b, 1), b, TOLERANCE, true)).toBe(true);
|
|
10
|
+
});
|
|
11
|
+
test('QuaternionNLERP: interpolates halfway', () => {
|
|
12
|
+
const a = QuaternionIdentity();
|
|
13
|
+
const b = QuaternionFromAxisAngle([0, 0, 1], Math.PI / 2);
|
|
14
|
+
const result = QuaternionNLERP(a, b, 0.5);
|
|
15
|
+
// Should be normalized and between a and b
|
|
16
|
+
expect(Math.abs(result[3])).toBeLessThan(1);
|
|
17
|
+
expect(Math.abs(result[3])).toBeGreaterThan(0.7);
|
|
18
|
+
});
|
|
19
|
+
test('QuaternionNLERP: handles double-cover (q and -q)', () => {
|
|
20
|
+
const a = QuaternionIdentity();
|
|
21
|
+
const b = [0, 0, 0, -1];
|
|
22
|
+
const result = QuaternionNLERP(a, b, 0.5);
|
|
23
|
+
// Should interpolate along the shortest path (equivalent to a)
|
|
24
|
+
expect(QuaternionEquals(result, a, TOLERANCE, true)).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
test('QuaternionSQUAD: smooth interpolation between four quaternions', () => {
|
|
27
|
+
const q0 = QuaternionFromAxisAngle([1, 0, 0], 0);
|
|
28
|
+
const q1 = QuaternionFromAxisAngle([1, 0, 0], Math.PI / 4);
|
|
29
|
+
const q2 = QuaternionFromAxisAngle([1, 0, 0], Math.PI / 2);
|
|
30
|
+
const q3 = QuaternionFromAxisAngle([1, 0, 0], (3 * Math.PI) / 4);
|
|
31
|
+
const result = QuaternionSQUAD(q0, q1, q2, q3, 0.5);
|
|
32
|
+
// Should be between q1 and q2
|
|
33
|
+
expect(result[3]).toBeLessThan(q1[3]);
|
|
34
|
+
expect(result[3]).toBeGreaterThan(q2[3]);
|
|
35
|
+
});
|
|
36
|
+
test('QuaternionCreatePath: slerp path returns endpoints for t=0 and t=1', () => {
|
|
37
|
+
const q1 = QuaternionIdentity();
|
|
38
|
+
const q2 = QuaternionFromAxisAngle([0, 1, 0], Math.PI / 2);
|
|
39
|
+
const path = [q1, q2];
|
|
40
|
+
const interp = QuaternionCreatePath(path, 'slerp');
|
|
41
|
+
expect(QuaternionEquals(interp(0), q1, TOLERANCE, true)).toBe(true);
|
|
42
|
+
expect(QuaternionEquals(interp(1), q2, TOLERANCE, true)).toBe(true);
|
|
43
|
+
});
|
|
44
|
+
test('QuaternionCreatePath: nlerp and squad produce valid quaternions', () => {
|
|
45
|
+
const q1 = QuaternionIdentity();
|
|
46
|
+
const q2 = QuaternionFromAxisAngle([0, 1, 0], Math.PI / 2);
|
|
47
|
+
const q3 = QuaternionFromAxisAngle([0, 1, 0], Math.PI);
|
|
48
|
+
const path = [q1, q2, q3];
|
|
49
|
+
const nlerpInterp = QuaternionCreatePath(path, 'nlerp');
|
|
50
|
+
const squadInterp = QuaternionCreatePath(path, 'squad');
|
|
51
|
+
const nlerpResult = nlerpInterp(0.5);
|
|
52
|
+
const squadResult = squadInterp(0.5);
|
|
53
|
+
// Should be normalized
|
|
54
|
+
const nlerpMag = Math.sqrt(nlerpResult.reduce((s, v) => s + (v * v), 0));
|
|
55
|
+
const squadMag = Math.sqrt(squadResult.reduce((s, v) => s + (v * v), 0));
|
|
56
|
+
expect(Math.abs(nlerpMag - 1)).toBeLessThan(TOLERANCE);
|
|
57
|
+
expect(Math.abs(squadMag - 1)).toBeLessThan(TOLERANCE);
|
|
58
|
+
});
|
|
59
|
+
test('QuaternionCreatePath: throws for <2 quaternions', () => {
|
|
60
|
+
expect(() => QuaternionCreatePath([], 'slerp')).toThrow();
|
|
61
|
+
expect(() => QuaternionCreatePath([[0, 0, 0, 1]], 'slerp')).toThrow();
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
//# sourceMappingURL=interpolation.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interpolation.spec.js","sourceRoot":"","sources":["../../src/quaternions/interpolation.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAC5F,OAAO,EAAE,kBAAkB,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAC;AAE1F,MAAM,SAAS,GAAG,IAAI,CAAC;AACvB,QAAQ,CAAC,0BAA0B,EAAE,GAAG,EAAE;IACzC,IAAI,CAAC,wDAAwD,EAAE,GAAG,EAAE;QACnE,MAAM,CAAC,GAAG,kBAAkB,EAAE,CAAC;Q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AC;QACrC,uBAAuB;QACvB,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACzE,MAAM,QAAQ,GAAG,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;QACzE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;QACvD,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IACxD,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,iDAAiD,EAAE,GAAG,EAAE;QAC5D,MAAM,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;QAC1D,MAAM,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;IACvE,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Predefined quaternions for common rotations and orientations.
|
|
3
|
+
* Provides convenient constants for frequently used quaternion values.
|
|
4
|
+
*/
|
|
5
|
+
import { TQuaternion } from './types.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a quaternion representing rotation around the X-axis.
|
|
8
|
+
*
|
|
9
|
+
* @param angle - Rotation angle in radians
|
|
10
|
+
* @returns Quaternion representing rotation around X-axis
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const q = QuaternionRotationX(Math.PI / 2); // 90° rotation around X-axis
|
|
14
|
+
*/
|
|
15
|
+
export declare function QuaternionRotationX(angle: number): TQuaternion;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a quaternion representing rotation around the Y-axis.
|
|
18
|
+
*
|
|
19
|
+
* @param angle - Rotation angle in radians
|
|
20
|
+
* @returns Quaternion representing rotation around Y-axis
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const q = QuaternionRotationY(Math.PI / 4); // 45° rotation around Y-axis
|
|
24
|
+
*/
|
|
25
|
+
export declare function QuaternionRotationY(angle: number): TQuaternion;
|
|
26
|
+
/**
|
|
27
|
+
* Creates a quaternion representing rotation around the Z-axis.
|
|
28
|
+
*
|
|
29
|
+
* @param angle - Rotation angle in radians
|
|
30
|
+
* @returns Quaternion representing rotation around Z-axis
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const q = QuaternionRotationZ(Math.PI); // 180° rotation around Z-axis
|
|
34
|
+
*/
|
|
35
|
+
export declare function QuaternionRotationZ(angle: number): TQuaternion;
|
|
36
|
+
//# sourceMappingURL=predefined.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predefined.d.ts","sourceRoot":"","sources":["../../src/quaternions/predefined.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAGzC;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE9D;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE9D;AAED;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,CAE9D"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Predefined quaternions for common rotations and orientations.
|
|
3
|
+
* Provides convenient constants for frequently used quaternion values.
|
|
4
|
+
*/
|
|
5
|
+
import { QuaternionFromAxisAngle } from './core.js';
|
|
6
|
+
/**
|
|
7
|
+
* Creates a quaternion representing rotation around the X-axis.
|
|
8
|
+
*
|
|
9
|
+
* @param angle - Rotation angle in radians
|
|
10
|
+
* @returns Quaternion representing rotation around X-axis
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* const q = QuaternionRotationX(Math.PI / 2); // 90° rotation around X-axis
|
|
14
|
+
*/
|
|
15
|
+
export function QuaternionRotationX(angle) {
|
|
16
|
+
return QuaternionFromAxisAngle([1, 0, 0], angle);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates a quaternion representing rotation around the Y-axis.
|
|
20
|
+
*
|
|
21
|
+
* @param angle - Rotation angle in radians
|
|
22
|
+
* @returns Quaternion representing rotation around Y-axis
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* const q = QuaternionRotationY(Math.PI / 4); // 45° rotation around Y-axis
|
|
26
|
+
*/
|
|
27
|
+
export function QuaternionRotationY(angle) {
|
|
28
|
+
return QuaternionFromAxisAngle([0, 1, 0], angle);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Creates a quaternion representing rotation around the Z-axis.
|
|
32
|
+
*
|
|
33
|
+
* @param angle - Rotation angle in radians
|
|
34
|
+
* @returns Quaternion representing rotation around Z-axis
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* const q = QuaternionRotationZ(Math.PI); // 180° rotation around Z-axis
|
|
38
|
+
*/
|
|
39
|
+
export function QuaternionRotationZ(angle) {
|
|
40
|
+
return QuaternionFromAxisAngle([0, 0, 1], angle);
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=predefined.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predefined.js","sourceRoot":"","sources":["../../src/quaternions/predefined.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAC;AAEpD;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAChD,OAAO,uBAAuB,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAClD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAChD,OAAO,uBAAuB,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAClD,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa;IAChD,OAAO,uBAAuB,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;AAClD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predefined.spec.d.ts","sourceRoot":"","sources":["../../src/quaternions/predefined.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { QuaternionRotationX, QuaternionRotationY, QuaternionRotationZ, } from './predefined.js';
|
|
2
|
+
import { QuaternionIdentity, QuaternionEquals, QuaternionFromAxisAngle } from './core.js';
|
|
3
|
+
const TOLERANCE = 1e-6;
|
|
4
|
+
describe('Predefined Quaternions', () => {
|
|
5
|
+
test('QuaternionIdentity returns [0,0,0,1]', () => {
|
|
6
|
+
expect(QuaternionIdentity()).toEqual([0, 0, 0, 1]);
|
|
7
|
+
expect(QuaternionEquals(QuaternionIdentity(), QuaternionIdentity(), TOLERANCE, true)).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
test('QuaternionRotationX returns correct quaternion for 90°', () => {
|
|
10
|
+
const angle = Math.PI / 2;
|
|
11
|
+
const expected = QuaternionFromAxisAngle([1, 0, 0], angle);
|
|
12
|
+
expect(QuaternionEquals(QuaternionRotationX(angle), expected, TOLERANCE, true)).toBe(true);
|
|
13
|
+
});
|
|
14
|
+
test('QuaternionRotationY returns correct quaternion for 90°', () => {
|
|
15
|
+
const angle = Math.PI / 2;
|
|
16
|
+
const expected = QuaternionFromAxisAngle([0, 1, 0], angle);
|
|
17
|
+
expect(QuaternionEquals(QuaternionRotationY(angle), expected, TOLERANCE, true)).toBe(true);
|
|
18
|
+
});
|
|
19
|
+
test('QuaternionRotationZ returns correct quaternion for 90°', () => {
|
|
20
|
+
const angle = Math.PI / 2;
|
|
21
|
+
const expected = QuaternionFromAxisAngle([0, 0, 1], angle);
|
|
22
|
+
expect(QuaternionEquals(QuaternionRotationZ(angle), expected, TOLERANCE, true)).toBe(true);
|
|
23
|
+
});
|
|
24
|
+
test('QuaternionRotationX/Y/Z are normalized', () => {
|
|
25
|
+
const angle = Math.PI / 3;
|
|
26
|
+
const qx = QuaternionRotationX(angle);
|
|
27
|
+
const qy = QuaternionRotationY(angle);
|
|
28
|
+
const qz = QuaternionRotationZ(angle);
|
|
29
|
+
const mag = (q) => Math.sqrt(q.reduce((s, v) => s + (v * v), 0));
|
|
30
|
+
expect(Math.abs(mag(qx) - 1)).toBeLessThan(TOLERANCE);
|
|
31
|
+
expect(Math.abs(mag(qy) - 1)).toBeLessThan(TOLERANCE);
|
|
32
|
+
expect(Math.abs(mag(qz) - 1)).toBeLessThan(TOLERANCE);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
//# sourceMappingURL=predefined.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"predefined.spec.js","sourceRoot":"","sources":["../../src/quaternions/predefined.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,mBAAmB,EACnB,mBAAmB,EACnB,mBAAmB,GACnB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAE,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YAAY,CAAC,SAAS,CAAC,CAAC;IACvD,CAAC,CAAC,CAAC;AACJ,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quaternion type definitions for rotation and orientation calculations.
|
|
3
|
+
* Quaternions provide a mathematically robust way to represent rotations in 3D space,
|
|
4
|
+
* avoiding gimbal lock and providing smooth interpolation capabilities.
|
|
5
|
+
*/
|
|
6
|
+
import { TVector3, TVector4 } from '../vectors/types.js';
|
|
7
|
+
import { IMatrix3 } from '../matrices/types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Quaternion type representing a rotation in 3D space as [x, y, z, w].
|
|
10
|
+
* The first three components (x, y, z) represent the vector part (rotation axis scaled by sin(θ/2)),
|
|
11
|
+
* and the fourth component (w) represents the scalar part (cos(θ/2)).
|
|
12
|
+
*
|
|
13
|
+
* Quaternions extend IVector4 to leverage vector operations while maintaining semantic meaning.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const identity: IQuaternion = [0, 0, 0, 1]; // No rotation
|
|
17
|
+
* const rotationX: IQuaternion = [1, 0, 0, 0]; // 180° rotation around X-axis
|
|
18
|
+
* const rotation90Y: IQuaternion = [0, 0.707, 0, 0.707]; // 90° rotation around Y-axis
|
|
19
|
+
*/
|
|
20
|
+
export type TQuaternion = TVector4;
|
|
21
|
+
/**
|
|
22
|
+
* Euler angles representation as [x, y, z] rotations in radians.
|
|
23
|
+
* Order of rotation is typically Z-Y-X (yaw-pitch-roll) unless specified otherwise.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* const euler: IEulerAngles = [0, Math.PI/4, 0]; // 45° pitch rotation
|
|
27
|
+
*/
|
|
28
|
+
export type TEulerAngles = TVector3;
|
|
29
|
+
/**
|
|
30
|
+
* Axis-angle representation as [axis_x, axis_y, axis_z, angle].
|
|
31
|
+
* The first three components define a normalized rotation axis,
|
|
32
|
+
* and the fourth component is the rotation angle in radians.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* const axisAngle: IAxisAngle = [0, 1, 0, Math.PI/2]; // 90° rotation around Y-axis
|
|
36
|
+
*/
|
|
37
|
+
export type TAxisAngle = TVector4;
|
|
38
|
+
/**
|
|
39
|
+
* Rotation matrix representation as a 3x3 matrix.
|
|
40
|
+
* Uses the standard IMatrix3 type for better integration with matrix operations.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const identity: IRotationMatrix = [
|
|
44
|
+
* [1, 0, 0], // First row
|
|
45
|
+
* [0, 1, 0], // Second row
|
|
46
|
+
* [0, 0, 1] // Third row
|
|
47
|
+
* ];
|
|
48
|
+
*/
|
|
49
|
+
export type TRotationMatrix = IMatrix3;
|
|
50
|
+
/**
|
|
51
|
+
* Union type representing any supported rotation representation.
|
|
52
|
+
* Useful for functions that can accept multiple rotation formats.
|
|
53
|
+
*/
|
|
54
|
+
export type TRotation = TQuaternion | TEulerAngles | TAxisAngle | TRotationMatrix;
|
|
55
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/quaternions/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AACzD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAEhD;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,CAAC;AAEnC;;;;;;GAMG;AACH,MAAM,MAAM,YAAY,GAAG,QAAQ,CAAC;AAEpC;;;;;;;GAOG;AACH,MAAM,MAAM,UAAU,GAAG,QAAQ,CAAC;AAElC;;;;;;;;;;GAUG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,CAAC;AAEvC;;;GAGG;AACH,MAAM,MAAM,SAAS,GAAG,WAAW,GAAG,YAAY,GAAG,UAAU,GAAG,eAAe,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Quaternion type definitions for rotation and orientation calculations.
|
|
3
|
+
* Quaternions provide a mathematically robust way to represent rotations in 3D space,
|
|
4
|
+
* avoiding gimbal lock and providing smooth interpolation capabilities.
|
|
5
|
+
*/
|
|
6
|
+
export {};
|
|
7
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/quaternions/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a random integer within the specified range (inclusive).
|
|
3
|
+
* @param min - Minimum value (inclusive)
|
|
4
|
+
* @param max - Maximum value (inclusive)
|
|
5
|
+
* @returns Random integer between min and max, or NaN if min > max
|
|
6
|
+
* @example RandomInt(1, 6) // Returns 1, 2, 3, 4, 5, or 6 (dice roll)
|
|
7
|
+
* @example RandomInt(-5, 5) // Returns any integer from -5 to 5
|
|
8
|
+
*/
|
|
9
|
+
export declare function RandomInt(min: number, max: number): number;
|
|
10
|
+
/**
|
|
11
|
+
* Generates a random floating-point number within the specified range.
|
|
12
|
+
* @param min - Minimum value (inclusive)
|
|
13
|
+
* @param max - Maximum value (exclusive)
|
|
14
|
+
* @returns Random float between min (inclusive) and max (exclusive), or NaN if min >= max
|
|
15
|
+
* @example RandomFloat(0, 1) // Returns 0.0 to 0.999...
|
|
16
|
+
* @example RandomFloat(-1.5, 1.5) // Returns any float from -1.5 to 1.499...
|
|
17
|
+
*/
|
|
18
|
+
export declare function RandomFloat(min: number, max: number): number;
|
|
19
|
+
/**
|
|
20
|
+
* Randomly selects one element from an array.
|
|
21
|
+
* @template T - The type of elements in the array
|
|
22
|
+
* @param array - Array to choose from
|
|
23
|
+
* @returns Random element from the array, or undefined if array is empty
|
|
24
|
+
* @example RandomChoice([1, 2, 3, 4, 5]) // Returns one of the numbers
|
|
25
|
+
* @example RandomChoice(['red', 'green', 'blue']) // Returns one of the colors
|
|
26
|
+
*/
|
|
27
|
+
export declare function RandomChoice<T>(array: T[]): T | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* Randomly selects multiple elements from an array without replacement.
|
|
30
|
+
* @template T - The type of elements in the array
|
|
31
|
+
* @param array - Array to choose from
|
|
32
|
+
* @param count - Number of elements to select
|
|
33
|
+
* @returns Array of randomly selected elements, or empty array if invalid inputs
|
|
34
|
+
* @example RandomSample([1, 2, 3, 4, 5], 3) // Returns 3 unique numbers
|
|
35
|
+
* @example RandomSample(['a', 'b', 'c'], 2) // Returns 2 unique letters
|
|
36
|
+
*/
|
|
37
|
+
export declare function RandomSample<T>(array: T[], count: number): T[];
|
|
38
|
+
/**
|
|
39
|
+
* Shuffles an array using the Fisher-Yates algorithm.
|
|
40
|
+
* @template T - The type of elements in the array
|
|
41
|
+
* @param array - Array to shuffle
|
|
42
|
+
* @param clone - If true, returns a shuffled copy; if false/undefined, modifies original array
|
|
43
|
+
* @returns Shuffled array (original reference if clone=false, new array if clone=true)
|
|
44
|
+
* @example RandomShuffle([1, 2, 3, 4, 5]) // Modifies and returns original array
|
|
45
|
+
* @example RandomShuffle([1, 2, 3, 4, 5], true) // Returns new shuffled array, original unchanged
|
|
46
|
+
*/
|
|
47
|
+
export declare function RandomShuffle<T>(array: T[], clone?: boolean): T[];
|
|
48
|
+
/**
|
|
49
|
+
* Generates a random boolean value.
|
|
50
|
+
* @param probability - Probability of returning true (0.0 to 1.0, default: 0.5)
|
|
51
|
+
* @returns Random boolean based on probability
|
|
52
|
+
* @example RandomBool() // 50% chance of true
|
|
53
|
+
* @example RandomBool(0.8) // 80% chance of true
|
|
54
|
+
*/
|
|
55
|
+
export declare function RandomBool(probability?: number): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Generates a random number following a normal (Gaussian) distribution.
|
|
58
|
+
* Uses the Box-Muller transform for generating normally distributed values.
|
|
59
|
+
* @param mean - Mean of the distribution (default: 0)
|
|
60
|
+
* @param standardDeviation - Standard deviation of the distribution (default: 1)
|
|
61
|
+
* @returns Random number from normal distribution
|
|
62
|
+
* @example RandomNormal() // Standard normal distribution (mean=0, std=1)
|
|
63
|
+
* @example RandomNormal(100, 15) // IQ-like distribution (mean=100, std=15)
|
|
64
|
+
*/
|
|
65
|
+
export declare function RandomNormal(mean?: number, standardDeviation?: number): number;
|
|
66
|
+
//# sourceMappingURL=random.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.d.ts","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAEA;;;;;;;GAOG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAI1D;AAED;;;;;;;GAOG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAI5D;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS,CAKzD;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC,EAAE,CAmB9D;AAED;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,CAAC,EAAE,CAWjE;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,WAAW,GAAE,MAAY,GAAG,OAAO,CAI7D;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,MAAU,EAAE,iBAAiB,GAAE,MAAU,GAAG,MAAM,CAOpF"}
|
package/build/random.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
const BOX_MULLER_COEFFICIENT = -2;
|
|
2
|
+
/**
|
|
3
|
+
* Generates a random integer within the specified range (inclusive).
|
|
4
|
+
* @param min - Minimum value (inclusive)
|
|
5
|
+
* @param max - Maximum value (inclusive)
|
|
6
|
+
* @returns Random integer between min and max, or NaN if min > max
|
|
7
|
+
* @example RandomInt(1, 6) // Returns 1, 2, 3, 4, 5, or 6 (dice roll)
|
|
8
|
+
* @example RandomInt(-5, 5) // Returns any integer from -5 to 5
|
|
9
|
+
*/
|
|
10
|
+
export function RandomInt(min, max) {
|
|
11
|
+
if (min > max)
|
|
12
|
+
return Number.NaN;
|
|
13
|
+
return Math.floor((Math.random() * (max - min + 1)) + min);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Generates a random floating-point number within the specified range.
|
|
17
|
+
* @param min - Minimum value (inclusive)
|
|
18
|
+
* @param max - Maximum value (exclusive)
|
|
19
|
+
* @returns Random float between min (inclusive) and max (exclusive), or NaN if min >= max
|
|
20
|
+
* @example RandomFloat(0, 1) // Returns 0.0 to 0.999...
|
|
21
|
+
* @example RandomFloat(-1.5, 1.5) // Returns any float from -1.5 to 1.499...
|
|
22
|
+
*/
|
|
23
|
+
export function RandomFloat(min, max) {
|
|
24
|
+
if (min >= max)
|
|
25
|
+
return Number.NaN;
|
|
26
|
+
return (Math.random() * (max - min)) + min;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Randomly selects one element from an array.
|
|
30
|
+
* @template T - The type of elements in the array
|
|
31
|
+
* @param array - Array to choose from
|
|
32
|
+
* @returns Random element from the array, or undefined if array is empty
|
|
33
|
+
* @example RandomChoice([1, 2, 3, 4, 5]) // Returns one of the numbers
|
|
34
|
+
* @example RandomChoice(['red', 'green', 'blue']) // Returns one of the colors
|
|
35
|
+
*/
|
|
36
|
+
export function RandomChoice(array) {
|
|
37
|
+
if (array.length === 0)
|
|
38
|
+
return undefined;
|
|
39
|
+
const index = RandomInt(0, array.length - 1);
|
|
40
|
+
return array[index];
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Randomly selects multiple elements from an array without replacement.
|
|
44
|
+
* @template T - The type of elements in the array
|
|
45
|
+
* @param array - Array to choose from
|
|
46
|
+
* @param count - Number of elements to select
|
|
47
|
+
* @returns Array of randomly selected elements, or empty array if invalid inputs
|
|
48
|
+
* @example RandomSample([1, 2, 3, 4, 5], 3) // Returns 3 unique numbers
|
|
49
|
+
* @example RandomSample(['a', 'b', 'c'], 2) // Returns 2 unique letters
|
|
50
|
+
*/
|
|
51
|
+
export function RandomSample(array, count) {
|
|
52
|
+
if (array.length === 0 || count <= 0 || count > array.length)
|
|
53
|
+
return [];
|
|
54
|
+
const result = [];
|
|
55
|
+
const indices = new Set();
|
|
56
|
+
while (result.length < count) {
|
|
57
|
+
const index = RandomInt(0, array.length - 1);
|
|
58
|
+
if (!indices.has(index)) {
|
|
59
|
+
indices.add(index);
|
|
60
|
+
const element = array[index];
|
|
61
|
+
if (element !== undefined) {
|
|
62
|
+
result.push(element);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Shuffles an array using the Fisher-Yates algorithm.
|
|
70
|
+
* @template T - The type of elements in the array
|
|
71
|
+
* @param array - Array to shuffle
|
|
72
|
+
* @param clone - If true, returns a shuffled copy; if false/undefined, modifies original array
|
|
73
|
+
* @returns Shuffled array (original reference if clone=false, new array if clone=true)
|
|
74
|
+
* @example RandomShuffle([1, 2, 3, 4, 5]) // Modifies and returns original array
|
|
75
|
+
* @example RandomShuffle([1, 2, 3, 4, 5], true) // Returns new shuffled array, original unchanged
|
|
76
|
+
*/
|
|
77
|
+
export function RandomShuffle(array, clone) {
|
|
78
|
+
const targetArray = clone ? [...array] : array;
|
|
79
|
+
for (let i = targetArray.length - 1; i > 0; i--) {
|
|
80
|
+
const j = RandomInt(0, i);
|
|
81
|
+
const temp = targetArray[i];
|
|
82
|
+
targetArray[i] = targetArray[j];
|
|
83
|
+
targetArray[j] = temp;
|
|
84
|
+
}
|
|
85
|
+
return targetArray;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Generates a random boolean value.
|
|
89
|
+
* @param probability - Probability of returning true (0.0 to 1.0, default: 0.5)
|
|
90
|
+
* @returns Random boolean based on probability
|
|
91
|
+
* @example RandomBool() // 50% chance of true
|
|
92
|
+
* @example RandomBool(0.8) // 80% chance of true
|
|
93
|
+
*/
|
|
94
|
+
export function RandomBool(probability = 0.5) {
|
|
95
|
+
if (probability < 0 || probability > 1)
|
|
96
|
+
return false;
|
|
97
|
+
return Math.random() < probability;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Generates a random number following a normal (Gaussian) distribution.
|
|
101
|
+
* Uses the Box-Muller transform for generating normally distributed values.
|
|
102
|
+
* @param mean - Mean of the distribution (default: 0)
|
|
103
|
+
* @param standardDeviation - Standard deviation of the distribution (default: 1)
|
|
104
|
+
* @returns Random number from normal distribution
|
|
105
|
+
* @example RandomNormal() // Standard normal distribution (mean=0, std=1)
|
|
106
|
+
* @example RandomNormal(100, 15) // IQ-like distribution (mean=100, std=15)
|
|
107
|
+
*/
|
|
108
|
+
export function RandomNormal(mean = 0, standardDeviation = 1) {
|
|
109
|
+
// Box-Muller transform
|
|
110
|
+
const u1 = Math.random();
|
|
111
|
+
const u2 = Math.random();
|
|
112
|
+
const z0 = Math.sqrt(BOX_MULLER_COEFFICIENT * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
|
113
|
+
return (z0 * standardDeviation) + mean;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=random.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"random.js","sourceRoot":"","sources":["../src/random.ts"],"names":[],"mappings":"AAAA,MAAM,sBAAsB,GAAG,CAAC,CAAC,CAAC;AAElC;;;;;;;GAOG;AACH,MAAM,UAAU,SAAS,CAAC,GAAW,EAAE,GAAW;IACjD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