Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/particles/qquickturbulence.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
5 | ** | - | ||||||||||||||||||
6 | ** This file is part of the QtQuick module of the Qt Toolkit. | - | ||||||||||||||||||
7 | ** | - | ||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
16 | ** | - | ||||||||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
24 | ** | - | ||||||||||||||||||
25 | ** GNU General Public License Usage | - | ||||||||||||||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
35 | ** | - | ||||||||||||||||||
36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
37 | ** | - | ||||||||||||||||||
38 | ****************************************************************************/ | - | ||||||||||||||||||
39 | - | |||||||||||||||||||
40 | #include "qquickturbulence_p.h" | - | ||||||||||||||||||
41 | #include "qquickparticlepainter_p.h"//TODO: Why was this needed again? | - | ||||||||||||||||||
42 | #include <cmath> | - | ||||||||||||||||||
43 | #include <cstdlib> | - | ||||||||||||||||||
44 | #include <QDebug> | - | ||||||||||||||||||
45 | #include <QQmlFile> | - | ||||||||||||||||||
46 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
47 | - | |||||||||||||||||||
48 | /*! | - | ||||||||||||||||||
49 | \qmltype Turbulence | - | ||||||||||||||||||
50 | \instantiates QQuickTurbulenceAffector | - | ||||||||||||||||||
51 | \inqmlmodule QtQuick.Particles | - | ||||||||||||||||||
52 | \ingroup qtquick-particles | - | ||||||||||||||||||
53 | \inherits Affector | - | ||||||||||||||||||
54 | \brief Provides fluid-like forces from a noise image. | - | ||||||||||||||||||
55 | - | |||||||||||||||||||
56 | The Turbulence Element scales the noise source over the area it affects, | - | ||||||||||||||||||
57 | and uses the curl of that source to generate force vectors. | - | ||||||||||||||||||
58 | - | |||||||||||||||||||
59 | Turbulence requires a fixed size. Unlike other affectors, a 0x0 Turbulence element | - | ||||||||||||||||||
60 | will affect no particles. | - | ||||||||||||||||||
61 | - | |||||||||||||||||||
62 | The source should be relatively smooth black and white noise, such as perlin noise. | - | ||||||||||||||||||
63 | */ | - | ||||||||||||||||||
64 | /*! | - | ||||||||||||||||||
65 | \qmlproperty real QtQuick.Particles::Turbulence::strength | - | ||||||||||||||||||
66 | - | |||||||||||||||||||
67 | The magnitude of the velocity vector at any point varies between zero and | - | ||||||||||||||||||
68 | the square root of two. It will then be multiplied by strength to get the | - | ||||||||||||||||||
69 | velocity per second for the particles affected by the turbulence. | - | ||||||||||||||||||
70 | */ | - | ||||||||||||||||||
71 | /*! | - | ||||||||||||||||||
72 | \qmlproperty url QtQuick.Particles::Turbulence::noiseSource | - | ||||||||||||||||||
73 | - | |||||||||||||||||||
74 | The source image to generate the turbulence from. It will be scaled to the size of the element, | - | ||||||||||||||||||
75 | so equal or larger sizes will give better results. Tweaking this image is the only way to tweak | - | ||||||||||||||||||
76 | behavior such as where vortices are or how many exist. | - | ||||||||||||||||||
77 | - | |||||||||||||||||||
78 | The source should be a relatively smooth black and white noise image, such as perlin noise. | - | ||||||||||||||||||
79 | A default image will be used if none is provided. | - | ||||||||||||||||||
80 | */ | - | ||||||||||||||||||
81 | - | |||||||||||||||||||
82 | QQuickTurbulenceAffector::QQuickTurbulenceAffector(QQuickItem *parent) : | - | ||||||||||||||||||
83 | QQuickParticleAffector(parent), | - | ||||||||||||||||||
84 | m_strength(10), m_lastT(0), m_gridSize(0), m_field(nullptr), m_vectorField(nullptr), m_inited(false) | - | ||||||||||||||||||
85 | { | - | ||||||||||||||||||
86 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||||||||
87 | - | |||||||||||||||||||
88 | void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &) | - | ||||||||||||||||||
89 | { | - | ||||||||||||||||||
90 | initializeGrid(); | - | ||||||||||||||||||
91 | } executed 16 times by 2 tests: end of block Executed by:
| 16 | ||||||||||||||||||
92 | - | |||||||||||||||||||
93 | QQuickTurbulenceAffector::~QQuickTurbulenceAffector() | - | ||||||||||||||||||
94 | { | - | ||||||||||||||||||
95 | if (m_field) {
| 2-6 | ||||||||||||||||||
96 | for (int i=0; i<m_gridSize; i++)
| 2-640 | ||||||||||||||||||
97 | free(m_field[i]); executed 640 times by 1 test: free(m_field[i]); Executed by:
| 640 | ||||||||||||||||||
98 | free(m_field); | - | ||||||||||||||||||
99 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
100 | if (m_vectorField) {
| 2-6 | ||||||||||||||||||
101 | for (int i=0; i<m_gridSize; i++)
| 2-640 | ||||||||||||||||||
102 | free(m_vectorField[i]); executed 640 times by 1 test: free(m_vectorField[i]); Executed by:
| 640 | ||||||||||||||||||
103 | free(m_vectorField); | - | ||||||||||||||||||
104 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
105 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||||||||||||||
106 | - | |||||||||||||||||||
107 | void QQuickTurbulenceAffector::initializeGrid() | - | ||||||||||||||||||
108 | { | - | ||||||||||||||||||
109 | if (!m_inited)
| 2-16 | ||||||||||||||||||
110 | return; executed 16 times by 2 tests: return; Executed by:
| 16 | ||||||||||||||||||
111 | - | |||||||||||||||||||
112 | int arg = qMax(width(), height()); | - | ||||||||||||||||||
113 | if (m_gridSize != arg) {
| 0-2 | ||||||||||||||||||
114 | if (m_field){ //deallocate and then reallocate grid
| 0-2 | ||||||||||||||||||
115 | for (int i=0; i<m_gridSize; i++)
| 0 | ||||||||||||||||||
116 | free(m_field[i]); never executed: free(m_field[i]); | 0 | ||||||||||||||||||
117 | free(m_field); | - | ||||||||||||||||||
118 | } never executed: end of block | 0 | ||||||||||||||||||
119 | if (m_vectorField) {
| 0-2 | ||||||||||||||||||
120 | for (int i=0; i<m_gridSize; i++)
| 0 | ||||||||||||||||||
121 | free(m_vectorField[i]); never executed: free(m_vectorField[i]); | 0 | ||||||||||||||||||
122 | free(m_vectorField); | - | ||||||||||||||||||
123 | } never executed: end of block | 0 | ||||||||||||||||||
124 | m_gridSize = arg; | - | ||||||||||||||||||
125 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
126 | - | |||||||||||||||||||
127 | m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*)); | - | ||||||||||||||||||
128 | for (int i=0; i<m_gridSize; i++)
| 2-640 | ||||||||||||||||||
129 | m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal)); executed 640 times by 1 test: m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal)); Executed by:
| 640 | ||||||||||||||||||
130 | m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*)); | - | ||||||||||||||||||
131 | for (int i=0; i<m_gridSize; i++)
| 2-640 | ||||||||||||||||||
132 | m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF)); executed 640 times by 1 test: m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF)); Executed by:
| 640 | ||||||||||||||||||
133 | - | |||||||||||||||||||
134 | QImage image; | - | ||||||||||||||||||
135 | if (!m_noiseSource.isEmpty())
| 0-2 | ||||||||||||||||||
136 | image = QImage(QQmlFile::urlToLocalFileOrQrc(m_noiseSource)).scaled(QSize(m_gridSize, m_gridSize)); never executed: image = QImage(QQmlFile::urlToLocalFileOrQrc(m_noiseSource)).scaled(QSize(m_gridSize, m_gridSize)); | 0 | ||||||||||||||||||
137 | if (image.isNull())
| 0-2 | ||||||||||||||||||
138 | image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize)); executed 2 times by 1 test: image = QImage(([]() noexcept -> QString { enum { Size = sizeof(u"" ":particleresources/noise.png")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" ":particleresources/noise.png" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }())).scaled(QSize(m_gridSize, m_gridSize)); Executed by:
executed 2 times by 1 test: return qstring_literal_temp; Executed by:
| 2 | ||||||||||||||||||
139 | - | |||||||||||||||||||
140 | for (int i=0; i<m_gridSize; i++)
| 2-640 | ||||||||||||||||||
141 | for (int j=0; j<m_gridSize; j++)
| 640-204800 | ||||||||||||||||||
142 | m_field[i][j] = qGray(image.pixel(QPoint(i,j))); executed 204800 times by 1 test: m_field[i][j] = qGray(image.pixel(QPoint(i,j))); Executed by:
| 204800 | ||||||||||||||||||
143 | for (int i=0; i<m_gridSize; i++){
| 2-640 | ||||||||||||||||||
144 | for (int j=0; j<m_gridSize; j++){
| 640-204800 | ||||||||||||||||||
145 | m_vectorField[i][j].setX(boundsRespectingField(i-1,j) - boundsRespectingField(i,j)); | - | ||||||||||||||||||
146 | m_vectorField[i][j].setY(boundsRespectingField(i,j) - boundsRespectingField(i,j-1)); | - | ||||||||||||||||||
147 | } executed 204800 times by 1 test: end of block Executed by:
| 204800 | ||||||||||||||||||
148 | } executed 640 times by 1 test: end of block Executed by:
| 640 | ||||||||||||||||||
149 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
150 | - | |||||||||||||||||||
151 | qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y) | - | ||||||||||||||||||
152 | { | - | ||||||||||||||||||
153 | if (x < 0)
| 640-818560 | ||||||||||||||||||
154 | x = 0; executed 640 times by 1 test: x = 0; Executed by:
| 640 | ||||||||||||||||||
155 | if (x >= m_gridSize)
| 0-819200 | ||||||||||||||||||
156 | x = m_gridSize - 1; never executed: x = m_gridSize - 1; | 0 | ||||||||||||||||||
157 | if (y < 0)
| 640-818560 | ||||||||||||||||||
158 | y = 0; executed 640 times by 1 test: y = 0; Executed by:
| 640 | ||||||||||||||||||
159 | if (y >= m_gridSize)
| 0-819200 | ||||||||||||||||||
160 | y = m_gridSize - 1; never executed: y = m_gridSize - 1; | 0 | ||||||||||||||||||
161 | return m_field[x][y]; executed 819200 times by 1 test: return m_field[x][y]; Executed by:
| 819200 | ||||||||||||||||||
162 | } | - | ||||||||||||||||||
163 | - | |||||||||||||||||||
164 | void QQuickTurbulenceAffector::ensureInit() | - | ||||||||||||||||||
165 | { | - | ||||||||||||||||||
166 | if (m_inited)
| 2-78 | ||||||||||||||||||
167 | return; executed 78 times by 1 test: return; Executed by:
| 78 | ||||||||||||||||||
168 | m_inited = true; | - | ||||||||||||||||||
169 | initializeGrid(); | - | ||||||||||||||||||
170 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
171 | - | |||||||||||||||||||
172 | void QQuickTurbulenceAffector::affectSystem(qreal dt) | - | ||||||||||||||||||
173 | { | - | ||||||||||||||||||
174 | if (!m_system || !m_enabled)
| 0-80 | ||||||||||||||||||
175 | return; never executed: return; | 0 | ||||||||||||||||||
176 | ensureInit(); | - | ||||||||||||||||||
177 | if (!m_gridSize)
| 0-80 | ||||||||||||||||||
178 | return; never executed: return; | 0 | ||||||||||||||||||
179 | - | |||||||||||||||||||
180 | updateOffsets();//### Needed if an ancestor is transformed. | - | ||||||||||||||||||
181 | - | |||||||||||||||||||
182 | QRect boundsRect(0,0,m_gridSize,m_gridSize); | - | ||||||||||||||||||
183 | foreach (QQuickParticleGroupData *gd, m_system->groupData){
| 0-160 | ||||||||||||||||||
184 | if (!activeGroup(gd->index))
| 0-80 | ||||||||||||||||||
185 | continue; never executed: continue; | 0 | ||||||||||||||||||
186 | foreach (QQuickParticleData *d, gd->data){
| 0-40080 | ||||||||||||||||||
187 | if (!shouldAffect(d))
| 16156-23844 | ||||||||||||||||||
188 | continue; executed 16156 times by 1 test: continue; Executed by:
| 16156 | ||||||||||||||||||
189 | QPoint pos = (QPointF(d->curX(m_system), d->curY(m_system)) - m_offset).toPoint(); | - | ||||||||||||||||||
190 | if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
| 0-23844 | ||||||||||||||||||
191 | continue; never executed: continue; | 0 | ||||||||||||||||||
192 | qreal fx = 0.0; | - | ||||||||||||||||||
193 | qreal fy = 0.0; | - | ||||||||||||||||||
194 | fx += m_vectorField[pos.x()][pos.y()].x() * m_strength; | - | ||||||||||||||||||
195 | fy += m_vectorField[pos.x()][pos.y()].y() * m_strength; | - | ||||||||||||||||||
196 | if (fx || fy){ | - | ||||||||||||||||||
197 | d->setInstantaneousVX(d->curVX(m_system)+ fx * dt, m_system); | - | ||||||||||||||||||
198 | d->setInstantaneousVY(d->curVY(m_system)+ fy * dt, m_system); | - | ||||||||||||||||||
199 | postAffect(d); | - | ||||||||||||||||||
200 | } executed 20708 times by 1 test: end of block Executed by:
| 20708 | ||||||||||||||||||
201 | } executed 23844 times by 1 test: end of block Executed by:
| 23844 | ||||||||||||||||||
202 | } executed 80 times by 1 test: end of block Executed by:
| 80 | ||||||||||||||||||
203 | } executed 80 times by 1 test: end of block Executed by:
| 80 | ||||||||||||||||||
204 | - | |||||||||||||||||||
205 | QT_END_NAMESPACE | - | ||||||||||||||||||
206 | - | |||||||||||||||||||
207 | #include "moc_qquickturbulence_p.cpp" | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |