Skip to content

Commit ac234ea

Browse files
knollengewaechsfm3MichaelBuessemeyerphilippotto
authored
Cleanup mjs module (#9059)
### URL of deployed dev instance (used for testing): - https://cleanupmjs.webknossos.xyz/ ### Steps to test: - test that the following still works: - all view modes (flight, orthogonal, oblique) - affine transformations - drag and drop skeleton nodes - loading of ad-hoc meshes - area measurement - brushing ### TODOs: - [x] maybe have a look at other places and remove Float32Arrays there, too ### Issues: - fixes #8666 ------ (Please delete unneeded items, merge only when none are left open) - [x] Added changelog entry (create a `$PR_NUMBER.md` file in `unreleased_changes` or use `./tools/create-changelog-entry.py`) - [ ] Added migration guide entry if applicable (edit the same file as for the changelog) - [ ] Updated [documentation](../blob/master/docs) if applicable - [ ] Adapted [wk-libs python client](https://github.com/scalableminds/webknossos-libs/tree/master/webknossos/webknossos/client) if relevant API parts change - [ ] Removed dev-only changes like prints and application.conf edits - [ ] Considered [common edge cases](../blob/master/.github/common_edge_cases.md) - [ ] Needs datastore update after deployment --------- Co-authored-by: Florian M <[email protected]> Co-authored-by: MichaelBuessemeyer <[email protected]> Co-authored-by: Philipp Otto <[email protected]>
1 parent 1b4f240 commit ac234ea

File tree

9 files changed

+68
-99
lines changed

9 files changed

+68
-99
lines changed

frontend/javascripts/libs/mjs.ts

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,23 @@
22
// https://github.com/imbcmdth/mjs/blob/master/index.js
33
// for all functions in M4x4, V2 and V3.
44
import _ from "lodash";
5-
import type { Vector3 as ThreeVector3 } from "three";
65
import type { Vector2, Vector3, Vector4 } from "viewer/constants";
76
import { chunk3 } from "viewer/model/helpers/chunk";
87

9-
import mjs from "mjs";
10-
11-
const { M4x4: BareM4x4, V2: BareV2, V3: BareV3 } = mjs(Float32Array);
12-
13-
type Vector3Like = Vector3 | Float32Array;
14-
type Vector2Like = Vector2 | Float32Array;
15-
16-
export type Vector16 = [
17-
number,
18-
number,
19-
number,
20-
number,
21-
number,
22-
number,
23-
number,
24-
number,
25-
number,
26-
number,
27-
number,
28-
number,
29-
number,
30-
number,
31-
number,
32-
number,
33-
];
34-
export type Matrix4x4 = Vector16 | Float32Array;
8+
import mjs, { type Vector16, type Matrix4x4 } from "mjs";
9+
10+
const { M4x4: BareM4x4, V2: BareV2, V3: BareV3 } = mjs(Array);
3511

3612
const M4x4 = {
3713
...BareM4x4,
3814
// Applies an affine transformation matrix on an array of points.
3915
transformPointsAffine(
4016
m: Matrix4x4,
41-
points: number[] | Float32Array,
42-
r?: Float32Array | Int32Array | null | undefined,
43-
): Float32Array | Int32Array {
17+
points: number[],
18+
r?: Array<number> | null | undefined,
19+
): Array<number> {
4420
if (r == null) {
45-
r = new Float32Array(points.length);
21+
r = new Array(points.length);
4622
}
4723

4824
const m00 = m[0];
@@ -85,10 +61,10 @@ const M4x4 = {
8561
transformPoints(
8662
m: Matrix4x4,
8763
points: number[],
88-
r?: Float32Array | null | undefined,
89-
): Float32Array {
64+
r?: Array<number> | null | undefined,
65+
): Array<number> {
9066
if (r == null) {
91-
r = new Float32Array(points.length);
67+
r = new Array(points.length);
9268
}
9369

9470
for (let i = 0; i < points.length; i += 3) {
@@ -112,7 +88,7 @@ const M4x4 = {
11288

11389
inverse(mat: Matrix4x4, dest?: Matrix4x4): Matrix4x4 {
11490
if (dest == null) {
115-
dest = new Float32Array(16);
91+
dest = new Array(16) as Vector16;
11692
}
11793

11894
const a00 = mat[0];
@@ -188,7 +164,7 @@ const M4x4 = {
188164
return m;
189165
}
190166

191-
if (r == null) r = new Float32Array(16);
167+
if (r == null) r = new Array(16) as Vector16;
192168

193169
r[0] = m[0];
194170
r[1] = m[4];
@@ -210,9 +186,9 @@ const M4x4 = {
210186
return r;
211187
},
212188

213-
extractTranslation(m: Matrix4x4, r?: Float32Array | null | undefined): Float32Array {
189+
extractTranslation(m: Matrix4x4, r?: Vector3 | null | undefined): Vector3 {
214190
if (r == null) {
215-
r = new Float32Array(3);
191+
r = new Array(3) as Vector3;
216192
}
217193

218194
r[0] = m[12];
@@ -236,8 +212,8 @@ const V2 = {
236212
max(vec1: Vector2, vec2: Vector2): Vector2 {
237213
return [Math.max(vec1[0], vec2[0]), Math.max(vec1[1], vec2[1])];
238214
},
239-
scale2(a: Vector2, k: Vector2, r?: Vector2Like): Vector2Like {
240-
if (r == null) r = new Float32Array(2);
215+
scale2(a: Vector2, k: Vector2, r?: Vector2): Vector2 {
216+
if (r == null) r = new Array(2) as Vector2;
241217
r[0] = a[0] * k[0];
242218
r[1] = a[1] * k[1];
243219
return r;
@@ -261,11 +237,9 @@ const V2 = {
261237

262238
const _tmpVec: Vector3 = [0, 0, 0];
263239

264-
// @ts-ignore TS claims that the implementation doesn't match the overloading
265-
function round(v: Vector3, r?: Vector3 | null | undefined): Vector3;
266-
function round(v: Vector3Like, r?: Float32Array | null | undefined) {
240+
function round(v: Vector3, r?: Vector3 | null | undefined) {
267241
if (r == null) {
268-
r = new Float32Array(3);
242+
r = new Array(3) as Vector3;
269243
}
270244

271245
r[0] = Math.round(v[0]);
@@ -274,20 +248,16 @@ function round(v: Vector3Like, r?: Float32Array | null | undefined) {
274248
return r;
275249
}
276250

277-
// @ts-ignore TS claims that the implementation doesn't match the overloading
278-
function divide3(a: ThreeVector3, k: ThreeVector3, r?: ThreeVector3): Vector3;
279-
function divide3(a: Vector3, k: Vector3, r?: Vector3): Vector3;
280-
function divide3(a: Float32Array, k: Float32Array, r?: Float32Array) {
281-
if (r == null) r = new Float32Array(3);
251+
function divide3(a: Vector3, k: Vector3, r?: Vector3): Vector3 {
252+
if (r == null) r = new Array(3) as Vector3;
282253
r[0] = a[0] / k[0];
283254
r[1] = a[1] / k[1];
284255
r[2] = a[2] / k[2];
285256
return r;
286257
}
287258

288-
function scale3(a: Vector3, k: Vector3, r?: Vector3): Vector3;
289-
function scale3(a: Vector3Like, k: Vector3Like, r?: Vector3Like): Vector3Like {
290-
if (r == null) r = new Float32Array(3);
259+
function scale3(a: Vector3, k: Vector3, r?: Vector3): Vector3 {
260+
if (r == null) r = new Array(3) as Vector3;
291261
r[0] = a[0] * k[0];
292262
r[1] = a[1] * k[1];
293263
r[2] = a[2] * k[2];
@@ -297,16 +267,16 @@ function scale3(a: Vector3Like, k: Vector3Like, r?: Vector3Like): Vector3Like {
297267
const V3 = {
298268
...BareV3,
299269
// Component-wise minimum of two vectors.
300-
min(vec1: Vector3Like, vec2: Vector3Like): Vector3 {
270+
min(vec1: Vector3, vec2: Vector3): Vector3 {
301271
return [Math.min(vec1[0], vec2[0]), Math.min(vec1[1], vec2[1]), Math.min(vec1[2], vec2[2])];
302272
},
303273

304274
// Component-wise maximum of two vectors.
305-
max(vec1: Vector3Like, vec2: Vector3Like): Vector3 {
275+
max(vec1: Vector3, vec2: Vector3): Vector3 {
306276
return [Math.max(vec1[0], vec2[0]), Math.max(vec1[1], vec2[1]), Math.max(vec1[2], vec2[2])];
307277
},
308278

309-
equals(vec1: Vector3Like, vec2: Vector3Like): boolean {
279+
equals(vec1: Vector3, vec2: Vector3): boolean {
310280
return vec1[0] === vec2[0] && vec1[1] === vec2[1] && vec1[2] === vec2[2];
311281
},
312282

@@ -344,15 +314,15 @@ const V3 = {
344314
return V3.floor(V3.scale3(vec, sourceMag));
345315
},
346316

347-
scaledSquaredDist(a: Vector3Like, b: Vector3Like, scale: Vector3) {
317+
scaledSquaredDist(a: Vector3, b: Vector3, scale: Vector3) {
348318
// Computes the distance between two vectors while respecting a 3 dimensional scale
349319
// Use _tmpVec as result variable (third parameter) to avoid allocations
350320
V3.sub(a, b, _tmpVec);
351321
V3.scale3(_tmpVec, scale, _tmpVec);
352322
return V3.lengthSquared(_tmpVec);
353323
},
354324

355-
scaledDist(a: Vector3Like, b: Vector3Like, scale: Vector3) {
325+
scaledDist(a: Vector3, b: Vector3, scale: Vector3) {
356326
const squaredDist = V3.scaledSquaredDist(a, b, scale);
357327
return Math.sqrt(squaredDist);
358328
},
@@ -399,4 +369,4 @@ const V4 = {
399369
},
400370
};
401371

402-
export { M4x4, V2, V3, V4 };
372+
export { M4x4, V2, V3, V4, type Matrix4x4 };

frontend/javascripts/types/mjs.d.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,45 @@
11
declare module "mjs" {
2-
type Vector3 = [number, number, number];
3-
type Vector3Like = Vector3 | Float32Array;
4-
type Vector2Like = Vector2 | Float32Array;
2+
type Vector3 = [number, number, number];
3+
export type Vector16 = [
4+
number,
5+
number,
6+
number,
7+
number,
8+
number,
9+
number,
10+
number,
11+
number,
12+
number,
13+
number,
14+
number,
15+
number,
16+
number,
17+
number,
18+
number,
19+
number,
20+
];
21+
export type Matrix4x4 = Vector16;
522

6-
export type Matrix4x4 =
7-
| [
8-
number,
9-
number,
10-
number,
11-
number,
12-
number,
13-
number,
14-
number,
15-
number,
16-
number,
17-
number,
18-
number,
19-
number,
20-
number,
21-
number,
22-
number,
23-
number,
24-
]
25-
| Float32Array;
26-
27-
export default (f: Float32ArrayConstructor) => ({
23+
export default (f: ArrayConstructor) => ({
2824
M4x4: {
2925
identity: Matrix4x4,
3026
translate: function (
3127
m: Vector3,
3228
points: Matrix4x4,
33-
r?: Float32Array | number[] | null | undefined,
29+
r?: number[] | null | undefined,
3430
): Matrix4x4 {},
3531
scale: function (
3632
s: Vector3,
3733
m: Matrix4x4,
38-
r?: Float32Array | number[] | null | undefined,
34+
r?: number[] | null | undefined,
3935
): Matrix4x4 {},
40-
scale1: function (s: number, m: Matrix4x4, r?: Float32Array | null | undefined): Matrix4x4 {},
36+
scale1: function (s: number, m: Matrix4x4, r?: number[] | null | undefined): Matrix4x4 {},
4137
mul: function (a: Matrix4x4, b: Matrix4x4, r?: Matrix4x4 | null | undefined): Matrix4x4 {},
4238
rotate: function (
4339
angle: number,
4440
axis: Vector3,
4541
m: Matrix4x4,
46-
r?: Float32Array | number[] | null | undefined,
42+
r?: number[] | null | undefined,
4743
): Matrix4x4 {},
4844
clone: function (m: Matrix4x4): Matrix4x4 {},
4945
transformLineAffine: function (m: Matrix4x4, v1: Vector3, v2: Vector3): Matrix4x4 {},
@@ -56,7 +52,7 @@ declare module "mjs" {
5652
},
5753
V3: {
5854
add: function (x: Vector3, y: Vector3, res?: Vector3): Vector3 {},
59-
sub: function (x: Vector3Like, y: Vector3Like, res?: Vector3Like): Vector3 {},
55+
sub: function (x: Vector3, y: Vector3, res?: Vector3): Vector3 {},
6056
cross: function (x: Vector3, y: Vector3, res?: Vector3): Vector3 {},
6157
length: function (x: Vector3): number {},
6258
lengthSquared: function (x: Vector3): number {},

frontend/javascripts/viewer/api/api_latest.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "admin/rest_api";
88
import PriorityQueue from "js-priority-queue";
99
import { InputKeyboardNoLoop } from "libs/input";
10-
import { M4x4, type Matrix4x4, V3, type Vector16 } from "libs/mjs";
10+
import { M4x4, type Matrix4x4, V3 } from "libs/mjs";
1111
import Request from "libs/request";
1212
import type { ToastStyle } from "libs/toast";
1313
import Toast from "libs/toast";
@@ -17,6 +17,7 @@ import { coalesce, mod } from "libs/utils";
1717
import window, { location } from "libs/window";
1818
import _ from "lodash";
1919
import messages from "messages";
20+
import type { Vector16 } from "mjs";
2021
import { Euler, MathUtils, Quaternion } from "three";
2122
import TWEEN from "tween.js";
2223
import { type APICompoundType, APICompoundTypeEnum, type ElementClass } from "types/api_types";
@@ -2745,8 +2746,8 @@ class DataApi {
27452746
* );
27462747
*/
27472748
_createTransformsFromSpecs(specs: Array<TransformSpec>) {
2748-
const makeTranslation = (x: number, y: number, z: number): Matrix4x4 =>
2749-
new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1]);
2749+
// biome-ignore format: don't format array
2750+
const makeTranslation = (x: number, y: number, z: number): Matrix4x4 => [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1];
27502751
const makeScale = (scale: Vector3, anchor: Vector3) =>
27512752
M4x4.mul(
27522753
M4x4.scale(scale, makeTranslation(anchor[0], anchor[1], anchor[2])),
@@ -2757,11 +2758,11 @@ class DataApi {
27572758
M4x4.mul(
27582759
makeTranslation(pos[0], pos[1], pos[2]),
27592760
// biome-ignore format: don't format array
2760-
new Float32Array([
2761+
[
27612762
Math.cos(thetaInRad), Math.sin(thetaInRad), 0, 0,
27622763
-Math.sin(thetaInRad), Math.cos(thetaInRad), 0, 0,
27632764
0, 0, 1, 0, 0, 0, 0, 1,
2764-
]),
2765+
],
27652766
),
27662767
makeTranslation(-pos[0], -pos[1], -pos[2]),
27672768
);

frontend/javascripts/viewer/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Matrix4x4 } from "mjs";
12
import { Euler, Matrix4 } from "three";
23
export type AdditionalCoordinate = { name: string; value: number };
34

@@ -383,7 +384,7 @@ export enum BLEND_MODES {
383384
Cover = "Cover",
384385
}
385386

386-
export const Identity4x4 = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
387+
export const Identity4x4: Matrix4x4 = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1];
387388
export const IdentityTransform = {
388389
type: "affine",
389390
affineMatrix: Identity4x4,

frontend/javascripts/viewer/geometries/helper_geometries.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class ContourGeometry {
8989
addEdgePoint(pos: Vector3) {
9090
const pointCount = this.vertexBuffer.getLength();
9191
const lastPoint = this.vertexBuffer.getBuffer().subarray((pointCount - 1) * 3, pointCount * 3);
92-
if (V3.equals(pos, lastPoint)) {
92+
if (V3.equals(pos, [lastPoint[0], lastPoint[1], lastPoint[2]])) {
9393
// Skip adding the point if it is the same as the last one.
9494
return;
9595
}

frontend/javascripts/viewer/model/accessors/flycam_accessor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import type { Matrix4x4 } from "libs/mjs";
21
import { M4x4, V3 } from "libs/mjs";
32
import { map3, mod } from "libs/utils";
43
import _ from "lodash";
54
import memoizeOne from "memoize-one";
5+
import type { Matrix4x4 } from "mjs";
66
import { type Euler, MathUtils, Matrix4, Object3D } from "three";
77
import type { AdditionalCoordinate, VoxelSize } from "types/api_types";
88
import { baseDatasetViewConfiguration } from "types/schemas/dataset_view_configuration.schema";

frontend/javascripts/viewer/model/bucket_data_handling/bucket_picker_strategies/flight_bucket_picker.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ export default function determineBucketsForFlight(
6363
};
6464

6565
const transformAndApplyMatrix = (vec: Vector3): Vector3 =>
66-
// @ts-ignore
67-
M4x4.transformPointsAffine(queryMatrix, transformToSphereCap(vec));
66+
M4x4.transformPointsAffine(queryMatrix, transformToSphereCap(vec)) as Vector3;
6867

6968
let traversedBucketsWithPriorities: Array<[Vector4, number]> = [];
7069
const maybeAddBucket = createDistinctBucketAdder(traversedBucketsWithPriorities);

frontend/javascripts/viewer/model/bucket_data_handling/bucket_picker_strategies/oblique_bucket_picker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ThreeDMap from "libs/ThreeDMap";
2-
import type { Matrix4x4 } from "libs/mjs";
32
import { M4x4, V3 } from "libs/mjs";
43
import _ from "lodash";
4+
import type { Matrix4x4 } from "mjs";
55
import type { OrthoViewWithoutTD, Vector2, Vector3, Vector4, ViewMode } from "viewer/constants";
66
import constants from "viewer/constants";
77
import traverse from "viewer/model/bucket_data_handling/bucket_traversals";

unreleased_changes/9059.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Changed
2+
- To improve performance, the use of Float32Arrays was omitted in favor of plain Arrays in our self-adjusted mjs module.

0 commit comments

Comments
 (0)