The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

scanlinedistributioncomputation.cpp 6.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#include "scanlinedistributioncomputation.h"
26 27 28
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/renderdata.h"
#include "core/pipeline/processordecoratorbackground.h"

#include "core/classification/simpletransferfunction.h"
#include "core/classification/geometry1dtransferfunction.h"
#include "core/classification/tfgeometry1d.h"

#include "core/tools/quadrenderer.h"

namespace campvis {
    const std::string ScanlineDistributionComputation::loggerCat_ = "CAMPVis.modules.vis.ScanlineDistributionComputation";


    ScanlineDistributionComputation::ScanlineDistributionComputation(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
        , p_inputImage("InputImage", "Input Image", "", DataNameProperty::READ)
        , p_outputImage("OutputImage", "Output Image", "predicatemask", DataNameProperty::WRITE)
49
        , p_scanningDirection("ScanningDirection", "US Scanning Direction", cgt::vec3(0.f, 1.f, 0.f), cgt::vec3(-1.f), cgt::vec3(1.f))
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        , p_threshold("Threshold", "Threshold", .1f, .01f, 1.f)
        , p_stepSize("StepSize", "Step Size", 2.f, 1.f, 16.f, 1.f, 1)
        , _shader(0)
    {
        addProperty(p_inputImage, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_outputImage);

        addProperty(p_scanningDirection);
        addProperty(p_threshold);
        addProperty(p_stepSize);
    }

    ScanlineDistributionComputation::~ScanlineDistributionComputation() {

    }

    void ScanlineDistributionComputation::init() {
        VisualizationProcessor::init();

        _shader = ShdrMgr.loadWithCustomGlslVersion("core/glsl/passthrough.vert", "", "modules/advancedusvis/glsl/scanlinedistributioncomputation.frag", generateHeader(), "400");
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");

        _viewportSizeProperty->s_changed.disconnect(this);
        setPropertyInvalidationLevel(*_viewportSizeProperty, VALID);
    }

    void ScanlineDistributionComputation::deinit() {
        ShdrMgr.dispose(_shader);
        VisualizationProcessor::deinit();
    }

    void ScanlineDistributionComputation::updateResult(DataContainer& dataContainer) {
        ImageRepresentationGL::ScopedRepresentation img(dataContainer, p_inputImage.getValue());

        if (img != 0 && _shader != 0) {
86 87
            cgt::svec3 size = img->getSize() / static_cast<size_t>(2);
            cgt::ivec2 viewportSize = size.xy();
88

89
            cgt::TextureUnit inputUnit, gradientUnit;
90 91 92
            inputUnit.activate();

            // create texture for result
93
            cgt::Texture* distanceTexture = new cgt::Texture(0, cgt::ivec3(size), GL_RGB, GL_RGB16F, GL_FLOAT, cgt::Texture::LINEAR);
94
            distanceTexture->uploadTexture();
95
            distanceTexture->setWrapping(cgt::Texture::CLAMP);
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

            // activate shader and bind textures
            _shader->activate();
            _shader->setUniform("_scanningDirection", p_scanningDirection.getValue());
            _shader->setUniform("_intensityThreshold", p_threshold.getValue());
            _shader->setUniform("_stepSize", p_stepSize.getValue());

            img->bind(_shader, inputUnit);

            // activate FBO and attach texture
            _fbo->activate();
            glViewport(0, 0, static_cast<GLsizei>(viewportSize.x), static_cast<GLsizei>(viewportSize.y));

            // render quad to compute difference measure by shader
            for (int z = 0; z < static_cast<int>(size.z); ++z) {
                float zTexCoord = static_cast<float>(z)/static_cast<float>(size.z) + .5f/static_cast<float>(size.z);
                _shader->setUniform("_zTexCoord", zTexCoord);
                _fbo->attachTexture(distanceTexture, GL_COLOR_ATTACHMENT0, 0, z);
                QuadRdr.renderQuad();
            }
            _fbo->detachAll();
            _fbo->deactivate();
            _shader->deactivate();


            // put resulting image into DataContainer
            ImageData* id = new ImageData(3, size, 3);
            ImageRepresentationGL::create(id, distanceTexture);
            id->setMappingInformation(img->getParent()->getMappingInformation());
            dataContainer.addData(p_outputImage.getValue(), id);

127
            cgt::TextureUnit::setZeroUnit();
128 129 130
            LGL_ERROR;
        }
        else {
131
            LDEBUG("No suitable input image found.");
132 133 134 135 136 137 138 139 140 141 142 143
        }
    }

    void ScanlineDistributionComputation::updateProperties(DataContainer dataContainer) {
    }

    std::string ScanlineDistributionComputation::generateHeader() const {
        std::string toReturn = "";
        return toReturn;
    }

}