abstracttransferfunction.cpp 7.41 KB
Newer Older
1
2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4
5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
8
9
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

30
31
#include "abstracttransferfunction.h"

32
#include "tbb/include/tbb/tbb.h"
33
34
35
#include "tgt/assert.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
36
#include "tgt/texture.h"
37
38
#include "tgt/textureunit.h"

39
40
#include "core/datastructures/imagedatalocal.h"

schultezub's avatar
schultezub committed
41
namespace campvis {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    
    class IntensityHistogramGenerator {
    public:
        IntensityHistogramGenerator(const ImageDataLocal* intensityData, AbstractTransferFunction::IntensityHistogramType* histogram)
            : _intensityData(intensityData)
            , _histogram(histogram)
        {}

        void operator() (const tbb::blocked_range<size_t>& range) const {
            for (size_t i = range.begin(); i != range.end(); ++i) {
                float value = _intensityData->getElementNormalized(i, 0);
                _histogram->addSample(&value);
            }
        }

    protected:
        const ImageDataLocal* _intensityData;
        AbstractTransferFunction::IntensityHistogramType* _histogram;
    };

// ================================================================================================
63

schultezub's avatar
schultezub committed
64
    const std::string AbstractTransferFunction::loggerCat_ = "CAMPVis.core.classification.AbstractTransferFunction";
65
66
67
68
69

    AbstractTransferFunction::AbstractTransferFunction(const tgt::svec3& size, const tgt::vec2& intensityDomain /*= tgt::vec2(0.f, 1.f)*/)
        : _size(size)
        , _intensityDomain(intensityDomain)
        , _texture(0)
70
71
        , _imageHandle(0)
        , _intensityHistogram(0)
72
    {
73
74
        _dirtyTexture = false;
        _dirtyHistogram = false;
75
76
77
78
    }

    AbstractTransferFunction::~AbstractTransferFunction() {
        if (_texture != 0)
79
            LWARNING("Called AbstractTransferFunction dtor without proper deinitialization - you just wasted resources!");
80
        delete _intensityHistogram;
81
82
83
84
85
86
87
    }

    void AbstractTransferFunction::deinit() {
        delete _texture;
        _texture = 0;
    }

88
    void AbstractTransferFunction::bind(tgt::Shader* shader, const tgt::TextureUnit& texUnit, const std::string& transFuncUniform /*= "_transferFunction"*/) {
89
90
        tgtAssert(shader != 0, "Shader must not be 0.");

91
        {
92
            // TODO:    lock here or in createTexture?
93
            tbb::mutex::scoped_lock lock(_localMutex);
94
            if (_texture == 0 || _dirtyTexture) {
95
                shader->deactivate();
96
                createTexture();
97
                shader->activate();
98
99
100
101
102
103
104
105
106
            }
        }

        texUnit.activate();
        _texture->bind();

        bool tmp = shader->getIgnoreUniformLocationError();
        shader->setIgnoreUniformLocationError(true);
        // TODO:    set domain mapping uniforms
107
108
109
110
111
112
113
114
115
116
117
118
119
        shader->setUniform(transFuncUniform + "._texture", texUnit.getUnitNumber());
        switch (getDimensionality()) {
            case 1:
                shader->setUniform(transFuncUniform + "._intensityDomain", tgt::vec2(_intensityDomain));
                break;
            case 2:
                shader->setUniform(transFuncUniform + "._intensityDomainX", tgt::vec2(_intensityDomain));
                break;
            default:
                tgtAssert(false, "Unsupported TF dimensionality!");
                break;
        }
        
120
121
122
        shader->setIgnoreUniformLocationError(tmp);
    }

123
124
125
126
127
128
129
130
131
    void AbstractTransferFunction::uploadTexture() {
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            if (_texture == 0 || _dirtyTexture) {
                createTexture();
            }
        }
    }

132
    void AbstractTransferFunction::setIntensityDomain(const tgt::vec2& newDomain) {
133
        tgtAssert(newDomain.x <= newDomain.y, "Intensity domain is not a valid interval.");
134
135
136
137
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _intensityDomain = newDomain;
        }
138
        _dirtyHistogram = true;
schultezub's avatar
schultezub committed
139
        s_changed();
140
141
    }

142
143
144
145
    const tgt::vec2& AbstractTransferFunction::getIntensityDomain() const {
        return _intensityDomain;
    }

146
147
148
149
    const tgt::Texture* AbstractTransferFunction::getTexture() {
        // TODO:    lock here or in createTexture?
        {
            tbb::mutex::scoped_lock lock(_localMutex);
150
            if (_texture == 0 || _dirtyTexture) {
151
152
153
154
155
156
                createTexture();
            }
        }
        return _texture;
    }

157
    DataHandle AbstractTransferFunction::getImageHandle() const {
158
159
160
        return _imageHandle;
    }

161
162
    void AbstractTransferFunction::setImageHandle(const DataHandle& imageHandle) {
        _imageHandle = imageHandle;
163
        _dirtyHistogram = true;
164
        s_imageHandleChanged();
165
166
167
168
169
170
    }

    void AbstractTransferFunction::computeIntensityHistogram() const {
        delete _intensityHistogram;
        _intensityHistogram = 0;

171
172
        if (_imageHandle.getData() != 0) {
            const ImageDataLocal* idl = dynamic_cast<const ImageDataLocal*>(_imageHandle.getData());
173
174
175
176
177
178
179
180
            if (idl != 0) {
                float mins = _intensityDomain.x;
                float maxs = _intensityDomain.y;
                size_t numBuckets = 512;
                _intensityHistogram = new IntensityHistogramType(&mins, &maxs, &numBuckets);
                tbb::parallel_for(tbb::blocked_range<size_t>(0, idl->getNumElements()), IntensityHistogramGenerator(idl, _intensityHistogram));
            }
        }
181
182

        _dirtyHistogram = false;
183
184
185
186
187
188
189
190
191
192
    }

    const AbstractTransferFunction::IntensityHistogramType* AbstractTransferFunction::getIntensityHistogram() const {
        if (_dirtyHistogram) {
            computeIntensityHistogram();
        }

        return _intensityHistogram;
    }

193
194

}