Starting from 2021-07-01, all LRZ GitLab users will be required to explicitly accept the GitLab Terms of Service. Please see the detailed information at https://doku.lrz.de/display/PUBLIC/GitLab and make sure that your projects conform to the requirements.

volumerenderer.cpp 12.3 KB
Newer Older
1

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

#include "volumerenderer.h"
27 28 29
#include "cgt/logmanager.h"
#include "cgt/shadermanager.h"
#include "cgt/textureunit.h"
30 31 32 33 34 35

#include "core/datastructures/imagedata.h"
#include "core/datastructures/imagerepresentationgl.h"

#include "core/classification/simpletransferfunction.h"

36
#include "../../raycasterfactory.h"
37
#include "cgt/opengljobprocessor.h"
38

39 40 41
namespace campvis {
    const std::string VolumeRenderer::loggerCat_ = "CAMPVis.modules.vis.VolumeRenderer";

42 43
    //static const std::string* raycastingProcessorName = &RaycasterFactory::getRef().getRegisteredRaycasters()[0];

44
    VolumeRenderer::VolumeRenderer(IVec2Property* viewportSizeProp, RaycastingProcessor* raycaster)
45
        : VisualizationProcessor(viewportSizeProp)
46
        , p_inputVolume("InputVolume", "Input Volume", "", DataNameProperty::READ)
47
        , p_camera("Camera", "Camera ID", "camera", DataNameProperty::READ)
48
        , p_outputImage("OutputImage", "Output Image", "vr.output", DataNameProperty::WRITE)
49 50
        , p_profileRaycaster("ProfileRaycaster", "Profile Raycaster's Execution Time", false)
        , _timerQueryRaycaster(0)
51 52 53
        , p_pgProps("PGGProps", "Proxy Geometry Generator")
        , p_eepProps("EEPProps", "Entry/Exit Points Generator")
        , p_raycasterProps("RaycasterProps", "Raycaster")
54
        , p_orientationOverlayProps("OrientationOverlayProps", "Orientation Overlay")
55
        , _pgGenerator()
56
        , _eepGenerator(viewportSizeProp)
57
        , _raycaster(raycaster)
58
        , _orientationOverlay(viewportSizeProp)
59
    {
60 61
        _raycaster->setViewportSizeProperty(viewportSizeProp);

62 63 64
        addProperty(p_inputVolume, AbstractProcessor::VALID);
        addProperty(p_outputImage, AbstractProcessor::VALID);
        addProperty(p_profileRaycaster, AbstractProcessor::VALID);
65

66 67 68
        p_pgProps.addPropertyCollection(_pgGenerator);
        _pgGenerator.p_sourceImageID.setVisible(false);
        _pgGenerator.p_geometryID.setVisible(false);
69
        addProperty(p_pgProps, AbstractProcessor::VALID);
70 71 72 73 74 75 76 77

        p_eepProps.addPropertyCollection(_eepGenerator);
        _eepGenerator.p_lqMode.setVisible(false);
        _eepGenerator.p_camera.setVisible(false);
        _eepGenerator.p_sourceImageID.setVisible(false);
        _eepGenerator.p_geometryID.setVisible(false);
        _eepGenerator.p_entryImageID.setVisible(false);
        _eepGenerator.p_exitImageID.setVisible(false);
78
        addProperty(p_eepProps, AbstractProcessor::VALID);
79

80 81 82 83 84 85 86 87 88
        const std::vector<std::string>& raycasters = RaycasterFactory::getRef().getRegisteredRaycasters();
        for (int i = 0; i < raycasters.size(); i++) {
            p_raycastingProcSelector.addOption(GenericOption<std::string>(raycasters[i], raycasters[i]));
        }
        if (_raycaster != nullptr) {
            p_raycastingProcSelector.selectByOption(_raycaster->getName());
        }
        addProperty(p_raycastingProcSelector);

89 90 91 92 93 94 95
        p_raycasterProps.addPropertyCollection(*_raycaster);
        _raycaster->p_lqMode.setVisible(false);
        _raycaster->p_camera.setVisible(false);
        _raycaster->p_sourceImageID.setVisible(false);
        _raycaster->p_entryImageID.setVisible(false);
        _raycaster->p_exitImageID.setVisible(false);
        _raycaster->p_targetImageID.setVisible(false);
96
        addProperty(p_raycasterProps, AbstractProcessor::VALID);
97

98 99 100
        p_orientationOverlayProps.addPropertyCollection(_orientationOverlay);
        addProperty(p_orientationOverlayProps, VALID);

101 102 103
        // setup shared properties
        p_inputVolume.addSharedProperty(&_pgGenerator.p_sourceImageID);
        p_inputVolume.addSharedProperty(&_eepGenerator.p_sourceImageID);
104
        p_inputVolume.addSharedProperty(&_raycaster->p_sourceImageID);
105
        p_inputVolume.addSharedProperty(&_orientationOverlay.p_sourceImageId);
106 107

        p_camera.addSharedProperty(&_eepGenerator.p_camera);
108
        p_camera.addSharedProperty(&_raycaster->p_camera);
109
        p_camera.addSharedProperty(&_orientationOverlay.p_camera);
110

111
        p_outputImage.addSharedProperty(&_orientationOverlay.p_targetImageId);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
112 113 114


        s_processorCanBeDeleted.connect(this, &VolumeRenderer::onProcessorCanBeDeleted);
115 116 117
    }

    VolumeRenderer::~VolumeRenderer() {
118
        delete _raycaster;
119 120 121 122 123 124
    }

    void VolumeRenderer::init() {
        VisualizationProcessor::init();
        _pgGenerator.init();
        _eepGenerator.init();
125
        _orientationOverlay.init();
126
        _raycaster->init();
127

128
        p_lqMode.addSharedProperty(&_raycaster->p_lqMode);
129

130 131
        _pgGenerator.s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
        _eepGenerator.s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
132
        _raycaster->s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
133
        _orientationOverlay.s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
134 135

        glGenQueries(1, &_timerQueryRaycaster);
136 137 138
    }

    void VolumeRenderer::deinit() {
139 140
        glDeleteQueries(1, &_timerQueryRaycaster);

141 142
        _pgGenerator.s_invalidated.disconnect(this);
        _eepGenerator.s_invalidated.disconnect(this);
143
        _raycaster->s_invalidated.disconnect(this);
144
        _orientationOverlay.s_invalidated.disconnect(this);
145

146 147
        _pgGenerator.deinit();
        _eepGenerator.deinit();
148
        _raycaster->deinit();
149
        _orientationOverlay.deinit();
150

151 152 153
        VisualizationProcessor::deinit();
    }

154
    void VolumeRenderer::updateResult(DataContainer& data) {
155 156 157
        if (getInvalidationLevel() & PG_INVALID) {
            _pgGenerator.process(data);
        }
158
        if (getInvalidationLevel() & EEP_INVALID) {
159 160
            _eepGenerator.process(data);
        }
161
        if (getInvalidationLevel() & RAYCASTER_INVALID) {
162 163 164 165 166 167 168 169 170 171 172 173
            if (p_profileRaycaster.getValue()) {
                glBeginQuery(GL_TIME_ELAPSED, _timerQueryRaycaster);
                _raycaster->process(data);
                glEndQuery(GL_TIME_ELAPSED);

                GLuint64 timer_result;
                glGetQueryObjectui64v(_timerQueryRaycaster, GL_QUERY_RESULT, &timer_result);
                LINFO("Raycaster Execution time: " << static_cast<double>(timer_result) / 1e06 << "ms.");
            }
            else {
                _raycaster->process(data);
            }            
174 175
        }

176 177
        _orientationOverlay.process(data);

178 179 180 181 182
        validate(INVALID_RESULT | PG_INVALID | EEP_INVALID | RAYCASTER_INVALID);
    }

    void VolumeRenderer::onProcessorInvalidated(AbstractProcessor* processor) {
        if (processor == &_pgGenerator) {
183
            invalidate(PG_INVALID | EEP_INVALID | RAYCASTER_INVALID);
184 185
        }
        else if (processor == &_eepGenerator) {
186
            invalidate(EEP_INVALID | RAYCASTER_INVALID);
187
        }
188
        else if (processor == _raycaster) {
189 190 191 192 193 194 195 196 197
            invalidate(RAYCASTER_INVALID);
        }

        invalidate(AbstractProcessor::INVALID_RESULT);
    }

    void VolumeRenderer::onPropertyChanged(const AbstractProperty* prop) {
        if (prop == &p_outputImage) {
            _pgGenerator.p_geometryID.setValue(p_outputImage.getValue() + ".geometry");
198 199
            _eepGenerator.p_geometryID.setValue(p_outputImage.getValue() + ".geometry");

200
            _eepGenerator.p_entryImageID.setValue(p_outputImage.getValue() + ".entrypoints");
201
            _raycaster->p_entryImageID.setValue(p_outputImage.getValue() + ".entrypoints");
202

203
            _eepGenerator.p_exitImageID.setValue(p_outputImage.getValue() + ".exitpoints");
204
            _raycaster->p_exitImageID.setValue(p_outputImage.getValue() + ".exitpoints");
205 206 207

            _raycaster->p_targetImageID.setValue(p_outputImage.getValue() + ".raycasted");
            _orientationOverlay.p_passThroughImageId.setValue(p_outputImage.getValue() + ".raycasted");
208
        }
209
        if (prop == &p_raycastingProcSelector) {
210
            RaycastingProcessor *currentRaycaster = _raycaster;
211 212
            // Change to previous raycaster if "Select Processor" is selected
            if (p_raycastingProcSelector.getOptionId() == p_raycastingProcSelector.getOptions()[0]._id) {
213
                p_raycastingProcSelector.selectById(currentRaycaster->getName());
214 215
                return;
            }
216
            if (p_raycastingProcSelector.getOptionId() == currentRaycaster->getName()) {
217 218 219
                return;
            }

220 221 222 223
            p_lqMode.removeSharedProperty(&currentRaycaster->p_lqMode);
            p_inputVolume.removeSharedProperty(&currentRaycaster->p_sourceImageID);
            p_camera.removeSharedProperty(&currentRaycaster->p_camera);
            p_outputImage.removeSharedProperty(&currentRaycaster->p_targetImageID);
224
            p_raycasterProps.clearProperties();
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
225 226
            //p_raycasterProps.deinitAllProperties();
            //removeProperty(p_raycasterProps);
227 228
            currentRaycaster->s_invalidated.disconnect(this);
            
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
229
            _raycaster = RaycasterFactory::getRef().createRaycaster(p_raycastingProcSelector.getOptionId(), _viewportSizeProperty);
230 231 232 233 234 235 236
            p_raycasterProps.addPropertyCollection(*_raycaster);
            //_raycaster->p_lqMode.setVisible(false);
            //_raycaster->p_camera.setVisible(false);
            //_raycaster->p_sourceImageID.setVisible(false);
            //_raycaster->p_entryImageID.setVisible(false);
            //_raycaster->p_exitImageID.setVisible(false);
            //_raycaster->p_targetImageID.setVisible(false);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
237
            //addProperty(p_raycasterProps, AbstractProcessor::VALID);
238

239 240 241 242 243 244 245 246
            p_lqMode.addSharedProperty(&_raycaster->p_lqMode);
            p_inputVolume.addSharedProperty(&_raycaster->p_sourceImageID);
            p_camera.addSharedProperty(&_raycaster->p_camera);
            p_outputImage.addSharedProperty(&_raycaster->p_targetImageID);
            _raycaster->s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
            
            cgt::OpenGLJobProcessor::ScopedSynchronousGlJobExecution jobGuard;
            currentRaycaster->deinit();
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
247
            _raycaster->init();
248
            invalidate(RAYCASTER_INVALID);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
249 250 251 252

            // queue the deletion of currentRaycaster as signal, to ensure that the deletion does
            // not happen before all previously emitted signals have been handled.
            s_processorCanBeDeleted.queueSignal(currentRaycaster);
253 254
        }

255 256 257
        VisualizationProcessor::onPropertyChanged(prop);
    }

258 259
    void VolumeRenderer::setViewportSizeProperty(IVec2Property* viewportSizeProp) {
        _eepGenerator.setViewportSizeProperty(viewportSizeProp);
260
        _raycaster->setViewportSizeProperty(viewportSizeProp);
261
        _orientationOverlay.setViewportSizeProperty(viewportSizeProp);
262 263 264 265

        VisualizationProcessor::setViewportSizeProperty(viewportSizeProp);
    }

266 267 268 269
    void VolumeRenderer::updateProperties(DataContainer& dataContainer) {
        // nothing to do here
    }

270 271 272
    RaycastingProcessor* VolumeRenderer::getRaycastingProcessor() {
        return _raycaster;
    }
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
273 274 275 276 277

    void VolumeRenderer::onProcessorCanBeDeleted(AbstractProcessor* processor) {
        delete processor;
    }

278 279
}