simpletransferfunction.cpp 3.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-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// 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
17
// 
18
19
20
21
// 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.
22
23
24
// 
// ================================================================================================

25
26
27
28
29
#include "simpletransferfunction.h"

#include "tgt/assert.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
30
#include "tgt/texture.h"
31
32
#include "tgt/textureunit.h"

schultezub's avatar
schultezub committed
33
namespace campvis {
34

schultezub's avatar
schultezub committed
35
    const std::string SimpleTransferFunction::loggerCat_ = "CAMPVis.core.classification.SimpleTransferFunction";
36
37
38

    SimpleTransferFunction::SimpleTransferFunction(size_t size, const tgt::vec2& intensityDomain /*= tgt::vec2(0.f, 1.f)*/) 
        : AbstractTransferFunction(tgt::svec3(size, 1, 1), intensityDomain)
39
        , _leftColor(0, 0, 0, 255)
40
41
42
43
44
45
46
47
        , _rightColor(255)
    {
    }

    SimpleTransferFunction::~SimpleTransferFunction() {

    }

48
49
50
51
52
53
54
    SimpleTransferFunction* SimpleTransferFunction::clone() const {
        SimpleTransferFunction* toReturn = new SimpleTransferFunction(_size.x, _intensityDomain);
        toReturn->setLeftColor(_leftColor);
        toReturn->setRightColor(_rightColor);
        return toReturn;
    }

55
56
57
58
59
60
61
62
63
    size_t SimpleTransferFunction::getDimensionality() const {
        return 1;
    }

    void SimpleTransferFunction::createTexture() {
        delete _texture;

        GLenum dataType = GL_UNSIGNED_BYTE;
        _texture = new tgt::Texture(_size, GL_RGBA, dataType, tgt::Texture::LINEAR);
schultezub's avatar
schultezub committed
64
        _texture->setWrapping(tgt::Texture::CLAMP_TO_EDGE);
65
66
67

        tgt::col4 diff = _rightColor - _leftColor;
        for (size_t i = 0; i < _size.x; ++i) {
68
            float multiplier = static_cast<float>(i) / (_size.x - 1);
69
70
71
72
73
74
75
            tgt::col4& texel = _texture->texel<tgt::col4>(i);
            for (size_t j = 0; j < 4; ++j) {
                texel[j] = static_cast<uint8_t>(_leftColor[j] + (static_cast<float>(diff[j]) * multiplier));
            }
        }

        _texture->uploadTexture();
76
        _dirtyTexture = false;
77
78
79
80
81
82
83
    }

    void SimpleTransferFunction::setLeftColor(const tgt::col4& color) {
        {
            tbb::mutex::scoped_lock lock(_localMutex);
            _leftColor = color;
        }
84
        _dirtyTexture = true;
schultezub's avatar
schultezub committed
85
        s_changed();
86
87
88
89
90
    }

    void SimpleTransferFunction::setRightColor(const tgt::col4& color) {
        {
            tbb::mutex::scoped_lock lock(_localMutex);
schultezub's avatar
schultezub committed
91
            _rightColor = color;
92
        }
93
        _dirtyTexture = true;
schultezub's avatar
schultezub committed
94
95
96
97
98
99
100
101
102
        s_changed();
    }

    const tgt::col4& SimpleTransferFunction::getLeftColor() const {
        return _leftColor;
    }

    const tgt::col4& SimpleTransferFunction::getRightColor() const {
        return _rightColor;
103
104
    }

105

106
}