Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dparticlerandomizer_p.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5
6#ifndef QQUICK3DPARTICLERANDOMIZER_H
7#define QQUICK3DPARTICLERANDOMIZER_H
8
9//
10// W A R N I N G
11// -------------
12//
13// This file is not part of the Qt API. It exists purely as an
14// implementation detail. This header file may change from version to
15// version without notice, or even be removed.
16//
17// We mean it.
18//
19
20#include <QRandomGenerator>
21#include <QList>
22#include <private/qglobal_p.h>
23
25
26/*
27
28Simple helper to get pseudo-random numbers which remain the same per particle & user.
29Particles don't need strong randomization and the ability to seed can be useful.
30
31Based on brief testing on my laptop, getting 1.000.000 random numbers:
32
331) Using QRandomGenerator::global()->generateDouble()
34-> ~120ms
35
362) Using QPRand::get(), with m_size 4096
37-> ~8ms
38
393) Using QPRand::get(i, j), with m_size 4096
40-> ~10ms
41
424) Using QPRand::get(i, j), with m_size 100000
43-> ~10ms
44
45So QPRand seems fast and increasing keys amount doesn't notably affect the performance,
46just the memory usage. With more particles m_size should be relatively big to make sure
47particles appear random enough.
48
49Bounded usage tips:
50- qrg->bounded(4.0) == QPRand::get() * 4.0
51- qrg->bounded(4) == int(QPRand::get() * 4)
52
53*/
54
55class QPRand
56{
57public:
58
59 // Add here new enums for diferent "users".
60 // This way e.g. particle can vary little on colors but more on sizes.
105
106 void init(quint32 seed, int size = 65536) {
107 m_size = size;
108 m_generator.seed(seed);
109 m_randomList.clear();
110 m_randomList.reserve(m_size);
111 for (int i = 0; i < m_size; i++)
112 m_randomList << float(m_generator.generateDouble());
113 }
114
115 void setDeterministic(bool deterministic) {
116 m_deterministic = deterministic;
117 }
118
119 // Return float 0.0 - 1.0
120 // With the same input values, returns always the same output.
121 inline float get(int particleIndex, UserType user = Default) {
122 if (!m_deterministic && user > DeterministicSeparator)
123 return get();
124 int i = (particleIndex + user) % m_size;
125 return m_randomList.at(i);
126 }
127
128 // Return float 0.0 - 1.0 from random list
129 // Note: Not deterministic between runs
130 inline float get() {
131 m_index = (m_index < m_size - 1) ? m_index + 1 : 0;
132 return m_randomList.at(m_index);
133 }
135 {
136 return m_generator;
137 }
138private:
139 QRandomGenerator m_generator;
140 int m_size = 0;
141 int m_index = 0;
142 bool m_deterministic = false;
143 QList<float> m_randomList;
144
145};
146
147QT_END_NAMESPACE
148
149#endif // QQUICK3DPARTICLERANDOMIZER_H
void init(quint32 seed, int size=65536)
QRandomGenerator generator() const
float get(int particleIndex, UserType user=Default)
void setDeterministic(bool deterministic)
Combined button and popup list for selecting options.
QT_BEGIN_NAMESPACE const float MIN_DURATION
\qmltype Attractor3D \inherits Affector3D \inqmlmodule QtQuick3D.Particles3D