summaryrefslogtreecommitdiffstats
path: root/examples/surfacechart/chartmodifier.cpp
blob: 48a4bcce8b48bf2a5259710c593272846669613a (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/****************************************************************************
**
** Copyright (C) 2013 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 QtDataVis3D 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 "chartmodifier.h"
#include <Q3DValueAxis>
#include <QSurfaceDataProxy>

#include <qmath.h>

#include <QDebug>

QT_DATAVIS3D_USE_NAMESPACE

ChartModifier::ChartModifier(Q3DSurface *chart)
    : m_chart(chart),
      m_xCount(10),
      m_zCount(10),
      m_activeSample(0)
{
    m_chart->setAxisX(new Q3DValueAxis);
    m_chart->setAxisY(new Q3DValueAxis);
    m_chart->setAxisZ(new Q3DValueAxis);

    QSurfaceDataProxy *proxy = new QSurfaceDataProxy;
    m_chart->setActiveDataProxy(proxy);
}

ChartModifier::~ChartModifier()
{
    delete m_chart;
}

void ChartModifier::toggleSmooth(bool enabled)
{
    qDebug() << "ChartModifier::toggleSmooth " << enabled;
    m_chart->setSmoothSurface(enabled);
}

void ChartModifier::toggleSurfaceGrid(bool enable)
{
    qDebug() << "ChartModifier::toggleSurfaceGrid" << enable;
    m_chart->setSurfaceGrid(enable);
}

void ChartModifier::toggleSqrtSin(bool enable)
{
    qreal biggest = -9999.0;
    qreal smallest = 9999.0;

    if (enable) {
        qDebug() << "Create Sqrt&Sin surface, (" << m_xCount << ", " << m_zCount << ")";

        qreal stepZ = 16.0 / qreal(m_zCount);
        qreal stepX = 16.0 / qreal(m_xCount);

        QSurfaceDataArray *dataArray = new QSurfaceDataArray;
        dataArray->reserve(m_zCount);
        for (qreal i = -8.0 + stepZ / 2.0 ; i < 8.0 ; i += stepZ) {
            QSurfaceDataRow *newRow = new QSurfaceDataRow(m_xCount);
            int index = 0;
            for (qreal j = -8.0 + stepX / 2.0; j < 8.0; j += stepX) {
                qreal R = qSqrt(i*i + j*j) + 0.01;
                qreal y = (sin(R)/R + 0.24) * 1.61;
                (*newRow)[index++] = y;
                if (y > biggest) biggest = y;
                if (y < smallest) smallest = y;
            }
            *dataArray << newRow;
        }

        m_chart->axisX()->setRange(0.0, qreal(m_xCount - 1));
        m_chart->axisY()->setRange(0.0, 2.0);
        m_chart->axisZ()->setRange(0.0, qreal(m_zCount - 1));
        m_chart->activeDataProxy()->resetArray(dataArray);

        m_activeSample = ChartModifier::SqrtSin;

        //qDebug() << "biggest = " << biggest << ", smallest = " << smallest;
    } else {
        qDebug() << "Remove surface";
    }
}

void ChartModifier::togglePlane(bool enable)
{
    qDebug() << "ChartModifier::togglePlane " << enable;

    if (enable) {
        QSurfaceDataArray *dataArray = new QSurfaceDataArray;
        qreal y = 2.0 / qreal(m_zCount - 1);
        dataArray->reserve(m_zCount);
        for (int i = 0; i < m_zCount; i++) {
            QSurfaceDataRow *newRow = new QSurfaceDataRow(m_xCount);
            for (int j = 0; j < m_xCount; j++)
                (*newRow)[j] = i * y;
            *dataArray << newRow;
        }

        m_chart->axisX()->setSegmentCount(3);
        m_chart->axisX()->setRange(0.0, qreal(m_xCount - 1));
        //m_chart->axisY()->setSegmentCount(4);
        m_chart->axisY()->setRange(0.0, 2.0);
        //m_chart->axisZ()->setSegmentCount(4/*m_zCount - 1*/);
        m_chart->axisZ()->setRange(0.0, qreal(m_zCount - 1));

        m_chart->activeDataProxy()->resetArray(dataArray);

        m_activeSample = ChartModifier::Plane;
    }
}

void ChartModifier::setHeightMapData(bool enable)
{
    if (enable) {
        QImage image(":/maps/map");

        QSurfaceDataArray *dataArray = new QSurfaceDataArray;
        uchar *bits = image.bits();

        int p = image.width() * 4 * (image.height() - 1);
        dataArray->reserve(image.height());
        qDebug() << image.height() << image.width();
        for (int i = image.height(); i > 0; i--, p -= image.width() * 4) {
            QSurfaceDataRow *newRow = new QSurfaceDataRow(image.width());
            for (int j = 0; j < image.width(); j++)
                (*newRow)[j] = (qreal(bits[p + (j * 4)]) + 1.0) / 1.0;
            *dataArray << newRow;
        }

        m_chart->axisX()->setRange(0.0, qreal(image.width() - 1));
        m_chart->axisY()->setRange(0.0, 255.0);
        m_chart->axisZ()->setRange(0.0, qreal(image.height() - 1));

        m_chart->activeDataProxy()->resetArray(dataArray);

        m_activeSample = ChartModifier::Map;
    }
}

void ChartModifier::toggleGridSliderLock(bool enable)
{
    m_gridSlidersLocked = enable;
    if (m_gridSlidersLocked) {
        m_gridSliderZ->setEnabled(false);
        m_gridSliderZ->setValue(m_gridSliderX->value());
    } else {
        m_gridSliderZ->setEnabled(true);
    }
}

void ChartModifier::adjustXCount(int count)
{
    m_xCount = count;
    if (m_gridSlidersLocked)
        m_gridSliderZ->setValue(count);

    updateSamples();

    qDebug() << "X count = " << count;
}

void ChartModifier::adjustZCount(int count)
{
    m_zCount = count;

    updateSamples();

    qDebug() << "Z count = " << count;
}

void ChartModifier::updateSamples()
{
    switch (m_activeSample) {
    case SqrtSin:
        toggleSqrtSin(true);
        break;

    case Plane:
        togglePlane(true);
        break;

    default:
        break;
    }
}