File: WorkingWhenSleepingLinear.cpp

package info (click to toggle)
camitk 5.2.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 358,388 kB
  • sloc: cpp: 86,984; xml: 1,295; sh: 1,280; ansic: 142; makefile: 112; perl: 84; sed: 20
file content (209 lines) | stat: -rw-r--r-- 7,443 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
/*****************************************************************************
 * $CAMITK_LICENCE_BEGIN$
 *
 * CamiTK - Computer Assisted Medical Intervention ToolKit
 * (c) 2001-2024 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$
 ****************************************************************************/
#include "WorkingWhenSleepingLinear.h"

#include <MedicalImageViewer.h>
#include <RendererWidget.h>
#include <InteractiveGeometryViewer.h>
#include <Application.h>
#include <SingleImageComponent.h>
#include <Log.h>

// disable warning generated by clang about the surrounded headers
#include <CamiTKDisableWarnings>
#include <vtkCamera.h>
#include <CamiTKReEnableWarnings>

using namespace camitk;

// --------------- Constructor -------------------
WorkingWhenSleepingLinear::WorkingWhenSleepingLinear(ActionExtension* extension) : Action(extension) {
// Setting name, description and input component
    numberOfWindows = 300;
    setName("Working When Sleeping (Linear)");
    setDescription("This action generates a linear exploration of slice sagittal, coronal and axial viewers");
    setComponentClassName("ImageComponent");

    // Setting classification family and tags
    setFamily("Tutorial");
    addTag("Working When Sleeping");

    // Setting the action's parameters
    setProperty("High Speed", QVariant(false));
    setProperty("Number Of Steps", QVariant(300));

}

// --------------- destructor -------------------
WorkingWhenSleepingLinear::~WorkingWhenSleepingLinear() {
    // Do not do anything yet.
    // Delete stuff if you create stuff
    // (except if you use smart pointers of course !!)
}

// --------------- apply -------------------
Action::ApplyStatus WorkingWhenSleepingLinear::apply() {

    foreach (Component* comp, getTargets()) {
        ImageComponent* input = dynamic_cast<ImageComponent*>(comp);
        process(input);
    }

    return SUCCESS;
}

void WorkingWhenSleepingLinear::process(ImageComponent* comp) {
    // Get the parameters
    bool highSpeed = property("High Speed").toBool();
    numberOfWindows = property("Number Of Steps").toInt();
    // Getting the input image
    vtkSmartPointer<vtkImageData> inputImage  = comp->getImageData();
    int dims[3];
    inputImage->GetDimensions(dims);

    // Getting the Viewer
    MedicalImageViewer* myMedicalImageViewer = dynamic_cast<MedicalImageViewer*>(Application::getViewer("Medical Image Viewer"));
    if (myMedicalImageViewer != nullptr) {
        myMedicalImageViewer->setVisibleViewer(MedicalImageViewer::VIEWER_ALL);
    }
    else {
        CAMITK_WARNING(tr("Cannot find \"Medical Image Viewer\". This viewer is mandatory for running this action."))
        return;
    }

    // Getting the Camera of the 3DViewer
    RendererWidget* myRendererWidget = dynamic_cast<InteractiveGeometryViewer*>(Application::getViewer("3D Viewer"))->getRendererWidget();
    vtkCamera* myCamera = myRendererWidget->getActiveCamera();

    // Getting the three images slices
    SingleImageComponent* SICSagittal = comp->getSagittalSlices();
    SingleImageComponent* SICCoronal = comp->getCoronalSlices();
    SingleImageComponent* SICAxial = comp->getAxialSlices();

    int currentSagittalSlice = 0;
    int sensSagittal = 1;

    int currentCoronalSlice = 0;
    int sensCoronal = 1;

    int currentAxialSlice = 0;
    int sensAxial = 1;

    int step = 1;
    if (highSpeed) {
        step = 5;
    }

    for (int n = 0; n < numberOfWindows; n++) {
        // modify 3DViewer Camera
        myCamera->Zoom(1. + .01 * sin((double)n * 3.14 / 50.));
        myCamera->Azimuth(step);
        myCamera->Elevation(step);
        myCamera->OrthogonalizeViewUp();
        myRendererWidget->update();

        // modify the current sagittal, coronal, axial slices
        if (SICSagittal != nullptr) {
            SICSagittal->setSlice(currentSagittalSlice);
            currentSagittalSlice += (step * sensSagittal);
            if (currentSagittalSlice <= 0) {
                sensSagittal = 1;
                currentSagittalSlice = 0;
            }
            else {
                if (currentSagittalSlice >= dims[0]) {
                    sensSagittal = -1;
                    currentSagittalSlice = dims[0] - 1;
                }
            }
            SICSagittal->refresh();
        }

        if (SICCoronal != nullptr) {
            SICCoronal->setSlice(currentCoronalSlice);
            currentCoronalSlice += (step * sensCoronal);
            if (currentCoronalSlice <= 0) {
                sensCoronal = 1;
                currentCoronalSlice = 0;
            }
            else {
                if (currentCoronalSlice >= dims[1]) {
                    sensCoronal = -1;
                    currentCoronalSlice = dims[1] - 1;
                }
            }
            SICCoronal->refresh();
        }

        if (SICAxial != nullptr) {
            SICAxial->setSlice(currentAxialSlice);
            currentAxialSlice += (step * sensAxial);
            if (currentAxialSlice <= 0) {
                sensAxial = 1;
                currentAxialSlice = 0;
            }
            else {
                if (currentAxialSlice >= dims[2]) {
                    sensAxial = -1;
                    currentAxialSlice = dims[2] - 1;
                }
            }
            SICAxial->refresh();
        }

        /* another way to do the same thing
                for (int i=0;i<3;i++)
                {
                    SIC[i]->setSlice(currentSlice[i]);
                    currentSlice[i] +=(step*currentSliceSens[i]);
                    if (currentSlice[i]<=0) { currentSliceSens[i]= 1; currentSlice[i]=0;}
                      else
                        if (currentSlice[i]>=dims[i]){ currentSliceSens[i]= -1; currentSlice[i]=dims[i]-1;}
                }

        */

        // Refresh the Viewers ....
        camitk::Application::setProgressBarValue(100 * n / numberOfWindows);

        myMedicalImageViewer->refresh();
        // In order to see the change in the viewers, the initial idea was to call
        // Application::processEvents();
        // but, as explained in the Qt documentation:
        // > In the event that you are running a local loop which calls this function continuously,
        // > without an event loop, the DeferredDelete events will not be processed. This can affect
        // > the behaviour of widgets, e.g. QToolTip, that rely on DeferredDelete events to function
        // > properly. An alternative would be to call sendPostedEvents() from within that local loop.
        Application::sendPostedEvents();
    }

    // set normal zoom
    myCamera->Zoom(1.);
    myRendererWidget->update();


}