tensorglyphrenderer.cpp 12.7 KB
Newer Older
1
2
3
4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6
7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 
// 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.
// 
// ================================================================================================

25
#include "tensorglyphrenderer.h"
26
27
28
29
30
31

#include "tgt/tgt_math.h"
#include "tgt/logmanager.h"
#include "tgt/shadermanager.h"

#include "core/datastructures/imagedata.h"
32
#include "core/datastructures/lightsourcedata.h"
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "core/datastructures/geometrydatafactory.h"
#include "core/datastructures/renderdata.h"

namespace campvis {

    static const GenericOption<TensorGlyphRenderer::GlyphType> glyphTypes[3] = {
        GenericOption<TensorGlyphRenderer::GlyphType>("ellipsoid", "Ellipsoid Glyph", TensorGlyphRenderer::ELLIPSOID),
        GenericOption<TensorGlyphRenderer::GlyphType>("cuboid", "Cuboid Glyph", TensorGlyphRenderer::CUBOID),
        GenericOption<TensorGlyphRenderer::GlyphType>("multi", "Multi Ellipsoid Glyph", TensorGlyphRenderer::MULTI)
    };

    static const GenericOption<TensorGlyphRenderer::SliceOrientation> sliceOrientationOptions[3] = {
        GenericOption<TensorGlyphRenderer::SliceOrientation>("z", "XY Plane", TensorGlyphRenderer::XY_PLANE),
        GenericOption<TensorGlyphRenderer::SliceOrientation>("y", "XZ Plane", TensorGlyphRenderer::XZ_PLANE),
        GenericOption<TensorGlyphRenderer::SliceOrientation>("x", "YZ Plane", TensorGlyphRenderer::YZ_PLANE)
    };

    const std::string TensorGlyphRenderer::loggerCat_ = "CAMPVis.modules.classification.TensorGlyphRenderer";

    TensorGlyphRenderer::TensorGlyphRenderer(IVec2Property* viewportSizeProp)
        : VisualizationProcessor(viewportSizeProp)
54
55
        , p_inputEigenvalues("InputEigenvalues", "Input Eigenvalues Image", "eigenvalues", DataNameProperty::READ)
        , p_inputEigenvectors("InputEigenvectors", "Input Eigenvectors Image", "eigenvectors", DataNameProperty::READ)
56
57
58
        , p_renderOutput("RenderOutput", "Output Image", "TensorGlyphRenderer.output", DataNameProperty::WRITE)
        , p_glyphType("GlyphType", "Glyph Type to Render", glyphTypes, 3)
        , p_glyphSize("GlyphSize", "Glyph Size", 1.f, .1f, 5.f)
59
60
        , p_enableShading("EnableShading", "Enable Shading", true)
        , p_lightId("LightId", "Input Light Source", "lightsource", DataNameProperty::READ)
61
        , p_camera("Camera", "Camera", tgt::Camera())
62
        , p_sliceOrientation("SliceOrientation", "Slice Orientation", sliceOrientationOptions, 3)
63
64
65
66
        , p_sliceNumber("SliceNumber", "Slice Number", 0, 0, 0)
        , _ellipsoidGeometry(0)
        , _cubeGeometry(0)
    {
67
68
69
70
71
        addProperty(p_inputEigenvalues, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_inputEigenvectors, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_renderOutput);
        addProperty(p_glyphType);
        addProperty(p_glyphSize);
72
73
74
75

        addProperty(p_enableShading, INVALID_RESULT | INVALID_PROPERTIES | INVALID_SHADER);
        addProperty(p_lightId);

76
77
78
        addProperty(p_camera);
        addProperty(p_sliceOrientation, INVALID_RESULT | INVALID_PROPERTIES);
        addProperty(p_sliceNumber);
79
80
81
82
83
84
85
86
87
    }

    TensorGlyphRenderer::~TensorGlyphRenderer() {

    }

    void TensorGlyphRenderer::init() {
        VisualizationProcessor::init();

88
        _shader = ShdrMgr.load("modules/tensor/glsl/tensorglyphrenderer.vert", "modules/tensor/glsl/tensorglyphrenderer.frag", generateGlslHeader());
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
        _cubeGeometry = GeometryDataFactory::createCube(tgt::Bounds(tgt::vec3(-.5f), tgt::vec3(.5f)), tgt::Bounds(tgt::vec3(0.f), tgt::vec3(1.f)));
        _ellipsoidGeometry = GeometryDataFactory::createSphere(8, 16);
    }

    void TensorGlyphRenderer::deinit() {
        ShdrMgr.dispose(_shader);

        delete _ellipsoidGeometry;
        _ellipsoidGeometry = 0;
        delete _cubeGeometry;
        _cubeGeometry = 0;


        VisualizationProcessor::deinit();
    }

    void TensorGlyphRenderer::updateResult(DataContainer& dataContainer) {
        if (_cubeGeometry == 0 || _ellipsoidGeometry == 0) {
            LERROR("Error initializing glyph geometries.");
            return;
        }

        GenericImageRepresentationLocal<float, 3>::ScopedRepresentation evals(dataContainer, p_inputEigenvalues.getValue());
        GenericImageRepresentationLocal<float, 9>::ScopedRepresentation evecs(dataContainer, p_inputEigenvectors.getValue());

        if (evals && evecs) {
            if (evals->getSize() == evecs->getSize()) {
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
                ScopedTypedData<LightSourceData> light(dataContainer, p_lightId.getValue());

                if (p_enableShading.getValue() == false || light != nullptr) {
                    const tgt::Camera& cam = p_camera.getValue();
                    const tgt::svec3& imgSize = evals->getSize();

                    glEnable(GL_DEPTH_TEST);
                    _shader->activate();

                    _shader->setIgnoreUniformLocationError(true);
                    _shader->setUniform("_viewportSizeRCP", 1.f / tgt::vec2(getEffectiveViewportSize()));
                    _shader->setUniform("_projectionMatrix", cam.getProjectionMatrix());
                    _shader->setUniform("_viewMatrix", cam.getViewMatrix());

                    if (p_enableShading.getValue() && light != nullptr) {
                        light->bind(_shader, "_lightSource");
                    }

                    FramebufferActivationGuard fag(this);
                    createAndAttachColorTexture();
                    createAndAttachDepthTexture();
                    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                    switch (p_sliceOrientation.getOptionValue()) {
                        case XY_PLANE:
                            for (size_t x = 0; x < imgSize.x; ++x) {
                                for (size_t y = 0; y < imgSize.y; ++y) {
                                    renderTensorGlyph(evals, evecs, tgt::ivec3(static_cast<int>(x), static_cast<int>(y), p_sliceNumber.getValue()));
                                }
145
                            }
146
147
148
149
150
151
                            break;
                        case XZ_PLANE:
                            for (size_t x = 0; x < imgSize.x; ++x) {
                                for (size_t z = 0; z < imgSize.z; ++z) {
                                    renderTensorGlyph(evals, evecs, tgt::ivec3(static_cast<int>(x), p_sliceNumber.getValue(), static_cast<int>(z)));
                                }
152
                            }
153
154
155
156
157
158
                            break;
                        case YZ_PLANE:
                            for (size_t y = 0; y < imgSize.y; ++y) {
                                for (size_t z = 0; z < imgSize.z; ++z) {
                                    renderTensorGlyph(evals, evecs, tgt::ivec3(p_sliceNumber.getValue(), static_cast<int>(y), static_cast<int>(z)));
                                }
159
                            }
160
161
                            break;
                    }
162
163


164
165
                    _shader->deactivate();
                    glDisable(GL_DEPTH_TEST);
166

167
168
169
170
171
                    dataContainer.addData(p_renderOutput.getValue(), new RenderData(_fbo));
                }
                else {
                    LDEBUG("Could not load light source from DataContainer.");
                }
172
173
174
175
176
177
178
179
180
181
            }
        }
        else {
            LERROR("Could not find suitable input data.");
        }
        
        validate(INVALID_RESULT);
    }

    void TensorGlyphRenderer::updateProperties(DataContainer& dataContainer) {
182
183
        p_lightId.setVisible(p_enableShading.getValue());

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
        GenericImageRepresentationLocal<float, 3>::ScopedRepresentation evals(dataContainer, p_inputEigenvalues.getValue());
        GenericImageRepresentationLocal<float, 9>::ScopedRepresentation evecs(dataContainer, p_inputEigenvectors.getValue());

        if (evals && evecs) {
            if (evals->getSize() == evecs->getSize()) {
                switch (p_sliceOrientation.getOptionValue()) {
                    case XY_PLANE:
                        p_sliceNumber.setMaxValue(static_cast<int>(evals->getSize().z - 1));
                        break;
                    case XZ_PLANE:
                        p_sliceNumber.setMaxValue(static_cast<int>(evals->getSize().y - 1));
                        break;
                    case YZ_PLANE:
                        p_sliceNumber.setMaxValue(static_cast<int>(evals->getSize().x - 1));
                        break;
                }
            }
            else {
                LERROR("Size of eigenvalue image and eigenvector image mismatch!");
            }
        }

        validate(INVALID_PROPERTIES);
    }

    void TensorGlyphRenderer::updateShader() {
        _shader->setHeaders(generateGlslHeader());
        _shader->rebuild();
        validate(INVALID_SHADER);
    }

    std::string TensorGlyphRenderer::generateGlslHeader() const {
216
217
218
        std::string toReturn;
        if (p_enableShading.getValue())
            toReturn += "#define ENABLE_SHADING\n";
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

        return toReturn;
    }

    void TensorGlyphRenderer::renderTensorGlyph(const GenericImageRepresentationLocal<float, 3>* evals, const GenericImageRepresentationLocal<float, 9>* evecs, const tgt::vec3& position) {
        /// minimum scale factor
        const float EPS = .1f;

        // gather value
        const tgt::vec3& eigenvalues = evals->getElement(position);
        const tgt::mat3& eigenvectors = evecs->getElement(position);
        if (eigenvalues == tgt::vec3::zero || eigenvectors == tgt::mat3::zero)
            return;

        // compute rotation matrix
        tgt::vec3 rotx = tgt::normalize(eigenvectors[0]);
        tgt::vec3 roty = tgt::normalize(eigenvectors[1]);
        tgt::vec3 rotz = tgt::normalize(eigenvectors[2]);
        tgt::mat4 rotationMatrix(rotx[0], rotx[1], rotx[2], 0.f,
            roty[0], roty[1], roty[2], 0.f,
            rotz[0], rotz[1], rotz[2], 0.f,
            0.f    , 0.f    , 0.f    , 1.f);

        float divScale = (1.f - 2.f*EPS)/(eigenvalues[0]);
        const tgt::mat4& voxelToWorldMatrix = evals->getParent()->getMappingInformation().getVoxelToWorldMatrix();

        // compute model matrix (without glyph-related transformation
        tgt::mat4 modelMatrix = voxelToWorldMatrix * tgt::mat4::createTranslation(position) * rotationMatrix * tgt::mat4::createScale(tgt::vec3(p_glyphSize.getValue()));

        // setup shader
        _shader->setUniform("_color", tgt::vec4(rotx, 1.f));

        switch (p_glyphType.getOptionValue()) {
            case CUBOID:
                // render single cuboid
                _shader->setUniform("_modelMatrix", modelMatrix * tgt::mat4::createScale(tgt::vec3((1.f - EPS), (EPS + divScale*eigenvalues[1]), (EPS + divScale*eigenvalues[2]))));
                _cubeGeometry->render(GL_POLYGON);
                break;

            case ELLIPSOID:
                // render single ellipsoid
                _shader->setUniform("_modelMatrix", modelMatrix * tgt::mat4::createScale(tgt::vec3((1.f - EPS), (EPS + divScale*eigenvalues[1]), (EPS + divScale*eigenvalues[2]))));
                _ellipsoidGeometry->render(GL_TRIANGLE_STRIP);
                break;

            case MULTI:
                // render three ellipsoids in different shapes
                _shader->setUniform("_modelMatrix", modelMatrix * tgt::mat4::createScale(tgt::vec3(divScale*eigenvalues[2], divScale*eigenvalues[2], divScale*eigenvalues[2])));
                _ellipsoidGeometry->render(GL_TRIANGLE_STRIP);
                _shader->setUniform("_modelMatrix", modelMatrix * tgt::mat4::createScale(tgt::vec3(divScale*eigenvalues[1], divScale*eigenvalues[1], EPS)));
                _ellipsoidGeometry->render(GL_TRIANGLE_STRIP);
                _shader->setUniform("_modelMatrix", modelMatrix * tgt::mat4::createScale(tgt::vec3(divScale*eigenvalues[0], EPS, EPS)));
                _ellipsoidGeometry->render(GL_TRIANGLE_STRIP);
                break;
        }
    }

}