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 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 | import type { ICamera, Point2, Point3 } from '../../../types';
import ResolvedViewportView from '../ResolvedViewportView';
import type {
WSIImageDataMetadata,
WSIMapViewLike,
} from '../../../utilities/WSIUtilities';
import {
canvasToIndexForWSI,
indexToCanvasForWSI,
indexToWorldWSIMetadata,
worldToIndexWSIMetadata,
} from './wsiTransformUtils';
import type { WSIViewState } from './WSIViewportTypes';
type WSIResolvedViewState = {
viewState: WSIViewState;
canvasHeight: number;
canvasWidth: number;
frameOfReferenceUID?: string | null;
metadata?: WSIImageDataMetadata;
view: WSIMapViewLike;
};
class WSIResolvedView extends ResolvedViewportView<WSIResolvedViewState> {
canvasToWorld(canvasPos: Point2): Point3 {
const indexPoint = canvasToIndexForWSI({
canvasHeight: this.state.canvasHeight,
canvasPos,
canvasWidth: this.state.canvasWidth,
view: this.state.view,
});
return indexToWorldWSIMetadata(this.state.metadata, indexPoint);
}
worldToCanvas(worldPos: Point3): Point2 {
return indexToCanvasForWSI({
canvasHeight: this.state.canvasHeight,
canvasWidth: this.state.canvasWidth,
indexPos: worldToIndexWSIMetadata(this.state.metadata, worldPos),
view: this.state.view,
});
}
getFrameOfReferenceUID(): string | undefined {
return this.state.frameOfReferenceUID || undefined;
}
withZoom(zoom: number, canvasPoint?: Point2): WSIResolvedView {
const nextZoom = Math.max(zoom, 0.001);
const nextResolution = this.getResolutionForZoom(nextZoom);
if (!canvasPoint) {
return this.cloneWithViewState({
...this.state.viewState,
resolution: nextResolution,
zoom: nextZoom,
});
}
const indexPoint = this.canvasToIndex(canvasPoint);
return this.cloneWithViewState({
...this.state.viewState,
centerIndex: this.getCenterIndexForCanvasPoint({
canvasPoint,
indexPoint,
resolution: nextResolution,
}),
resolution: nextResolution,
zoom: nextZoom,
});
}
protected buildICamera(): ICamera {
const focalPoint = this.canvasToWorld([
this.state.canvasWidth / 2,
this.state.canvasHeight / 2,
]);
const xSpacing = this.state.metadata?.spacing?.[0] || 1;
const resolution = this.getResolution();
return {
parallelProjection: true,
focalPoint,
position: focalPoint,
viewUp: [0, -1, 0],
parallelScale: this.state.canvasHeight * resolution * xSpacing,
viewPlaneNormal: [0, 0, 1],
rotation:
this.state.view.getRotation?.() || this.state.viewState.rotation || 0,
};
}
private canvasToIndex(canvasPos: Point2): Point2 {
const indexPoint = canvasToIndexForWSI({
canvasHeight: this.state.canvasHeight,
canvasPos,
canvasWidth: this.state.canvasWidth,
view: this.state.view,
});
return [indexPoint[0], indexPoint[1]];
}
private getResolution(): number {
return (
this.state.view.getResolution?.() || this.state.viewState.resolution || 1
);
}
private getResolutionForZoom(zoom: number): number {
const viewZoom =
this.state.view.getZoom?.() || this.state.viewState.zoom || 1;
return (
this.state.view.getResolutionForZoom?.(zoom) ??
this.getResolution() / Math.pow(2, zoom - viewZoom)
);
}
private getCenterIndexForCanvasPoint(args: {
canvasPoint: Point2;
indexPoint: Point2;
resolution: number;
}): [number, number] {
const { canvasPoint, indexPoint, resolution } = args;
const pixelRatio =
typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
const halfCanvasX = Math.max(this.state.canvasWidth, 1) / 2;
const halfCanvasY = Math.max(this.state.canvasHeight, 1) / 2;
const deltaCanvasX = canvasPoint[0] * pixelRatio - halfCanvasX;
const deltaCanvasY = canvasPoint[1] * pixelRatio - halfCanvasY;
const rotation =
this.state.view.getRotation?.() || this.state.viewState.rotation || 0;
const cos = Math.cos(rotation);
const sin = Math.sin(rotation);
const rotatedDeltaX = cos * deltaCanvasX + sin * deltaCanvasY;
const rotatedDeltaY = -sin * deltaCanvasX + cos * deltaCanvasY;
return [
indexPoint[0] - rotatedDeltaX * resolution,
indexPoint[1] + rotatedDeltaY * resolution,
];
}
private cloneWithViewState(viewState: WSIViewState): WSIResolvedView {
return new WSIResolvedView({
...this.state,
viewState,
});
}
}
export type { WSIResolvedViewState };
export default WSIResolvedView;
|