The expiration time for new job artifacts in CI/CD pipelines is now 30 days (GitLab default). Previously generated artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

abstracttransferfunction.cpp 6.84 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
        delete _intensityHistogram;
80
81
82
83
84
85
86
87
88
89
90
    }

    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);
91
            if (_texture == 0 || _dirtyTexture) {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
                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);
    }

107
108
109
110
111
112
113
114
115
    void AbstractTransferFunction::uploadTexture() {
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            if (_texture == 0 || _dirtyTexture) {
                createTexture();
            }
        }
    }

116
    void AbstractTransferFunction::setIntensityDomain(const tgt::vec2& newDomain) {
117
        tgtAssert(newDomain.x <= newDomain.y, "Intensity domain is not a valid interval.");
118
119
120
121
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _intensityDomain = newDomain;
        }
122
        _dirtyHistogram = true;
schultezub's avatar
schultezub committed
123
        s_changed();
124
125
    }

126
127
128
129
    const tgt::vec2& AbstractTransferFunction::getIntensityDomain() const {
        return _intensityDomain;
    }

130
131
132
133
    const tgt::Texture* AbstractTransferFunction::getTexture() {
        // TODO:    lock here or in createTexture?
        {
            tbb::mutex::scoped_lock lock(_localMutex);
134
            if (_texture == 0 || _dirtyTexture) {
135
136
137
138
139
140
                createTexture();
            }
        }
        return _texture;
    }

141
    DataHandle AbstractTransferFunction::getImageHandle() const {
142
143
144
        return _imageHandle;
    }

145
146
    void AbstractTransferFunction::setImageHandle(const DataHandle& imageHandle) {
        _imageHandle = imageHandle;
147
148
149
150
151
152
153
        _dirtyHistogram = true;
    }

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

154
155
        if (_imageHandle.getData() != 0) {
            const ImageDataLocal* idl = dynamic_cast<const ImageDataLocal*>(_imageHandle.getData());
156
157
158
159
160
161
162
163
            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));
            }
        }
164
165

        _dirtyHistogram = false;
166
167
168
169
170
171
172
173
174
175
    }

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

        return _intensityHistogram;
    }

176
177

}