cudaconfidencemapsdemo.cpp 16.2 KB
Newer Older
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
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
// 
// ================================================================================================

#include "cudaconfidencemapsdemo.h"

27
28
29
30
31
32
33
34
35
36
#include <iomanip>
#include <sstream>
#include <QDateTime>
#include <QDir>

#ifdef CAMPVIS_HAS_MODULE_DEVIL
#include <IL/il.h>
#include <IL/ilu.h>
#endif

37
#include "core/datastructures/imagedata.h"
38
#include "core/datastructures/imagerepresentationgl.h"
39
40
#include "core/classification/geometry1dtransferfunction.h"
#include "core/classification/tfgeometry1d.h"
41
#include "cgt/event/keyevent.h"
42
43
44

namespace campvis {

45
46
47
48
    CudaConfidenceMapsDemo::CudaConfidenceMapsDemo(DataContainer* dc)
        : AutoEvaluationPipeline(dc)
        , _usIgtlReader()
        , _usCropFilter(&_canvasSize)
49
        , _usBlurFilter(&_canvasSize)
50
        , _usResampler(&_canvasSize)
51
        , _usMapsSolver()
52
        , _usFusion(&_canvasSize)
53
        , _usFanRenderer(&_canvasSize)
54
55
        , p_millisecondBudget("MillisecondBudget", "Milliseconds per frame", 24.0f, 10.0f, 60.0f)
        , p_connectDisconnectButton("ConnectToIGTLink", "Connect/Disconnect")
Declara Denis's avatar
Declara Denis committed
56
57
        , p_resamplingScale("ResampleScale", "Resample Scale", 0.5f, 0.01f, 1.0f)
        , p_beta("Beta", "Beta", 80.0f, 1.0f, 200.0f)
58
59
60
        , p_showAdvancedOptions("ShowAdvancedOptions", "Advanced options...", false)
        , p_useAlphaBetaFilter("UseAlphaBetaFilter", "Alpha-Beta-Filter", true)
        , p_gaussianFilterSize("GaussianSigma", "Blur amount", 2.5f, 1.0f, 10.0f)
Declara Denis's avatar
Declara Denis committed
61
        , p_gradientScaling("GradientScaling", "Scaling factor for gradients", 2.0f, 0.001, 10)
62
63
        , p_alpha("Alpha", "Alpha", 2.0f, 0.0f, 10.0f)
        , p_gamma("Gamma", "Gamma", 0.03f, 0.0f, 0.4f, 0.001, 4)
64
        , p_fanHalfAngle("FanHalfAngle", "Fan Half Angle", 28.0f, 1.0f, 90.0f)
65
        , p_fanInnerRadius("FanInnerRadius", "Fan Inner Radius", 0.222f, 0.001f, 0.999f)
66
        , p_useSpacingEncodedFanGeometry("UseSpacingEncodedFanGeomtry", "Use spacing encoded fan geometry", true)
67
68
69
        , p_recordingDirectory("RecordingDirectory", "Recording output direcotry", "D:\\us_acquisitions\\")
        , p_enableRecording("EnableRecording", "Enable recording", false)
        , _recordedFrames(0)
Declara Denis's avatar
Declara Denis committed
70
        , _statisticsLastUpdateTime()
71
    {
72
73
74
        // Calculate file prefix, using date, hour, minute and second of when the pipeline was created
        _filePrefix = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss_").toStdString();

75
76
        addProcessor(&_usIgtlReader);
        addProcessor(&_usCropFilter);
77
        addProcessor(&_usBlurFilter);
78
        addProcessor(&_usResampler);
79
        addProcessor(&_usMapsSolver);
80
        addProcessor(&_usFusion);
81
        addProcessor(&_usFanRenderer);
82

83
        addProperty(p_millisecondBudget);
84
        addProperty(p_connectDisconnectButton);
85
        addProperty(p_resamplingScale);
86
87
88
89
90
91
        addProperty(p_beta);

        addProperty(p_showAdvancedOptions);

        addProperty(p_useAlphaBetaFilter);
        addProperty(p_gaussianFilterSize);
Declara Denis's avatar
Declara Denis committed
92
        addProperty(p_gradientScaling);
93
94
95
96
        addProperty(p_alpha);
        addProperty(p_gamma);
        addProperty(p_fanHalfAngle);
        addProperty(p_fanInnerRadius);
97
98
99
        addProperty(p_recordingDirectory);
        addProperty(p_enableRecording);

100
101
        addProperty(p_useSpacingEncodedFanGeometry);

102
        setAdvancedPropertiesVisibility(false);
103
104
105
106
107
108
109
110
    }

    CudaConfidenceMapsDemo::~CudaConfidenceMapsDemo() {
    }

    void CudaConfidenceMapsDemo::init() {
        AutoEvaluationPipeline::init();

111
        // Set intial options
112
113
114
        _usIgtlReader.p_receiveImages.setValue(true);
        _usIgtlReader.p_receiveTransforms.setValue(false);
        _usIgtlReader.p_receivePositions.setValue(false);
115
        _usResampler.p_resampleScale.setValue(0.25f);
116
117
118
119
        // Set transfer function
        Geometry1DTransferFunction* tf = new Geometry1DTransferFunction(256);
        tf->addGeometry(TFGeometry1D::createQuad(cgt::vec2(0.0f, 0.5f), cgt::col4(0, 0, 0, 255), cgt::col4(0, 0, 0, 0)));
        _usFusion.p_confidenceTF.replaceTF(tf);
120
121

        // Create connectors
122
123
        _usIgtlReader.p_targetImagePrefix.setValue("us.igtl.");

124
125
        _usCropFilter.p_inputImage.setValue("us.igtl.CAMPUS");
        _usCropFilter.p_outputImage.setValue("us");
126
127

        _usBlurFilter.p_inputImage.setValue("us");
128
        _usBlurFilter.p_outputImage.setValue("us.blurred");
129
130
        _usBlurFilter.p_outputImage.addSharedProperty(&_usResampler.p_inputImage);
        _usBlurFilter.p_outputImage.addSharedProperty(&_usFusion.p_blurredImageId);
131

132
133
        _usResampler.p_outputImage.setValue("us.resampled");
        _usResampler.p_outputImage.addSharedProperty(&_usMapsSolver.p_inputImage);
134

135
        _usMapsSolver.p_outputConfidenceMap.setValue("us.confidence");
136
137
        _usMapsSolver.p_outputConfidenceMap.addSharedProperty(&_usFusion.p_confidenceImageID);

138
        _usFusion.p_usImageId.setValue("us");
139
140
        _usFusion.p_targetImageID.setValue("us.fusion");
        _usFusion.p_view.setValue(12);
141
142
        _usFusion.p_renderToTexture.setValue(true);
        _usFusion.p_targetImageID.addSharedProperty(&_usFanRenderer.p_inputImage);
143
144
        _usFusion.p_transferFunction.setAutoFitWindowToData(false);
        _usFusion.p_confidenceTF.setAutoFitWindowToData(false);
145

146
147
148
149
150
        _usFanRenderer.p_renderTargetID.setValue("us.fused_fan");
        _usFanRenderer.p_innerRadius.setValue(120.0f/540.0f);
        _usFanRenderer.p_halfAngle.setValue(37);

        _renderTargetID.setValue("us.fused_fan");
151
152

        // Bind pipeline proeprties to processor properties
153
154
        p_connectDisconnectButton.s_clicked.connect(this, &CudaConfidenceMapsDemo::toggleIGTLConnection);

155
156
        p_gaussianFilterSize.addSharedProperty(&_usBlurFilter.p_sigma);
        p_resamplingScale.addSharedProperty(&_usResampler.p_resampleScale);
Declara Denis's avatar
Declara Denis committed
157
        p_gradientScaling.addSharedProperty(&_usMapsSolver.p_gradientScaling);
158
159
160
161
162
163
        p_alpha.addSharedProperty(&_usMapsSolver.p_paramAlpha);
        p_beta.addSharedProperty(&_usMapsSolver.p_paramBeta);
        p_gamma.addSharedProperty(&_usMapsSolver.p_paramGamma);
        p_useAlphaBetaFilter.addSharedProperty(&_usMapsSolver.p_useAlphaBetaFilter);
        p_fanHalfAngle.addSharedProperty(&_usFanRenderer.p_halfAngle);
        p_fanInnerRadius.addSharedProperty(&_usFanRenderer.p_innerRadius);
164
165
166
167
168
169
    }

    void CudaConfidenceMapsDemo::deinit() {
        AutoEvaluationPipeline::deinit();
    }

170
    void CudaConfidenceMapsDemo::executePipeline() {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
        // Only launch the pipeline if the IgtlReader has recieved new data
        // FIXME: It would be better to check if a new image actually arrived, instead
        // of just checking the invaildation state of the IGTLReader
        if (!_usIgtlReader.isValid()) {
            float millisecondBudget = p_millisecondBudget.getValue();
            auto startTime = tbb::tick_count::now();

            // Make sure that the whole pipeline gets invalidated
            _usBlurFilter.invalidate(AbstractProcessor::INVALID_RESULT);
            _usCropFilter.invalidate(AbstractProcessor::INVALID_RESULT);
            _usResampler.invalidate(AbstractProcessor::INVALID_RESULT);
            _usMapsSolver.invalidate(AbstractProcessor::INVALID_RESULT);
            _usFusion.invalidate(AbstractProcessor::INVALID_RESULT);

            executeProcessorAndCheckOpenGLState(&_usIgtlReader);
            executeProcessorAndCheckOpenGLState(&_usCropFilter);
            executeProcessorAndCheckOpenGLState(&_usBlurFilter) ;
            executeProcessorAndCheckOpenGLState(&_usResampler);

            auto solverStartTime = tbb::tick_count::now();
            _usMapsSolver.p_millisecondBudget.setValue(millisecondBudget);
            executeProcessorAndCheckOpenGLState(&_usMapsSolver);
            auto solverEndTime = tbb::tick_count::now();

195
196
197
198
199
200
201
202
203
204
205
            // Read fan geomtry from encoded image...
            if (p_useSpacingEncodedFanGeometry.getValue()) {
                ImageRepresentationGL::ScopedRepresentation img(*_data, _usCropFilter.p_inputImage.getValue());
                auto image = reinterpret_cast<const ImageData*>(_data->getData(_usCropFilter.p_inputImage.getValue()).getData());
                if (image != nullptr) {
                    cgt::vec3 encodedData = image->getMappingInformation().getVoxelSize();
                    p_fanHalfAngle.setValue(encodedData.x / 2.0f);
                    p_fanInnerRadius.setValue(encodedData.y);
                }
            }

206
207
            executeProcessorAndCheckOpenGLState(&_usFusion);
            executeProcessorAndCheckOpenGLState(&_usFanRenderer);
208
                
209
210
211
212
213
214
215
216
            auto endTime = tbb::tick_count::now();

            if ((startTime - _statisticsLastUpdateTime).seconds() > 0.5f) {
                _statisticsLastUpdateTime = startTime;

                auto ms = (endTime - startTime).seconds() * 1000.0f;
                auto solverMs = (solverEndTime - solverStartTime).seconds() * 1000.0f;
                std::stringstream string;
217
                string << "Mode: " << _usFusion.p_view.getOptionValue() << std::endl;
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
                string << "Execution time: " << static_cast<int>(ms) << "ms" << std::endl;
                string << "Solver time: " << static_cast<int>(solverMs) << "ms" << std::endl;
                string << "CG Iterations: " << _usMapsSolver.getActualConjugentGradientIterations() << std::endl;
                string << "Error: " << _usMapsSolver.getResidualNorm() << std::endl;
                _usFanRenderer.p_text.setValue(string.str());
            }

            if (p_enableRecording.getValue()) {
                _recordedFrames++;
                QDir dir(QString::fromStdString(p_recordingDirectory.getValue()));
                std::stringstream filename;
                filename << _filePrefix << std::setw(4) << std::setfill('0') << _recordedFrames << ".png";
                auto url = dir.absoluteFilePath(QString::fromStdString(filename.str())).toStdString();

                ScopedTypedData<ImageData> rd(*_data, "us");
                const ImageRepresentationGL *rep = rd->getRepresentation<ImageRepresentationGL>();
                if (rep != 0) {
#ifdef CAMPVIS_HAS_MODULE_DEVIL
                    if (!cgt::FileSystem::dirExists(dir.absolutePath().toStdString()))
                        cgt::FileSystem::createDirectory(dir.absolutePath().toStdString());

                    // get color buffer content
                    GLubyte* colorBuffer = rep->getTexture()->downloadTextureToBuffer(GL_RED, GL_UNSIGNED_BYTE);
                    cgt::ivec2 size = rep->getSize().xy();

                    // create Devil image from image data and write it to file
                    ILuint img;
                    ilGenImages(1, &img);
                    ilBindImage(img);

                    // put pixels into IL-Image
                    ilTexImage(size.x, size.y, 1, 1, GL_LUMINANCE, IL_UNSIGNED_BYTE, colorBuffer);
                    ilEnable(IL_FILE_OVERWRITE);
                    ilResetWrite();
                    ILboolean success = ilSaveImage(url.c_str());
                    ilDeleteImages(1, &img);

                    delete[] colorBuffer;

                    if (!success) {
                        LERROR("Could not save image to file: " << ilGetError());
                    }
#endif
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
    void CudaConfidenceMapsDemo::onEvent(cgt::Event* e) {
        // Allow for rapid switching between different visualizations
        //  F1: Ultrasound only
        //  F2: Sharpness
        //  F3: LAB
        //  F4: Color overlay
        //  F5: CM only
        if (typeid(*e) == typeid(cgt::KeyEvent)) {
            cgt::KeyEvent *keyEvent = reinterpret_cast<cgt::KeyEvent*>(e);
            if (keyEvent == nullptr) return;

            if (keyEvent->pressed()) {
                bool eventHandled = true;
                switch (keyEvent->keyCode()) {
                case cgt::KeyEvent::K_F1:
                    _usFusion.p_view.setValue(0); // US only
                    break;
                case cgt::KeyEvent::K_F2:
                    _usFusion.p_view.setValue(10); // Sharpness
                    {
                        Geometry1DTransferFunction* tf = new Geometry1DTransferFunction(256);
                        tf->addGeometry(TFGeometry1D::createQuad(cgt::vec2(0.0f, 1.0f), cgt::col4(0, 0, 0, 255), cgt::col4(0, 0, 0, 0)));
                        _usFusion.p_confidenceTF.replaceTF(tf);
                    }
                    break;
                case cgt::KeyEvent::K_F3:
                    _usFusion.p_view.setValue(8); // LAB
                    {
                        Geometry1DTransferFunction* tf = new Geometry1DTransferFunction(256);
                        tf->addGeometry(TFGeometry1D::createQuad(cgt::vec2(0.0f, 0.5f), cgt::col4(0, 0, 0, 255), cgt::col4(0, 0, 0, 0)));
                        _usFusion.p_confidenceTF.replaceTF(tf);
                        _usFusion.p_hue.setValue(0.23f);
                    }
                    break;
                case cgt::KeyEvent::K_F4:
                    _usFusion.p_view.setValue(12); // Color overlay
                    {
                        Geometry1DTransferFunction* tf = new Geometry1DTransferFunction(256);
                        tf->addGeometry(TFGeometry1D::createQuad(cgt::vec2(0.0f, 0.5f), cgt::col4(0, 0, 0, 255), cgt::col4(0, 0, 0, 0)));
                        _usFusion.p_confidenceTF.replaceTF(tf);
                        _usFusion.p_hue.setValue(0.15f);
                    }
                    break;
                case cgt::KeyEvent::K_F5:
                    _usFusion.p_view.setValue(2); // CM Only
                    break;
                default:
                    eventHandled = false;
                };
                if (eventHandled) {
                    e->accept();
                    // Force HUD statistics to be updated
                    _statisticsLastUpdateTime = tbb::tick_count();
                }
            }
        }
    }

324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    void CudaConfidenceMapsDemo::onPropertyChanged(const AbstractProperty* prop) {
        if (prop == &p_showAdvancedOptions) {
            setAdvancedPropertiesVisibility(p_showAdvancedOptions.getValue());
            return;
        }
        else if (prop == &_renderTargetID && _renderTargetID.getValue() != "us.fused_fan") {
            // Prevent the program from changing the render target
            _renderTargetID.setValue("us.fused_fan");
        }
        
        AutoEvaluationPipeline::onPropertyChanged(prop);
    }

    void CudaConfidenceMapsDemo::toggleIGTLConnection() {
        // Simulate a click on the currently visible button of the IGTL reader,
        // causing it to either connect or disconnect from a server.
        if (_usIgtlReader.p_connect.isVisible())
            _usIgtlReader.p_connect.click();
        else
            _usIgtlReader.p_disconnect.click();
344
    }
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

    void CudaConfidenceMapsDemo::setAdvancedPropertiesVisibility(bool visible) {
        if (p_showAdvancedOptions.getValue() != visible) {
            p_showAdvancedOptions.setValue(visible);
        }

        p_useAlphaBetaFilter.setVisible(visible);
        p_gaussianFilterSize.setVisible(visible);
        p_gradientScaling.setVisible(visible);
        p_alpha.setVisible(visible);
        p_gamma.setVisible(visible);
        p_fanHalfAngle.setVisible(visible);
        p_fanInnerRadius.setVisible(visible);
        p_recordingDirectory.setVisible(visible);
        p_enableRecording.setVisible(visible);
    }

362
}