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 12.5 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 "cgt/opengljobprocessor.h"
37
#include "core/tools/stringutils.h"
38

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

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

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

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

        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);
77
        addProperty(p_eepProps, AbstractProcessor::VALID);
78

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

88
89
90
91
92
93
94
        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);
95
        addProperty(p_raycasterProps, AbstractProcessor::VALID);
96

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

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

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

110
        p_outputImage.addSharedProperty(&_orientationOverlay.p_targetImageId);
111
112
113
    }

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

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

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

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

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

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

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

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

147
148
149
        VisualizationProcessor::deinit();
    }

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

172
173
        _orientationOverlay.process(data);

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

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

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

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

            _raycaster->p_targetImageID.setValue(p_outputImage.getValue() + ".raycasted");
            _orientationOverlay.p_passThroughImageId.setValue(p_outputImage.getValue() + ".raycasted");
204
        }
205
        if (prop == &p_raycastingProcSelector) {
206
207
208
209
            // lock this processor while we exchange the ray caster to make sure nobody
            // will call process() in between.
            AbstractProcessor::ScopedLock lockGuard(this);

210
211
            RaycastingProcessor *currentRaycaster = _raycaster;
            if (p_raycastingProcSelector.getOptionId() == currentRaycaster->getName()) {
212
213
214
                return;
            }

215
216
217
218
            p_lqMode.removeSharedProperty(&currentRaycaster->p_lqMode);
            p_inputVolume.removeSharedProperty(&currentRaycaster->p_sourceImageID);
            p_camera.removeSharedProperty(&currentRaycaster->p_camera);
            p_outputImage.removeSharedProperty(&currentRaycaster->p_targetImageID);
219
            p_raycasterProps.clearProperties();
220
221
            currentRaycaster->s_invalidated.disconnect(this);
            
222
223
224
            _raycaster = dynamic_cast<RaycastingProcessor*>(ProcessorFactory::getRef().createProcessor(p_raycastingProcSelector.getOptionId(), _viewportSizeProperty));
            cgtAssert(_raycaster != 0, "Raycaster must not be 0.");

225
            p_raycasterProps.addPropertyCollection(*_raycaster);
226
227
228
229
230
231
            _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);
232

233
234
235
236
237
238
239
            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;
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
240
            _raycaster->init();
241
242
243
244
245
            _raycaster->p_sourceImageID.setValue(currentRaycaster->p_sourceImageID.getValue());
            _raycaster->p_entryImageID.setValue(currentRaycaster->p_entryImageID.getValue());
            _raycaster->p_exitImageID.setValue(currentRaycaster->p_exitImageID.getValue());
            _raycaster->p_targetImageID.setValue(currentRaycaster->p_targetImageID.getValue());
            _raycaster->p_camera.setValue(currentRaycaster->p_camera.getValue());
246
            _raycaster->p_transferFunction.replaceTF(currentRaycaster->p_transferFunction.getTF()->clone());
247
248
249
250
            _raycaster->p_jitterStepSizeMultiplier.setValue(currentRaycaster->p_jitterStepSizeMultiplier.getValue());
            _raycaster->p_samplingRate.setValue(currentRaycaster->p_samplingRate.getValue());

            currentRaycaster->deinit();
251
            invalidate(PG_INVALID | EEP_INVALID | RAYCASTER_INVALID | AbstractProcessor::INVALID_RESULT);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
252

253
            delete currentRaycaster;
254
255
        }

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

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

        VisualizationProcessor::setViewportSizeProperty(viewportSizeProp);
    }

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

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

275
276
}