raycastingprocessor.cpp 7.82 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/renderdata.h"
38
39
40

#include "core/classification/simpletransferfunction.h"

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

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

    RaycastingProcessor::~RaycastingProcessor() {

    }

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

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

    void RaycastingProcessor::process(DataContainer& data) {
87
        ImageRepresentationGL::ScopedRepresentation img(data, p_sourceImageID.getValue());
88
89
        DataContainer::ScopedTypedData<RenderData> entryPoints(data, p_entryImageID.getValue());
        DataContainer::ScopedTypedData<RenderData> exitPoints(data, p_exitImageID.getValue());
90
91
92

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

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

                _shader->activate();
106
107

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

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

116
                const tgt::Camera& cam = p_camera.getValue();
117
118
                float n = cam.getNearDist();
                float f = cam.getFarDist();
119
                _shader->setUniform("_cameraPosition", cam.getPosition());
120
121
122
123
124
125
                _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);

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

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

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

155
        validate(INVALID_RESULT);
156
157
158
    }

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

}