Repository navigation
Expand file tree
/
Copy pathFluid.h
More file actions
112 lines (94 loc) · 2.7 KB
/
Copy pathFluid.h
File metadata and controls
112 lines (94 loc) · 2.7 KB
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
#pragma once
#include <vector>
#include <tuple>
struct vec2
{
float x, y;
vec2(float x = 0.0, float y = 0.0) : x(x), y(y) {}
vec2 operator+(const vec2& other) const
{
return vec2(x + other.x, y + other.y);
}
vec2& operator+=(const vec2& other)
{
x += other.x;
y += other.y;
return *this;
}
vec2 operator-(const vec2& other) const
{
return vec2(x - other.x, y - other.y);
}
vec2 operator-=(const vec2& other)
{
x -= other.x;
y -= other.y;
return *this;
}
vec2 operator*(const float s) const
{
return vec2(x * s, y * s);
}
vec2 operator*(const int s) const
{
return vec2(x * s, y * s);
}
};
struct Particle
{
vec2 pos;
vec2 vel;
float radius;
// methods
Particle() : radius(0.005) {}
};
class Fluid
{
public:
Fluid(int size, int n_particles);
std::vector<int> simulate(float dt);
enum CellType { SOLID_CELL = 0, AIR_CELL = 1, FLUID_CELL = 2 };
private:
void genParticles();
void initBoundaries();
std::tuple<size_t, size_t> findCell(Particle p);
void clearGrid();
void particles2Grid();
void pushParticlesApart(int numIters);
void updateCellType();
void solveIncompressibility(float dt);
void grid2Particles();
void integrateParticles(float dt, vec2 gravity);
void handleParticleCollisions();
bool isUFaceValid(int i, int j) const;
bool isVFaceValid(int i, int j) const;
float velDivAtCell(int cellX, int cellY);
float sampleGrid(const std::vector<float>& grid, float gx, float gy,
int iMax, int jMax) const;
int idxX(int i, int j) const { return i + (size+1)*j; }
int idxY(int i, int j) const { return i + size*j; }
int idxC(int i, int j) const { return i + size*j; }
private:
int size;
float cellSize;
std::vector<float> s; //float, used in arithmetic during pressure solve
std::vector<int> cellType;
std::vector<Particle> particles;
std::vector<float> preVelocitiesX, preVelocitiesY;
std::vector<float> prevScatterVelocitiesX, prevScatterVelocitiesY;
std::vector<float> velocitiesX, velocitiesY;
std::vector<float> weightsX, weightsY;
std::vector<float> pressures;
std::vector<float> divergence;
//spatial hashing
float particleSpacing;
int pNumX, pNumY, pNumCells;
std::vector<int> numCellParticles;
std::vector<int> firstCellParticle;
std::vector<int> cellParticleIds;
static constexpr int numParticleIters = 2;
static constexpr int numPressureIters = 50;
static constexpr float overRelaxation = 1.9f;
static constexpr float density = 1000.0f;
float flipRatio = 0.9f;
};