1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Data Visualization module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "datasource.h"
31#include <QtCore/qmath.h>
32#include <QtGui/QRgb>
33#include <QtGui/QVector3D>
34
35using namespace QtDataVisualization;
36
37Q_DECLARE_METATYPE(QCustom3DVolume *)
38
39DataSource::DataSource(QObject *parent) :
40 QObject(parent)
41{
42 qRegisterMetaType<QCustom3DVolume *>();
43}
44
45DataSource::~DataSource()
46{
47}
48
49void DataSource::fillVolume(QCustom3DVolume *volumeItem)
50{
51 // Generate example texture data for an half-ellipsoid with a section missing.
52 // This can take a while if the dimensions are large, so we support incremental data generation.
53
54 int index = 0;
55 int textureSize = 256;
56 QVector3D midPoint(float(textureSize) / 2.0f,
57 float(textureSize) / 2.0f,
58 float(textureSize) / 2.0f);
59
60 QVector<uchar> *textureData = new QVector<uchar>(textureSize * textureSize * textureSize / 2);
61 for (int i = 0; i < textureSize; i++) {
62 for (int j = 0; j < textureSize / 2; j++) {
63 for (int k = 0; k < textureSize; k++) {
64 int colorIndex = 0;
65 // Take a section out of the ellipsoid
66 if (i >= textureSize / 2 || j >= textureSize / 4 || k >= textureSize / 2) {
67 QVector3D distVec = QVector3D(float(k), float(j * 2), float(i)) - midPoint;
68 float adjLen = qMin(a: 255.0f, b: (distVec.length() * 512.0f / float(textureSize)));
69 colorIndex = 255 - int(adjLen);
70 }
71
72 (*textureData)[index] = colorIndex;
73 index++;
74 }
75 }
76 }
77
78 volumeItem->setScaling(QVector3D(2.0f, 1.0f, 2.0f));
79 volumeItem->setTextureWidth(textureSize);
80 volumeItem->setTextureHeight(textureSize / 2);
81 volumeItem->setTextureDepth(textureSize);
82 volumeItem->setTextureFormat(QImage::Format_Indexed8);
83 volumeItem->setTextureData(textureData);
84
85 QVector<QRgb> colorTable(256);
86
87 for (int i = 1; i < 256; i++) {
88 if (i < 15)
89 colorTable[i] = qRgba(r: 0, g: 0, b: 0, a: 0);
90 else if (i < 60)
91 colorTable[i] = qRgba(r: (i * 2) + 120, g: 0, b: 0, a: 15);
92 else if (i < 120)
93 colorTable[i] = qRgba(r: 0, g: ((i - 60) * 2) + 120, b: 0, a: 50);
94 else if (i < 180)
95 colorTable[i] = qRgba(r: 0, g: 0, b: ((i - 120) * 2) + 120, a: 255);
96 else
97 colorTable[i] = qRgba(r: i, g: i, b: i, a: 255);
98 }
99
100 volumeItem->setColorTable(colorTable);
101}
102

source code of qtdatavis3d/tests/manual/qmlvolume/datasource.cpp