Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#ifndef QQUICK3DPARTICLERANDOMIZER_H
5#define QQUICK3DPARTICLERANDOMIZER_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QRandomGenerator>
19#include <QList>
20#include <private/qglobal_p.h>
21
23
24/*
25
26Simple helper to get pseudo-random numbers which remain the same per particle & user.
27Particles don't need strong randomization and the ability to seed can be useful.
28
29Based on brief testing on my laptop, getting 1.000.000 random numbers:
30
311) Using QRandomGenerator::global()->generateDouble()
32-> ~120ms
33
342) Using QPRand::get(), with m_size 4096
35-> ~8ms
36
373) Using QPRand::get(i, j), with m_size 4096
38-> ~10ms
39
404) Using QPRand::get(i, j), with m_size 100000
41-> ~10ms
42
43So QPRand seems fast and increasing keys amount doesn't notably affect the performance,
44just the memory usage. With more particles m_size should be relatively big to make sure
45particles appear random enough.
46
47Bounded usage tips:
48- qrg->bounded(4.0) == QPRand::get() * 4.0
49- qrg->bounded(4) == int(QPRand::get() * 4)
50
51*/
52
53class QPRand
54{
55public:
56
57 // Add here new enums for diferent "users".
58 // This way e.g. particle can vary little on colors but more on sizes.
103
104 void init(quint32 seed, int size = 65536) {
105 m_size = size;
106 m_generator.seed(seed);
107 m_randomList.clear();
108 m_randomList.reserve(m_size);
109 for (int i = 0; i < m_size; i++)
110 m_randomList << float(m_generator.generateDouble());
111 }
112
113 void setDeterministic(bool deterministic) {
114 m_deterministic = deterministic;
115 }
116
117 // Return float 0.0 - 1.0
118 // With the same input values, returns always the same output.
119 inline float get(int particleIndex, UserType user = Default) {
120 if (!m_deterministic && user > DeterministicSeparator)
121 return get();
122 int i = (particleIndex + user) % m_size;
123 return m_randomList.at(i);
124 }
125
126 // Return float 0.0 - 1.0 from random list
127 // Note: Not deterministic between runs
128 inline float get() {
129 m_index = (m_index < m_size - 1) ? m_index + 1 : 0;
130 return m_randomList.at(m_index);
131 }
133 {
134 return m_generator;
135 }
136private:
137 QRandomGenerator m_generator;
138 int m_size = 0;
139 int m_index = 0;
140 bool m_deterministic = false;
141 QList<float> m_randomList;
142
143};
144
146
147#endif // QQUICK3DPARTICLERANDOMIZER_H
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void reserve(qsizetype size)
Definition qlist.h:753
void clear()
Definition qlist.h:434
void init(quint32 seed, int size=65536)
QRandomGenerator generator() const
float get(int particleIndex, UserType user=Default)
void setDeterministic(bool deterministic)
\inmodule QtCore \reentrant
Definition qrandom.h:21
double generateDouble()
Generates one random qreal in the canonical range [0, 1) (that is, inclusive of zero and exclusive of...
Definition qrandom.h:58
void seed(quint32 s=1)
Reseeds this object using the value seed as the seed.
Definition qrandom.h:167
Combined button and popup list for selecting options.
GLenum GLuint GLintptr GLsizeiptr size
[1]
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
unsigned int quint32
Definition qtypes.h:50