abstracttransferfunction.cpp 7.69 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"

schultezub's avatar
schultezub committed
32
#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
#include "core/datastructures/imagerepresentationlocal.h"
40

schultezub's avatar
schultezub committed
41
namespace campvis {
42
43
44
    
    class IntensityHistogramGenerator {
    public:
45
        IntensityHistogramGenerator(const ImageRepresentationLocal* intensityData, AbstractTransferFunction::IntensityHistogramType* histogram)
46
47
48
49
50
51
52
53
54
55
56
57
            : _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:
58
        const ImageRepresentationLocal* _intensityData;
59
60
61
62
        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"*/, const std::string& transFuncParamsUniform /*= "_transferFunctionParameters"*/) {
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
            }
        }

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

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

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

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

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

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

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

160
161
162
163
164
    void AbstractTransferFunction::setImageHandle(DataHandle imageHandle) {
        tgtAssert(
            imageHandle.getData() == 0 || dynamic_cast<const ImageData*>(imageHandle.getData()) != 0, 
            "The data in the image handle must either be 0 or point to a valid ImageData object!");

165
        _imageHandle = imageHandle;
166
        _dirtyHistogram = true;
167
        s_imageHandleChanged();
168
169
170
171
172
173
    }

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

174
175
176
177
        ImageRepresentationLocal::ScopedRepresentation repLocal(_imageHandle);
        if (repLocal != 0) {
            float mins = _intensityDomain.x;
            float maxs = _intensityDomain.y;
178
            size_t numBuckets = std::min(WeaklyTypedPointer::numBytes(repLocal->getWeaklyTypedPointer()._baseType) << 8, static_cast<size_t>(512));
179
180
            _intensityHistogram = new IntensityHistogramType(&mins, &maxs, &numBuckets);
            tbb::parallel_for(tbb::blocked_range<size_t>(0, repLocal->getNumElements()), IntensityHistogramGenerator(repLocal, _intensityHistogram));
181
        }
182
183

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

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

        return _intensityHistogram;
    }

194
195

}