aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/qsggeometry/setVertexCount-spiral/SpiralItem.cpp
blob: eaffc6990578a539de6a06bcbbadfab390262831 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
// Copyright (C) 2024 Stan Morris.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QSGGeometry>
#include <QSGGeometryNode>
#include <QSGVertexColorMaterial>
#include <cmath>
#include <numbers>
#include "SpiralItem.h"

namespace {
    std::array<int, 4> getParts(const QColor &color) {
        return {color.red(), color.green(), color.blue(), color.alpha()};
    }

    using ColorPalette = std::array<QColor, 3>;
    auto allocPalette = ColorPalette { QColor("aqua"), QColor("deepskyblue"), QColor("yellow") };
    auto setVCPalette = ColorPalette { QColor("yellow"), QColor("orange"), QColor("white") };
}

SpiralItem::SpiralItem()
{
    setFlag(QQuickItem::ItemHasContents);
    setWidth(960);
    setHeight(960);

    connect(this, &SpiralItem::vertexCountChanged,
            this, &QQuickItem::update);
    connect(this, &SpiralItem::resizeByVertexCountChanged,
            this, &SpiralItem::handleResizeByVertexCountChanged);

    emit resizeByVertexCountChanged();
}

QSGNode *SpiralItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *) {
    auto *n = static_cast<QSGGeometryNode *>(oldNode);
    if (!n) {
        n = new QSGGeometryNode();
        setupGeometryNode(n);
    }

    if (auto *geometry = n->geometry()) {
        if (m_vertexCount != geometry->vertexCount()) {
            updateGeometry(geometry);
        }
    }

    n->markDirty(QSGNode::DirtyGeometry);

    return n;
}

void SpiralItem::updateGeometry(QSGGeometry *&geometry) {
    if (m_resetVertices) {
        m_resetVertices = false;

        // must reallocate vertices if using setVertexCount to resize
        if (m_resizeByVertexCount)
            reallocateVertices(geometry, m_maxVertices);
    }

    const int newCount = qBound(0, m_vertexCount, m_maxVertices);
    if (m_resizeByVertexCount) {
        // change vertex count using new setVertexCount() function
        geometry->setVertexCount(newCount);
    } else {
        // change vertex count using allocate()
        reallocateVertices(geometry, newCount);
    }

    logStatistics();
}

void SpiralItem::setupGeometryNode(QSGGeometryNode *geometryNode)
{
    // Set the geometry on the node
    auto *geometry = createGeometry();
    geometryNode->setGeometry(geometry);
    geometryNode->setFlag(QSGNode::OwnsGeometry);

    // Set a ColoredPoint2D material
    geometryNode->setMaterial(new QSGVertexColorMaterial());
    geometryNode->setFlag(QSGNode::OwnsMaterial);
}

QSGGeometry *SpiralItem::createGeometry()
{
    auto *geometry = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(),
                                     m_maxVertices);

    geometry->setDrawingMode(QSGGeometry::DrawLineStrip);
    geometry->setLineWidth(3);

    // populate the geometry
    populateVertices(geometry, m_maxVertices);
    populateColors(geometry, m_maxVertices);

    return geometry;
}

void SpiralItem::populateVertices(QSGGeometry *geometry, const int count)
{
    constexpr double pi = 3.14159265f;
    constexpr int linesPerRing = 720;
    constexpr float angleIncrement = 2 * pi / linesPerRing;
    constexpr float lineSeparation = 6;
    constexpr float radiusIncrement = lineSeparation / linesPerRing;

    float radius = 1.5f;
    float xOffset = width()/2;
    float yOffset = height()/2;

    float angle = float(2 * pi / 1000.f);;

    // Iterate through all vertices
    auto *pt = geometry->vertexDataAsColoredPoint2D();
    for (int i = 0; i < count; ++i, ++pt) {
        pt->x = radius * cos(angle) + xOffset;
        pt->y = radius * sin(angle) + yOffset;

        radius += radiusIncrement;

        // Keep angle in 2*pi radians -- makes debugging easier
        angle += angleIncrement;
        if (angle > 2 * M_PI)
            angle = (2 * M_PI) - angle;
    }
}

void SpiralItem::populateColors(QSGGeometry *geometry, const int count)
{
    constexpr int segmentLength = 20;

    const auto& colors = m_resizeByVertexCount ? setVCPalette : allocPalette;

    auto *pt = geometry->vertexDataAsColoredPoint2D();
    for (int i = 0; i < count; ++i, ++pt) {
        const bool isEndOfSpiral = i <= 20*segmentLength
                                   || i > (m_maxVertices - segmentLength);
        auto index = isEndOfSpiral              ? 2 :
                     ((i/segmentLength)%2 == 0) ? 1
                                                : 0;
        const auto [r, g, b, a] = getParts(colors[index]);

        pt->r = r;
        pt->g = g;
        pt->b = b;
        pt->a = a;
    }
}

void SpiralItem::reallocateVertices(QSGGeometry *geometry, const int count) {
    geometry->allocate(count);
    populateVertices(geometry, count);
    populateColors(geometry, count);
}

void SpiralItem::logStatistics()
{
    if (!m_animating)
        return;

    if (m_vertexCount > m_maxCount)
        m_maxCount = m_vertexCount;

    if (m_vertexCount < m_minCount)
        m_minCount = m_vertexCount;

    if (m_statisticsTimer.elapsed() >= 1000) {
        qDebug() << m_mode
                 << "\tfps:" << m_fps
                 << "\tvertices:" << m_minCount << "-" << m_maxCount;
        m_statisticsTimer.restart();
        m_maxCount = -1;
        m_minCount = m_maxVertices;
    }
}

int SpiralItem::vertexCount() const
{
    return m_vertexCount;
}

void SpiralItem::setVertexCount(int newVertexCount)
{
    if (m_vertexCount == newVertexCount)
        return;
    m_vertexCount = newVertexCount;
    emit vertexCountChanged();
}

int SpiralItem::maxVertices() const
{
    return m_maxVertices;
}

int SpiralItem::fps() const
{
    return m_fps;
}

void SpiralItem::setFps(int newFps)
{
    if (m_fps == newFps)
        return;
    m_fps = newFps;
    emit fpsChanged();
}

void SpiralItem::toggleMode()
{
    if (!m_animating) {
        // Set up to alternate between techniques for resizing geometry
        m_statisticsTimer.start();
        m_animating = true;
    }

    // Toggle between using setVertexCount() and allocate() to resize geometry
    setResizeByVertexCount(!m_resizeByVertexCount);

    m_resetVertices = true;
}

void SpiralItem::handleResizeByVertexCountChanged()
{
    setMode(m_resizeByVertexCount ? "setVertexCount()" : "allocate()");
}

QString SpiralItem::mode() const
{
    return m_mode;
}

void SpiralItem::setMode(const QString &newMode)
{
    if (m_mode == newMode)
        return;
    m_mode = newMode;
    emit modeChanged();
}

bool SpiralItem::resizeByVertexCount() const
{
    return m_resizeByVertexCount;
}

void SpiralItem::setResizeByVertexCount(bool newResizeByVertexCount)
{
    if (m_resizeByVertexCount == newResizeByVertexCount)
        return;
    m_resizeByVertexCount = newResizeByVertexCount;
    emit resizeByVertexCountChanged();
}