File: Fluids2D.cpp

package info (click to toggle)
libwildmagic 5.17%2Bcleaned1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 90,124 kB
  • sloc: cpp: 215,940; csh: 637; sh: 91; makefile: 40
file content (247 lines) | stat: -rw-r--r-- 6,801 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.0 (2010/01/01)

#include "Fluids2D.h"

WM5_WINDOW_APPLICATION(Fluids2D);

//----------------------------------------------------------------------------
Fluids2D::Fluids2D ()
    :
    WindowApplication2("SamplePhysics/Fluids2D",0, 0, 512, 512,
        Float4(0.9f, 0.9f, 0.9f, 1.0f)),
        mTextColor(1.0f, 1.0f, 0.0f, 1.0f)
{
    mSmoke = 0;

    ColorRGB key[9] =
    {
        ColorRGB(  0,   0,   0),  // black
        ColorRGB(128,  64,  64),  // brown
        ColorRGB(128,   0, 255),  // violet
        ColorRGB(  0,   0, 255),  // blue
        ColorRGB(  0, 255,   0),  // green
        ColorRGB(255, 255,   0),  // yellow
        ColorRGB(255, 128,   0),  // orange
        ColorRGB(255,   0,   0),  // red
        ColorRGB(255, 255, 255)   // white
    };

    for (int i = 0, j = 0; i < 8; ++i)
    {
        for (int k = 0; k < 32; ++k, ++j)
        {
            float t = k/32.0f;
            float omt = 1.0f - t;
            mColor[j].r = (unsigned char)(omt*key[i].r + t*key[i+1].r);
            mColor[j].g = (unsigned char)(omt*key[i].g + t*key[i+1].g);
            mColor[j].b = (unsigned char)(omt*key[i].b + t*key[i+1].b);
        }
    }

    mSingleStep = false;
    mDrawColored = false;
    mDrawVortices = true;
}
//----------------------------------------------------------------------------
bool Fluids2D::OnInitialize ()
{
    if (!WindowApplication2::OnInitialize())
    {
        return false;
    }

    float x0 = 0.0f;
    float y0 = 0.0f;
    float x1 = 1.0f;
    float y1 = 1.0f;
    float dt = 0.001f;
    float denViscosity = 0.0001f;
    float velViscosity = 0.0001f;
    int imax = 255;
    int jmax = 255;
    int numGaussSeidelIterations = 32;
    bool densityDirichlet = true;
    int numVortices = 64;
    mSmoke = new0 Smoke2D<float>(x0, y0, x1, y1, dt, denViscosity,
        velViscosity, imax, jmax, numGaussSeidelIterations, densityDirichlet,
        numVortices);

    mSmoke->Initialize();
    DoFlip(true);
    OnDisplay();

    return true;
}
//----------------------------------------------------------------------------
void Fluids2D::OnTerminate ()
{
    delete0(mSmoke);
    WindowApplication2::OnTerminate();
}
//----------------------------------------------------------------------------
void Fluids2D::OnDisplay ()
{
    // Draw the density values.
    int imax = mSmoke->GetIMax();
    int jmax = mSmoke->GetJMax();
    float** density = mSmoke->GetDensity();
    for (int j = 0; j <= jmax; ++j)
    {
        for (int i = 0; i <= imax; ++i)
        {
            float value = density[j][i];
            if (value > 1.0f)
            {
                value = 1.0f;
            }

            ColorRGB color;
            if (mDrawColored)
            {
                color = mColor[(int)(255.0f*value)];
            }
            else
            {
                unsigned char gray = (unsigned char)(255.0f*value);
                color.r = gray;
                color.g = gray;
                color.b = gray;
            }

            SetPixel(2*i, 2*j, color);
            SetPixel(2*i+1, 2*j, color);
            SetPixel(2*i, 2*j+1, color);
            SetPixel(2*i+1, 2*j+1, color);
        }
    }

    if (mDrawVortices)
    {
        // Draw the vortex centers, magenta for counterclockwise and cyan for
        // clockwise.
        const int numActive = mSmoke->GetNumActiveVortices();
        float xMin = mSmoke->GetX0();
        float yMin = mSmoke->GetY0();
        float invXRange = 1.0f/(mSmoke->GetX1() - mSmoke->GetX0());
        float invYRange = 1.0f/(mSmoke->GetY1() - mSmoke->GetY0());
        for (int k = 0; k < numActive; ++k)
        {
            Vector2f center = mSmoke->GetVortexCenter(k);
            int x = (int)((GetWidth()-1)*(center.X() - xMin)*invXRange);
            int y = (int)((GetHeight()-1)*(center.Y() - yMin)*invYRange);

            ColorRGB color;
            if (mSmoke->GetVortexAmplitude(k) > 0.0f)
            {
                color.r = 255;
                color.g = 0;
                color.b = 255;
            }
            else
            {
                color.r = 0;
                color.g = 255;
                color.b = 255;
            }

            SetThickPixel(x, y, 1, color);
        }
    }

    WindowApplication2::OnDisplay();
}
//----------------------------------------------------------------------------
void Fluids2D::ScreenOverlay ()
{
    DrawFrameRate(8, GetHeight()-8, mTextColor);
}
//----------------------------------------------------------------------------
void Fluids2D::OnIdle ()
{
    MeasureTime();

    if (!mSingleStep)
    {
        mSmoke->DoSimulationStep();
        OnDisplay();
    }

    UpdateFrameCount();
}
//----------------------------------------------------------------------------
bool Fluids2D::OnKeyDown (unsigned char key, int x, int y)
{
    if (WindowApplication2::OnKeyDown(key, x, y))
    {
        return true;
    }

    switch (key)
    {
    case '0':
        mSmoke->Initialize();
        OnDisplay();
        return true;
    case ' ':  // simulate
        if (mSingleStep)
        {
            mSmoke->DoSimulationStep();
            OnDisplay();
        }
        return true;
    case 's':
    case 'S':
        mSingleStep = !mSingleStep;
        return true;
    case 'c':
    case 'C':
        mDrawColored = !mDrawColored;
        return true;
    case 'v':
    case 'V':
        mDrawVortices = !mDrawVortices;
        return true;
    case '+':
    case '=':
    {
        int numActive = mSmoke->GetNumActiveVortices();
        if (numActive < mSmoke->GetNumVortices())
        {
            mSmoke->SetNumActiveVortices(numActive + 1);
        }
        return true;
    }
    case '-':
    case '_':
    {
        int numActive = mSmoke->GetNumActiveVortices();
        if (numActive > 0)
        {
            mSmoke->SetNumActiveVortices(numActive - 1);
        }
        return true;
    }
    case 'g':
    {
        float gravity = mSmoke->GetGravity() - 0.1f;
        if (gravity < 0.0f)
        {
            gravity = 0.0f;
        }
        mSmoke->SetGravity(gravity);
        return true;
    }
    case 'G':
        mSmoke->SetGravity(mSmoke->GetGravity() + 0.1f);
        return true;
    }

    return false;
}
//----------------------------------------------------------------------------