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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | 1x 1x 1x 1x 1x | import macro from 'vtk.js/Sources/macros'; import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData'; import vtkPolyData from 'vtk.js/Sources/Common/DataModel/PolyData'; import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray'; const { vtkErrorMacro } = macro; // see itk.js PixelTypes.js const ITKJSPixelTypes = { Unknown: 0, Scalar: 1, RGB: 2, RGBA: 3, Offset: 4, Vector: 5, Point: 6, CovariantVector: 7, SymmetricSecondRankTensor: 8, DiffusionTensor3D: 9, Complex: 10, FixedArray: 11, Array: 12, Matrix: 13, VariableLengthVector: 14, VariableSizeMatrix: 15, }; // itk-wasm pixel types from https://github.com/InsightSoftwareConsortium/itk-wasm/blob/master/src/core/PixelTypes.ts const ITKWASMPixelTypes = { Unknown: 'Unknown', Scalar: 'Scalar', RGB: 'RGB', RGBA: 'RGBA', Offset: 'Offset', Vector: 'Vector', Point: 'Point', CovariantVector: 'CovariantVector', SymmetricSecondRankTensor: 'SymmetricSecondRankTensor', DiffusionTensor3D: 'DiffusionTensor3D', Complex: 'Complex', FixedArray: 'FixedArray', Array: 'Array', Matrix: 'Matrix', VariableLengthVector: 'VariableLengthVector', VariableSizeMatrix: 'VariableSizeMatrix', }; const vtkArrayTypeToItkComponentType = new Map([ ['Uint8Array', 'uint8'], ['Int8Array', 'int8'], ['Uint16Array', 'uint16'], ['Int16Array', 'int16'], ['Uint32Array', 'uint32'], ['Int32Array', 'int32'], ['Float32Array', 'float32'], ['Float64Array', 'float64'], ]); const itkComponentTypeToVtkArrayType = new Map([ ['uint8', 'Uint8Array'], ['int8', 'Int8Array'], ['uint16', 'Uint16Array'], ['int16', 'Int16Array'], ['uint32', 'Uint32Array'], ['int32', 'Int32Array'], ['float32', 'Float32Array'], ['float64', 'Float64Array'], ]); /** * Converts an itk-wasm Image to a vtk.js vtkImageData. * * Requires an itk-wasm Image as input. */ export function convertItkToVtkImage(itkImage, options = {}) { const vtkImage = { origin: [0, 0, 0], spacing: [1, 1, 1], }; const dimensions = [1, 1, 1]; const direction = [1, 0, 0, 0, 1, 0, 0, 0, 1]; // Check whether itkImage is an itk.js Image or an itk-wasm Image? const isITKWasm = itkImage.direction.data === undefined; const ITKPixelTypes = isITKWasm ? ITKWASMPixelTypes : ITKJSPixelTypes; for (let idx = 0; idx < itkImage.imageType.dimension; ++idx) { vtkImage.origin[idx] = itkImage.origin[idx]; vtkImage.spacing[idx] = itkImage.spacing[idx]; dimensions[idx] = itkImage.size[idx]; for (let col = 0; col < itkImage.imageType.dimension; ++col) { // ITK (and VTKMath) use a row-major index axis, but the direction // matrix on the vtkImageData is a webGL matrix, which uses a // column-major data layout. Transpose the direction matrix from // itkImage when instantiating that vtkImageData direction matrix. if (isITKWasm) { direction[col + idx * 3] = itkImage.direction[idx + col * itkImage.imageType.dimension]; } else { direction[col + idx * 3] = itkImage.direction.data[idx + col * itkImage.imageType.dimension]; } } } // Create VTK Image Data const imageData = vtkImageData.newInstance(vtkImage); // Create VTK point data -- the data associated with the pixels / voxels const pointData = vtkDataArray.newInstance({ name: options.scalarArrayName || 'Scalars', values: itkImage.data, numberOfComponents: itkImage.imageType.components, }); imageData.setDirection(direction); imageData.setDimensions(...dimensions); // Always associate multi-component pixel types with vtk.js point data // scalars to facilitate multi-component volume rendering imageData.getPointData().setScalars(pointData); // Associate the point data that are 3D vectors / tensors // Refer to itk-js/src/PixelTypes.js for numerical values switch ( isITKWasm ? ITKPixelTypes[itkImage.imageType.pixelType] : itkImage.imageType.pixelType ) { case ITKPixelTypes.Scalar: break; case ITKPixelTypes.RGB: break; case ITKPixelTypes.RGBA: break; case ITKPixelTypes.Offset: break; case ITKPixelTypes.Vector: if ( itkImage.imageType.dimension === 3 && itkImage.imageType.components === 3 ) { imageData.getPointData().setVectors(pointData); } break; case ITKPixelTypes.Point: break; case ITKPixelTypes.CovariantVector: if ( itkImage.imageType.dimension === 3 && itkImage.imageType.components === 3 ) { imageData.getPointData().setVectors(pointData); } break; case ITKPixelTypes.SymmetricSecondRankTensor: if ( itkImage.imageType.dimension === 3 && itkImage.imageType.components === 6 ) { imageData.getPointData().setTensors(pointData); } break; case ITKPixelTypes.DiffusionTensor3D: if ( itkImage.imageType.dimension === 3 && itkImage.imageType.components === 6 ) { imageData.getPointData().setTensors(pointData); } break; case ITKPixelTypes.Complex: break; case ITKPixelTypes.FixedArray: break; case ITKPixelTypes.Array: break; case ITKPixelTypes.Matrix: break; case ITKPixelTypes.VariableLengthVector: break; case ITKPixelTypes.VariableSizeMatrix: break; default: vtkErrorMacro( `Cannot handle unexpected itk-wasm pixel type ${itkImage.imageType.pixelType}` ); return null; } return imageData; } /** * Converts a vtk.js vtkImageData to an itk-wasm Image. * * Requires a vtk.js vtkImageData as input. * */ export function convertVtkToItkImage(vtkImage, copyData = false) { const dimension = 3; const itkImage = { imageType: { dimension, pixelType: ITKWASMPixelTypes.Scalar, componentType: '', components: 1, }, name: 'vtkImageData', origin: vtkImage.getOrigin(), spacing: vtkImage.getSpacing(), direction: new Float64Array(9), size: vtkImage.getDimensions(), }; const direction = vtkImage.getDirection(); // Transpose the direction matrix from column-major to row-major for (let idx = 0; idx < dimension; ++idx) { for (let idy = 0; idy < dimension; ++idy) { itkImage.direction[idx + idy * dimension] = direction[idy + idx * dimension]; } } const pointData = vtkImage.getPointData(); let vtkArray; if (pointData.getTensors() !== null) { itkImage.imageType.pixelType = ITKWASMPixelTypes.DiffusionTensor3D; vtkArray = pointData.getTensors(); } else if (pointData.getVectors() != null) { itkImage.imageType.pixelType = ITKWASMPixelTypes.Vector; vtkArray = pointData.getVectors(); } else { vtkArray = pointData.getScalars(); } itkImage.imageType.componentType = vtkArrayTypeToItkComponentType.get( vtkArray.getDataType() ); if (copyData) { // Copy the data array itkImage.data = vtkArray.getData().slice(0); } else { itkImage.data = vtkArray.getData(); } return itkImage; } /** * Converts an itk-wasm PolyData to a vtk.js vtkPolyData. * * Requires an itk-wasm PolyData as input. */ export function convertItkToVtkPolyData(itkPolyData, options = {}) { const pointDataArrays = []; if (itkPolyData.pointData.length) { pointDataArrays.push({ data: { vtkClass: 'vtkDataArray', name: options.pointDataName || 'PointData', numberOfComponents: itkPolyData.polyDataType.pointPixelComponents, size: itkPolyData.pointData.length, dataType: itkComponentTypeToVtkArrayType.get( itkPolyData.polyDataType.pointPixelComponentType ), buffer: itkPolyData.pointData.buffer, values: itkPolyData.pointData, }, }); } const cellDataArrays = []; if (itkPolyData.cellData.length) { cellDataArrays.push({ data: { vtkClass: 'vtkDataArray', name: options.cellDataName || 'CellData', numberOfComponents: itkPolyData.polyDataType.pointPixelComponents, size: itkPolyData.cellData.length, dataType: itkComponentTypeToVtkArrayType.get( itkPolyData.polyDataType.pointPixelComponentType ), buffer: itkPolyData.cellData.buffer, values: itkPolyData.cellData, }, }); } const vtkPolyDataModel = { points: { vtkClass: 'vtkPoints', name: '_points', numberOfComponents: 3, size: itkPolyData.points.length, dataType: 'Float32Array', buffer: itkPolyData.points.buffer, values: itkPolyData.points, }, verts: { vtkClass: 'vtkCellArray', name: '_verts', numberOfComponents: 1, size: itkPolyData.verticesBufferSize, dataType: 'Uint32Array', buffer: itkPolyData.vertices.buffer, values: itkPolyData.vertices, }, lines: { vtkClass: 'vtkCellArray', name: '_lines', numberOfComponents: 1, size: itkPolyData.linesBufferSize, dataType: 'Uint32Array', buffer: itkPolyData.lines.buffer, values: itkPolyData.lines, }, polys: { vtkClass: 'vtkCellArray', name: '_polys', numberOfComponents: 1, size: itkPolyData.polygonsBufferSize, dataType: 'Uint32Array', buffer: itkPolyData.polygons.buffer, values: itkPolyData.polygons, }, strips: { vtkClass: 'vtkCellArray', name: '_strips', numberOfComponents: 1, size: itkPolyData.triangleStripsBufferSize, dataType: 'Uint32Array', buffer: itkPolyData.triangleStrips.buffer, values: itkPolyData.triangleStrips, }, pointData: { vtkClass: 'vtkDataSetAttributes', activeGlobalIds: -1, activeNormals: -1, activePedigreeIds: -1, activeScalars: -1, activeTCoords: -1, activeTensors: -1, activeVectors: -1, copyFieldFlags: [], doCopyAllOff: false, doCopyAllOn: true, arrays: pointDataArrays, }, cellData: { vtkClass: 'vtkDataSetAttributes', activeGlobalIds: -1, activeNormals: -1, activePedigreeIds: -1, activeScalars: -1, activeTCoords: -1, activeTensors: -1, activeVectors: -1, copyFieldFlags: [], doCopyAllOff: false, doCopyAllOn: true, arrays: cellDataArrays, }, }; // Create VTK PolyData const polyData = vtkPolyData.newInstance(vtkPolyDataModel); const pd = polyData.getPointData(); const cd = polyData.getCellData(); if (itkPolyData.pointData.length) { // Associate the point data that are 3D vectors / tensors switch (ITKWASMPixelTypes[itkPolyData.polyDataType.pointPixelType]) { case ITKWASMPixelTypes.Scalar: pd.setScalars(pd.getArrayByIndex(0)); break; case ITKWASMPixelTypes.RGB: break; case ITKWASMPixelTypes.RGBA: break; case ITKWASMPixelTypes.Offset: break; case ITKWASMPixelTypes.Vector: if (itkPolyData.polyDataType.pointPixelComponents === 3) { pd.setVectors(pd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.Point: break; case ITKWASMPixelTypes.CovariantVector: if (itkPolyData.polyDataType.pointPixelComponents === 3) { pd.setVectors(pd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.SymmetricSecondRankTensor: if (itkPolyData.polyDataType.pointPixelComponents === 6) { pd.setTensors(pd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.DiffusionTensor3D: if (itkPolyData.polyDataType.pointPixelComponents === 6) { pd.setTensors(pd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.Complex: break; case ITKWASMPixelTypes.FixedArray: break; case ITKWASMPixelTypes.Array: break; case ITKWASMPixelTypes.Matrix: break; case ITKWASMPixelTypes.VariableLengthVector: break; case ITKWASMPixelTypes.VariableSizeMatrix: break; default: vtkErrorMacro( `Cannot handle unexpected itk-wasm pixel type ${itkPolyData.polyDataType.pointPixelType}` ); return null; } } if (itkPolyData.cellData.length) { // Associate the cell data that are 3D vectors / tensors switch (ITKWASMPixelTypes[itkPolyData.polyDataType.cellPixelType]) { case ITKWASMPixelTypes.Scalar: cd.setScalars(cd.getArrayByIndex(0)); break; case ITKWASMPixelTypes.RGB: break; case ITKWASMPixelTypes.RGBA: break; case ITKWASMPixelTypes.Offset: break; case ITKWASMPixelTypes.Vector: if (itkPolyData.polyDataType.pointPixelComponents === 3) { cd.setVectors(cd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.Point: break; case ITKWASMPixelTypes.CovariantVector: if (itkPolyData.polyDataType.pointPixelComponents === 3) { cd.setVectors(cd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.SymmetricSecondRankTensor: if (itkPolyData.polyDataType.pointPixelComponents === 6) { cd.setTensors(cd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.DiffusionTensor3D: if (itkPolyData.polyDataType.pointPixelComponents === 6) { cd.setTensors(cd.getArrayByIndex(0)); } break; case ITKWASMPixelTypes.Complex: break; case ITKWASMPixelTypes.FixedArray: break; case ITKWASMPixelTypes.Array: break; case ITKWASMPixelTypes.Matrix: break; case ITKWASMPixelTypes.VariableLengthVector: break; case ITKWASMPixelTypes.VariableSizeMatrix: break; default: vtkErrorMacro( `Cannot handle unexpected itk-wasm pixel type ${itkPolyData.polyDataType.pointPixelType}` ); return null; } } return polyData; } /** * Converts a vtk.js vtkPolyData to an itk-wasm PolyData. * * Requires a vtk.js vtkPolyData as input. * */ export function convertVtkToItkPolyData(polyData, options = {}) { const itkPolyData = { polyDataType: { pointPixelComponentType: 'float32', pointPixelComponents: 1, pointPixelType: 'Scalar', cellPixelComponentType: 'float32', cellPixelComponents: 1, cellPixelType: 'Scalar', }, numberOfPoints: polyData.getNumberOfPoints(), points: polyData.getPoints().getData(), verticesBufferSize: polyData.getVerts().getNumberOfValues(), vertices: polyData.getVerts().getData(), linesBufferSize: polyData.getLines().getNumberOfValues(), lines: polyData.getLines().getData(), polygonsBufferSize: polyData.getPolys().getNumberOfValues(), polygons: polyData.getPolys().getData(), triangleStripsBufferSize: polyData.getStrips().getNumberOfValues(), triangleStrips: polyData.getStrips().getData(), numberOfPointPixels: 0, pointData: new Float32Array(), numberOfCellPixels: 0, cellData: new Float32Array(), }; const pd = polyData.getPointData(); if (pd.getNumberOfArrays()) { const pdArray = options.pointDataName ? pd.getArrayByName(options.pointDataName) : pd.getArrayByIndex(0); itkPolyData.numberOfPointPixels = pdArray.getNumberOfTuples(); itkPolyData.pointData = pdArray.getData(); itkPolyData.polyDataType.pointPixelComponentType = vtkArrayTypeToItkComponentType.get(pdArray.getDataType()); // default to the same type itkPolyData.polyDataType.cellPixelComponentType = itkPolyData.polyDataType.pointPixelComponentType; itkPolyData.polyDataType.pointPixelComponents = pdArray.getNumberOfComponents(); itkPolyData.polyDataType.cellPixelComponents = itkPolyData.polyDataType.pointPixelComponents; if (pd.getTensors() === pdArray) { itkPolyData.polyDataType.pointPixelType = ITKWASMPixelTypes.SymmetricSecondRankTensor; } else if (pd.getVectors() === pdArray) { itkPolyData.polyDataType.pointPixelType = ITKWASMPixelTypes.Vector; } itkPolyData.polyDataType.cellPixelType = itkPolyData.polyDataType.pointPixelType; } const cd = polyData.getCellData(); if (cd.getNumberOfArrays()) { const cdArray = options.cellDataName ? pd.getArrayByName(options.cellDataName) : pd.getArrayByIndex(0); itkPolyData.numberOfCellPixels = cdArray.getNumberOfTuples(); itkPolyData.cellData = cdArray.getData(); itkPolyData.polyDataType.cellPixelComponentType = vtkArrayTypeToItkComponentType.get(cdArray.getDataType()); itkPolyData.polyDataType.cellPixelComponents = cdArray.getNumberOfComponents(); if (cd.getTensors() === cdArray) { itkPolyData.polyDataType.cellPixelType = ITKWASMPixelTypes.SymmetricSecondRankTensor; } else if (cd.getVectors() === cdArray) { itkPolyData.polyDataType.cellPixelType = ITKWASMPixelTypes.Vector; } else { itkPolyData.polyDataType.cellPixelType = ITKWASMPixelTypes.Scalar; } } return itkPolyData; } export default { convertItkToVtkImage, convertVtkToItkImage, convertItkToVtkPolyData, convertVtkToItkPolyData, }; |