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
qquick3dparticlesystemlogging.cpp
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
7#include <float.h> // FLT_MAX
8
10
11/*!
12 \qmltype ParticleSystem3DLogging
13 \inherits QtObject
14 \inqmlmodule QtQuick3D.Particles3D
15 \brief Provides information of the particle system.
16 \since 6.2
17
18 The \c ParticleSystem3DLogging type provides information about particle system statistics.
19 This element cannot be created directly, but can be retrieved from a \l ParticleSystem3D.
20*/
21
22QQuick3DParticleSystemLogging::QQuick3DParticleSystemLogging(QObject *parent)
23 : QObject(parent)
24{
25}
26
27/*!
28 \qmlproperty int ParticleSystem3DLogging::loggingInterval
29
30 This property defines in milliseconds how often the logging data is updated.
31 Longer update time increases the accuracy of \l {ParticleSystem3DLogging::time}{time} and
32 \l {ParticleSystem3DLogging::timeAverage}{timeAverage}, while shorter update times keep
33 the data more up to date.
34
35 The default value is \c 1000.
36*/
37
38int QQuick3DParticleSystemLogging::loggingInterval() const
39{
40 return m_loggingInterval;
41}
42
43void QQuick3DParticleSystemLogging::setLoggingInterval(int interval)
44{
45 if (m_loggingInterval == interval)
46 return;
47
48 m_loggingInterval = interval;
49 Q_EMIT loggingIntervalChanged();
50}
51
52/*!
53 \qmlproperty int ParticleSystem3DLogging::updates
54 \readonly
55
56 This property holds the amount of particle system updates since the last logging.
57 When \a loggingInterval is 1000 (default), this can be considered to match the fps.
58*/
59int QQuick3DParticleSystemLogging::updates() const
60{
61 return m_updates;
62}
63
64/*!
65 \qmlproperty int ParticleSystem3DLogging::particlesMax
66 \readonly
67
68 This property holds the maximum amount of particles in this system.
69 Maximum amount is the sum of system particles \l {Particle3D::maxAmount}{maxAmount} properties.
70*/
71int QQuick3DParticleSystemLogging::particlesMax() const
72{
73 return m_particlesMax;
74}
75
76/*!
77 \qmlproperty int ParticleSystem3DLogging::particlesUsed
78 \readonly
79
80 This property holds the amount of particles currently in use in this system.
81 This value should be close to \l particlesMax at some point of particle system
82 animation. If it is much smaller, consider decreasing \l {Particle3D::maxAmount}{maxAmount} values.
83 If it reaches \l particlesMax, particles are used effectively but it can also mean that
84 particles are reused before they reach the end of their \l {ParticleEmitter3D::lifeSpan}{lifeSpan}.
85 In this case, consider increasing the \l {Particle3D::maxAmount}{maxAmount} values.
86*/
87int QQuick3DParticleSystemLogging::particlesUsed() const
88{
89 return m_particlesUsed;
90}
91
92/*!
93 \qmlproperty real ParticleSystem3DLogging::time
94 \readonly
95
96 This property holds the time in milliseconds used for emitting and animating particles
97 in each frame.
98*/
99float QQuick3DParticleSystemLogging::time() const
100{
101 return m_time;
102}
103
104/*!
105 \qmlproperty real ParticleSystem3DLogging::timeAverage
106 \readonly
107
108 This property holds the average time in milliseconds used for emitting and animating
109 particles in each frame. Average is calculated from the middle 50% of the past
110 max 100 logging updates. So when \l loggingInterval is 1000, this represents an
111 average \l time in past 100 seconds. This can be used for measuring the performance
112 of current particle system.
113*/
114float QQuick3DParticleSystemLogging::timeAverage() const
115{
116 return m_timeAverage;
117}
118
119/*!
120 \qmlproperty real ParticleSystem3DLogging::timeDeviation
121 \since 6.3
122 \readonly
123
124 This property holds the deviation of the average times in milliseconds.
125 The value is the difference between maximum and minimum values of middle 50%
126 of the results, also called interquartile range (IQR).
127 Bigger deviation means that the times fluctuate more so \l timeAverage
128 can be considered to be less accurate.
129*/
130float QQuick3DParticleSystemLogging::timeDeviation() const
131{
132 return m_timeDeviation;
133}
134
135void QQuick3DParticleSystemLogging::updateTimes(qint64 time)
136{
137 m_time = float(time / 1000000.0) / m_updates;
138
139 m_totalTimesList.append(m_time);
140
141 // Keep max amount of times stored and remove the oldest values
142 const int MAX_TIMES = 100;
143 if (m_totalTimesList.size() > MAX_TIMES)
144 m_totalTimesList.removeFirst();
145
146 auto sortedTimes = m_totalTimesList;
147 std::sort(sortedTimes.begin(), sortedTimes.end());
148
149 // Calculate average from stored times.
150 // Only take into account the middle 50% of the values.
151 // This gives us interquartile range (IQR) and the average time among IQR.
152 if (sortedTimes.size() > 5) {
153 // Skip 25%, count 50%, so maxItem at 75%
154 const int skipAmount = roundf(0.25f * float(sortedTimes.size()));
155 const int maxItem = sortedTimes.size() - skipAmount;
156 int countAmount = 0;
157 double totalTime = 0.0;
158 float maxTime = 0.0f;
159 float minTime = FLT_MAX;
160 for (int i = skipAmount; i < maxItem; i++) {
161 const float time = sortedTimes.at(i);
162 totalTime += time;
163 minTime = std::min(minTime, time);
164 maxTime = std::max(maxTime, time);
165 countAmount++;
166 }
167 m_timeAverage = float(totalTime / countAmount);
168 m_timeDeviation = maxTime - minTime;
169 Q_EMIT timeAverageChanged();
170 Q_EMIT timeDeviationChanged();
171 }
172 Q_EMIT timeChanged();
173}
174
175void QQuick3DParticleSystemLogging::resetData()
176{
177 m_updates = 0;
178 m_particlesMax = 0;
179 m_particlesUsed = 0;
180 m_time = 0.0f;
181 m_timeAverage = 0.0f;
182 m_timeDeviation = 0.0f;
183 m_totalTimesList.clear();
184 Q_EMIT updatesChanged();
185 Q_EMIT particlesMaxChanged();
186 Q_EMIT particlesUsedChanged();
187 Q_EMIT timeChanged();
188 Q_EMIT timeAverageChanged();
189 Q_EMIT timeDeviationChanged();
190}
191
192QT_END_NAMESPACE
Combined button and popup list for selecting options.