2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

raycastingprocessor.cpp 7.86 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
32
33
34
35
36
#include "raycastingprocessor.h"

#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"
#include "tgt/textureunit.h"

#include "core/datastructures/imagedata.h"
37
#include "core/datastructures/imagerepresentationrendertarget.h"
38

39
40
41

#include "core/classification/simpletransferfunction.h"

schultezub's avatar
schultezub committed
42
43
namespace campvis {
    const std::string RaycastingProcessor::loggerCat_ = "CAMPVis.modules.vis.RaycastingProcessor";
44

45
    RaycastingProcessor::RaycastingProcessor(IVec2Property& renderTargetSize, const std::string& fragmentShaderFileName, bool bindEntryExitDepthTextures)
schultezub's avatar
schultezub committed
46
        : VisualizationProcessor(renderTargetSize)
47
48
49
50
51
        , p_sourceImageID("sourceImageID", "Input Image", "", DataNameProperty::READ)
        , p_entryImageID("entryImageID", "Input Entry Points Image", "", DataNameProperty::READ)
        , p_exitImageID("exitImageID", "Input Exit Points Image", "", DataNameProperty::READ)
        , p_camera("camera", "Camera")
        , p_transferFunction("transferFunction", "Transfer Function", new SimpleTransferFunction(256))
52
        , p_samplingRate("SamplingRate", "Sampling Rate", 2.f, 0.1f, 10.f)
53
54
        , p_jitterEntryPoints("jitterEntryPoints", "Jitter Entry Points", true)
        , p_jitterStepSizeMultiplier("jitterStepSizeMultiplier", "Jitter Step Size Multiplier", .5f, .1f, 1.f)
55
56
57
        , _fragmentShaderFilename(fragmentShaderFileName)
        , _shader(0)
        , _bindEntryExitDepthTextures(bindEntryExitDepthTextures)
58
        , _sourceImageTimestamp(0)
59
    {
60
61
62
63
64
        addProperty(&p_sourceImageID);
        addProperty(&p_entryImageID);
        addProperty(&p_exitImageID);
        addProperty(&p_camera);  
        addProperty(&p_transferFunction);
65
        addProperty(&p_samplingRate);
66
67
        addProperty(&p_jitterEntryPoints);
        addProperty(&p_jitterStepSizeMultiplier);
68
69
70
71
72
73
74
75
    }

    RaycastingProcessor::~RaycastingProcessor() {

    }

    void RaycastingProcessor::init() {
        VisualizationProcessor::init();
76
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", _fragmentShaderFilename, generateHeader(), false);
77
78
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");
79
80
81
82
83
84
85
86
87
    }

    void RaycastingProcessor::deinit() {
        ShdrMgr.dispose(_shader);
        _shader = 0;
        VisualizationProcessor::deinit();
    }

    void RaycastingProcessor::process(DataContainer& data) {
88
89
90
        ImageRepresentationGL::ScopedRepresentation img(data, p_sourceImageID.getValue());
        ImageRepresentationRenderTarget::ScopedRepresentation entryPoints(data, p_entryImageID.getValue());
        ImageRepresentationRenderTarget::ScopedRepresentation exitPoints(data, p_exitImageID.getValue());
91
92
93

        if (img != 0 && entryPoints != 0 && exitPoints != 0) {
            if (img->getDimensionality() == 3) {
94
95
96
97
98
99
                if (img.getDataHandle().getTimestamp() != _sourceImageTimestamp) {
                    // source DataHandle has changed
                    _sourceImageTimestamp = img.getDataHandle().getTimestamp();
                    p_transferFunction.getTF()->setImageHandle(img.getDataHandle());
                }

100
                if (hasInvalidShader()) {
101
102
                    _shader->setHeaders(generateHeader());
                    _shader->rebuild();
103
                    validate(INVALID_SHADER);
104
105
106
                }

                _shader->activate();
107
108

                _shader->setIgnoreUniformLocationError(true);
109
                decorateRenderProlog(data, _shader);
110
                _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(_renderTargetSize.getValue()));
111
112
                _shader->setUniform("_jitterEntryPoints", p_jitterEntryPoints.getValue());
                _shader->setUniform("_jitterStepSizeMultiplier", p_jitterStepSizeMultiplier.getValue());
113
114
115

                float samplingStepSize = 1.f / (p_samplingRate.getValue() * tgt::max(img->getSize()));
                _shader->setUniform("_samplingStepSize", samplingStepSize);
116

117
                const tgt::Camera& cam = p_camera.getValue();
118
119
                float n = cam.getNearDist();
                float f = cam.getFarDist();
120
                _shader->setUniform("_cameraPosition", cam.getPosition());
121
122
123
124
125
126
                _shader->setUniform("const_to_z_e_1", 0.5f + 0.5f*((f+n)/(f-n)));
                _shader->setUniform("const_to_z_e_2", ((f-n)/(f*n)));
                _shader->setUniform("const_to_z_w_1", ((f*n)/(f-n)));
                _shader->setUniform("const_to_z_w_2", 0.5f*((f+n)/(f-n))+0.5f);
                _shader->setIgnoreUniformLocationError(false);

127
                tgt::TextureUnit volumeUnit, entryUnit, exitUnit, tfUnit;
128
                img->bind(_shader, volumeUnit, "_volume", "_volumeTextureParams");
129
                p_transferFunction.getTF()->bind(_shader, tfUnit);
130
131

                if (! _bindEntryExitDepthTextures) {
132
133
                    entryPoints->bindColorTexture(_shader, entryUnit, "_entryPoints", "_entryParams");
                    exitPoints->bindColorTexture(_shader, exitUnit, "_exitPoints", "_exitParams");
134
                    processImpl(data, img);
135
136
137
                }
                else {
                    tgt::TextureUnit entryUnitDepth, exitUnitDepth;
138
139
                    entryPoints->bind(_shader, entryUnit, entryUnitDepth, "_entryPoints", "_entryPointsDepth", "_entryParams");
                    exitPoints->bind(_shader, exitUnit, exitUnitDepth, "_exitPoints", "_exitPointsDepth", "_exitParams");
140
                    processImpl(data, img);
141
142
                }

143
                decorateRenderEpilog(_shader);
144
145
146
147
148
149
150
151
152
153
154
155
                _shader->deactivate();
                tgt::TextureUnit::setZeroUnit();
                LGL_ERROR;
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
            LERROR("No suitable input image found.");
        }

156
        validate(INVALID_RESULT);
157
158
159
    }

    std::string RaycastingProcessor::generateHeader() const {
160
        std::string toReturn = getDecoratedHeader();
161
162
163
164
        return toReturn;
    }

}