predicatevolumeexplorer.cpp 8.53 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 
// 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.
// 
// ================================================================================================

25
#include "predicatevolumeexplorer.h"
26
27
28
29
30
31
32
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"

#include "core/datastructures/facegeometry.h"
#include "core/datastructures/geometrydatafactory.h"
#include "core/datastructures/renderdata.h"
33
#include "core/datastructures/genericimagerepresentationlocal.h"
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include "core/classification/simpletransferfunction.h"
#include "core/pipeline/processordecoratorbackground.h"
#include "core/tools/quadrenderer.h"

#include <algorithm>
#include <functional>

namespace campvis {
    const std::string PredicateVolumeExplorer::loggerCat_ = "CAMPVis.modules.vis.PredicateVolumeExplorer";

    PredicateVolumeExplorer::PredicateVolumeExplorer(IVec2Property* viewportSizeProp)
        : VolumeExplorer(viewportSizeProp, new PointPredicateRaycaster(0))
        , p_inputLabels("InputLabels", "Input Label Image", "", DataNameProperty::READ)
        , p_inputSnr("InputSnr", "Input SNR", "", DataNameProperty::READ)
        , p_inputVesselness("InputVesselness", "Input Vesselness", "", DataNameProperty::READ)
        , p_inputConfidence("InputConfidence", "Input Confidence", "", DataNameProperty::READ)
        , p_histogram("PredicateHistogram", "Point Predicate Histogram")
52
        , p_pbProperties("PredicateBitsetProperties", "Predicate Bitset Properties")
53
54
55
56
57
        , _predicateEvaluation(_viewportSizeProperty)
        , _bitmaskHandle(nullptr)
    {
        p_inputVolume.addSharedProperty(&_predicateEvaluation.p_inputImage);

58
        addProperty(p_inputLabels);
59
60
61
        p_inputLabels.addSharedProperty(&(static_cast<PointPredicateRaycaster*>(_raycaster.getRaycastingProcessor()))->p_inputLabels);
        p_inputLabels.addSharedProperty(&_predicateEvaluation.p_inputLabels);

62
        addProperty(p_inputSnr);
63
64
65
        p_inputSnr.addSharedProperty(&(static_cast<PointPredicateRaycaster*>(_raycaster.getRaycastingProcessor()))->p_inputSnr);
        p_inputSnr.addSharedProperty(&_predicateEvaluation.p_inputSnr);

66
        addProperty(p_inputVesselness);
67
68
69
        p_inputVesselness.addSharedProperty(&(static_cast<PointPredicateRaycaster*>(_raycaster.getRaycastingProcessor()))->p_inputVesselness);
        p_inputVesselness.addSharedProperty(&_predicateEvaluation.p_inputVesselness);

70
        addProperty(p_inputConfidence);
71
72
73
        p_inputConfidence.addSharedProperty(&(static_cast<PointPredicateRaycaster*>(_raycaster.getRaycastingProcessor()))->p_inputConfidence);
        p_inputConfidence.addSharedProperty(&_predicateEvaluation.p_inputConfidence);

74
        addProperty(p_histogram);
75
        p_pbProperties.addPropertyCollection(_predicateEvaluation);
76
        addProperty(p_pbProperties, VALID);
77
78
79
80
81
82
83
84
85
86
87
88

        p_histogram.addSharedProperty(&(static_cast<PointPredicateRaycaster*>(_raycaster.getRaycastingProcessor())->p_predicateHistogram));
        p_histogram.addSharedProperty(&_predicateEvaluation.p_histogram);
    }

    PredicateVolumeExplorer::~PredicateVolumeExplorer() {
    }

    void PredicateVolumeExplorer::init() {
        VolumeExplorer::init();
        _predicateEvaluation.init();
        _predicateEvaluation.s_invalidated.connect<PredicateVolumeExplorer>(this, &PredicateVolumeExplorer::onProcessorInvalidated);
89
90

        p_enableScribbling.setValue(true);
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
    }

    void PredicateVolumeExplorer::deinit() {
        _predicateEvaluation.s_invalidated.disconnect(this);
        _predicateEvaluation.deinit();
        _bitmaskHandle = DataHandle(0);
        VolumeExplorer::deinit();
    }

    void PredicateVolumeExplorer::updateResult(DataContainer& data) {
        // launch sub-renderers if necessary
        if (getInvalidationLevel() & BITSET_INVALID) {
            _predicateEvaluation.process(data);
            
            ImageRepresentationLocal::ScopedRepresentation repLocal(data, _predicateEvaluation.p_outputImage.getValue());
            _bitmaskHandle = repLocal.getDataHandle();
            validate(BITSET_INVALID);
        }

        VolumeExplorer::updateResult(data);
        validate(INVALID_RESULT);
    }

    void PredicateVolumeExplorer::onPropertyChanged(const AbstractProperty* prop) {
        if (prop == &p_inputVolume || prop == &p_histogram) {
            invalidate(BITSET_INVALID);
        }

        VolumeExplorer::onPropertyChanged(prop);
    }

    void PredicateVolumeExplorer::onProcessorInvalidated(AbstractProcessor* processor) {
        // make sure to only invalidate ourself if the invalidation is not triggered by us
        // => the _locked state is a trustworthy source for this information :)
        if (! isLocked()) {
            if (processor == &_predicateEvaluation) {
                invalidate(BITSET_INVALID);
            }

            invalidate(AbstractProcessor::INVALID_RESULT);
        }

        VolumeExplorer::onProcessorInvalidated(processor);
    }

    void PredicateVolumeExplorer::onEvent(tgt::Event* e) {
137
138
139
140
141
142
143
        bool doHistogramUpdateFromScribbles = false;

        // intercept the finishing of a scribble paint, so that we can trigger the predicate histogram update.
        if (typeid(*e) == typeid(tgt::MouseEvent)) {
            tgt::MouseEvent* me = static_cast<tgt::MouseEvent*>(e);
            if (_scribblePointer != nullptr && me->action() == tgt::MouseEvent::RELEASED) {
                doHistogramUpdateFromScribbles = true;
144
145
146
147
            }
        }

        VolumeExplorer::onEvent(e);
148
149
150

        if (doHistogramUpdateFromScribbles)
            updatePredicateHistogramFromScribbles();
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
    }

    void PredicateVolumeExplorer::updatePredicateHistogramFromScribbles() {
        std::vector<int> yesHistogram = computeBitHistogram(_yesScribbles);
        std::vector<int> noHistogram = computeBitHistogram(_noScribbles);
        std::vector<float> differences(yesHistogram.size());

        // compute differences, their sum and their absolute sum
        int diffSumAbs = 0;
        std::transform(
            yesHistogram.begin(), yesHistogram.end(), noHistogram.begin(), differences.begin(), 
            [&] (int a, int b) -> int { diffSumAbs += std::abs(a - b); return a - b; });

        if (diffSumAbs == 0)
            return;

        // compute 
        float quantile = .25f / static_cast<float>(diffSumAbs);
        std::for_each(differences.begin(), differences.end(), [&] (float& f) { f *= quantile; });
        p_histogram.adjustImportances(differences, p_histogram.getCurrentHistogramDistribution());

        invalidate(INVALID_RESULT);
    }

175
    std::vector<int> PredicateVolumeExplorer::computeBitHistogram(const std::vector<tgt::vec3>& voxels) {
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
        std::vector<int> toReturn = std::vector<int>(p_histogram.getPredicateHistogram()->getPredicates().size(), 0);

        if (_bitmaskHandle.getData() != nullptr) {
            const ImageData* id = static_cast<const ImageData*>(_bitmaskHandle.getData());
            if (const GenericImageRepresentationLocal<BitmaskType, 1>* repLocal = id->getRepresentation< GenericImageRepresentationLocal<BitmaskType, 1> >()) {
                // traverse all voxels and for each bitmask check each bit whether its set
                for (size_t i = 0; i < voxels.size(); ++i) {
                    BitmaskType bm = repLocal->getElement(voxels[i]);

                    for (size_t bit = 0; bit < toReturn.size(); ++bit) {
                        // if bit is set, increment bit counter
                        if (bm & 1 << bit)
                            ++(toReturn[bit]);
                    }
                }
            }
        }

        return toReturn;
    }

}