sliceextractor.cpp 5.67 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

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

schultezub's avatar
schultezub committed
47
48
    SliceExtractor::SliceExtractor(GenericProperty<tgt::ivec2>& canvasSize)
        : 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);
schultezub's avatar
schultezub committed
59
60
61
62
63
64
    }

    SliceExtractor::~SliceExtractor() {

    }

schultezub's avatar
schultezub committed
65
    void SliceExtractor::init() {
schultezub's avatar
schultezub committed
66
        VisualizationProcessor::init();
67
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "modules/vis/glsl/sliceextractor.frag", "", false);
schultezub's avatar
schultezub committed
68
69
    }

70
    void SliceExtractor::deinit() {
schultezub's avatar
schultezub committed
71
        VisualizationProcessor::deinit();
72
73
74
        ShdrMgr.dispose(_shader);
    }

schultezub's avatar
schultezub committed
75
    void SliceExtractor::process(DataContainer& data) {
76
        DataContainer::ScopedTypedData<ImageData> img(data, p_sourceImageID.getValue());
schultezub's avatar
schultezub committed
77
78
79

        if (img != 0) {
            if (img->getDimensionality() == 3) {
80
81
82
83
84
85
                if (img.getDataHandle().getTimestamp() != _sourceImageTimestamp) {
                    // source DataHandle has changed
                    updateProperties(img.getDataHandle());
                    _sourceImageTimestamp = img.getDataHandle().getTimestamp();
                }

schultezub's avatar
schultezub committed
86
                const tgt::svec3& imgSize = img->getSize();
87
88
89
90
                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
91

schultezub's avatar
schultezub committed
92
                _shader->activate();
93
                tgt::TextureUnit inputUnit, tfUnit;
schultezub's avatar
schultezub committed
94
                glData->bind(_shader, inputUnit);
95
                p_transferFunction.getTF()->bind(_shader, tfUnit);
schultezub's avatar
schultezub committed
96

97
                rt.second->activate();
schultezub's avatar
schultezub committed
98
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
99
                _shader->setAttributeLocation(0, "in_Position");
100
                _shader->setAttributeLocation(1, "in_TexCoord");
101
                QuadRdr.renderQuad();
102
                rt.second->deactivate();
schultezub's avatar
schultezub committed
103
104

                _shader->deactivate();
105
                tgt::TextureUnit::setZeroUnit();
schultezub's avatar
schultezub committed
106

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

        _invalidationLevel.setValid();
schultezub's avatar
schultezub committed
120
    }
121

122
123
124
    void SliceExtractor::updateProperties(DataHandle img) {
        p_transferFunction.getTF()->setImageHandle(img);
        const tgt::svec3& imgSize = static_cast<const ImageData*>(img.getData())->getSize();
125
126
        if (p_sliceNumber.getMaxValue() != imgSize.z - 1){
            p_sliceNumber.setMaxValue(imgSize.z - 1);
127
128
129
        }
    }

schultezub's avatar
schultezub committed
130
}