11import { vec3 } from 'gl-matrix' ;
22import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData' ;
33import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray' ;
4+ import vtkMath from 'vtk.js/Sources/Common/Core/Math' ;
45
56import buildMetadata from './data/buildMetadata.js' ;
67import imageDataCache from './data/imageDataCache.js' ;
78import sortDatasetsByImagePosition from './data/sortDatasetsByImagePosition.js' ;
89
10+ //Tolerance for ImageOrientationPatient
11+ const iopTolerance = 1e-6 ;
12+
913export default function getImageData ( imageIds , displaySetInstanceUid ) {
1014 const cachedImageDataObject = imageDataCache . get ( displaySetInstanceUid ) ;
1115
@@ -15,11 +19,32 @@ export default function getImageData(imageIds, displaySetInstanceUid) {
1519
1620 const { metaData0, metaDataMap, imageMetaData0 } = buildMetadata ( imageIds ) ;
1721
18- const { rowCosines, columnCosines } = metaData0 ;
22+ let { rowCosines, columnCosines } = metaData0 ;
23+
24+ //correct for the 32bit float header issue before orthogonalizing
25+ //https://github.com/OHIF/Viewers/issues/2847
26+ for ( let i = 0 ; i < rowCosines . length ; i ++ ) {
27+ if ( Math . abs ( rowCosines [ i ] ) < iopTolerance ) {
28+ rowCosines [ i ] = 0 ;
29+ }
30+ if ( Math . abs ( columnCosines [ i ] ) < iopTolerance ) {
31+ columnCosines [ i ] = 0 ;
32+ }
33+ }
1934 const rowCosineVec = vec3 . fromValues ( ...rowCosines ) ;
2035 const colCosineVec = vec3 . fromValues ( ...columnCosines ) ;
2136 const scanAxisNormal = vec3 . cross ( [ ] , rowCosineVec , colCosineVec ) ;
2237
38+ let direction = [ rowCosineVec , colCosineVec , scanAxisNormal ] ;
39+ vtkMath . orthogonalize3x3 ( direction , direction ) ;
40+
41+ //setDirection expects orthogonal matrix
42+ const orthogonalizedDirection = [
43+ ...direction [ 0 ] ,
44+ ...direction [ 1 ] ,
45+ ...direction [ 2 ] ,
46+ ] ;
47+
2348 const { spacing, origin, sortedDatasets } = sortDatasetsByImagePosition (
2449 scanAxisNormal ,
2550 metaDataMap
@@ -65,11 +90,9 @@ export default function getImageData(imageIds, displaySetInstanceUid) {
6590 } ) ;
6691
6792 const imageData = vtkImageData . newInstance ( ) ;
68- const direction = [ ...rowCosineVec , ...colCosineVec , ...scanAxisNormal ] ;
69-
7093 imageData . setDimensions ( xVoxels , yVoxels , zVoxels ) ;
7194 imageData . setSpacing ( xSpacing , ySpacing , zSpacing ) ;
72- imageData . setDirection ( direction ) ;
95+ imageData . setDirection ( orthogonalizedDirection ) ;
7396 imageData . setOrigin ( ...origin ) ;
7497 imageData . getPointData ( ) . setScalars ( scalarArray ) ;
7598
0 commit comments