Skip to content

Commit 2b9bab2

Browse files
dakerfloryst
authored andcommitted
fix(Camera): add support for getFrustumPlanes
1 parent 527c2d9 commit 2b9bab2

File tree

5 files changed

+96
-23
lines changed

5 files changed

+96
-23
lines changed

Sources/Common/Core/Math/index.d.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function getSeed(): number;
203203
export function random(minValue: number, maxValue: number): number;
204204

205205
/**
206-
* Addition of two 3-vectors.
206+
* Add two 3-vectors.
207207
* @param {Vector3} a The first 3D vector.
208208
* @param {Vector3} b The second 3D vector.
209209
* @param {Vector3} out The output 3D vector.
@@ -215,7 +215,7 @@ export function random(minValue: number, maxValue: number): number;
215215
export function add(a: Vector3, b: Vector3, out: Vector3): Vector3;
216216

217217
/**
218-
* Subtraction of two 3-vectors.
218+
* Substract two 3-vectors.
219219
* @param {Vector3} a The first 3D vector.
220220
* @param {Vector3} b The second 3D vector.
221221
* @param {Vector3} out The output 3D vector.
@@ -227,7 +227,7 @@ export function add(a: Vector3, b: Vector3, out: Vector3): Vector3;
227227
export function subtract(a: Vector3, b: Vector3, out: Vector3): Vector3;
228228

229229
/**
230-
*
230+
* Multiply a 3-vector by a scalar.
231231
* @param {Vector3} vec
232232
* @param {Number} scalar
233233
* @example
@@ -238,7 +238,7 @@ export function subtract(a: Vector3, b: Vector3, out: Vector3): Vector3;
238238
export function multiplyScalar(vec: Vector3, scalar: number): Vector3;
239239

240240
/**
241-
*
241+
* Multiply a 3-vector by a scalar.
242242
* @param {Vector2} vec
243243
* @param {Number} scalar
244244
* @example
@@ -249,7 +249,7 @@ export function multiplyScalar(vec: Vector3, scalar: number): Vector3;
249249
export function multiplyScalar2D(vec: Vector2, scalar: number): Vector2;
250250

251251
/**
252-
*
252+
* Multiply two 3-vectors.
253253
* @param {Vector3} a
254254
* @param {Vector3} b
255255
* @param {Number} scalar
@@ -267,7 +267,7 @@ export function multiplyAccumulate(
267267
): Vector3;
268268

269269
/**
270-
*
270+
* Multiply two 2-vectors.
271271
* @param {Vector2} a
272272
* @param {Vector2} b
273273
* @param {Number} scalar
@@ -285,7 +285,7 @@ export function multiplyAccumulate2D(
285285
): Vector2;
286286

287287
/**
288-
*
288+
* Dot product of two 3-vectors.
289289
* @param {Vector3} x
290290
* @param {Vector3} y
291291
* @example
@@ -312,17 +312,23 @@ export function outer(x: Vector3, y: Vector3, out_3x3: Matrix3x3): void;
312312
export function cross(x: Vector3, y: Vector3, out: Vector3): Vector3;
313313

314314
/**
315-
*
316-
* @param {Number[]} x
317-
* @param {Number} n
315+
* Compute the norm of a vector.
316+
* @param {Number[]} x The vector to compute the norm of.
317+
* @param {Number} [n] The number of components to consider (default is 3).
318318
*/
319-
export function norm(x: number[], n: number): number;
319+
export function norm(x: number[], n?: number): number;
320320

321321
/**
322322
* Normalize in place. Returns norm.
323-
* @param {Vector3} x The vector to normlize.
323+
* @param {Number[]} x The vector to normalize.
324324
*/
325-
export function normalize(x: Vector3): number;
325+
export function normalize(x: number[]): number;
326+
327+
/**
328+
* Normalize in place. Returns norm.
329+
* @param {Number[]} x The vector to normalize.
330+
*/
331+
export function normalize4D(x: number[]): number;
326332

327333
/**
328334
* Given a unit vector v1, find two unit vectors v2 and v3 such that v1 cross v2 = v3
@@ -339,10 +345,10 @@ export function perpendiculars(
339345
): void;
340346

341347
/**
342-
*
343-
* @param {Vector3} a
344-
* @param {Vector3} b
345-
* @param {Vector3} projection
348+
* Project vector `a` onto vector `b` and returns the result in projection vector.
349+
* @param {Vector3} a The first 3D vector.
350+
* @param {Vector3} b The second 3D vector.
351+
* @param {Vector3} projection The projection 3D vector.
346352
*/
347353
export function projectVector(
348354
a: Vector3,
@@ -351,7 +357,7 @@ export function projectVector(
351357
): boolean;
352358

353359
/**
354-
*
360+
* Compute the dot product of two 2D vectors.
355361
* @param {Vector2} x
356362
* @param {Vector2} y
357363
*/

Sources/Common/Core/Math/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,17 @@ export function normalize(x) {
275275
return den;
276276
}
277277

278+
export function normalize4D(x) {
279+
const den = norm(x, 3);
280+
if (den !== 0.0) {
281+
x[0] /= den;
282+
x[1] /= den;
283+
x[2] /= den;
284+
x[3] /= den;
285+
}
286+
return den;
287+
}
288+
278289
export function perpendiculars(x, y, z, theta) {
279290
const x2 = x[0] * x[0];
280291
const y2 = x[1] * x[1];

Sources/Common/Core/Math/test/testMath.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,27 @@ test('Test transpose3x3', (t) => {
195195
t.end();
196196
});
197197

198+
test('Test normalize', (t) => {
199+
const x = [1, 2, 3];
200+
const res1 = vtkMath.normalize(x);
201+
t.ok(res1 === Math.sqrt(14), 'normalize x');
202+
t.ok(x[0] === 1 / Math.sqrt(14), 'normalize x[0]');
203+
t.ok(x[1] === 2 / Math.sqrt(14), 'normalize x[1]');
204+
t.ok(x[2] === 3 / Math.sqrt(14), 'normalize x[2]');
205+
t.end();
206+
});
207+
208+
test('Test normalize4D', (t) => {
209+
const x = [2, 3, 4, 5];
210+
const res1 = vtkMath.normalize4D(x);
211+
t.ok(res1 === Math.sqrt(29), 'normalize x');
212+
t.ok(x[0] === 2 / Math.sqrt(29), 'normalize4D x[0]');
213+
t.ok(x[1] === 3 / Math.sqrt(29), 'normalize4D x[1]');
214+
t.ok(x[2] === 4 / Math.sqrt(29), 'normalize4D x[2]');
215+
t.ok(x[3] === 5 / Math.sqrt(29), 'normalize4D x[3]');
216+
t.end();
217+
});
218+
198219
test('Test Multiply Matrix', (t) => {
199220
const nullMatrix = [0, 0, 0, 0];
200221
const m2x2 = [1, 1, 2, 4];

Sources/Rendering/Core/Camera/index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ export interface vtkCamera extends vtkObject {
156156
getFreezeFocalPoint(): boolean;
157157

158158
/**
159-
* Not implemented yet
160159
* Get the plane equations that bound the view frustum.
161-
* @param {Number} aspect Camera frustum aspect ratio.
160+
* @param {Number} [aspect] Camera frustum aspect ratio (default: 1.0).
161+
* @param {Float64Array} [planes] Optional array to fill with the plane equations.
162162
*/
163-
getFrustumPlanes(aspect: number): void;
163+
getFrustumPlanes(aspect?: number, planes?: Float64Array): Float64Array;
164164

165165
/**
166166
* Not implemented yet

Sources/Rendering/Core/Camera/index.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,44 @@ function vtkCamera(publicAPI, model) {
343343
publicAPI.setObliqueAngles = (alpha, beta) => {};
344344
publicAPI.getOrientation = () => {};
345345
publicAPI.getOrientationWXYZ = () => {};
346-
publicAPI.getFrustumPlanes = (aspect) => {
347-
// Return array of 24 params (4 params for each of 6 plane equations)
346+
347+
publicAPI.getFrustumPlanes = (
348+
aspect = 1.0,
349+
planes = new Float64Array(24)
350+
) => {
351+
const normals = [
352+
// Left
353+
[1, 0, 0, 1],
354+
// Right
355+
[-1, 0, 0, 1],
356+
// Bottom
357+
[0, 1, 0, 1],
358+
// Top
359+
[0, -1, 0, 1],
360+
// Near
361+
[0, 0, 1, 1],
362+
// Far
363+
[0, 0, -1, 1],
364+
];
365+
366+
// Get the composite projection matrix
367+
const matrix = publicAPI.getCompositeProjectionMatrix(aspect, -1, 1);
368+
369+
// Transform the normals to world coordinates
370+
for (let i = 0; i < 6; i++) {
371+
vec4.transformMat4(normals[i], normals[i], matrix);
372+
373+
// Normalize the plane normal
374+
vtkMath.normalize4D(normals[i]);
375+
376+
planes[4 * i + 0] = normals[i][0];
377+
planes[4 * i + 1] = normals[i][1];
378+
planes[4 * i + 2] = normals[i][2];
379+
planes[4 * i + 3] = normals[i][3];
380+
}
381+
return planes;
348382
};
383+
349384
publicAPI.getCameraLightTransformMatrix = (matrix) => {
350385
mat4.copy(matrix, model.cameraLightTransform);
351386
return matrix;

0 commit comments

Comments
 (0)