Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | import { mat4, vec3 } from 'gl-matrix';
import { utilities as csUtils } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
/**
* Rotates a viewport camera (position, focal point and viewUp) around a world
* pivot point by the given angle (radians) about the given axis.
*
* LEGACY VIEWPORTS ONLY: this utility works through the free `setCamera`
* orientation write that only legacy (stack/volume) viewports expose. Native
* generic viewports have no such write and are left untouched (returns
* false); rotate those through the view-reference API instead, the way
* SliceIntersectionTool does (`setViewReference` with a rotated
* viewPlaneNormal/viewUp around the pivot).
*
* The transform is rigid: the camera-to-focal-point distance and the
* orthogonality of the camera basis are preserved.
*
* Returns true when the camera was rotated.
*/
export default function rotateViewportAroundWorldPoint(
viewport: Types.IViewport,
pivot: Types.Point3,
axis: Types.Point3,
angle: number
): boolean {
if (!viewport || !pivot || !axis || !Number.isFinite(angle) || angle === 0) {
return false;
}
if (csUtils.isGenericViewport(viewport)) {
return false;
}
const normalizedAxis = vec3.normalize(vec3.create(), axis);
if (vec3.length(normalizedAxis) < 1e-10) {
return false;
}
const camera = viewport.getCamera?.();
const { position, focalPoint, viewUp } = camera ?? {};
if (!position || !focalPoint || !viewUp) {
return false;
}
const transform = mat4.create();
mat4.translate(transform, transform, pivot);
mat4.rotate(transform, transform, angle, normalizedAxis);
mat4.translate(transform, transform, [-pivot[0], -pivot[1], -pivot[2]]);
const newPosition = vec3.transformMat4(vec3.create(), position, transform);
const newFocalPoint = vec3.transformMat4(
vec3.create(),
focalPoint,
transform
);
// viewUp is a direction: transform the point (position + viewUp) and
// re-derive the direction relative to the new position.
const viewUpPoint = vec3.add(vec3.create(), position, viewUp);
const newViewUpPoint = vec3.transformMat4(
vec3.create(),
viewUpPoint,
transform
);
const newViewUp = vec3.subtract(vec3.create(), newViewUpPoint, newPosition);
viewport.setCamera({
position: [newPosition[0], newPosition[1], newPosition[2]],
focalPoint: [newFocalPoint[0], newFocalPoint[1], newFocalPoint[2]],
viewUp: [newViewUp[0], newViewUp[1], newViewUp[2]],
});
return true;
}
|