volumerenderer.cpp 12.9 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

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

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

43
    VolumeRenderer::VolumeRenderer(IVec2Property* viewportSizeProp, RaycastingProcessor* raycaster)
44
        : VisualizationProcessor(viewportSizeProp)
45
        , p_inputVolume("InputVolume", "Input Volume", "", DataNameProperty::READ)
46
        , p_camera("Camera", "Camera ID", "camera", DataNameProperty::READ)
47
        , p_outputImage("OutputImage", "Output Image", "vr.output", DataNameProperty::WRITE)
48
49
        , p_profileRaycaster("ProfileRaycaster", "Profile Raycaster's Execution Time", false)
        , _timerQueryRaycaster(0)
50
51
52
        , p_pgProps("PGGProps", "Proxy Geometry Generator")
        , p_eepProps("EEPProps", "Entry/Exit Points Generator")
        , p_raycasterProps("RaycasterProps", "Raycaster")
53
        , p_orientationOverlayProps("OrientationOverlayProps", "Orientation Overlay")
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
        const std::vector<std::string>& raycasters = ProcessorFactory::getRef().getRegisteredProcessors();
80
81
82
83
84
85
86
87
        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);

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);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
111
112
113


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

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

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

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

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

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

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

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

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

150
151
152
        VisualizationProcessor::deinit();
    }

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

175
176
        _orientationOverlay.process(data);

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

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

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

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

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

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

229
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);

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;
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
244
            _raycaster->init();
245
246
247
248
249
            _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());
250
            _raycaster->p_transferFunction.replaceTF(currentRaycaster->p_transferFunction.getTF()->clone());
251
252
253
254
            _raycaster->p_jitterStepSizeMultiplier.setValue(currentRaycaster->p_jitterStepSizeMultiplier.getValue());
            _raycaster->p_samplingRate.setValue(currentRaycaster->p_samplingRate.getValue());

            currentRaycaster->deinit();
255
            invalidate(RAYCASTER_INVALID);
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
256
257
258
259

            // 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);
260
261
        }

262
263
264
        VisualizationProcessor::onPropertyChanged(prop);
    }

265
266
    void VolumeRenderer::setViewportSizeProperty(IVec2Property* viewportSizeProp) {
        _eepGenerator.setViewportSizeProperty(viewportSizeProp);
267
        _raycaster->setViewportSizeProperty(viewportSizeProp);
268
        _orientationOverlay.setViewportSizeProperty(viewportSizeProp);
269
270
271
272

        VisualizationProcessor::setViewportSizeProperty(viewportSizeProp);
    }

273
274
275
276
    void VolumeRenderer::updateProperties(DataContainer& dataContainer) {
        // nothing to do here
    }

277
278
279
    RaycastingProcessor* VolumeRenderer::getRaycastingProcessor() {
        return _raycaster;
    }
Christian Schulte zu Berge's avatar
Christian Schulte zu Berge committed
280
281
282
283
284

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

285
286
}