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

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

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

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
37
#include "core/datastructures/renderdata.h"
38 39 40
#include "core/pipeline/processordecoratorbackground.h"

#include "core/classification/simpletransferfunction.h"
41 42
#include "core/classification/geometry1dtransferfunction.h"
#include "core/classification/tfgeometry1d.h"
43 44 45 46 47 48

#include "core/tools/quadrenderer.h"

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

49
    GenericOption<std::string> viewOptions[12] = {
50 51
        GenericOption<std::string>("us", "Ultrasound Only"),
        GenericOption<std::string>("smoothed", "Smoothed US Only"),
52
        GenericOption<std::string>("cm", "Confidence Map US Only"),
53 54
        GenericOption<std::string>("mappingSaturationHSV", "Mapping Uncertainty to Saturation (HSV)"),
        GenericOption<std::string>("mappingSaturationHSL", "Mapping Uncertainty to Saturation (HSL)"),
55 56 57
        GenericOption<std::string>("mappingSaturationTSL", "Mapping Uncertainty to Saturation (TSL)"),
        GenericOption<std::string>("mappingChromacityHCL", "Mapping Uncertainty to Chromacity (HCL)"),
        GenericOption<std::string>("mappingChromacityHCY", "Mapping Uncertainty to Chromacity (HCY)"),
58 59
        GenericOption<std::string>("mappingLAB", "Mapping Uncertainty L*a*b*"),
        GenericOption<std::string>("mappingHunterLAB", "Mapping Uncertainty Hunter L*a*b*"),
60 61
        GenericOption<std::string>("mappingSharpness", "Mapping Uncertainty to Sharpness"),
        GenericOption<std::string>("pixelate", "Pixelate (Experimental)")
62 63
    };

64 65
    AdvancedUsFusion::AdvancedUsFusion(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
66
        , p_usImageId("UsImageId", "Ultrasound Input Image", "", DataNameProperty::READ, AbstractProcessor::INVALID_PROPERTIES | AbstractProcessor::INVALID_RESULT)
67
        , p_blurredImageId("BlurredImageId", "Blurred Ultrasound Image", "", DataNameProperty::READ)
68 69 70
        , p_gradientImageID("GradientImageId", "Gradient Input Image", "", DataNameProperty::READ)
        , p_confidenceImageID("ConfidenceImageId", "Confidence Map Input", "", DataNameProperty::READ)
        , p_targetImageID("targetImageID", "Output Image", "", DataNameProperty::WRITE)
71
        , p_sliceNumber("sliceNumber", "Slice Number", 0, 0, 0)
72
        , p_transferFunction("transferFunction", "Transfer Function", new SimpleTransferFunction(256))
73
        , p_confidenceTF("ConfidenceTF", "Confidence to Uncertainty TF", new Geometry1DTransferFunction(256))
74
        , p_view("View", "Image to Render", viewOptions, 12)
75 76
        , p_confidenceScaling("ConfidenceScaling", "Confidence Scaling", 1.f, .001f, 1000.f, 0.1f)
        , p_blurredScaling("BlurredScaling", "Blurred Scaling", 1.f, .001f, 1000.f, 0.1f)
77
        , p_hue("Hue", "Hue for Uncertainty Mapping", .15f, 0.f, 1.f)
78
        , p_use3DTexture("Use3DTexture", "Use 3D Texture", false)
79 80 81
        , _shader(0)
    {
        addProperty(&p_usImageId);
82
        addProperty(&p_blurredImageId);
83 84
        addProperty(&p_gradientImageID);
        addProperty(&p_confidenceImageID);
85
        addProperty(&p_blurredScaling);
86 87 88
        addProperty(&p_targetImageID);
        addProperty(&p_sliceNumber);
        addProperty(&p_transferFunction);
89
        addProperty(&p_confidenceTF);
90
        addProperty(&p_view);
91 92
        addProperty(&p_confidenceScaling);
        addProperty(&p_hue);
93

94 95 96
        Geometry1DTransferFunction* tf = static_cast<Geometry1DTransferFunction*>(p_confidenceTF.getTF());
        tf->addGeometry(TFGeometry1D::createQuad(tgt::vec2(0.f, 1.f), tgt::col4(0, 0, 0, 96), tgt::col4(0, 0, 0, 0)));

97 98 99 100 101 102 103 104 105
        decoratePropertyCollection(this);
    }

    AdvancedUsFusion::~AdvancedUsFusion() {

    }

    void AdvancedUsFusion::init() {
        VisualizationProcessor::init();
106
        _shader = ShdrMgr.loadSeparate("core/glsl/passthrough.vert", "modules/advancedusvis/glsl/advancedusfusion.frag", generateHeader(), false);
107 108 109 110 111 112 113 114 115 116 117
        _shader->setAttributeLocation(0, "in_Position");
        _shader->setAttributeLocation(1, "in_TexCoord");
    }

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

    void AdvancedUsFusion::process(DataContainer& data) {
        ImageRepresentationGL::ScopedRepresentation img(data, p_usImageId.getValue());
118
        ImageRepresentationGL::ScopedRepresentation blurred(data, p_blurredImageId.getValue());
119 120
        ImageRepresentationGL::ScopedRepresentation confidence(data, p_confidenceImageID.getValue());

121
        if (img != 0 && blurred != 0 && confidence != 0) {
122
            if (img->getDimensionality() >= 2) {
123
                FramebufferActivationGuard fag(this);
124 125
                createAndAttachColorTexture();
                createAndAttachDepthTexture();
126 127 128

                _shader->activate();
                decorateRenderProlog(data, _shader);
129 130
                if (p_use3DTexture.getValue())
                    _shader->setUniform("_sliceNumber", p_sliceNumber.getValue());
131
                _shader->setUniform("_viewIndex", p_view.getValue());
132 133
                _shader->setUniform("_confidenceScaling", p_confidenceScaling.getValue());
                _shader->setUniform("_hue", p_hue.getValue());
134
                _shader->setUniform("_blurredScale", 1.f / p_blurredScaling.getValue());
135
                
136
                tgt::TextureUnit usUnit, blurredUnit, confidenceUnit, tfUnit, tf2Unit;
137 138 139
                img->bind(_shader, usUnit, "_usImage", "_usTextureParams");
                blurred->bind(_shader, blurredUnit, "_blurredImage", "_blurredTextureParams");
                confidence->bind(_shader, confidenceUnit, "_confidenceMap", "_confidenceTextureParams");
140
                p_transferFunction.getTF()->bind(_shader, tfUnit);
141
                p_confidenceTF.getTF()->bind(_shader, tf2Unit, "_confidenceTF", "_confidenceTFParams");
142 143 144 145 146 147 148 149

                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                QuadRdr.renderQuad();

                decorateRenderEpilog(_shader);
                _shader->deactivate();
                tgt::TextureUnit::setZeroUnit();

150
                data.addData(p_targetImageID.getValue(), new RenderData(_fbo));
151 152 153 154 155 156 157 158 159
            }
            else {
                LERROR("Input image must have dimensionality of 3.");
            }
        }
        else {
            LERROR("No suitable input image found.");
        }

160
        validate(INVALID_RESULT);
161 162
    }

163
    void AdvancedUsFusion::updateProperties(DataContainer dc) {
164
        ScopedTypedData<ImageData> img(dc, p_usImageId.getValue());
165 166 167

        p_transferFunction.getTF()->setImageHandle(img.getDataHandle());
        const tgt::svec3& imgSize = img->getSize();
168
        if (p_sliceNumber.getMaxValue() != imgSize.z - 1){
169
            p_sliceNumber.setMaxValue(static_cast<int>(imgSize.z) - 1);
170
        }
171 172 173
        p_use3DTexture.setValue(img->getDimensionality() == 3);

        validate(AbstractProcessor::INVALID_PROPERTIES);
174 175 176 177 178 179 180
    }

    std::string AdvancedUsFusion::generateHeader() const {
        std::string toReturn = getDecoratedHeader();
        if (p_use3DTexture.getValue())
            toReturn += "#define USE_3D_TEX 1\n";
        return toReturn;
181 182 183
    }

}