sliceextractor.cpp 5.89 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.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30 31
#include "sliceextractor.h"
#include "tgt/logmanager.h"
32
#include "tgt/shadermanager.h"
schultezub's avatar
schultezub committed
33
#include "tgt/textureunit.h"
34

schultezub's avatar
schultezub committed
35
#include "core/datastructures/imagedata.h"
36 37
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/imagerepresentationrendertarget.h"
38
#include "core/pipeline/processordecoratorbackground.h"
schultezub's avatar
schultezub committed
39

40 41
#include "core/classification/simpletransferfunction.h"

42 43
#include "core/tools/quadrenderer.h"

schultezub's avatar
schultezub committed
44 45
namespace campvis {
    const std::string SliceExtractor::loggerCat_ = "CAMPVis.modules.vis.SliceExtractor";
schultezub's avatar
schultezub committed
46

47
    SliceExtractor::SliceExtractor(IVec2Property& canvasSize)
schultezub's avatar
schultezub committed
48
        : VisualizationProcessor(canvasSize)
49 50 51 52
        , p_sourceImageID("sourceImageID", "Input Image", "", DataNameProperty::READ)
        , p_targetImageID("targetImageID", "Output Image", "", DataNameProperty::WRITE)
        , p_sliceNumber("sliceNumber", "Slice Number", 0, 0, 0)
        , p_transferFunction("transferFunction", "Transfer Function", new SimpleTransferFunction(256))
schultezub's avatar
schultezub committed
53
        , _shader(0)
schultezub's avatar
schultezub committed
54
    {
55 56 57 58
        addProperty(&p_sourceImageID);
        addProperty(&p_targetImageID);
        addProperty(&p_sliceNumber);
        addProperty(&p_transferFunction);
59 60 61

        //addDecorator(new ProcessorDecoratorBackground());
        decoratePropertyCollection(this);
schultezub's avatar
schultezub committed
62 63 64 65 66 67
    }

    SliceExtractor::~SliceExtractor() {

    }

schultezub's avatar
schultezub committed
68
    void SliceExtractor::init() {
schultezub's avatar
schultezub committed
69
        VisualizationProcessor::init();
70
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "modules/vis/glsl/sliceextractor.frag", "", false);
71 72
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");
schultezub's avatar
schultezub committed
73 74
    }

75
    void SliceExtractor::deinit() {
schultezub's avatar
schultezub committed
76
        VisualizationProcessor::deinit();
77 78 79
        ShdrMgr.dispose(_shader);
    }

schultezub's avatar
schultezub committed
80
    void SliceExtractor::process(DataContainer& data) {
81
        DataContainer::ScopedTypedData<ImageData> img(data, p_sourceImageID.getValue());
schultezub's avatar
schultezub committed
82 83 84

        if (img != 0) {
            if (img->getDimensionality() == 3) {
85 86 87 88 89 90
                if (img.getDataHandle().getTimestamp() != _sourceImageTimestamp) {
                    // source DataHandle has changed
                    updateProperties(img.getDataHandle());
                    _sourceImageTimestamp = img.getDataHandle().getTimestamp();
                }

schultezub's avatar
schultezub committed
91
                const tgt::svec3& imgSize = img->getSize();
92 93 94 95
                ImageData* slice = img->getSubImage(tgt::svec3(0, 0, p_sliceNumber.getValue()), tgt::svec3(imgSize.x, imgSize.y, p_sliceNumber.getValue()+1));

                const ImageRepresentationGL* glData = slice->getRepresentation<ImageRepresentationGL>();
                std::pair<ImageData*, ImageRepresentationRenderTarget*> rt = ImageRepresentationRenderTarget::createWithImageData(_renderTargetSize.getValue());
schultezub's avatar
schultezub committed
96

schultezub's avatar
schultezub committed
97
                _shader->activate();
98
                decorateRenderProlog(data, _shader);
99
                tgt::TextureUnit inputUnit, tfUnit;
schultezub's avatar
schultezub committed
100
                glData->bind(_shader, inputUnit);
101
                p_transferFunction.getTF()->bind(_shader, tfUnit);
schultezub's avatar
schultezub committed
102

103
                rt.second->activate();
schultezub's avatar
schultezub committed
104
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
105
                QuadRdr.renderQuad();
106
                rt.second->deactivate();
schultezub's avatar
schultezub committed
107

108
                decorateRenderEpilog(_shader);
schultezub's avatar
schultezub committed
109
                _shader->deactivate();
110
                tgt::TextureUnit::setZeroUnit();
schultezub's avatar
schultezub committed
111

112 113
                data.addData(p_targetImageID.getValue(), rt.first);
                p_targetImageID.issueWrite();
schultezub's avatar
schultezub committed
114
                delete slice;
schultezub's avatar
schultezub committed
115 116 117 118 119 120 121 122
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
            LERROR("No suitable input image found.");
        }
schultezub's avatar
schultezub committed
123

124
        validate(INVALID_RESULT);
schultezub's avatar
schultezub committed
125
    }
schultezub's avatar
schultezub committed
126

127 128 129
    void SliceExtractor::updateProperties(DataHandle img) {
        p_transferFunction.getTF()->setImageHandle(img);
        const tgt::svec3& imgSize = static_cast<const ImageData*>(img.getData())->getSize();
130 131
        if (p_sliceNumber.getMaxValue() != imgSize.z - 1){
            p_sliceNumber.setMaxValue(imgSize.z - 1);
schultezub's avatar
schultezub committed
132 133 134
        }
    }

schultezub's avatar
schultezub committed
135
}