Skip to content

Commit

Permalink
Merge pull request #62 from dvisionlab/cropFix
Browse files Browse the repository at this point in the history
Crop fix
  • Loading branch information
ronzim authored Jul 23, 2021
2 parents 9ab4518 + a4816ce commit ecb43f1
Show file tree
Hide file tree
Showing 5 changed files with 6,100 additions and 15 deletions.
3,032 changes: 3,031 additions & 1 deletion dist/diglettk.js

Large diffs are not rendered by default.

3,032 changes: 3,031 additions & 1 deletion docs/examples/diglettk.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/examples/vr.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ <h3 style="margin-top: 20px; margin-bottom: 10px;">Code Example</h3>
let data = larvitar.buildData(serie, false);
console.log(serie)
const image = diglettk.buildVtkVolume(header, data);
image.setOrigin([0,0,0]) // little hack to temporarily fix https://github.com/Kitware/vtk-js/issues/2008
// run vr
vr = new diglettk.VRView(element);
// set an image
Expand Down
29 changes: 29 additions & 0 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import vtkPlane from "vtk.js/Sources/Common/DataModel/Plane";
import vtkVolume from "vtk.js/Sources/Rendering/Core/Volume";
import vtkVolumeMapper from "vtk.js/Sources/Rendering/Core/VolumeMapper";

import { vec3, quat, mat4 } from "gl-matrix";

export function buildVtkVolume(header, data) {
const dims = [
header.volume.cols,
Expand Down Expand Up @@ -304,3 +306,30 @@ export function getVideoCardInfo() {
error: "no WEBGL_debug_renderer_info"
};
}

export function getCroppingPlanes(imageData, ijkPlanes) {
const rotation = quat.create();
mat4.getRotation(rotation, imageData.getIndexToWorld());

const rotateVec = vec => {
const out = [0, 0, 0];
vec3.transformQuat(out, vec, rotation);
return out;
};

const [iMin, iMax, jMin, jMax, kMin, kMax] = ijkPlanes;
const origin = imageData.indexToWorld([iMin, jMin, kMin]);
// opposite corner from origin
const corner = imageData.indexToWorld([iMax, jMax, kMax]);
return [
// X min/max
vtkPlane.newInstance({ normal: rotateVec([1, 0, 0]), origin }),
vtkPlane.newInstance({ normal: rotateVec([-1, 0, 0]), origin: corner }),
// Y min/max
vtkPlane.newInstance({ normal: rotateVec([0, 1, 0]), origin }),
vtkPlane.newInstance({ normal: rotateVec([0, -1, 0]), origin: corner }),
// X min/max
vtkPlane.newInstance({ normal: rotateVec([0, 0, 1]), origin }),
vtkPlane.newInstance({ normal: rotateVec([0, 0, -1]), origin: corner })
];
}
21 changes: 8 additions & 13 deletions src/vrView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import vtkActor from "vtk.js/Sources/Rendering/Core/Actor";
import vtkSphereSource from "vtk.js/Sources/Filters/Sources/SphereSource";
import vtkCoordinate from "vtk.js/Sources/Rendering/Core/Coordinate";

import { createVolumeActor } from "./utils/utils";
import { createVolumeActor, getCroppingPlanes } from "./utils/utils";
import { applyStrategy } from "./utils/strategies";

import { createPreset } from "./utils/colormaps";
Expand Down Expand Up @@ -404,28 +404,26 @@ export class VRView {
* Setup crop widget
*/
setupCropWidget() {
// setup widget manager and widget
const widgetManager = vtkWidgetManager.newInstance();
// widgetManager.setUseSvgLayer(false);
widgetManager.setRenderer(this.renderer);

// widget factory
const widget = vtkImageCroppingWidget.newInstance();
// instance of a widget associated with a renderer
const viewWidget = widgetManager.addWidget(widget);

// setup crop filter
const cropFilter = vtkImageCropFilter.newInstance();
// listen to cropping widget state to inform the crop filter
const cropState = widget.getWidgetState().getCroppingPlanes();
cropState.onModified(() => {
cropFilter.setCroppingPlanes(cropState.getPlanes());
const planes = getCroppingPlanes(image, cropState.getPlanes());
mapper.removeAllClippingPlanes();
planes.forEach(plane => {
mapper.addClippingPlane(plane);
});
mapper.modified();
});

// wire up the reader, crop filter, and mapper
let mapper = this.actor.getMapper();
let image = mapper.getInputData();
cropFilter.setCroppingPlanes(...image.getExtent());

widget.copyImageDataDescription(image);

widget.set({
Expand All @@ -434,9 +432,6 @@ export class VRView {
cornerHandlesEnabled: true
});

cropFilter.setInputData(image);
mapper.setInputConnection(cropFilter.getOutputPort());

widgetManager.enablePicking();

this._widgetManager = widgetManager;
Expand Down

0 comments on commit ecb43f1

Please sign in to comment.