Skip to content

Commit 80a2f91

Browse files
committed
fix(Glyph3DMapper): use correct shift-scale transform
Previous function to apply shift and scale transform to the existing glyph matrix was incorrect because it did not account for scaling of rotation components. The glyph matrices can contain rotation components, which when combined with a shift-scale operation will get affected by scaling. It is therefore, better to use proper matrix multiplications rather than trying to recreate the effect through manipulation of individual components of a matrix.
1 parent 21037fc commit 80a2f91

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

Sources/Rendering/OpenGL/CellArrayBufferObject/helpers.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { mat4, quat, vec3 } from 'gl-matrix';
2+
13
export function computeCoordShiftAndScale(points) {
24
// Find out if shift scale should be used
35
// Compute squares of diagonal size and distance from the origin
@@ -41,4 +43,19 @@ export function computeCoordShiftAndScale(points) {
4143
};
4244
}
4345

44-
export default { computeCoordShiftAndScale };
46+
export function computeInverseShiftAndScaleMatrix(coordShift, coordScale) {
47+
const inverseScale = new Float64Array(3);
48+
vec3.inverse(inverseScale, coordScale);
49+
50+
const matrix = new Float64Array(16);
51+
mat4.fromRotationTranslationScale(
52+
matrix,
53+
quat.create(),
54+
coordShift,
55+
inverseScale
56+
);
57+
58+
return matrix;
59+
}
60+
61+
export default { computeCoordShiftAndScale, computeInverseShiftAndScaleMatrix };

Sources/Rendering/OpenGL/CellArrayBufferObject/index.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
1-
import { mat4, quat, vec3 } from 'gl-matrix';
1+
import { vec3 } from 'gl-matrix';
22

33
import macro from 'vtk.js/Sources/macros';
44
import vtkBufferObject from 'vtk.js/Sources/Rendering/OpenGL/BufferObject';
55
import { ObjectType } from 'vtk.js/Sources/Rendering/OpenGL/BufferObject/Constants';
66
import { Representation } from 'vtk.js/Sources/Rendering/Core/Property/Constants';
7-
import { computeCoordShiftAndScale } from 'vtk.js/Sources/Rendering/OpenGL/CellArrayBufferObject/helpers';
7+
import {
8+
computeCoordShiftAndScale,
9+
computeInverseShiftAndScaleMatrix,
10+
} from 'vtk.js/Sources/Rendering/OpenGL/CellArrayBufferObject/helpers';
811

912
const { vtkErrorMacro } = macro;
1013

1114
// ----------------------------------------------------------------------------
1215
// Static functions
1316
// ----------------------------------------------------------------------------
1417

15-
function computeInverseShiftAndScaleMatrix(coordShift, coordScale) {
16-
const inverseScale = new Float64Array(3);
17-
vec3.inverse(inverseScale, coordScale);
18-
19-
const matrix = new Float64Array(16);
20-
mat4.fromRotationTranslationScale(
21-
matrix,
22-
quat.create(),
23-
coordShift,
24-
inverseScale
25-
);
26-
27-
return matrix;
28-
}
29-
3018
function shouldApplyCoordShiftAndScale(coordShift, coordScale) {
3119
if (coordShift === null || coordScale === null) {
3220
return false;

Sources/Rendering/OpenGL/Glyph3DMapper/index.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import vtkHardwareSelector from 'vtk.js/Sources/Rendering/OpenGL/HardwareSelecto
77
import vtkProperty from 'vtk.js/Sources/Rendering/Core/Property';
88
import vtkOpenGLPolyDataMapper from 'vtk.js/Sources/Rendering/OpenGL/PolyDataMapper';
99
import vtkShaderProgram from 'vtk.js/Sources/Rendering/OpenGL/ShaderProgram';
10-
import { computeCoordShiftAndScale } from 'vtk.js/Sources/Rendering/OpenGL/CellArrayBufferObject/helpers';
10+
import {
11+
computeCoordShiftAndScale,
12+
computeInverseShiftAndScaleMatrix,
13+
} from 'vtk.js/Sources/Rendering/OpenGL/CellArrayBufferObject/helpers';
1114

1215
import { registerOverride } from 'vtk.js/Sources/Rendering/OpenGL/ViewNodeFactory';
1316
import { primTypes } from '../Helper';
@@ -23,16 +26,6 @@ const EndEvent = { type: 'EndEvent' };
2326
const MAT4_BYTE_SIZE = 64;
2427
const MAT4_ELEMENT_COUNT = 16;
2528

26-
function applyShiftScaleToMat(mat, shift, scale) {
27-
// the translation component of a 4x4 column-major matrix
28-
mat[12] = (mat[12] - shift[0]) * scale[0];
29-
mat[13] = (mat[13] - shift[1]) * scale[1];
30-
mat[14] = (mat[14] - shift[2]) * scale[2];
31-
mat[0] *= scale[0];
32-
mat[5] *= scale[1];
33-
mat[10] *= scale[2];
34-
}
35-
3629
// ----------------------------------------------------------------------------
3730
// vtkOpenGLSphereMapper methods
3831
// ----------------------------------------------------------------------------
@@ -714,13 +707,18 @@ function vtkOpenGLGlyph3DMapper(publicAPI, model) {
714707

715708
if (useShiftAndScale) {
716709
const buf = garray.buffer;
710+
const shiftScaleMat = computeInverseShiftAndScaleMatrix(
711+
coordShift,
712+
coordScale
713+
);
714+
mat4.invert(shiftScaleMat, shiftScaleMat);
717715
for (
718716
let ptIdx = 0;
719717
ptIdx < garray.byteLength;
720718
ptIdx += MAT4_BYTE_SIZE
721719
) {
722720
const mat = new Float32Array(buf, ptIdx, MAT4_ELEMENT_COUNT);
723-
applyShiftScaleToMat(mat, coordShift, coordScale);
721+
mat4.multiply(mat, shiftScaleMat, mat);
724722
}
725723
}
726724

0 commit comments

Comments
 (0)