Avoid string-based lookup in IS_SIGNAL_CONNECTED
[profile/ivi/qtdeclarative.git] / src / particles / qquickturbulence.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qquickturbulence_p.h"
43 #include "qquickparticlepainter_p.h"//TODO: Why was this needed again?
44 #include <cmath>
45 #include <cstdlib>
46 #include <QDebug>
47 QT_BEGIN_NAMESPACE
48
49 /*!
50     \qmlclass Turbulence QQuickTurbulenceAffector
51     \inqmlmodule QtQuick.Particles 2
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.Particles2::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.Particles2::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(0), m_vectorField(0), m_inited(false)
85 {
86 }
87
88 void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &)
89 {
90     initializeGrid();
91 }
92
93 QQuickTurbulenceAffector::~QQuickTurbulenceAffector()
94 {
95     if (m_field) {
96         for (int i=0; i<m_gridSize; i++)
97             free(m_field[i]);
98         free(m_field);
99     }
100     if (m_vectorField) {
101         for (int i=0; i<m_gridSize; i++)
102             free(m_vectorField[i]);
103         free(m_vectorField);
104     }
105 }
106
107 void QQuickTurbulenceAffector::initializeGrid()
108 {
109     if (!m_inited)
110         return;
111
112     int arg = qMax(width(), height());
113     if (m_gridSize != arg) {
114         if (m_field){ //deallocate and then reallocate grid
115             for (int i=0; i<m_gridSize; i++)
116                 free(m_field[i]);
117             free(m_field);
118             m_system = 0;
119         }
120         if (m_vectorField) {
121             for (int i=0; i<m_gridSize; i++)
122                 free(m_vectorField[i]);
123             free(m_vectorField);
124         }
125         m_gridSize = arg;
126     }
127
128     m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
129     for (int i=0; i<m_gridSize; i++)
130         m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
131     m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
132     for (int i=0; i<m_gridSize; i++)
133         m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
134
135     QImage image;
136     if (!m_noiseSource.isEmpty())
137         image = QImage(m_noiseSource.toLocalFile()).scaled(QSize(m_gridSize, m_gridSize));
138     if (image.isNull())
139         image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
140
141     for (int i=0; i<m_gridSize; i++)
142         for (int j=0; j<m_gridSize; j++)
143             m_field[i][j] = qRed(image.pixel(QPoint(i,j)));//Red as proxy for Value
144     for (int i=0; i<m_gridSize; i++){
145         for (int j=0; j<m_gridSize; j++){
146             m_vectorField[i][j].setX(boundsRespectingField(i,j) - boundsRespectingField(i,j-1));
147             m_vectorField[i][j].setY(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
148         }
149     }
150 }
151
152 qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
153 {
154     if (x < 0)
155         x = 0;
156     if (x >= m_gridSize)
157         x = m_gridSize - 1;
158     if (y < 0)
159         y = 0;
160     if (y >= m_gridSize)
161         y = m_gridSize - 1;
162     return m_field[x][y];
163 }
164
165 void QQuickTurbulenceAffector::ensureInit()
166 {
167     if (m_inited)
168         return;
169     m_inited = true;
170     initializeGrid();
171 }
172
173 void QQuickTurbulenceAffector::affectSystem(qreal dt)
174 {
175     if (!m_system || !m_enabled)
176         return;
177     ensureInit();
178     if (!m_gridSize)
179         return;
180
181     updateOffsets();//### Needed if an ancestor is transformed.
182
183     QRect boundsRect(0,0,m_gridSize,m_gridSize);
184     foreach (QQuickParticleGroupData *gd, m_system->groupData){
185         if (!activeGroup(m_system->groupData.key(gd)))
186             continue;
187         foreach (QQuickParticleData *d, gd->data){
188             if (!shouldAffect(d))
189                 continue;
190             QPoint pos = (QPointF(d->curX(), d->curY()) - m_offset).toPoint();
191             if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
192                 continue;
193             qreal fx = 0.0;
194             qreal fy = 0.0;
195             fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
196             fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
197             if (fx || fy){
198                 d->setInstantaneousVX(d->curVX()+ fx * dt);
199                 d->setInstantaneousVY(d->curVY()+ fy * dt);
200                 postAffect(d);
201             }
202         }
203     }
204 }
205
206 QT_END_NAMESPACE