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

volumerenderer.cpp 11.8 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);
112
113
114
    }

    VolumeRenderer::~VolumeRenderer() {
115
        delete _raycaster;
116
117
118
119
120
121
    }

    void VolumeRenderer::init() {
        VisualizationProcessor::init();
        _pgGenerator.init();
        _eepGenerator.init();
122
        _orientationOverlay.init();
123
        _raycaster->init();
124

125
        p_lqMode.addSharedProperty(&_raycaster->p_lqMode);
126

127
128
        _pgGenerator.s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
        _eepGenerator.s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
129
        _raycaster->s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
130
        _orientationOverlay.s_invalidated.connect(this, &VolumeRenderer::onProcessorInvalidated);
131
132

        glGenQueries(1, &_timerQueryRaycaster);
133
134
135
    }

    void VolumeRenderer::deinit() {
136
137
        glDeleteQueries(1, &_timerQueryRaycaster);

138
139
        _pgGenerator.s_invalidated.disconnect(this);
        _eepGenerator.s_invalidated.disconnect(this);
140
        _raycaster->s_invalidated.disconnect(this);
141
        _orientationOverlay.s_invalidated.disconnect(this);
142

143
144
        _pgGenerator.deinit();
        _eepGenerator.deinit();
145
        _raycaster->deinit();
146
        _orientationOverlay.deinit();
147

148
149
150
        VisualizationProcessor::deinit();
    }

151
    void VolumeRenderer::updateResult(DataContainer& data) {
152
153
154
        if (getInvalidationLevel() & PG_INVALID) {
            _pgGenerator.process(data);
        }
155
        if (getInvalidationLevel() & EEP_INVALID) {
156
157
            _eepGenerator.process(data);
        }
158
        if (getInvalidationLevel() & RAYCASTER_INVALID) {
159
160
161
162
163
164
165
166
167
168
169
170
            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);
            }            
171
172
        }

173
174
        _orientationOverlay.process(data);

175
176
177
178
179
        validate(INVALID_RESULT | PG_INVALID | EEP_INVALID | RAYCASTER_INVALID);
    }

    void VolumeRenderer::onProcessorInvalidated(AbstractProcessor* processor) {
        if (processor == &_pgGenerator) {
180
            invalidate(PG_INVALID | EEP_INVALID | RAYCASTER_INVALID);
181
182
        }
        else if (processor == &_eepGenerator) {
183
            invalidate(EEP_INVALID | RAYCASTER_INVALID);
184
        }
185
        else if (processor == _raycaster) {
186
187
188
189
190
191
192
193
194
            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");
195
196
            _eepGenerator.p_geometryID.setValue(p_outputImage.getValue() + ".geometry");

197
            _eepGenerator.p_entryImageID.setValue(p_outputImage.getValue() + ".entrypoints");
198
            _raycaster->p_entryImageID.setValue(p_outputImage.getValue() + ".entrypoints");
199

200
            _eepGenerator.p_exitImageID.setValue(p_outputImage.getValue() + ".exitpoints");
201
            _raycaster->p_exitImageID.setValue(p_outputImage.getValue() + ".exitpoints");
202
203
204

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

217
218
219
220
            p_lqMode.removeSharedProperty(&currentRaycaster->p_lqMode);
            p_inputVolume.removeSharedProperty(&currentRaycaster->p_sourceImageID);
            p_camera.removeSharedProperty(&currentRaycaster->p_camera);
            p_outputImage.removeSharedProperty(&currentRaycaster->p_targetImageID);
221
            p_raycasterProps.clearProperties();
222
223
224
225
            p_raycasterProps.deinitAllProperties();
            removeProperty(p_raycasterProps);
            currentRaycaster->s_invalidated.disconnect(this);
            
226
227
228
229
230
231
232
233
234
235
            _raycaster = RaycasterFactory::getRef().createRaycaster(p_raycastingProcSelector.getOptionId(), p_viewportSizeProp);
            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);
            addProperty(p_raycasterProps, AbstractProcessor::VALID);

236
237
238
239
240
241
242
243
            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();
244
245
246
247
            delete currentRaycaster;
            invalidate(RAYCASTER_INVALID);
        }

248
249
250
        VisualizationProcessor::onPropertyChanged(prop);
    }

251
252
    void VolumeRenderer::setViewportSizeProperty(IVec2Property* viewportSizeProp) {
        _eepGenerator.setViewportSizeProperty(viewportSizeProp);
253
        _raycaster->setViewportSizeProperty(viewportSizeProp);
254
        _orientationOverlay.setViewportSizeProperty(viewportSizeProp);
255
256
257
258

        VisualizationProcessor::setViewportSizeProperty(viewportSizeProp);
    }

259
260
261
262
    void VolumeRenderer::updateProperties(DataContainer& dataContainer) {
        // nothing to do here
    }

263
264
265
    RaycastingProcessor* VolumeRenderer::getRaycastingProcessor() {
        return _raycaster;
    }
266
267
}