import { vec3 } from 'gl-matrix'; import vtkPlane from 'vtk.js/Sources/Common/DataModel/Plane';
function doPicking(p1, p2, mapper) { const imageData = mapper.getCurrentImage(); const extent = imageData.getExtent();
const ijk = [extent[0], extent[2], extent[4]]; const { ijkMode } = mapper.getClosestIJKAxis(); let nSlice = mapper.isA('vtkImageArrayMapper') ? mapper.getSubSlice() : mapper.getSlice(); if (ijkMode !== mapper.getSlicingMode()) { nSlice = mapper.getSliceAtPosition(nSlice); } ijk[ijkMode] += nSlice; const worldOrigin = [0, 0, 0]; imageData.indexToWorld(ijk, worldOrigin);
ijk[ijkMode] += 1; const worldNormal = [0, 0, 0]; imageData.indexToWorld(ijk, worldNormal); worldNormal[0] -= worldOrigin[0]; worldNormal[1] -= worldOrigin[1]; worldNormal[2] -= worldOrigin[2]; vec3.normalize(worldNormal, worldNormal);
const intersect = vtkPlane.intersectWithLine( p1, p2, worldOrigin, worldNormal ); if (intersect.intersection) { const point = intersect.x; const absoluteIJK = [0, 0, 0]; imageData.worldToIndex(point, absoluteIJK); return { t: intersect.t, absoluteIJK, }; } return null; }
export function intersectWithLineForPointPicking(p1, p2, mapper) { const pickingData = doPicking(p1, p2, mapper); if (pickingData) { const imageData = mapper.getCurrentImage(); const extent = imageData.getExtent();
const ijk = [ Math.round(pickingData.absoluteIJK[0]), Math.round(pickingData.absoluteIJK[1]), Math.round(pickingData.absoluteIJK[2]), ];
if ( ijk[0] < extent[0] || ijk[0] > extent[1] || ijk[1] < extent[2] || ijk[1] > extent[3] || ijk[2] < extent[4] || ijk[2] > extent[5] ) { return null; }
return { t: pickingData.t, ijk, }; } return null; }
export function intersectWithLineForCellPicking(p1, p2, mapper) { const pickingData = doPicking(p1, p2, mapper); if (pickingData) { const imageData = mapper.getCurrentImage(); const extent = imageData.getExtent(); const absIJK = pickingData.absoluteIJK;
const ijk = [ Math.floor(absIJK[0]), Math.floor(absIJK[1]), Math.floor(absIJK[2]), ];
if ( ijk[0] < extent[0] || ijk[0] > extent[1] - 1 || ijk[1] < extent[2] || ijk[1] > extent[3] - 1 || ijk[2] < extent[4] || ijk[2] > (extent[5] ? extent[5] - 1 : extent[5]) ) { return null; }
const pCoords = [ absIJK[0] - ijk[0], absIJK[1] - ijk[1], absIJK[2] - ijk[2], ];
return { t: pickingData.t, ijk, pCoords, }; } return null; }
|