pointpredicatehistogram.cpp 6.19 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// 
// 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 "pointpredicatehistogram.h"

#include "core/tools/stringutils.h"

namespace campvis {

    const std::string PointPredicateHistogram::loggerCat_ = "CAMPVis.modules.advancedusvis.PointPredicateHistogram";;


    PointPredicateHistogram::PointPredicateHistogram() {

    }

    PointPredicateHistogram::~PointPredicateHistogram() {

    }

    void PointPredicateHistogram::addPredicate(AbstractPointPredicate* predicateToAdd) {
        predicateToAdd->p_importance.setValue(_predicates.empty() ? 1.f : 0.f);
        predicateToAdd->s_configurationChanged.connect(this, &PointPredicateHistogram::onPredicateConfigurationChanged);
        predicateToAdd->s_enabledChanged.connect(this, &PointPredicateHistogram::onPredicateEnabledChanged);

        _predicates.push_back(predicateToAdd);
48
        s_headerChanged.emitSignal();
49
50
51
52
53
54
55
56
57
58
59
    }

    void PointPredicateHistogram::removePredicate(size_t i) {
        tgtAssert(i < _predicates.size(), "Index out of bounds!");

        _predicates[i]->s_configurationChanged.disconnect(this);
        _predicates[i]->s_enabledChanged.disconnect(this);
        _predicates[i]->deinit();
        delete _predicates[i];

        _predicates.erase(_predicates.begin() + i);
60
        s_headerChanged.emitSignal();
61
62
    }

63
    void PointPredicateHistogram::resetPredicates(bool resetColors) {
64
65
66
        float imp = 1.f / _predicates.size();
        for (size_t i = 0; i < _predicates.size(); ++i) {
            _predicates[i]->p_importance.setValue(imp);
67
68
            if (resetColors)
                _predicates[i]->p_color.setValue(tgt::vec2(0.f));
69
70
        }

71
        s_configurationChanged.emitSignal();
72
73
74
75
76
77
78
79
80
81
82
83
84
    }

    std::vector<AbstractPointPredicate*>& PointPredicateHistogram::getPredicates() {
        return _predicates;
    }

    std::string PointPredicateHistogram::getGlslHeader() const {
        std::string toReturn;
        for (size_t i = 0; i < _predicates.size(); ++i) {
            toReturn += _predicates[i]->getGlslHeader();
        }

        toReturn +=
85
            "vec4 performPredicateBasedShading(" + _predicateFunctionArgumentString + ") {\n"
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
            "    float impCount = 0.0;\n"
            "    float hueCount = 0.0;\n"
            "    vec4 impSum = vec4(0.0, 0.0, 0.0, 1.0);\n";

        for (size_t i = 0; i < _predicates.size(); ++i) {
            if (_predicates[i]->isEnabled()) {
                toReturn += 
                    "    if " + _predicates[i]->getPredicateEvaluationGlslString() + " {\n"
                    "        float imp = pow(" + _predicates[i]->getImportanceUniformName() + " * " + StringUtils::toString(_predicates.size()) + ", 2.0);\n"
                    "        impSum.x += " + _predicates[i]->getColorUniformName() + ".x * " + _predicates[i]->getColorUniformName() + ".y * imp;\n"
                    "        impSum.y += " + _predicates[i]->getColorUniformName() + ".y * imp;\n"
                    "        impSum.z += " + _predicates[i]->getIntensityHackUniformName() + ";\n"
                    "        impSum.a += 1.0;\n"
                    "        hueCount += " + _predicates[i]->getColorUniformName() + ".y * imp;\n"
                    "        impCount += imp;\n"
                    "    }\n";
            }
        }

        toReturn +=
            "    if (impCount > 0.0) {\n"
            "        impSum.x /= hueCount;\n"
            "        impSum.y /= impCount;\n"
            "        impSum.a = impCount / impSum.a;\n"
            "    }\n"
            "    else\n"
            "        impSum = vec4(0.0);\n"
            "    return impSum;\n"
            "}\n\n";

        toReturn +=
117
            "uint computePredicateBitfield(" + _predicateFunctionArgumentString + ") {\n"
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
            "    uint toReturn = 0U;\n";

        for (size_t i = 0; i < _predicates.size(); ++i) {
            toReturn += 
                "    if " + _predicates[i]->getPredicateEvaluationGlslString() + " {\n"
                "        toReturn = bitfieldInsert(toReturn, 0xFFFFFFFF, " + StringUtils::toString(i) + ", 1);\n"
                "    }\n";
        }

        toReturn +=
            "    return toReturn;\n"
            "}\n\n";

        return toReturn;
    }

    void PointPredicateHistogram::setupRenderShader(tgt::Shader* shader) const {
        for (size_t i = 0; i < _predicates.size(); ++i) {
            //if (_predicates[i]->isEnabled())
                _predicates[i]->setupShader(shader);
        }
    }

    void PointPredicateHistogram::onPredicateConfigurationChanged() {
142
        s_configurationChanged.emitSignal();
143
144
145
    }

    void PointPredicateHistogram::onPredicateEnabledChanged() {
146
        s_headerChanged.emitSignal();
147
148
149
150
151
152
153
154
155
156
157
158
    }

    void PointPredicateHistogram::lock() {
        for (size_t i = 0; i < _predicates.size(); ++i)
            _predicates[i]->lockAllProperties();
    }

    void PointPredicateHistogram::unlock() {
        for (size_t i = 0; i < _predicates.size(); ++i)
            _predicates[i]->unlockAllProperties();
    }

159
160
161
162
    void PointPredicateHistogram::setPredicateFunctionArgumentString(const std::string& pfas) {
        _predicateFunctionArgumentString = pfas;
    }

163
}