forked from Kitware/vtk-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestModelTransformMatrix.js
More file actions
321 lines (254 loc) · 10.7 KB
/
Copy pathtestModelTransformMatrix.js
File metadata and controls
321 lines (254 loc) · 10.7 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
import { describe, it, expect, beforeEach } from 'vitest';
import { mat4, vec3 } from 'gl-matrix';
import vtkCamera from 'vtk.js/Sources/Rendering/Core/Camera';
import vtkTransform from 'vtk.js/Sources/Common/Transform/Transform';
describe('Camera Model Transform Matrix', () => {
let camera;
beforeEach(() => {
camera = vtkCamera.newInstance();
});
describe('Basic Model Transform API', () => {
it('should support setting and getting model transform matrix', () => {
const transform = vtkTransform.newInstance();
transform.scale(2, 3, 4);
const matrix = transform.getMatrix();
camera.setModelTransformMatrix(matrix);
const retrieved = camera.getModelTransformMatrix();
expect(retrieved).toBeDefined();
expect(retrieved).toEqual(matrix);
transform.delete();
});
it('should handle null model transform matrix', () => {
expect(camera.getModelTransformMatrix()).toBeNull();
});
it('should allow clearing model transform matrix', () => {
const transform = vtkTransform.newInstance();
transform.scale(2, 2, 2);
camera.setModelTransformMatrix(transform.getMatrix());
camera.setModelTransformMatrix(null);
expect(camera.getModelTransformMatrix()).toBeNull();
transform.delete();
});
});
describe('View Matrix Composition with Model Transform', () => {
it('should compose model transform with view matrix for camera-computed view matrix', () => {
// Setup camera position
camera.setPosition(0, 0, 10);
camera.setFocalPoint(0, 0, 0);
camera.setViewUp(0, 1, 0);
// Create a simple scale transform
const transform = vtkTransform.newInstance();
transform.scale(1, 1, 2); // Scale Z by 2x
const viewMatrixWithoutTransform = camera.getViewMatrix();
camera.setModelTransformMatrix(transform.getMatrix());
const viewMatrixWithTransform = camera.getViewMatrix();
// Matrices should be different after applying transform
expect(viewMatrixWithTransform).not.toEqual(viewMatrixWithoutTransform);
transform.delete();
});
it('should compose model transform with manual view matrix', () => {
// Set a manual view matrix
const manualViewMatrix = mat4.create();
mat4.lookAt(
manualViewMatrix,
[0, 0, 10], // eye
[0, 0, 0], // center
[0, 1, 0] // up
);
mat4.transpose(manualViewMatrix, manualViewMatrix);
camera.setViewMatrix(manualViewMatrix);
// Apply model transform
const transform = vtkTransform.newInstance();
transform.scale(2, 2, 2);
camera.setModelTransformMatrix(transform.getMatrix());
const result = camera.getViewMatrix();
// Should be a new array (composition result)
expect(result).toBeDefined();
expect(result.length).toBe(16);
transform.delete();
});
});
describe('World-Space Transform Application (Fix Verification)', () => {
it('should apply model transform in world space (pre-multiplication)', () => {
// Setup camera
camera.setPosition(10, 10, 10);
camera.setFocalPoint(0, 0, 0);
camera.setViewUp(0, 1, 0);
// Create vertical exaggeration transform (scale Z)
const scaleZ = 2;
const transform = vtkTransform.newInstance();
transform.scale(1, 1, scaleZ);
const modelTransformMatrix = transform.getMatrix();
// Get view matrix with transform applied
camera.setModelTransformMatrix(modelTransformMatrix);
const viewMatrixWithTransform = camera.getViewMatrix();
// Get view matrix without transform for comparison
camera.setModelTransformMatrix(null);
const viewMatrixWithoutTransform = camera.getViewMatrix();
// The transform should affect the view matrix
expect(viewMatrixWithTransform).not.toEqual(viewMatrixWithoutTransform);
// Verify the transform is in world space by testing point transformation
// A point in world space that would be affected by Z-scaling
const testPoint = vec3.fromValues(0, 0, 1);
const transformedPoint = vec3.create();
// Apply view matrix to test point
vec3.transformMat4(transformedPoint, testPoint, viewMatrixWithTransform);
// The Z-scale should be reflected in the transformation
// This verifies world-space application (not camera-space)
expect(transformedPoint).toBeDefined();
transform.delete();
});
it('should maintain vertical exaggeration alignment regardless of camera rotation', () => {
// This test verifies the fix: transforms should be world-space invariant
// to camera orientation
// Create a Z-scale transform (vertical exaggeration)
const transform = vtkTransform.newInstance();
transform.scale(1, 1, 2); // 2x vertical exaggeration
const modelTransformMatrix = transform.getMatrix();
// Get view matrix at initial orientation
camera.setPosition(0, 0, 10);
camera.setFocalPoint(0, 0, 0);
camera.setViewUp(0, 1, 0);
camera.setModelTransformMatrix(modelTransformMatrix);
const viewMatrix1 = camera.getViewMatrix();
// Get view matrix after camera rotation
// Rotate camera around focal point (elevation)
camera.elevation(45);
const viewMatrix2 = camera.getViewMatrix();
// Both should incorporate the same model transform
// The model transform should affect both view matrices
expect(viewMatrix1).toBeDefined();
expect(viewMatrix2).toBeDefined();
// Verify both matrices are different (due to camera rotation)
// but both contain the model transform
let matricesDiffer = false;
for (let i = 0; i < 16; i++) {
if (Math.abs(viewMatrix1[i] - viewMatrix2[i]) > 1e-6) {
matricesDiffer = true;
break;
}
}
expect(matricesDiffer).toBe(true); // Camera rotation should change view matrix
// The key test: model transform should be pre-multiplied
// This is verified by checking that the Z-scaling is preserved
// in world space regardless of camera orientation
camera.elevation(-45); // Reset
expect(camera.getModelTransformMatrix()).toEqual(modelTransformMatrix);
transform.delete();
});
it('should apply scale transform correctly for vertical exaggeration use case', () => {
// Replicate the actual vertical exaggeration use case from the MRE
camera.setPosition(5, 5, 10);
camera.setFocalPoint(0, 0, 0);
camera.setViewUp(0, 0, 1); // Z-up scene (like in the MRE)
// Apply 2x vertical exaggeration
const exaggeration = 2;
const transform = vtkTransform.newInstance();
transform.scale(1, 1, exaggeration); // Scale world Z-axis
camera.setModelTransformMatrix(transform.getMatrix());
const viewMatrix = camera.getViewMatrix();
// Verify view matrix is created successfully
expect(viewMatrix).toBeDefined();
expect(viewMatrix.length).toBe(16);
// Test with different exaggerations
const exaggerations = [0.5, 1, 1.5, 2, 3, 5];
exaggerations.forEach((scale) => {
const t = vtkTransform.newInstance();
t.scale(1, 1, scale);
camera.setModelTransformMatrix(t.getMatrix());
const vm = camera.getViewMatrix();
expect(vm).toBeDefined();
expect(vm.length).toBe(16);
t.delete();
});
transform.delete();
});
});
describe('Matrix Multiplication Order Verification', () => {
it('should use pre-multiplication (modelTransform * viewMatrix)', () => {
// This test specifically verifies that the fix uses pre-multiplication
// (world-space) rather than post-multiplication (camera-space)
camera.setPosition(5, 5, 5);
camera.setFocalPoint(0, 0, 0);
camera.setViewUp(0, 1, 0);
// Create a translation in the model transform
const transform = vtkTransform.newInstance();
transform.translate(1, 0, 0); // Translate along X
const modelTransformMatrix = transform.getMatrix();
camera.setModelTransformMatrix(modelTransformMatrix);
const viewMatrix = camera.getViewMatrix();
// Get the view matrix without transform
camera.setModelTransformMatrix(null);
const viewMatrixOnly = camera.getViewMatrix();
// Manually compute pre-multiplication (world space)
const preMultiplied = mat4.create();
mat4.multiply(preMultiplied, modelTransformMatrix, viewMatrixOnly);
// Manually compute post-multiplication (camera space)
const postMultiplied = mat4.create();
mat4.multiply(postMultiplied, viewMatrixOnly, modelTransformMatrix);
// The actual result should match pre-multiplication (world space)
let preMultipliedMatches = true;
for (let i = 0; i < 16; i++) {
if (Math.abs(viewMatrix[i] - preMultiplied[i]) > 1e-6) {
preMultipliedMatches = false;
break;
}
}
expect(preMultipliedMatches).toBe(true);
// And should NOT match post-multiplication
let postMultipliedMatches = true;
for (let i = 0; i < 16; i++) {
if (Math.abs(viewMatrix[i] - postMultiplied[i]) > 1e-6) {
postMultipliedMatches = false;
break;
}
}
expect(postMultipliedMatches).toBe(false);
transform.delete();
});
});
describe('Edge Cases and Robustness', () => {
it('should handle identity transform', () => {
const transform = vtkTransform.newInstance();
// No operations on identity transform
camera.setModelTransformMatrix(transform.getMatrix());
const viewMatrix = camera.getViewMatrix();
expect(viewMatrix).toBeDefined();
transform.delete();
});
it('should handle multiple transform applications', () => {
const transform = vtkTransform.newInstance();
transform.scale(2, 2, 2);
camera.setModelTransformMatrix(transform.getMatrix());
// Update multiple times
for (let i = 0; i < 5; i++) {
const vm = camera.getViewMatrix();
expect(vm).toBeDefined();
// Change scale
const t = vtkTransform.newInstance();
t.scale(1, 1, i + 1);
camera.setModelTransformMatrix(t.getMatrix());
t.delete();
}
transform.delete();
});
it('should work with various camera parameters', () => {
const positions = [
[0, 0, 10],
[10, 10, 10],
[-5, 5, 20],
[0, 0, -10],
];
const transform = vtkTransform.newInstance();
transform.scale(1, 1, 3);
camera.setModelTransformMatrix(transform.getMatrix());
positions.forEach((pos) => {
camera.setPosition(...pos);
const vm = camera.getViewMatrix();
expect(vm).toBeDefined();
expect(vm.length).toBe(16);
});
transform.delete();
});
});
});