abstracttransferfunction.cpp 6.76 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
// ================================================================================================
// 
// This file is part of the TUMVis Visualization Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
//      Christian Schulte zu Berge (christian.szb@in.tum.de)
//      Chair for Computer Aided Medical Procedures
//      Technische Universitt Mnchen
//      Boltzmannstr. 3, 85748 Garching b. Mnchen, Germany
// 
// 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.
// 
// ================================================================================================

29
30
#include "abstracttransferfunction.h"

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

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

40
namespace TUMVis {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    
    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;
    };

// ================================================================================================
62
63
64
65
66
67
68

    const std::string AbstractTransferFunction::loggerCat_ = "TUMVis.core.classification.AbstractTransferFunction";

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

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

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

    void AbstractTransferFunction::bind(tgt::Shader* shader, const tgt::TextureUnit& texUnit, const std::string& textureUniform /*= "_tfTex"*/, const std::string& textureParametersUniform /*= "_tgTextureParameters"*/) {
        // TODO:    lock here or in createTexture?
        {
            tbb::mutex::scoped_lock lock(_localMutex);
92
            if (_texture == 0 || _dirtyTexture) {
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
                createTexture();
            }
        }

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

        bool tmp = shader->getIgnoreUniformLocationError();
        shader->setIgnoreUniformLocationError(true);
        // TODO:    set domain mapping uniforms
        shader->setUniform(textureUniform, texUnit.getUnitNumber());
        shader->setUniform(textureParametersUniform + "._intensityDomain", tgt::vec2(_intensityDomain));
        shader->setIgnoreUniformLocationError(tmp);
    }

    void AbstractTransferFunction::setIntensityDomain(const tgt::vec2& newDomain) {
109
        tgtAssert(newDomain.x <= newDomain.y, "Intensity domain is not a valid interval.");
110
111
112
113
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _intensityDomain = newDomain;
        }
114
        _dirtyHistogram = true;
schultezub's avatar
schultezub committed
115
        s_changed();
116
117
    }

118
119
120
121
    const tgt::vec2& AbstractTransferFunction::getIntensityDomain() const {
        return _intensityDomain;
    }

122
123
124
125
    const tgt::Texture* AbstractTransferFunction::getTexture() {
        // TODO:    lock here or in createTexture?
        {
            tbb::mutex::scoped_lock lock(_localMutex);
126
            if (_texture == 0 || _dirtyTexture) {
127
128
129
130
131
132
                createTexture();
            }
        }
        return _texture;
    }

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    const DataHandle* AbstractTransferFunction::getImageHandle() const {
        return _imageHandle;
    }

    void AbstractTransferFunction::setImageHandle(const DataHandle* imageHandle) {
        delete _imageHandle;

        if (imageHandle == 0)
            _imageHandle = 0;
        else
            _imageHandle = new DataHandle(*imageHandle);

        _dirtyHistogram = true;
    }

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

        if (_imageHandle != 0) {
            const ImageDataLocal* idl = dynamic_cast<const ImageDataLocal*>(_imageHandle->getData());
            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));
            }
        }
162
163

        _dirtyHistogram = false;
164
165
166
167
168
169
170
171
172
173
    }

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

        return _intensityHistogram;
    }

174
175

}