11.3.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

multivolumemprrenderer.cpp 11.7 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
// ================================================================================================
// 
// 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 "multivolumemprrenderer.h"

#include "cgt/glmath.h"
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"

#include "core/classification/simpletransferfunction.h"
#include "core/datastructures/cameradata.h"
#include "core/datastructures/geometrydatafactory.h"
#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"
#include "core/datastructures/imageseries.h"
#include "core/datastructures/lightsourcedata.h"
#include "core/datastructures/renderdata.h"
#include "core/datastructures/meshgeometry.h"
#include "core/pipeline/processordecoratorgradient.h"
#include "core/tools/quadrenderer.h"

namespace campvis {
namespace neuro {

    const std::string MultiVolumeMprRenderer::loggerCat_ = "CAMPVis.modules.vis.MultiVolumeMprRenderer";

    MultiVolumeMprRenderer::MultiVolumeMprRenderer(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
        , p_sourceImage1("SourceImage1", "First Input Image", "", DataNameProperty::READ)
        , p_sourceImage2("SourceImage2", "Second Input Image", "", DataNameProperty::READ)
        , p_sourceImage3("SourceImage3", "Third Input Image", "", DataNameProperty::READ)
        , p_camera("Camera", "Camera ID", "camera", DataNameProperty::READ)
        , p_outputImageId("OutputImageId", "Output Image ID", "MultiVolumeMprRenderer.output", DataNameProperty::WRITE)
        , p_transferFunction1("TransferFunction1", "Transfer Function for First image", new SimpleTransferFunction(256))
        , p_transferFunction2("TransferFunction2", "Transfer Function for Second image", new SimpleTransferFunction(256))
        , p_transferFunction3("TransferFunction3", "Transfer Function for Third image", new SimpleTransferFunction(256))
        , p_planeNormal("PlaneNormal", "Clipping Plane Normal", cgt::vec3(0.f, 0.f, 1.f), cgt::vec3(-1.f), cgt::vec3(1.f), cgt::vec3(.1f), cgt::ivec3(2))
60
        , p_planeDistance("PlaneDistance", "Clipping Plane Distance", 0.f, -1000.f, 1000.f, .5f, 1)
61 62 63
        , p_planeSize("PlaneSize", "Clipping Plane Size", 100.f, 0.f, 1000.f, 1.f, 1)
        , p_use2DProjection("Use3dRendering", "Use 3D Rendering instead of 2D", true)
        , p_relativeToImageCenter("RelativeToImageCenter", "Construct Plane Relative to Image Center", true)
64 65 66
        , p_showWireframe("ShowWireframe", "Show Wireframe", true)
        , p_lineWidth("LineWidth", "Line Width", 1.f, .1f, 10.f)
        , p_transparency("Transparency", "Minimum Transparency", 0.5f, 0.f, 1.f)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        , _shader(nullptr)
    {
        addProperty(p_sourceImage1, INVALID_PROPERTIES | INVALID_RESULT);
        addProperty(p_sourceImage2, INVALID_PROPERTIES | INVALID_RESULT);
        addProperty(p_sourceImage3, INVALID_PROPERTIES | INVALID_RESULT);
        addProperty(p_camera);
        addProperty(p_outputImageId);

        addProperty(p_transferFunction1);
        addProperty(p_transferFunction2);
        addProperty(p_transferFunction3);

        addProperty(p_planeNormal);
        addProperty(p_planeDistance);
        addProperty(p_planeSize);
        addProperty(p_use2DProjection, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_relativeToImageCenter);
84 85 86 87

        addProperty(p_showWireframe, INVALID_RESULT | INVALID_SHADER);
        addProperty(p_lineWidth);
        addProperty(p_transparency);
88 89 90 91 92 93 94 95
    }

    MultiVolumeMprRenderer::~MultiVolumeMprRenderer() {

    }

    void MultiVolumeMprRenderer::init() {
        VisualizationProcessor::init();
96
        _shader = ShdrMgr.load("modules/vis/glsl/geometryrenderer.vert", "modules/vis/glsl/geometryrenderer.geom", "modules/neuro/glsl/multivolumemprrenderer.frag", generateHeader());
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    }

    void MultiVolumeMprRenderer::deinit() {
        ShdrMgr.dispose(_shader);
        _shader = nullptr;

        VisualizationProcessor::deinit();
    }

    void MultiVolumeMprRenderer::updateResult(DataContainer& dataContainer) {
        ImageRepresentationGL::ScopedRepresentation image1(dataContainer, p_sourceImage1.getValue());
        ImageRepresentationGL::ScopedRepresentation image2(dataContainer, p_sourceImage2.getValue());
        ImageRepresentationGL::ScopedRepresentation image3(dataContainer, p_sourceImage3.getValue());
        ScopedTypedData<CameraData> camera(dataContainer, p_camera.getValue());


        if (image1 && image2 && image3 && camera) {
            if (p_use2DProjection.getValue() || camera != nullptr) {
                // Construct the clipping plane in world coordinates
                cgt::vec3 n = cgt::normalize(p_planeNormal.getValue());
                cgt::vec3 temp(1.0, 0.0, 0.0);
                if (abs(cgt::dot(temp, n)) > 0.9)
                    temp = cgt::vec3(0.0, 1.0, 0.0);

                cgt::vec3 inPlaneA = cgt::normalize(cgt::cross(n, temp)) * 0.5f * p_planeSize.getValue();
                cgt::vec3 inPlaneB = cgt::normalize(cgt::cross(n, inPlaneA)) * 0.5f * p_planeSize.getValue();
                cgt::vec3 base = (n * -p_planeDistance.getValue());

                // move to image center if wanted
                if (p_relativeToImageCenter.getValue()) {
                    cgt::Bounds b;
                    if (image1)
                        b.addVolume(image1->getParent()->getWorldBounds());
                    if (image2)
                        b.addVolume(image2->getParent()->getWorldBounds());
                    if (image3)
                        b.addVolume(image3->getParent()->getWorldBounds());
                    base += b.center();
                }

                // construct the four texCoords
                std::vector<cgt::vec3> texCoords;
                texCoords.push_back(base + inPlaneA + inPlaneB);
                texCoords.push_back(base - inPlaneA + inPlaneB);
                texCoords.push_back(base - inPlaneA - inPlaneB);
                texCoords.push_back(base + inPlaneA - inPlaneB);
                FaceGeometry slice(texCoords, texCoords);

                // perform the rendering
                glEnable(GL_DEPTH_TEST);
                _shader->activate();
148
                if (p_showWireframe.getValue()) {
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
149 150 151 152 153
                    // calculate viewport matrix for NDC -> viewport conversion
                    cgt::vec2 halfViewport = cgt::vec2(getEffectiveViewportSize()) / 2.f;
                    cgt::mat4 viewportMatrix = cgt::mat4::createTranslation(cgt::vec3(halfViewport, 0.f)) * cgt::mat4::createScale(cgt::vec3(halfViewport, 1.f));
                    _shader->setUniform("_viewportMatrix", viewportMatrix);

154 155 156
                    _shader->setUniform("_lineWidth", p_lineWidth.getValue());
                    _shader->setUniform("_transparency", p_transparency.getValue());
                }
157

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
                if (p_use2DProjection.getValue()) {
                    // generate a camera position that simulates 2D rendering
                    // this way it is easier to achieve the correct aspect ratio in all cases
                    cgt::vec3 camPosition = base - p_planeSize.getValue() * n;
                    float ratio = static_cast<float>(getEffectiveViewportSize().x) / getEffectiveViewportSize().y;

                    // experimentally discovered: 
                    // if the camera distance is half as big as the plane size, a field of view of
                    // 54 allows to see everything
                    float fovy = 54.f;

                    cgt::Camera c(camPosition, base, inPlaneA, fovy, ratio, 0.1f, 10000.f);
                    _shader->setUniform("_projectionMatrix", c.getProjectionMatrix());
                    _shader->setUniform("_viewMatrix", c.getViewMatrix());
                }
                else {
                    _shader->setUniform("_projectionMatrix", camera->getCamera().getProjectionMatrix());
                    _shader->setUniform("_viewMatrix", camera->getCamera().getViewMatrix());
                }

                cgt::TextureUnit volumeUnit1, volumeUnit2, volumeUnit3, tf1Unit, tf2Unit, tf3Unit;
                image1->bind(_shader, volumeUnit1, "_volume1", "_volumeParams1");
                image2->bind(_shader, volumeUnit2, "_volume2", "_volumeParams2");
                image3->bind(_shader, volumeUnit3, "_volume3", "_volumeParams3");
                p_transferFunction1.getTF()->bind(_shader, tf1Unit, "_transferFunction1", "_transferFunctionParams1");
                p_transferFunction2.getTF()->bind(_shader, tf2Unit, "_transferFunction2", "_transferFunctionParams2");
                p_transferFunction3.getTF()->bind(_shader, tf3Unit, "_transferFunction3", "_transferFunctionParams3");

                FramebufferActivationGuard fag(this);
187 188
                createAndAttachTexture(GL_RGBA8);
                createAndAttachTexture(GL_RGBA32F);
189
                createAndAttachDepthTexture();
190 191 192

                static const GLenum buffers[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 };
                glDrawBuffers(2, buffers);
193 194 195 196
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                slice.render(GL_TRIANGLE_FAN);

197
                glDrawBuffers(1, buffers);
198 199 200 201 202 203 204 205 206 207 208 209 210
                _shader->deactivate();
                cgt::TextureUnit::setZeroUnit();
                glDisable(GL_DEPTH_TEST);

                dataContainer.addData(p_outputImageId.getValue(), new RenderData(_fbo));
            }
        }
        else {
            LDEBUG("No suitable input data found!");
        }
    }

    std::string MultiVolumeMprRenderer::generateHeader() const {
211 212 213 214 215 216
        std::string toReturn = "#define HAS_GEOMETRY_SHADER\n";

        if (p_showWireframe.getValue())
            toReturn += "#define WIREFRAME_RENDERING\n";

        return toReturn;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    }

    void MultiVolumeMprRenderer::updateProperties(DataContainer& dataContainer) {
        ImageRepresentationGL::ScopedRepresentation image1(dataContainer, p_sourceImage1.getValue());
        ImageRepresentationGL::ScopedRepresentation image2(dataContainer, p_sourceImage2.getValue());
        ImageRepresentationGL::ScopedRepresentation image3(dataContainer, p_sourceImage3.getValue());

        if (image1)
            p_transferFunction1.setImageHandle(image1.getDataHandle());
        else
            p_transferFunction1.setImageHandle(DataHandle(nullptr));

        if (image2)
            p_transferFunction2.setImageHandle(image2.getDataHandle());
        else
            p_transferFunction2.setImageHandle(DataHandle(nullptr));

        if (image3)
            p_transferFunction3.setImageHandle(image3.getDataHandle());
        else
            p_transferFunction3.setImageHandle(DataHandle(nullptr));
    }

    void MultiVolumeMprRenderer::updateShader() {
        _shader->setHeaders(generateHeader());
        _shader->rebuild();
    }

}
}