@@ -14,7 +14,7 @@ import vtkVolume from 'vtk.js/Sources/Rendering/Core/Volume';
1414const { EVENTS } = cornerstoneTools ;
1515window . cornerstoneTools = cornerstoneTools ;
1616
17- function setupSyncedBrush ( imageDataObject , element ) {
17+ function setupSyncedBrush ( imageDataObject ) {
1818 // Create buffer the size of the 3D volume
1919 const dimensions = imageDataObject . dimensions ;
2020 const width = dimensions [ 0 ] ;
@@ -128,10 +128,7 @@ class VTKCornerstonePaintingSyncExample extends Component {
128128 } ;
129129
130130 const imageDataObject = getImageData ( imageIds , displaySetInstanceUid ) ;
131- const labelMapInputData = setupSyncedBrush (
132- imageDataObject ,
133- this . cornerstoneElements [ 0 ]
134- ) ;
131+ const labelMapInputData = setupSyncedBrush ( imageDataObject ) ;
135132
136133 this . onMeasurementsChanged = event => {
137134 if ( event . type !== EVENTS . LABELMAP_MODIFIED ) {
@@ -160,7 +157,7 @@ class VTKCornerstonePaintingSyncExample extends Component {
160157 ) ;
161158 }
162159
163- onPaintEnd = ( ) => {
160+ onPaintEnd = strokeBuffer => {
164161 const element = this . cornerstoneElements [ 0 ] ;
165162 const enabledElement = cornerstone . getEnabledElement ( element ) ;
166163 const { getters, setters } = cornerstoneTools . getModule ( 'segmentation' ) ;
@@ -174,16 +171,35 @@ class VTKCornerstonePaintingSyncExample extends Component {
174171
175172 const stackData = stackState . data [ 0 ] ;
176173 const numberOfFrames = stackData . imageIds . length ;
174+ const segmentIndex = labelmap3D . activeSegmentIndex ;
177175
178- // TODO -> Can do more efficiently if we can grab the strokeBuffer from vtk-js.
179176 for ( let i = 0 ; i < numberOfFrames ; i ++ ) {
180- const labelmap2D = getters . labelmap2DByImageIdIndex (
181- labelmap3D ,
182- i ,
183- rows ,
184- columns
185- ) ;
186- setters . updateSegmentsOnLabelmap2D ( labelmap2D ) ;
177+ let labelmap2D = labelmap3D . labelmaps2D [ i ] ;
178+
179+ if ( labelmap2D && labelmap2D . segmentsOnLabelmap . includes ( segmentIndex ) ) {
180+ continue ;
181+ }
182+
183+ const frameLength = rows * columns ;
184+ const byteOffset = frameLength * i ;
185+ const strokeArray = new Uint8Array ( strokeBuffer , byteOffset , frameLength ) ;
186+
187+ const strokeOnFrame = strokeArray . some ( element => element === 1 ) ;
188+
189+ if ( ! strokeOnFrame ) {
190+ continue ;
191+ }
192+
193+ if ( labelmap2D ) {
194+ labelmap2D . segmentsOnLabelmap . push ( segmentIndex ) ;
195+ } else {
196+ labelmap2D = getters . labelmap2DByImageIdIndex (
197+ labelmap3D ,
198+ i ,
199+ rows ,
200+ columns
201+ ) ;
202+ }
187203 }
188204
189205 cornerstone . updateImage ( element ) ;
0 commit comments