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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 160x 800x 800x 800x 5600x 5600x 5600x 39200x 39200x 39200x 11680x 11680x 11680x 11680x 11680x 160x 952x 952x 952x 952x 952x 3942x 3942x 252448x 252448x 22398806x 22398806x 22398806x 11477338x 11477338x 11477338x 11477338x 952x | import type { vec3 } from 'gl-matrix'; import type { vtkImageData } from '@kitware/vtk.js/Common/DataModel/ImageData'; import type BoundsIJK from '../types/BoundsIJK'; import type { CPUImageData, Point3 } from '../types'; import { createPositionCallback } from './createPositionCallback'; export type PointInShape = { value: number; index: number; pointIJK: vec3; pointLPS: vec3 | number[]; }; export type PointInShapeCallback = ({ value, index, pointIJK, pointLPS, }: { value: number; index: number; pointIJK: Point3; pointLPS: Point3; }) => void; export type ShapeFnCriteria = (pointLPS: vec3, pointIJK: vec3) => boolean; /** * Options for the pointInShapeCallback function. */ export interface PointInShapeOptions { /** Function to determine if a point is inside the shape */ pointInShapeFn: ShapeFnCriteria; /** Callback function for each point in the shape */ callback?: PointInShapeCallback; /** Bounds of the volume in IJK coordinates */ boundsIJK?: BoundsIJK; /** Whether to return the set of points in the shape */ returnPoints?: boolean; // Add other options here for future optimizations // For example: // orthogonalVector?: vec3; // basisPoints?: { min: Point3, max: Point3 }; // minMaxGenerator?: (row: number) => { min: number, max: number }; } /** * For each point in the image (If boundsIJK is not provided, otherwise, for each * point in the provided bounding box), It runs the provided callback IF the point * passes the provided criteria to be inside the shape (which is defined by the * provided pointInShapeFn) * * You must record points in the callback function if you wish to have an array * of the called points. * * @param imageData - The image data object. * @param dimensions - The dimensions of the image. * @param pointInShapeFn - A function that takes a point in LPS space and returns * true if the point is in the shape and false if it is not. * @param callback - A function that will be called for * every point in the shape. * @param boundsIJK - The bounds of the volume in IJK coordinates. */ export function pointInShapeCallback( imageData: vtkImageData | CPUImageData, options: PointInShapeOptions ): Array<PointInShape> | undefined { const { pointInShapeFn, callback, boundsIJK, returnPoints = false } = options; let scalarData; // if getScalarData is a method on imageData Iif ((imageData as CPUImageData).getScalarData) { scalarData = (imageData as CPUImageData).getScalarData(); } else { const scalars = (imageData as vtkImageData).getPointData().getScalars(); Iif (scalars) { scalarData = scalars.getData(); } else { // @ts-ignore const { voxelManager } = imageData.get('voxelManager') || {}; Eif (voxelManager) { scalarData = voxelManager.getCompleteScalarDataArray(); } } } const dimensions = imageData.getDimensions(); const defaultBoundsIJK = [ [0, dimensions[0]], [0, dimensions[1]], [0, dimensions[2]], ]; const bounds = boundsIJK || defaultBoundsIJK; const pointsInShape = iterateOverPointsInShape({ imageData, bounds, scalarData, pointInShapeFn, callback, }); return returnPoints ? pointsInShape : undefined; } export function iterateOverPointsInShape({ imageData, bounds, scalarData, pointInShapeFn, callback, }) { const [[iMin, iMax], [jMin, jMax], [kMin, kMax]] = bounds; const { numComps } = imageData as { numComps: number }; const dimensions = imageData.getDimensions(); const indexToWorld = createPositionCallback(imageData); const pointIJK = [0, 0, 0] as Point3; const xMultiple = numComps || scalarData.length / dimensions[2] / dimensions[1] / dimensions[0]; const yMultiple = dimensions[0] * xMultiple; const zMultiple = dimensions[1] * yMultiple; const pointsInShape: Array<PointInShape> = []; for (let k = kMin; k <= kMax; k++) { pointIJK[2] = k; const indexK = k * zMultiple; for (let j = jMin; j <= jMax; j++) { pointIJK[1] = j; const indexJK = indexK + j * yMultiple; for (let i = iMin; i <= iMax; i++) { pointIJK[0] = i; const pointLPS = indexToWorld(pointIJK); // The current world position (pointLPS) is now in currentPos if (pointInShapeFn(pointLPS, pointIJK)) { const index = indexJK + i * xMultiple; let value; Iif (xMultiple > 2) { value = [ scalarData[index], scalarData[index + 1], scalarData[index + 2], ]; } else { value = scalarData[index]; } pointsInShape.push({ value, index, pointIJK, pointLPS: pointLPS.slice(), }); callback({ value, index, pointIJK, pointLPS }); } } } } return pointsInShape; } export function iterateOverPointsInShapeVoxelManager({ voxelManager, bounds, imageData, pointInShapeFn, callback, returnPoints, }) { const [[iMin, iMax], [jMin, jMax], [kMin, kMax]] = bounds; const indexToWorld = createPositionCallback(imageData); const pointIJK = [0, 0, 0] as Point3; const pointsInShape: Array<PointInShape> = []; for (let k = kMin; k <= kMax; k++) { pointIJK[2] = k; for (let j = jMin; j <= jMax; j++) { pointIJK[1] = j; for (let i = iMin; i <= iMax; i++) { pointIJK[0] = i; const pointLPS = indexToWorld(pointIJK); if (pointInShapeFn(pointLPS, pointIJK)) { const index = voxelManager.toIndex(pointIJK); const value = voxelManager.getAtIndex(index); Iif (returnPoints) { pointsInShape.push({ value, index, pointIJK: [...pointIJK], pointLPS: pointLPS.slice(), }); } callback?.({ value, index, pointIJK, pointLPS }); } } } } return pointsInShape; } |