File: Slice.cpp

package info (click to toggle)
camitk 6.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 389,496 kB
  • sloc: cpp: 103,476; sh: 2,448; python: 1,618; xml: 984; makefile: 128; perl: 84; sed: 20
file content (683 lines) | stat: -rw-r--r-- 26,558 bytes parent folder | download
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
 *
 * Visit http://camitk.imag.fr for more information
 *
 * This file is part of CamiTK.
 *
 * CamiTK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * CamiTK is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with CamiTK.  If not, see <http://www.gnu.org/licenses/>.
 *
 * $CAMITK_LICENCE_END$
 ****************************************************************************/

// -- Core stuff
#include "Slice.h"

#include <Log.h>

// -- vtk stuff
// disable warning generated by clang about the surrounded headers
#include "CamiTKDisableWarnings"
#include <vtkViewport.h>
#include <vtkProperty.h>
#include <vtkImageMapper3D.h>
#include <vtkDataSetMapper.h>
#include "CamiTKReEnableWarnings"

#include <vtkQuad.h>
#include <vtkImageMapToColors.h>
#include <vtkTrivialProducer.h>
#include <vtkImageChangeInformation.h>

namespace camitk {
// -------------------- constructor --------------------
Slice::Slice(vtkSmartPointer<vtkImageData> volume, SliceOrientation orientation, vtkSmartPointer<vtkWindowLevelLookupTable> lookupTable) : lut{lookupTable} {
    sliceOrientation = orientation;
    init();
    setOriginalVolume(volume);
    setSlice(getNumberOfSlices() / 2);
}


// -------------------- Destructor --------------------
Slice::~Slice() {
}

// -------------------- init --------------------
void Slice::init() {
    currentSliceIndex = 0;

    for (int i = 0; i < 3; i++) {
        originalSpacing[i] = 1.0;
    }

    originalVolume = nullptr;
    image3DActor = nullptr;
    image2DActor  = nullptr;
    pickPlaneActor = nullptr;
    pickPlaneActorPointSet = nullptr;
    pixelActor = nullptr;
    pixelActorPointSet = nullptr;
    image2DReslicer = nullptr;
}

// -------------------- getImageData --------------------
vtkSmartPointer<vtkImageData> Slice::getImageData() const {
    return originalVolume;
}

// -------------------- setOriginalVolume --------------------
void Slice::setOriginalVolume(vtkSmartPointer<vtkImageData> volume) {
    // If there were already a referenced volume,
    //  de-reference the smart pointer.
    originalVolume = volume;

    // Get the original volume information
    // Original volume dimensions in number of voxels (x, y and z)
    int originalDimensions[3];
    originalVolume->GetDimensions(originalDimensions);
    originalVolume->GetSpacing(originalSpacing);

    for (int i = 0; i < 3; i++) {
        // compute original size (nb of slice * spacing)
        originalSize[i] = originalDimensions[i] * originalSpacing[i];

        // As originalSpacing[i] will be used to get the slice number
        // for 2D images, lets replace 0.0 by 1.0 to avoid division by 0
        if (originalSpacing[i] == 0.0) {
            originalSpacing[i] = 1.0;
        }
    }

    // Prepare all the visualization pipeline
    initActors();
}

// -------------------- setArbitraryTransform --------------------
void Slice::setArbitraryTransform(vtkSmartPointer<vtkTransform> transform) {
    image2DReslicer->SetResliceTransform(transform);
    image2DReslicer->Update();
}

// -------------------- pixelPicked --------------------
void Slice::pixelPicked(double x, double y, double z) {
    return;
}

// -------------------- reslicedToVolumeCoords --------------------
void Slice::reslicedToVolumeCoords(const double* ijk, double* xyz) {
    xyz[0] = ijk[0] * originalSpacing[0];
    xyz[1] = ijk[1] * originalSpacing[1];
    xyz[2] = ijk[2] * originalSpacing[2];
}

// -------------------- volumeToReslicedCoords --------------------
void Slice::volumeToReslicedCoords(const double* xyz, double* ijk) {
    ijk[0] = xyz[0] / originalSpacing[0];
    ijk[1] = xyz[1] / originalSpacing[1];
    ijk[2] = xyz[2] / originalSpacing[2];
}

// -------------------- getNumberOfSlices --------------------
int Slice::getNumberOfSlices() const {
    // Array containing first and last indices of the image in each direction
    // 0 & 1 -> x; 2 and 3 -> y; 4 & 5 -> z
    int extent[6];
    originalVolume->GetExtent(extent);

    int nbSlices;
    switch (sliceOrientation) {
        case AXIAL:
        case AXIAL_NEURO:
            nbSlices = extent[5] - extent[4] + 1;
            break;
        case CORONAL:
            nbSlices = extent[3] - extent[2] + 1;
            break;
        case SAGITTAL:
            nbSlices = extent[1] - extent[0] + 1;
            break;
        default:
            nbSlices = 0;
            break;
    }

    return nbSlices;
}

// -------------------- getSlice --------------------
int Slice::getSlice() const {
    return currentSliceIndex;
}

// -------------------- setSlice --------------------
void Slice::setSlice(int s) {
    // Check if s is inside the volume bounds
    if (s < 0) {
        currentSliceIndex = 0;
    }
    else {
        if (s >= getNumberOfSlices()) {
            currentSliceIndex = getNumberOfSlices() - 1;
        }
        else {
            currentSliceIndex = s;
        }
    }

    // Array containing first and last indices of the image in each direction
    // 0 & 1 -> x; 2 and 3 -> y; 4 & 5 -> z
    int extent[6];
    originalVolume->GetExtent(extent);

    switch (sliceOrientation) {
        case AXIAL:
        case AXIAL_NEURO:
            image3DActor->SetDisplayExtent(extent[0], extent[1], extent[2], extent[3], currentSliceIndex, currentSliceIndex);
            image2DActor->SetDisplayExtent(extent[0], extent[1], extent[2], extent[3], currentSliceIndex, currentSliceIndex);
            break;
        case CORONAL:
            image3DActor->SetDisplayExtent(extent[0], extent[1], currentSliceIndex, currentSliceIndex, extent[4], extent[5]);
            image2DActor->SetDisplayExtent(extent[0], extent[1], currentSliceIndex, currentSliceIndex, extent[4], extent[5]);
            break;
        case SAGITTAL:
            image3DActor->SetDisplayExtent(currentSliceIndex, currentSliceIndex, extent[2], extent[3], extent[4], extent[5]);
            image2DActor->SetDisplayExtent(currentSliceIndex, currentSliceIndex, extent[2], extent[3], extent[4], extent[5]);
            break;
        default:
            break;
    }

    updatePickPlane();

    // hide pixel actor for now (it should be visible only when user Ctrl-Click on the image
    pixelActor->VisibilityOff();
}


// -------------------- setSlice --------------------
void Slice::setSlice(double x, double y, double z) {
    // At this point, coordinates are expressed in the data Frame coordinate system
    double ijk[3] = {0.0, 0.0, 0.0};
    double xyz[3] = {x, y, z};

    // Tranform real coordinates to index coordinates
    volumeToReslicedCoords(xyz, ijk);

    switch (sliceOrientation) {
        case AXIAL:
        case AXIAL_NEURO:
            setSlice(rint(ijk[2]));
            break;
        case CORONAL:
            setSlice(rint(ijk[1]));
            break;
        case SAGITTAL:
            setSlice(rint(ijk[0]));
            break;
        default:
            break;
    }

    // Set pixel position in current slice.
    setPixelRealPosition(x, y, z);
}

// -------------------- getNumberOfColors --------------------
int Slice::getNumberOfColors() const {
    return originalVolume->GetScalarTypeMax() - originalVolume->GetScalarTypeMin();
}

// -------------------- setPixelRealPosition --------------------
void Slice::setPixelRealPosition(double x, double y, double z) {
    // update and force visibility of the pixel actor
    updatePixelActor(x, y, z);
    pixelActor->VisibilityOn();
}

// -------------------- get2DImageActor --------------------
vtkSmartPointer<vtkImageActor> Slice::get2DImageActor() const {
    return image2DActor;
}

// -------------------- get3DImageActor --------------------
vtkSmartPointer<vtkImageActor> Slice::get3DImageActor() const {
    return image3DActor;
}

// -------------------- getPixelActor --------------------
vtkSmartPointer<vtkActor> Slice::getPixelActor() {
    return pixelActor;
}

// -------------------- getPickPlaneActor --------------------
vtkSmartPointer<vtkActor> Slice::getPickPlaneActor() const {
    return pickPlaneActor;
}

// -------------------- initActors --------------------
void Slice::initActors() {
    // Add LUT color map filter if grayscale image or nothing if color image
    vtkAlgorithmOutput* outputPort = nullptr;
    vtkSmartPointer<vtkImageMapToColors> imgToMapFilter; // for grayscale
    vtkSmartPointer<vtkTrivialProducer> virtualFilter;   // for color image, not filter required

    // set the lookupTable for grayscale images
    if (originalVolume->GetNumberOfScalarComponents() == 1) {
        imgToMapFilter = vtkSmartPointer<vtkImageMapToColors>::New();
        imgToMapFilter->SetInputData(originalVolume);
        imgToMapFilter->SetLookupTable(lut);
        outputPort = imgToMapFilter->GetOutputPort();
    }
    else {
        // Do not use lut, create a virtual filter to obtain an output port with no modification
        virtualFilter = vtkSmartPointer<vtkTrivialProducer>::New();
        virtualFilter->SetOutput(originalVolume);
        virtualFilter->Update();
        outputPort = virtualFilter->GetOutputPort();  // Or wrap as algorithm if needed
    }

    // reslicer for the arbitrary slice
    image2DReslicer = vtkSmartPointer<vtkImageReslice>::New();
    image2DReslicer->SetInputConnection(outputPort);
    // 2D output
    image2DReslicer->SetOutputDimensionality(2);
    // generate image wherever there is a input voxel (and therefore modify the image dimensions if possible)
    image2DReslicer->AutoCropOutputOn();

    // for the 2D and 3D image actors it is either directly plugged to the output of imgToMapFilter
    // (if this is a perpendicular orientation) or to the reslicer (if this is an arbitrary orientation)
    image3DActor = vtkSmartPointer<vtkImageActor>::New();
    image3DActor->InterpolateOn();

    image2DActor = vtkSmartPointer<vtkImageActor>::New();
    image2DActor->InterpolateOn();

    if (sliceOrientation == ARBITRARY) {
        image3DActor->GetMapper()->SetInputConnection(image2DReslicer->GetOutputPort());
        image2DActor->GetMapper()->SetInputConnection(image2DReslicer->GetOutputPort());
    }
    else {
        image3DActor->GetMapper()->SetInputConnection(outputPort);
        image2DActor->GetMapper()->SetInputConnection(outputPort);
    }

    // Picked plane
    initPickPlaneActor();
    updatePickPlane();

    // Picked pixel
    initPixelActor();
    updatePixelActor();
}

// -------------------- initPickPlaneActor --------------------
void Slice::initPickPlaneActor() {
    // create the picked plane actor 3D geometry
    vtkSmartPointer<vtkPoints> planePoints = vtkSmartPointer<vtkPoints>::New();
    planePoints->SetNumberOfPoints(8);

    // create zero-positioned points
    for (int i = 0; i < planePoints->GetNumberOfPoints(); i++) {
        planePoints->InsertPoint(i, 0.0, 0.0, 0.0);
    }

    // vtkQuad is defined by the four points (0,1,2,3) in counterclockwise order.
    vtkSmartPointer<vtkQuad> leftQuad = vtkSmartPointer<vtkQuad>::New();
    leftQuad->GetPointIds()->SetId(0, 0);
    leftQuad->GetPointIds()->SetId(1, 1);
    leftQuad->GetPointIds()->SetId(2, 2);
    leftQuad->GetPointIds()->SetId(3, 3);

    vtkSmartPointer<vtkQuad> topQuad = vtkSmartPointer<vtkQuad>::New();
    topQuad->GetPointIds()->SetId(0, 4);
    topQuad->GetPointIds()->SetId(1, 5);
    topQuad->GetPointIds()->SetId(2, 1);
    topQuad->GetPointIds()->SetId(3, 0);

    vtkSmartPointer<vtkQuad> rightQuad = vtkSmartPointer<vtkQuad>::New();
    rightQuad->GetPointIds()->SetId(0, 5);
    rightQuad->GetPointIds()->SetId(1, 4);
    rightQuad->GetPointIds()->SetId(2, 7);
    rightQuad->GetPointIds()->SetId(3, 6);

    vtkSmartPointer<vtkQuad> bottomQuad = vtkSmartPointer<vtkQuad>::New();
    bottomQuad->GetPointIds()->SetId(0, 3);
    bottomQuad->GetPointIds()->SetId(1, 2);
    bottomQuad->GetPointIds()->SetId(2, 6);
    bottomQuad->GetPointIds()->SetId(3, 7);

    // Create the unstructured grid that includes the two quads
    pickPlaneActorPointSet = vtkSmartPointer<vtkUnstructuredGrid>::New();
    pickPlaneActorPointSet->Allocate(4);
    pickPlaneActorPointSet->InsertNextCell(leftQuad->GetCellType(), leftQuad->GetPointIds());
    pickPlaneActorPointSet->InsertNextCell(topQuad->GetCellType(), topQuad->GetPointIds());
    pickPlaneActorPointSet->InsertNextCell(rightQuad->GetCellType(), rightQuad->GetPointIds());
    pickPlaneActorPointSet->InsertNextCell(bottomQuad->GetCellType(), bottomQuad->GetPointIds());
    pickPlaneActorPointSet->SetPoints(planePoints);

    // Create the corresponding mapper
    vtkSmartPointer<vtkDataSetMapper> pickPlaneMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    pickPlaneMapper->SetInputData(pickPlaneActorPointSet);

    // instantiate the actor
    pickPlaneActor = vtkSmartPointer<vtkActor>::New();
    pickPlaneActor->SetMapper(pickPlaneMapper);
    pickPlaneActor->GetProperty()->SetAmbient(1.0);
    pickPlaneActor->GetProperty()->SetDiffuse(1.0);

    // Update pixel actor properties
    switch (sliceOrientation) {
        case ARBITRARY:
            pickPlaneActor->GetProperty()->SetColor(1.0, 1.0, 0.0);
            break;
        case AXIAL_NEURO:
            pickPlaneActor->GetProperty()->SetColor(0.0, 0.0, 1.0);
            break;
        case AXIAL:
            pickPlaneActor->GetProperty()->SetColor(0.0, 0.0, 1.0);
            break;
        case CORONAL:
            pickPlaneActor->GetProperty()->SetColor(0.0, 1.0, 0.0);
            break;
        case SAGITTAL:
            pickPlaneActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
            break;
        default:
            pickPlaneActor->GetProperty()->SetColor(1.0, 1.0, 1.0);
            break;
    }
    pickPlaneActor->GetProperty()->SetLineWidth(1.0);
    pickPlaneActor->GetProperty()->SetRepresentationToWireframe();

    //-- pickPlaneActor can not be picked
    pickPlaneActor->PickableOff();

    // by default, the plane actor is always visible
    pickPlaneActor->VisibilityOn();
}

// -------------------- initPixelActor --------------------
void Slice::initPixelActor() {
    // create the pixel actor 3D geometry
    vtkSmartPointer<vtkPoints> pixelPoints = vtkSmartPointer<vtkPoints>::New();
    pixelPoints->SetNumberOfPoints(8);

    // create zero-positionned points
    for (int i = 0; i < pixelPoints->GetNumberOfPoints(); i++) {
        pixelPoints->InsertPoint(i, 0.0, 0.0, 0.0);
    }

    // vtkQuad is defined by the four points (0,1,2,3) in counterclockwise order.
    vtkSmartPointer<vtkQuad> verticalQuad = vtkSmartPointer<vtkQuad>::New();
    verticalQuad->GetPointIds()->SetId(0, 0);
    verticalQuad->GetPointIds()->SetId(1, 1);
    verticalQuad->GetPointIds()->SetId(2, 2);
    verticalQuad->GetPointIds()->SetId(3, 3);

    vtkSmartPointer<vtkQuad> horizontalQuad = vtkSmartPointer<vtkQuad>::New();
    horizontalQuad->GetPointIds()->SetId(0, 4);
    horizontalQuad->GetPointIds()->SetId(1, 5);
    horizontalQuad->GetPointIds()->SetId(2, 6);
    horizontalQuad->GetPointIds()->SetId(3, 7);

    // Create the unstructured grid that includes the two quads
    pixelActorPointSet = vtkSmartPointer<vtkUnstructuredGrid>::New();
    pixelActorPointSet->Allocate(2);
    pixelActorPointSet->InsertNextCell(horizontalQuad->GetCellType(), horizontalQuad->GetPointIds());
    pixelActorPointSet->InsertNextCell(verticalQuad->GetCellType(), verticalQuad->GetPointIds());
    pixelActorPointSet->SetPoints(pixelPoints);

    // Create the corresponding mapper
    vtkSmartPointer<vtkDataSetMapper> pixelMapper = vtkSmartPointer<vtkDataSetMapper>::New();
    pixelMapper->SetInputData(pixelActorPointSet);

    // instantiate the actor
    pixelActor = vtkSmartPointer<vtkActor>::New();
    pixelActor->SetMapper(pixelMapper);
    pixelActor->GetProperty()->SetAmbient(1.0);
    pixelActor->GetProperty()->SetDiffuse(1.0);

    // Update pixel actor properties
    switch (sliceOrientation) {
        case ARBITRARY:
            pixelActor->GetProperty()->SetColor(1.0, 1.0, 0.0);
            break;
        case AXIAL_NEURO:
            pixelActor->GetProperty()->SetColor(0.0, 0.0, 1.0);
            break;
        case AXIAL:
            pixelActor->GetProperty()->SetColor(0.0, 0.0, 1.0);
            break;
        case CORONAL:
            pixelActor->GetProperty()->SetColor(0.0, 1.0, 0.0);
            break;
        case SAGITTAL:
            pixelActor->GetProperty()->SetColor(1.0, 0.0, 0.0);
            break;
        default:
            pixelActor->GetProperty()->SetColor(1.0, 1.0, 1.0);
            break;
    }
    pixelActor->GetProperty()->SetLineWidth(1.0);
    pixelActor->GetProperty()->SetRepresentationToWireframe();

    //-- pixelActor can not be picked
    pixelActor->PickableOff();

    // by default, the pixel actor is not visible (it should be only visible when user Ctrl-Click on the image)
    pixelActor->VisibilityOff();
}

// -------------------- updatePickPlane --------------------
void Slice::updatePickPlane() {
    // Be careful, the center of the first voxel (0,0,0) is displayed at coordinates (0.0, 0.0, 0.0).
    // Pixels within borders are represented (in 2D) only by their half, quarter or eighth depending on their coordinates.
    // see also bug #65 "Unexpected image behaviors in Viewers"

    // update the point positions - become the bounds come from the Actor, they are expressed in worldFrame
    double bounds[6];
    image2DActor->GetBounds(bounds);

    // If an axis has 0 thickness, set the bounds around it to slice thickness (-thickness/2, +thickness/2)
    for (int axis = 0; axis < 3; ++axis) {
        if (bounds[2 * axis] == bounds[2 * axis + 1]) {
            bounds[2 * axis] -= originalSpacing[axis] / 2.0;
            bounds[2 * axis + 1] += originalSpacing[axis] / 2.0;
        }
    }
    pickPlaneActorPointSet->GetPoints()->SetPoint(0, bounds[0], bounds[2], bounds[4]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(1, bounds[0], bounds[2], bounds[5]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(2, bounds[0], bounds[3], bounds[5]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(3, bounds[0], bounds[3], bounds[4]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(4, bounds[1], bounds[2], bounds[4]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(5, bounds[1], bounds[2], bounds[5]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(6, bounds[1], bounds[3], bounds[5]);
    pickPlaneActorPointSet->GetPoints()->SetPoint(7, bounds[1], bounds[3], bounds[4]);
    // Needed to notify the vtk pipeline of the change in the geometry (and therefore update the actor)
    pickPlaneActorPointSet->Modified();
}

// ----------------- updatePixelActorPosition -----------------
void Slice::updatePixelActor() {
    double xMin = 0.0;
    double xMax = originalSize[0];
    double yMin = 0.0;
    double yMax = originalSize[1];
    double zMin = 0.0;
    double zMax = originalSize[2];
    // Draw the bounding box around the center of the slice
    updatePixelActor(xMin + (xMax - xMin) / 2,  yMin + (yMax - yMin) / 2, zMin + (zMax - zMin) / 2);
}

void Slice::updatePixelActor(double x, double y, double z) {
    // x,y,z are in data Frame, bounds are in worldFrame
    double bounds[6];
    image2DActor->GetBounds(bounds);

    double sliceHalfThickness;
    int perpendicularAxis = 0;
    // If an axis has 0 thickness, set the bounds around it to slice thickness (-thickness/2, +thickness/2)
    for (int axis = 0; axis < 3; ++axis) {
        if (bounds[2 * axis] == bounds[2 * axis + 1]) {
            perpendicularAxis = axis;
            bounds[2 * axis] -= originalSpacing[axis] / 2.0;
            bounds[2 * axis + 1] += originalSpacing[axis] / 2.0;
        }
    }

    // update the point positions
    switch (sliceOrientation) {
        case AXIAL_NEURO:
        case AXIAL:
            sliceHalfThickness = originalSpacing[2] / 2.0;
            // vertical quad
            pixelActorPointSet->GetPoints()->SetPoint(0, x, 0.0, z - sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(1, x, 0.0, z + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(2, x, originalSize[1], z + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(3, x, originalSize[1], z - sliceHalfThickness);
            // horizontal quad
            pixelActorPointSet->GetPoints()->SetPoint(4, 0.0, y, z - sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(5, 0.0, y, z + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(6, originalSize[0], y, z + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(7, originalSize[0], y, z - sliceHalfThickness);
            break;

        case CORONAL:
            sliceHalfThickness = originalSpacing[1] / 2.0;
            // vertical quad
            pixelActorPointSet->GetPoints()->SetPoint(0, x, y - sliceHalfThickness, originalSize[2]);
            pixelActorPointSet->GetPoints()->SetPoint(1, x, y + sliceHalfThickness, originalSize[2]);
            pixelActorPointSet->GetPoints()->SetPoint(2, x, y + sliceHalfThickness, 0.0);
            pixelActorPointSet->GetPoints()->SetPoint(3, x, y - sliceHalfThickness, 0.0);
            // horizontal quad
            pixelActorPointSet->GetPoints()->SetPoint(4, 0.0, y - sliceHalfThickness, z);
            pixelActorPointSet->GetPoints()->SetPoint(5, 0.0, y + sliceHalfThickness, z);
            pixelActorPointSet->GetPoints()->SetPoint(6, originalSize[0], y + sliceHalfThickness, z);
            pixelActorPointSet->GetPoints()->SetPoint(7, originalSize[0], y - sliceHalfThickness, z);
            break;

        case SAGITTAL:
            sliceHalfThickness = originalSpacing[0] / 2.0;
            // vertical quad
            pixelActorPointSet->GetPoints()->SetPoint(0, x - sliceHalfThickness, y, originalSize[2]);
            pixelActorPointSet->GetPoints()->SetPoint(1, x + sliceHalfThickness, y, originalSize[2]);
            pixelActorPointSet->GetPoints()->SetPoint(2, x + sliceHalfThickness, y, 0.0);
            pixelActorPointSet->GetPoints()->SetPoint(3, x - sliceHalfThickness, y, 0.0);
            // horizontal quad
            pixelActorPointSet->GetPoints()->SetPoint(4, x - sliceHalfThickness, 0.0, z);
            pixelActorPointSet->GetPoints()->SetPoint(5, x + sliceHalfThickness, 0.0, z);
            pixelActorPointSet->GetPoints()->SetPoint(6, x + sliceHalfThickness, originalSize[1], z);
            pixelActorPointSet->GetPoints()->SetPoint(7, x - sliceHalfThickness, originalSize[1], z);
            break;

        case ARBITRARY: {
            vtkSmartPointer<vtkMatrix4x4> T_P2L = vtkSmartPointer<vtkMatrix4x4>::New();
            if (image2DReslicer->GetResliceAxes() != nullptr) {
                T_P2L->DeepCopy(image2DReslicer->GetResliceAxes());
            }
            T_P2L->Invert();
            double pixel_P[4] = {x, y, z, 1.0};
            double pixel_L[4];
            T_P2L->MultiplyPoint(pixel_P, pixel_L);

            sliceHalfThickness = originalSpacing[2] / 2.0;

            image2DActor->GetBounds(bounds);
            // vertical quad
            pixelActorPointSet->GetPoints()->SetPoint(0, pixel_L[0], bounds[2], pixel_L[2] - sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(1, pixel_L[0], bounds[2], pixel_L[2] + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(2, pixel_L[0], bounds[3], pixel_L[2] + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(3, pixel_L[0], bounds[3], pixel_L[2] - sliceHalfThickness);
            // horizontal quad
            pixelActorPointSet->GetPoints()->SetPoint(4, bounds[0], pixel_L[1], pixel_L[2] - sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(5, bounds[0], pixel_L[1], pixel_L[2] + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(6, bounds[1], pixel_L[1], pixel_L[2] + sliceHalfThickness);
            pixelActorPointSet->GetPoints()->SetPoint(7, bounds[1], pixel_L[1], pixel_L[2] - sliceHalfThickness);
            break;
        }
        break;
        default:
            break;
    }
    // Needed to notify the vtk pipeline of the change in the geometry (and therefore update the actor)
    pixelActorPointSet->Modified();
}





//------------------------------- addProp ----------------------------------------
bool Slice::addProp(const QString& name, vtkSmartPointer< vtkProp > prop) {
    if (!extraProp.contains(name)) {
        extraProp.insert(name, prop);
        return true;
    }
    else {
        return false;
    }
}

//------------------------------- getProp ----------------------------------------
vtkSmartPointer< vtkProp > Slice::getProp(const QString& name) {
    if (extraProp.contains(name)) {
        return extraProp.value(name);
    }
    else {
        return nullptr;
    }
}

//------------------------------- getNumberOfProp ----------------------------------------
unsigned int Slice::getNumberOfProp() const {
    return extraProp.size();
}

//------------------------------- getProp ----------------------------------------
vtkSmartPointer< vtkProp > Slice::getProp(unsigned int index) {
    return extraProp.values().at(index);
}

//------------------------------- removeProp ----------------------------------------
bool Slice::removeProp(const QString& name) {
    if (extraProp.contains(name)) {
        // remove the prop from any renderer/consummers
        vtkSmartPointer<vtkProp> prop = extraProp.value(name);
        prop->VisibilityOff();

        for (int i = 0; i < prop->GetNumberOfConsumers(); i++) {
            vtkViewport* viewer = vtkViewport::SafeDownCast(prop->GetConsumer(i));

            if (viewer) {
                viewer->RemoveViewProp(prop);
            }
        }

        // remove it from the maps
        extraProp.remove(name);
        return true;
    }
    else {
        return false;
    }
}




}