2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

cudaconfidencemapssolver.cpp 6.02 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
27
28
29
30
31
// ================================================================================================
// 
// 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 "cudaconfidencemapssolver.h"

#include "core/datastructures/transformdata.h"
#include "core/datastructures/positiondata.h"

#include "core/datastructures/imagedata.h"
#include "core/datastructures/genericimagerepresentationlocal.h"
32
#include "core/datastructures/imagerepresentationgl.h"
33
34
35
36
37
38
39
40
41
#include "core/tools/stringutils.h"

namespace campvis {
    const std::string CudaConfidenceMapsSolver::loggerCat_ = "CAMPVis.modules.cudaconfidencemaps.solver";

    CudaConfidenceMapsSolver::CudaConfidenceMapsSolver() 
        : AbstractProcessor()
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputConfidenceMap("OutputConfidenceMap", "Output Confidence Map", "us.confidence", DataNameProperty::WRITE)
42
        , p_resetResult("ResetSolution", "Reset solution vector")
43
        , p_use8Neighbourhood("Use8Neighbourhood", "Use 8 Neighbourhood (otherwise 4)", true)
44
        , p_millisecondBudget("IterationCount", "Conjugate Gradient Iterations", 25.0f, 1.0f, 1000.0f)
45
46
47
48
        , p_gradientScaling("GradientScaling", "Scaling factor for gradients", 2.0f, 0.001f, 10.0f)
        , p_paramAlpha("Alpha", "Alpha (TGC)", 2.0f, 0.001f, 10.0f)
        , p_paramBeta("Beta", "Beta (Weight mapping)", 20.0f, 0.001f, 200.0f)
        , p_paramGamma("Gamma", "Gamma (Diagonal penalty)", 0.03f, 0.001f, 0.5f)
49
        , p_useAlphaBetaFilter("UseAlphaBetaFilter", "Use Alpha-Beta-Filter", true)
50
51
        , p_filterAlpha("FilterAlpha", "Filter Alpha", 0.36f, 0.0f, 1.0f)
        , p_filterBeta("FilterBeta", "Filter Beta", 0.005f, 0.0f, 1.0f)
Declara Denis's avatar
Declara Denis committed
52
        , _solver()
53
54
55
56
57
    {

        addProperty(p_inputImage);
        addProperty(p_outputConfidenceMap);

58
        addProperty(p_resetResult);
59
        addProperty(p_use8Neighbourhood);
60
        addProperty(p_millisecondBudget);
61
62
63
64
        addProperty(p_gradientScaling);
        addProperty(p_paramAlpha);
        addProperty(p_paramBeta);
        addProperty(p_paramGamma);
65
66
67
68

        addProperty(p_useAlphaBetaFilter);
        addProperty(p_filterAlpha);
        addProperty(p_filterBeta);
69
70
    }

71
    CudaConfidenceMapsSolver::~CudaConfidenceMapsSolver() { }
72

73
    void CudaConfidenceMapsSolver::init() {
74
75
        p_resetResult.s_clicked.connect(this, &CudaConfidenceMapsSolver::resetSolutionVector);
        resetSolutionVector();
76
77
    }

78
79
80
    void CudaConfidenceMapsSolver::deinit() {
        p_resetResult.s_clicked.disconnect(this);
    }
81
82

    void CudaConfidenceMapsSolver::updateResult(DataContainer& data) {
83

84
85
        ImageRepresentationLocal::ScopedRepresentation img(data, p_inputImage.getValue());
        if (img != 0) {
86
            bool use8Neighbourhood = p_use8Neighbourhood.getValue();
87
            float millisecondBudget = p_millisecondBudget.getValue();
88
89
90
91
92
            float gradientScaling = p_gradientScaling.getValue();
            float alpha = p_paramAlpha.getValue();
            float beta = p_paramBeta.getValue();
            float gamma = p_paramGamma.getValue();

93
94
95
96
            // Setup the solver with the current Alpha-Beta-Filter settings
            _solver.enableAlphaBetaFilter(p_useAlphaBetaFilter.getValue());
            _solver.setAlphaBetaFilterParameters(p_filterAlpha.getValue(), p_filterBeta.getValue());

97
            cgt::ivec3 size = img->getSize();
98
            size_t elementCount = cgt::hmul(size);
99
100
            auto image = (unsigned char*)img->getWeaklyTypedPointer()._pointer;

101
            _solver.uploadImage(image, size.x, size.y, gradientScaling, alpha, beta, gamma, use8Neighbourhood);
102
            _solver.solve(millisecondBudget);
103

104
105
            const float *solution = _solver.getSolution(size.x, size.y);

Declara Denis's avatar
Declara Denis committed
106
107
            // FIXME: Instead of copying the solution to a local representation first it would make
            // sense to directly create an opengl representation!
108
            ImageData *id = new ImageData(img->getParent()->getDimensionality(), size, img->getParent()->getNumChannels());
Declara Denis's avatar
Declara Denis committed
109
110
111
112
            cgt::Texture* resultTexture = new cgt::Texture(GL_TEXTURE_2D, size, GL_R32F, cgt::Texture::LINEAR);
            resultTexture->setWrapping(cgt::Texture::MIRRORED_REPEAT);
            resultTexture->uploadTexture(reinterpret_cast<const GLubyte*>(solution), GL_RED, GL_FLOAT);
            ImageRepresentationGL::create(id, resultTexture);
113
114
115
            id->setMappingInformation(img->getParent()->getMappingInformation());
            data.addData(p_outputConfidenceMap.getValue(), id);
        }
116
117
    }

118
    void CudaConfidenceMapsSolver::updateProperties(DataContainer& dataContainer) { }
119

120
121
122
123
124
125
126
127
128
129
    int CudaConfidenceMapsSolver::getActualConjugentGradientIterations() const
    {
        return _solver.getSolutionIterationCount();
    }

    float CudaConfidenceMapsSolver::getResidualNorm() const
    {
        return _solver.getSolutionResidualNorm();
    }

130
131
    void CudaConfidenceMapsSolver::resetSolutionVector() {
        // Create a linear gradient image of the same size as the input image
Declara Denis's avatar
Declara Denis committed
132
        _solver.resetSolution();
133
    }
134
}