remove some unnecessary CONFIG additions
[profile/ivi/qtdeclarative.git] / src / particles / qquickturbulence.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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     \qmltype Turbulence
51     \instantiates QQuickTurbulenceAffector
52     \inqmlmodule QtQuick.Particles 2
53     \ingroup qtquick-particles
54     \inherits Affector
55     \brief Provides fluid-like forces from a noise image
56
57     The Turbulence Element scales the noise source over the area it affects,
58     and uses the curl of that source to generate force vectors.
59
60     Turbulence requires a fixed size. Unlike other affectors, a 0x0 Turbulence element
61     will affect no particles.
62
63     The source should be relatively smooth black and white noise, such as perlin noise.
64 */
65 /*!
66     \qmlproperty real QtQuick.Particles2::Turbulence::strength
67
68     The magnitude of the velocity vector at any point varies between zero and
69     the square root of two. It will then be multiplied by strength to get the
70     velocity per second for the particles affected by the turbulence.
71 */
72 /*!
73     \qmlproperty url QtQuick.Particles2::Turbulence::noiseSource
74
75     The source image to generate the turbulence from. It will be scaled to the size of the element,
76     so equal or larger sizes will give better results. Tweaking this image is the only way to tweak
77     behavior such as where vortices are or how many exist.
78
79     The source should be a relatively smooth black and white noise image, such as perlin noise.
80     A default image will be used if none is provided.
81 */
82
83 QQuickTurbulenceAffector::QQuickTurbulenceAffector(QQuickItem *parent) :
84     QQuickParticleAffector(parent),
85     m_strength(10), m_lastT(0), m_gridSize(0), m_field(0), m_vectorField(0), m_inited(false)
86 {
87 }
88
89 void QQuickTurbulenceAffector::geometryChanged(const QRectF &, const QRectF &)
90 {
91     initializeGrid();
92 }
93
94 QQuickTurbulenceAffector::~QQuickTurbulenceAffector()
95 {
96     if (m_field) {
97         for (int i=0; i<m_gridSize; i++)
98             free(m_field[i]);
99         free(m_field);
100     }
101     if (m_vectorField) {
102         for (int i=0; i<m_gridSize; i++)
103             free(m_vectorField[i]);
104         free(m_vectorField);
105     }
106 }
107
108 void QQuickTurbulenceAffector::initializeGrid()
109 {
110     if (!m_inited)
111         return;
112
113     int arg = qMax(width(), height());
114     if (m_gridSize != arg) {
115         if (m_field){ //deallocate and then reallocate grid
116             for (int i=0; i<m_gridSize; i++)
117                 free(m_field[i]);
118             free(m_field);
119             m_system = 0;
120         }
121         if (m_vectorField) {
122             for (int i=0; i<m_gridSize; i++)
123                 free(m_vectorField[i]);
124             free(m_vectorField);
125         }
126         m_gridSize = arg;
127     }
128
129     m_field = (qreal**)malloc(m_gridSize * sizeof(qreal*));
130     for (int i=0; i<m_gridSize; i++)
131         m_field[i] = (qreal*)malloc(m_gridSize * sizeof(qreal));
132     m_vectorField = (QPointF**)malloc(m_gridSize * sizeof(QPointF*));
133     for (int i=0; i<m_gridSize; i++)
134         m_vectorField[i] = (QPointF*)malloc(m_gridSize * sizeof(QPointF));
135
136     QImage image;
137     if (!m_noiseSource.isEmpty())
138         image = QImage(m_noiseSource.toLocalFile()).scaled(QSize(m_gridSize, m_gridSize));
139     if (image.isNull())
140         image = QImage(QStringLiteral(":particleresources/noise.png")).scaled(QSize(m_gridSize, m_gridSize));
141
142     for (int i=0; i<m_gridSize; i++)
143         for (int j=0; j<m_gridSize; j++)
144             m_field[i][j] = qRed(image.pixel(QPoint(i,j)));//Red as proxy for Value
145     for (int i=0; i<m_gridSize; i++){
146         for (int j=0; j<m_gridSize; j++){
147             m_vectorField[i][j].setX(boundsRespectingField(i,j) - boundsRespectingField(i,j-1));
148             m_vectorField[i][j].setY(boundsRespectingField(i-1,j) - boundsRespectingField(i,j));
149         }
150     }
151 }
152
153 qreal QQuickTurbulenceAffector::boundsRespectingField(int x, int y)
154 {
155     if (x < 0)
156         x = 0;
157     if (x >= m_gridSize)
158         x = m_gridSize - 1;
159     if (y < 0)
160         y = 0;
161     if (y >= m_gridSize)
162         y = m_gridSize - 1;
163     return m_field[x][y];
164 }
165
166 void QQuickTurbulenceAffector::ensureInit()
167 {
168     if (m_inited)
169         return;
170     m_inited = true;
171     initializeGrid();
172 }
173
174 void QQuickTurbulenceAffector::affectSystem(qreal dt)
175 {
176     if (!m_system || !m_enabled)
177         return;
178     ensureInit();
179     if (!m_gridSize)
180         return;
181
182     updateOffsets();//### Needed if an ancestor is transformed.
183
184     QRect boundsRect(0,0,m_gridSize,m_gridSize);
185     foreach (QQuickParticleGroupData *gd, m_system->groupData){
186         if (!activeGroup(m_system->groupData.key(gd)))
187             continue;
188         foreach (QQuickParticleData *d, gd->data){
189             if (!shouldAffect(d))
190                 continue;
191             QPoint pos = (QPointF(d->curX(), d->curY()) - m_offset).toPoint();
192             if (!boundsRect.contains(pos,true))//Need to redo bounds checking due to quantization.
193                 continue;
194             qreal fx = 0.0;
195             qreal fy = 0.0;
196             fx += m_vectorField[pos.x()][pos.y()].x() * m_strength;
197             fy += m_vectorField[pos.x()][pos.y()].y() * m_strength;
198             if (fx || fy){
199                 d->setInstantaneousVX(d->curVX()+ fx * dt);
200                 d->setInstantaneousVY(d->curVY()+ fy * dt);
201                 postAffect(d);
202             }
203         }
204     }
205 }
206
207 QT_END_NAMESPACE