abstracttransferfunction.h 6.27 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
#ifndef ABSTRACTTRANSFERFUNCTION_H__
#define ABSTRACTTRANSFERFUNCTION_H__

#include "sigslot/sigslot.h"
#include "tgt/vector.h"
30
31
#include <tbb/atomic.h>
#include <tbb/mutex.h>
32

33
#include "core/coreapi.h"
34
35
36
#include "core/datastructures/datahandle.h"
#include "core/tools/concurrenthistogram.h"

37
38
39
40
#include <string>

namespace tgt {
    class Shader;
41
    class Texture;
42
43
44
    class TextureUnit;
}

schultezub's avatar
schultezub committed
45
namespace campvis {
46
47
48
49
50
51
52
53
54
55
56
57
58
59

    /**
     * Abstract base class for transfer functions.
     * 
     * The granularity of the transfer function is determined by its size which is directly mapped to 
     * the OpenGL texture size. During classification the transfer function is mapped to the intensity 
     * domain.
     * 
     * \note    Transfer function objects are supposed to be thread-safe as follows:
     *          a) Access to non-OpenGL internals is protected by the local mutex.
     *          b) All OpenGL-related methods must be called by a thread with a valid and locked OpenGL
     *             context. Even though other internals might be changed meanwhile, this ensures that
     *             the OpenGL stuff (e.g. the texture) stays valid for this time.
     */
60
    class CAMPVIS_CORE_API AbstractTransferFunction {
61
    public:
62
63
        typedef ConcurrentGenericHistogramND<float, 1> IntensityHistogramType;

64
65
66
67
68
69
70
71
72
73
74
75
        /**
         * Creates a new AbstractTransferFunction.
         * \param   size            Size of the transfer function texture
         * \param   intensityDomain Intensity Domain where the transfer function is mapped to during classification
         */
        AbstractTransferFunction(const tgt::svec3& size, const tgt::vec2& intensityDomain = tgt::vec2(0.f, 1.f));

        /**
         * Destructor, make sure to delete the OpenGL texture beforehand by calling deinit() with a valid OpenGL context!
         */
        virtual ~AbstractTransferFunction();

76
77
78
79
80
81
        /**
         * Clones this transfer function.
         * \return  A clone of this transfer function, caller takes ownership of the pointer.
         */
        virtual AbstractTransferFunction* clone() const = 0;

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        /**
         * Deletes the OpenGL texture, hence, this methods has to be called from a thread with a valid OpenGL context!
         */
        virtual void deinit();

        /**
         * Returns the dimensionality of the transfer function.
         * \return  The dimensionality of the transfer function.
         */
        virtual size_t getDimensionality() const = 0;

        /**
         * Binds the transfer function OpenGL texture to the given texture and sets up uniforms.
         * \note    Calling thread must have a valid OpenGL context.
         * \param   shader                      Shader used for rendering
         * \param   texUnit                     Texture unit to bind texture to
98
99
         * \param   transFuncUniform            Uniform name to store the TF sampler
         * \param   transFuncParamsUniform      Uniform name to store the TF parameters struct
100
         */
101
        void bind(tgt::Shader* shader, const tgt::TextureUnit& texUnit, const std::string& transFuncUniform = "_transferFunction", const std::string& transFuncParamsUniform = "_transferFunctionParams");
102
        
103
        /**
104
         * Sets the intensity domain where the transfer function is mapped to during classification.
105
106
107
108
         * \param   newDomain   new intensity domain
         */
        void setIntensityDomain(const tgt::vec2& newDomain);

109
110
111
112
113
114
        /**
         * Returns the intensity domain where the transfer function is mapped to during classification.
         * \return _intensityDomain
         */
        const tgt::vec2& getIntensityDomain() const;

115
116
117
118
119
120
        /**
         * Gets the OpenGL lookup texture storing the TF.
         * \note    Calling thread must have a valid OpenGL context.
         * \return  _texture
         */
        const tgt::Texture* getTexture();
121
        
122
        /// Signal emitted when transfer function has changed.
schultezub's avatar
schultezub committed
123
        sigslot::signal0<> s_changed;
124
125
        /// Signal emitted when the intensity domain has changed
        sigslot::signal0<> s_intensityDomainChanged;
126

127
    protected:
128
129
130
131
132
        /**
         * Computes the intensity histogram;
         */
        void computeIntensityHistogram() const;

133
134
135
136
137
138
        /**
         * Creates the texture and uploads it to OpenGL.
         * Gets called by bind() with the local mutex already acquired.
         */
        virtual void createTexture() = 0;

139
140
141
142
143
        tgt::svec3 _size;                   ///< Size of the transfer function texture
        tgt::vec2 _intensityDomain;         ///< Intensity Domain where the transfer function is mapped to during classification
        tgt::Texture* _texture;             ///< OpenGL lookup texture storing the TF
        tbb::atomic<bool> _dirtyTexture;    ///< Flag whether the OpenGL texture has to be updated

144
        mutable tbb::mutex _localMutex;     ///< mutex protecting the local members
145
146
147
148
149
150
151
152

        static const std::string loggerCat_;

    };

}

#endif // ABSTRACTTRANSFERFUNCTION_H__