-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathapi_skeleton_latest.spec.ts
More file actions
404 lines (358 loc) · 15.2 KB
/
api_skeleton_latest.spec.ts
File metadata and controls
404 lines (358 loc) · 15.2 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import { Keyboard } from "keyboardjs";
import { map3 } from "libs/utils";
import testRotations from "test/fixtures/test_rotations";
import { setupWebknossosForTesting, type WebknossosTestContext } from "test/helpers/apiHelpers";
import { Euler, MathUtils, Matrix4, Quaternion } from "three";
import { userSettings } from "types/schemas/user_settings.schema";
import {
OrthoBaseRotations,
OrthoViewToNumber,
OrthoViewValuesWithoutTDView,
type Vector3,
} from "viewer/constants";
import { enforceSkeletonTracing } from "viewer/model/accessors/skeletontracing_accessor";
import { setRotationAction } from "viewer/model/actions/flycam_actions";
import { setMappingEnabledAction } from "viewer/model/actions/settings_actions";
import { setTreeGroupsAction } from "viewer/model/actions/skeletontracing_actions";
import { setViewportAction } from "viewer/model/actions/view_mode_actions";
import {
eulerAngleToReducerInternalMatrix,
reducerInternalMatrixToEulerAngle,
} from "viewer/model/helpers/rotation_helpers";
import Store from "viewer/store";
import { makeBasicGroupObject } from "viewer/view/right_border_tabs/trees_tab/tree_hierarchy_view_helpers";
import { beforeEach, describe, expect, it, vi } from "vitest";
const toRadian = (arr: Vector3): Vector3 => [
MathUtils.degToRad(arr[0]),
MathUtils.degToRad(arr[1]),
MathUtils.degToRad(arr[2]),
];
function applyRotationInFlycamReducerSpace(
flycamRotationInRadian: Vector3,
rotationToApply: Euler,
): Vector3 {
// eulerAngleToReducerInternalMatrix and reducerInternalMatrixToEulerAngle are tested in rotation_helpers.spec.ts.
// Calculate expected rotation and make it a quaternion for equal comparison.
const rotationMatrix = eulerAngleToReducerInternalMatrix(flycamRotationInRadian);
const rotationMatrixWithViewport = rotationMatrix.multiply(
new Matrix4().makeRotationFromEuler(rotationToApply),
);
const resultingAngle = reducerInternalMatrixToEulerAngle(rotationMatrixWithViewport);
return resultingAngle;
}
describe("API Skeleton", () => {
beforeEach<WebknossosTestContext>(async (context) => {
await setupWebknossosForTesting(context, "skeleton", undefined, {
dontDispatchWkInitialized: true,
});
});
it<WebknossosTestContext>("getActiveNodeId should get the active node id", ({ api }) => {
expect(api.tracing.getActiveNodeId()).toBe(3);
});
it<WebknossosTestContext>("setActiveNode should set the active node id", ({ api }) => {
api.tracing.setActiveNode(1);
expect(api.tracing.getActiveNodeId()).toBe(1);
});
it<WebknossosTestContext>("getActiveTree should get the active tree id", ({ api }) => {
api.tracing.setActiveNode(3);
expect(api.tracing.getActiveTreeId()).toBe(2);
});
it<WebknossosTestContext>("getActiveTreeGroupId should get the active group id", ({ api }) => {
expect(api.tracing.getActiveTreeGroupId()).toBe(null);
});
it<WebknossosTestContext>("setActiveTreeGroupId should set the active group id", ({ api }) => {
api.tracing.setActiveTreeGroup(3);
expect(api.tracing.getActiveTreeGroupId()).toBe(3);
});
it<WebknossosTestContext>("getAllNodes should get a list of all nodes", ({ api }) => {
const nodes = api.tracing.getAllNodes();
expect(nodes.length).toBe(3);
});
it<WebknossosTestContext>("getCommentForNode should get the comment of a node", ({ api }) => {
const comment = api.tracing.getCommentForNode(3);
expect(comment).toBe("Test");
});
it<WebknossosTestContext>("getCommentForNode should throw an error if the supplied treeId doesn't exist", ({
api,
}) => {
expect(() => api.tracing.getCommentForNode(3, 3)).toThrow();
});
it<WebknossosTestContext>("setCommentForNode should set the comment of a node", ({ api }) => {
const COMMENT = "a comment";
api.tracing.setCommentForNode(COMMENT, 2);
const comment = api.tracing.getCommentForNode(2);
expect(comment).toBe(COMMENT);
});
it<WebknossosTestContext>("setCommentForNode should throw an error if the supplied nodeId doesn't exist", ({
api,
}) => {
expect(() => api.tracing.setCommentForNode("another comment", 4)).toThrow();
});
it<WebknossosTestContext>("getCameraPosition should return the current camera position", ({
api,
}) => {
const cameraPosition = api.tracing.getCameraPosition();
expect(cameraPosition).toEqual([1, 2, 3]);
});
it<WebknossosTestContext>("setCameraPosition should set the current camera position", ({
api,
}) => {
api.tracing.setCameraPosition([7, 8, 9]);
const cameraPosition = api.tracing.getCameraPosition();
expect(cameraPosition).toEqual([7, 8, 9]);
});
it<WebknossosTestContext>("Data Api: getLayerNames should get an array of all layer names", ({
api,
}) => {
expect(api.data.getLayerNames().length).toBe(2);
expect(api.data.getLayerNames().includes("segmentation")).toBe(true);
expect(api.data.getLayerNames().includes("color")).toBe(true);
});
it<WebknossosTestContext>("Data Api: setMapping should throw an error if the layer name is not valid", ({
api,
}) => {
expect(() => api.data.setMapping("nonExistingLayer", [1, 3])).toThrow();
});
it<WebknossosTestContext>("Data Api: setMapping should set a mapping of a layer", ({
api,
model,
}) => {
const cube = model.getCubeByLayerName("segmentation");
expect(Store.getState().temporaryConfiguration.activeMappingByLayer.segmentation.mapping).toBe(
null,
);
api.data.setMapping("segmentation", [1, 3]);
expect(
Store.getState().temporaryConfiguration.activeMappingByLayer.segmentation.mapping,
).not.toBe(null);
// Workaround: This is usually called after the mapping textures were created successfully
// and can be rendered, which doesn't happen in this test scenario
Store.dispatch(setMappingEnabledAction("segmentation", true));
expect(cube.mapId(1)).toBe(3);
});
it<WebknossosTestContext>("Data Api: getBoundingBox should throw an error if the layer name is not valid", ({
api,
}) => {
expect(() => api.data.getBoundingBox("nonExistingLayer")).toThrow();
});
it<WebknossosTestContext>("Data Api: getBoundingBox should get the bounding box of a layer", ({
api,
}) => {
const correctBoundingBox = [
[0, 0, 0],
[10240, 10240, 10240],
];
const boundingBox = api.data.getBoundingBox("color");
expect(boundingBox).toEqual(correctBoundingBox);
});
it<WebknossosTestContext>("Data Api: getDataValue should throw an error if the layer name is not valid", async ({
api,
}) => {
await expect(api.data.getDataValue("nonExistingLayer", [1, 2, 3])).rejects.toThrow();
});
it<WebknossosTestContext>("Data Api: getDataValue should get the data value for a layer, position and zoomstep", async ({
api,
model,
}) => {
// Currently, this test only makes sure pullQueue.pull is being called and the bucketLoaded
// event is being triggered.
// There is another spec for pullqueue.ts
const cube = model.getCubeByLayerName("segmentation");
const position: Vector3 = [100, 100, 100];
const zoomStep = 0;
const bucketAddress = cube.positionToZoomedAddress(position, null, zoomStep);
const bucket = cube.getOrCreateBucket(bucketAddress);
vi.spyOn(cube.pullQueue, "pull").mockReturnValue();
vi.spyOn(cube, "getDataValue").mockReturnValue(1337);
const promise = api.data.getDataValue("segmentation", position, zoomStep).then((dataValue) => {
expect(dataValue).toBe(1337);
});
expect(bucket.type).toBe("data");
if (bucket.type === "data") {
bucket.trigger("bucketLoaded");
}
return promise;
});
it<WebknossosTestContext>("User Api: setConfiguration should set and get a user configuration value", ({
api,
}) => {
const MOVE_VALUE = 100;
api.user.setConfiguration("moveValue", MOVE_VALUE);
expect(api.user.getConfiguration("moveValue")).toBe(MOVE_VALUE);
});
it<WebknossosTestContext>("User Api: setConfiguration should clamp a user configuration value if it is outside of the valid range", ({
api,
}) => {
const MOVE_VALUE = 1;
api.user.setConfiguration("moveValue", MOVE_VALUE);
expect(api.user.getConfiguration("moveValue")).toBe(userSettings.moveValue.minimum);
});
it<WebknossosTestContext>("Utils Api: registerKeyHandler should register a key handler and return a handler to unregister it again", async ({
api,
}) => {
const bindSpy = vi.spyOn(Keyboard.prototype, "bind").mockReturnThis();
const unbindSpy = vi.spyOn(Keyboard.prototype, "unbind").mockReturnThis();
const binding = api.utils.registerKeyHandler("g", { onPressed: () => {} });
expect(bindSpy).toHaveBeenCalled();
binding.unregister();
expect(unbindSpy).toHaveBeenCalled();
});
it<WebknossosTestContext>("Utils Api: registerOverwrite should overwrite an existing function", ({
api,
}) => {
let bool = false;
api.utils.registerOverwrite("SET_ACTIVE_NODE", (_store, call, action) => {
bool = true;
call(action);
});
api.tracing.setActiveNode(2);
// The added instructions should have been executed
expect(bool).toBe(true);
// And the original method should have been called
expect(api.tracing.getActiveNodeId()).toBe(2);
});
it<WebknossosTestContext>("Calling a volume api function in a skeleton tracing should throw an error", ({
api,
}) => {
expect(() => api.tracing.getActiveCellId()).toThrow();
});
it<WebknossosTestContext>("getTreeName should get the name of a tree", ({ api }) => {
const name = api.tracing.getTreeName(2);
expect(name).toBe("explorative_2017-08-09_sample_user_002");
});
it<WebknossosTestContext>("getTreeName should get the name of the active tree if no treeId is specified", ({
api,
}) => {
api.tracing.setActiveNode(1);
const name = api.tracing.getTreeName();
expect(name).toBe("explorative_2017-08-09_sample_user_001");
});
it<WebknossosTestContext>("getTreeName should throw an error if the supplied treeId doesn't exist", ({
api,
}) => {
expect(() => api.tracing.getTreeName(5)).toThrow();
});
it<WebknossosTestContext>("setTreeName should set the name of a tree", ({ api }) => {
const NAME = "a tree";
api.tracing.setTreeName(NAME, 2);
const name = api.tracing.getTreeName(2);
expect(name).toBe(NAME);
});
it<WebknossosTestContext>("setTreeName should set the name of the active tree if no treeId is specified", ({
api,
}) => {
const NAME = "a tree";
api.tracing.setActiveNode(1);
api.tracing.setTreeName(NAME);
const name = api.tracing.getTreeName(1);
expect(name).toBe(NAME);
});
it<WebknossosTestContext>("getTreeGroups should get all tree groups and set a tree group", ({
api,
}) => {
Store.dispatch(
setTreeGroupsAction([makeBasicGroupObject(3, "group 3"), makeBasicGroupObject(7, "group 7")]),
);
expect(api.tracing.getTreeGroups()).toEqual([
{
name: "group 3",
groupId: 3,
isExpanded: false,
},
{
name: "group 7",
groupId: 7,
isExpanded: false,
},
]);
api.tracing.setTreeGroup(2, 3);
api.tracing.setTreeGroup(1, 7);
const skeletonTracing = enforceSkeletonTracing(Store.getState().annotation);
expect(skeletonTracing.trees.getOrThrow(2).groupId).toBe(3);
expect(skeletonTracing.trees.getOrThrow(1).groupId).toBe(7);
});
it<WebknossosTestContext>("renameSkeletonGroup should rename a tree group", ({ api }) => {
Store.dispatch(
setTreeGroupsAction([makeBasicGroupObject(3, "group 3"), makeBasicGroupObject(7, "group 7")]),
);
api.tracing.renameSkeletonGroup(7, "renamed");
expect(enforceSkeletonTracing(Store.getState().annotation).treeGroups[1].name).toBe("renamed");
});
it<WebknossosTestContext>("setTreeVisibility should set the visibility of a tree", ({ api }) => {
api.tracing.setTreeVisibility(2, false);
expect(enforceSkeletonTracing(Store.getState().annotation).trees.getOrThrow(2).isVisible).toBe(
false,
);
api.tracing.setTreeVisibility(2, true);
expect(enforceSkeletonTracing(Store.getState().annotation).trees.getOrThrow(2).isVisible).toBe(
true,
);
});
it<WebknossosTestContext>("should create skeleton nodes with correct properties.", ({ api }) => {
const flycamRotation = [0, 0, 0] as Vector3;
for (const planeId of OrthoViewValuesWithoutTDView) {
const rotationForComparison = applyRotationInFlycamReducerSpace(
flycamRotation,
OrthoBaseRotations[planeId],
);
const rotationQuaternion = new Quaternion().setFromEuler(new Euler(...rotationForComparison));
Store.dispatch(setRotationAction(flycamRotation));
Store.dispatch(setViewportAction(planeId));
api.tracing.createNode([10, 10, 10], { activate: true });
const skeletonTracing = enforceSkeletonTracing(Store.getState().annotation);
// Throw error if no node / tree is active by passing -1 as id.
const newNode = skeletonTracing.trees
.getOrThrow(skeletonTracing.activeTreeId || -1)
.nodes.getOrThrow(skeletonTracing.activeNodeId || -1);
const propsToCheck = {
untransformedPosition: newNode.untransformedPosition,
additionalCoordinates: newNode.additionalCoordinates,
viewport: newNode.viewport,
mag: newNode.mag,
};
expect(propsToCheck).toStrictEqual({
untransformedPosition: [10, 10, 10],
additionalCoordinates: [],
viewport: OrthoViewToNumber[planeId],
mag: 0,
});
const newNodeQuaternion = new Quaternion().setFromEuler(
new Euler(...toRadian(newNode.rotation)),
);
expect(
rotationQuaternion.angleTo(newNodeQuaternion),
`Node rotation ${newNode.rotation} is not nearly equal to ${map3(MathUtils.radToDeg, rotationForComparison)} in viewport ${planeId}.`,
).toBeLessThan(0.000001);
}
});
it<WebknossosTestContext>("should create skeleton nodes with correct rotation when flycam is rotated in all three viewports.", ({
api,
}) => {
for (const testRotation of testRotations) {
for (const planeId of OrthoViewValuesWithoutTDView) {
const rotationInRadian = toRadian(testRotation);
const resultingAngle = applyRotationInFlycamReducerSpace(
rotationInRadian,
OrthoBaseRotations[planeId],
);
const rotationQuaternion = new Quaternion().setFromEuler(new Euler(...resultingAngle));
// Test node creation.
Store.dispatch(setRotationAction(testRotation));
Store.dispatch(setViewportAction(planeId));
api.tracing.createNode([10, 10, 10], { activate: true });
const skeletonTracing = enforceSkeletonTracing(Store.getState().annotation);
// Throw error if no node / tree is active by passing -1 as id.
const newNode = skeletonTracing.trees
.getOrThrow(skeletonTracing.activeTreeId || -1)
.nodes.getOrThrow(skeletonTracing.activeNodeId || -1);
const newNodeQuaternion = new Quaternion().setFromEuler(
new Euler(...toRadian(newNode.rotation)),
);
expect(
rotationQuaternion.angleTo(newNodeQuaternion),
`Node rotation ${newNode.rotation} is not nearly equal to ${map3(MathUtils.radToDeg, resultingAngle)} in viewport ${planeId}.`,
).toBeLessThan(0.000001);
}
}
});
});