@@ -33,9 +33,9 @@ export default class SliceRepresentation extends Component {
3333 this . lookupTable . applyColorMap ( preset ) ;
3434 this . piecewiseFunction = vtkPiecewiseFunction . newInstance ( ) ;
3535 this . actor = vtkImageSlice . newInstance ( { visibility : false } ) ;
36- this . mapper = vtkImageMapper . newInstance ( ) ;
36+ // use the mapper instance if provided, otherwise create default instance.
37+ this . mapper = props . mapperInstance ?? vtkImageMapper . newInstance ( ) ;
3738 this . actor . setMapper ( this . mapper ) ;
38-
3939 this . actor . getProperty ( ) . setRGBTransferFunction ( 0 , this . lookupTable ) ;
4040 // this.actor.getProperty().setScalarOpacity(0, this.piecewiseFunction);
4141 this . actor . getProperty ( ) . setInterpolationTypeToLinear ( ) ;
@@ -105,7 +105,11 @@ export default class SliceRepresentation extends Component {
105105 if ( property && ( ! previous || property !== previous . property ) ) {
106106 changed = this . actor . getProperty ( ) . set ( property ) || changed ;
107107 }
108- if ( mapper && ( ! previous || mapper !== previous . mapper ) ) {
108+ if (
109+ mapper &&
110+ ( ! previous || mapper !== previous . mapper ) &&
111+ mapper !== this . mapper
112+ ) {
109113 changed = this . mapper . set ( mapper ) || changed ;
110114 }
111115 if (
@@ -146,25 +150,35 @@ export default class SliceRepresentation extends Component {
146150 }
147151 }
148152
149- // ijk
150- if ( iSlice != null && ( ! previous || iSlice !== previous . iSlice ) ) {
151- changed = this . mapper . setISlice ( iSlice ) || changed ;
152- }
153- if ( jSlice != null && ( ! previous || jSlice !== previous . jSlice ) ) {
154- changed = this . mapper . setJSlice ( jSlice ) || changed ;
155- }
156- if ( kSlice != null && ( ! previous || kSlice !== previous . kSlice ) ) {
157- changed = this . mapper . setKSlice ( kSlice ) || changed ;
158- }
159- // xyz
160- if ( xSlice != null && ( ! previous || xSlice !== previous . xSlice ) ) {
161- changed = this . mapper . setXSlice ( xSlice ) || changed ;
162- }
163- if ( ySlice != null && ( ! previous || ySlice !== previous . ySlice ) ) {
164- changed = this . mapper . setYSlice ( ySlice ) || changed ;
165- }
166- if ( zSlice != null && ( ! previous || zSlice !== previous . zSlice ) ) {
167- changed = this . mapper . setZSlice ( zSlice ) || changed ;
153+ // check if we have valid input
154+ if ( this . validData ) {
155+ if ( this . mapper . isA ( 'vtkImageMapper' ) ) {
156+ // ijk
157+ if ( iSlice != null && ( ! previous || iSlice !== previous . iSlice ) ) {
158+ changed = this . mapper . setISlice ( iSlice ) || changed ;
159+ }
160+ if ( jSlice != null && ( ! previous || jSlice !== previous . jSlice ) ) {
161+ changed = this . mapper . setJSlice ( jSlice ) || changed ;
162+ }
163+ if ( kSlice != null && ( ! previous || kSlice !== previous . kSlice ) ) {
164+ changed = this . mapper . setKSlice ( kSlice ) || changed ;
165+ }
166+ // xyz
167+ if ( xSlice != null && ( ! previous || xSlice !== previous . xSlice ) ) {
168+ changed = this . mapper . setXSlice ( xSlice ) || changed ;
169+ }
170+ if ( ySlice != null && ( ! previous || ySlice !== previous . ySlice ) ) {
171+ changed = this . mapper . setYSlice ( ySlice ) || changed ;
172+ }
173+ if ( zSlice != null && ( ! previous || zSlice !== previous . zSlice ) ) {
174+ changed = this . mapper . setZSlice ( zSlice ) || changed ;
175+ }
176+ } else if ( this . mapper . isA ( 'vtkImageArrayMapper' ) ) {
177+ // vtkImageArrayMapper only supports k-slicing
178+ if ( kSlice != null && ( ! previous || kSlice !== previous . kSlice ) ) {
179+ changed = this . mapper . setSlice ( kSlice ) || changed ;
180+ }
181+ }
168182 }
169183
170184 // actor visibility
@@ -186,6 +200,11 @@ export default class SliceRepresentation extends Component {
186200 this . validData = true ;
187201 this . actor . setVisibility ( this . currentVisibility ) ;
188202
203+ // reset camera after input data is lazy-loaded
204+ if ( this . view && this . view . props . autoResetCamera ) {
205+ this . view . resetCamera ( ) ;
206+ }
207+
189208 // trigger render
190209 this . dataChanged ( ) ;
191210 }
@@ -194,16 +213,18 @@ export default class SliceRepresentation extends Component {
194213 dataChanged ( ) {
195214 if ( this . props . colorDataRange === 'auto' ) {
196215 this . mapper . update ( ) ;
197- const input = this . mapper . getInputData ( ) ;
198- const array = input && input . getPointData ( ) ?. getScalars ( ) ;
199- const dataRange = array && array . getRange ( ) ;
200- if ( dataRange ) {
201- this . lookupTable . setMappingRange ( ...dataRange ) ;
202- this . lookupTable . updateRange ( ) ;
203- this . piecewiseFunction . setNodes ( [
204- { x : dataRange [ 0 ] , y : 0 , midpoint : 0.5 , sharpness : 0 } ,
205- { x : dataRange [ 1 ] , y : 1 , midpoint : 0.5 , sharpness : 0 } ,
206- ] ) ;
216+ if ( this . mapper . getInputData ( ) ) {
217+ const input = this . mapper . getCurrentImage ( ) ;
218+ const array = input && input . getPointData ( ) ?. getScalars ( ) ;
219+ const dataRange = array && array . getRange ( ) ;
220+ if ( dataRange ) {
221+ this . lookupTable . setMappingRange ( ...dataRange ) ;
222+ this . lookupTable . updateRange ( ) ;
223+ this . piecewiseFunction . setNodes ( [
224+ { x : dataRange [ 0 ] , y : 0 , midpoint : 0.5 , sharpness : 0 } ,
225+ { x : dataRange [ 1 ] , y : 1 , midpoint : 0.5 , sharpness : 0 } ,
226+ ] ) ;
227+ }
207228 }
208229
209230 if ( this . view ) {
@@ -229,6 +250,12 @@ SliceRepresentation.propTypes = {
229250 */
230251 mapper : PropTypes . object ,
231252
253+ /**
254+ * Optional parameter to set vtk mapper instance from outside.
255+ * Allows to control which mapper class {vtkImageMapper, vtkImageArrayMapper} to use.
256+ */
257+ mapperInstance : PropTypes . object ,
258+
232259 /**
233260 * Properties to set to the slice/actor
234261 */
0 commit comments