summaryrefslogtreecommitdiffstats
path: root/examples/datavisualization/volumetric/volumetric.cpp
blob: 8e61ecf266f7dc24ab08f701e5d1f0a1c822eeb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the QtDataVisualization module.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "volumetric.h"
#include <QtDataVisualization/qvalue3daxis.h>
#include <QtDataVisualization/q3dscene.h>
#include <QtDataVisualization/q3dcamera.h>
#include <QtDataVisualization/q3dtheme.h>
#include <QtCore/qmath.h>
#include <QtGui/QRgb>
#include <QtWidgets/QLabel>
#include <QtCore/QDebug>

using namespace QtDataVisualization;

const int textureSize = 256;

VolumetricModifier::VolumetricModifier(Q3DScatter *scatter)
    : m_graph(scatter),
      m_volumeItem(0),
      m_sliceIndexX(textureSize / 2),
      m_sliceIndexY(textureSize / 4),
      m_sliceIndexZ(textureSize / 2)
{
    m_graph->activeTheme()->setType(Q3DTheme::ThemeQt);
    m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityNone);
    m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFront);
    m_graph->setOrthoProjection(true);

    createVolume();

    m_graph->addCustomItem(m_volumeItem);
    m_graph->setMeasureFps(true);

    QObject::connect(m_graph->scene()->activeCamera(), &Q3DCamera::zoomLevelChanged, this,
                     &VolumetricModifier::handleZoomLevelChange);
    QObject::connect(m_graph, &QAbstract3DGraph::currentFpsChanged, this,
                     &VolumetricModifier::handleFpsChange);
}

VolumetricModifier::~VolumetricModifier()
{
    delete m_graph;
}

void VolumetricModifier::setFpsLabel(QLabel *fpsLabel)
{
    m_fpsLabel = fpsLabel;
}

void VolumetricModifier::sliceX(int enabled)
{
    m_volumeItem->setSliceIndexX(enabled ? m_sliceIndexX : -1);
}

void VolumetricModifier::sliceY(int enabled)
{
    m_volumeItem->setSliceIndexY(enabled ? m_sliceIndexY : -1);
}

void VolumetricModifier::sliceZ(int enabled)
{
    m_volumeItem->setSliceIndexZ(enabled ? m_sliceIndexZ : -1);
}

void VolumetricModifier::adjustSliceX(int value)
{
    m_sliceIndexX = value / (1024 / textureSize);
    if (m_volumeItem->sliceIndexX() != -1)
        m_volumeItem->setSliceIndexX(m_sliceIndexX);
}

void VolumetricModifier::adjustSliceY(int value)
{
    m_sliceIndexY = value / (1024 / textureSize * 2);
    if (m_volumeItem->sliceIndexY() != -1)
        m_volumeItem->setSliceIndexY(m_sliceIndexY);
}

void VolumetricModifier::adjustSliceZ(int value)
{
    m_sliceIndexZ = value / (1024 / textureSize);
    if (m_volumeItem->sliceIndexZ() != -1)
        m_volumeItem->setSliceIndexZ(m_sliceIndexZ);
}

void VolumetricModifier::handleZoomLevelChange()
{
    // Zooming inside volumetric object causes ugly clipping issues, so restrict zoom level a bit
    if (m_graph->scene()->activeCamera()->zoomLevel() > 200)
        m_graph->scene()->activeCamera()->setZoomLevel(200);
}

void VolumetricModifier::handleFpsChange(qreal fps)
{
    const QString fpsFormat = QStringLiteral("Fps: %1");
    int fps10 = int(fps * 10.0);
    m_fpsLabel->setText(fpsFormat.arg(QString::number(qreal(fps10) / 10.0)));
}

void VolumetricModifier::createVolume()
{
    m_volumeItem = new QCustom3DVolume;
    m_volumeItem->setScaling(QVector3D(2.0f, 1.0f, 2.0f));
    m_volumeItem->setTextureWidth(textureSize);
    m_volumeItem->setTextureHeight(textureSize / 2);
    m_volumeItem->setTextureDepth(textureSize);
    m_volumeItem->setTextureFormat(QImage::Format_Indexed8);

    // Generate color table. First color is fully transparent used to fill outer
    // portions of the volume. The second, red layer, is somewhat transparent.
    // Rest of to colors are opaque.
    QVector<QRgb> colors;
    colors.resize(256);

    colors[0] = qRgba(0, 0, 0, 0);
    for (int i = 1; i < 256; i++) {
        if (i < 60) {
            colors[i] = qRgba((i * 2) + 120, 0, 0, 100);
        } else if (i < 120) {
            colors[i] = qRgba(0, ((i - 60) * 2) + 120, 0, 255);
        } else if (i < 180) {
            colors[i] = qRgba(0, 0, ((i - 120) * 2) + 120, 255);
        } else {
            colors[i] = qRgba(i, i, i, 255);
        }
    }
    m_volumeItem->setColorTable(colors);

    // Generate texture data for an half-ellipsoid.
    // This can take a while if the dimensions are large.
    // Note that in real world cases, the texture data is usually be supplied
    // as a stack of slice images.

    QVector<uchar> *textureData = new QVector<uchar>(textureSize * textureSize * textureSize / 2);

    QVector3D midPoint(float(textureSize) / 2.0f,
                       float(textureSize) / 2.0f,
                       float(textureSize) / 2.0f);

    int index = 0;
    for (int i = 0; i < textureSize; i++) {
        for (int j = 0; j < textureSize / 2; j++) {
            for (int k = 0; k < textureSize; k++) {
                int colorIndex = 0;
                // Take a slice out of the ellipsoid
                if (i >= textureSize / 2 || j >= textureSize / 4 || k >= textureSize / 2) {
                    QVector3D distVec = QVector3D(float(k), float(j * 2), float(i)) - midPoint;
                    float adjLen = qMin(255.0f, (distVec.length() * 512.0f / float(textureSize)));
                    if (adjLen < 230)
                        colorIndex = 255 - int(adjLen);
                    else
                        colorIndex = 0;
                }

                (*textureData)[index] = colorIndex;
                index++;
            }
        }
    }

    m_volumeItem->setTextureData(textureData);
}