-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfluid.cpp
More file actions
151 lines (126 loc) · 3.58 KB
/
fluid.cpp
File metadata and controls
151 lines (126 loc) · 3.58 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
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
#include <stdio.h>
#define JGL2_IMPLEMENTATION
#include <JGL2/JGL.hpp>
#include <iostream>
#include <cmath>
#include <Eigen/Core>
#include <JGL2/JGL.hpp>
#include <JGL2/Simulation3DView.hpp>
#include <bits/stdc++.h>
using namespace JGL2;
using namespace jm;
ImageViewer* view = nullptr;
const int GRID_W = 256;
const int GRID_H = 256;
float* r = nullptr;
jm::vec3* u = nullptr;
bool pushed = false;
jm::vec2 cursorPt;
template<typename T>
T sample(T* r, float x, float y, int w, int h) {
int xi = int(floor(x));
int yi = int(floor(y));
float xt = x - xi;
float yt = y - yi;
int x1 = min(max(0, xi), w - 1);
int x2 = min(max(0, xi+1), w - 1);
int y1 = min(max(0, yi), h - 1);
int y2 = min(max(0, yi+1), h - 1);
return mix(mix(r[y1 * w + x1], r[y1 * w + x2], xt), mix(r[y2 * w + x1], r[y2 * w + x2], xt), yt);
}
template<typename T>
void advection(T* r, int w, int h, jm::vec3* u, float dt) {
T* tmp = new T[w * h];
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) {
tmp[y * w + x] = sample(r, x - u[y * w + x].x*dt, y - u[y * w + x].y*dt, w, h);
}
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++)
r[y * w + x] = tmp[y * w + x];
delete[]tmp;
}
template<typename T>
void diffuse(T* r, int w, int h, float k, float dt) {
float kdt = k * dt;
for (int iter = 0; iter < 5; iter++) {
for (int y = 1; y < h - 1; y++) for (int x = 1; x < w - 1; x++) {
int idx = y * GRID_W + x;
r[idx] = (r[idx] + kdt * (r[x + 1 + y * w] + r[x - 1 + y * w] + r[x + (y + 1) * w] + r[x + (y - 1) * w])) / (1 + 4 * kdt);
}
}
}
void init() {
if(r) delete r;
r = new float[GRID_W*GRID_H];
if (u) delete u;
u = new jm::vec3[GRID_W*GRID_H];
for( int i=0; i<GRID_W*GRID_H; i++) { r[i]=0; u[i]=jm::vec3(0); }
}
void conserveMass(vec3* u, int w, int h) {
float* p = new float[w * h];
for (int i = 0; i < w * h; i++) p[i] = 0;
for (int iter = 0; iter < 10; iter++) {
for (int y = 1; y < h - 1; y++) for (int x = 1; x < w - 1; x++) {
int i = x + y * w;
p[i] = ((u[i + 1].x - u[i - 1].x + u[i + w].y - u[i - w].y) - (p[i - 1] + p[i + 1] + p[i - w] + p[i + w])) * -0.25f;
}
}
for (int y = 1; y < h - 1; y++) for (int x = 1; x < w - 1; x++) {
int i = x + y * w;
u[i].xy -= vec2(p[i + 1] - p[i - 1], p[i + w] - p[i - w]) / 2.0f;
}
delete[] p;
}
void draw(const rct_t&) {
if( pushed && cursorPt.x>=0 && cursorPt.y>=0 && cursorPt.x<GRID_W && cursorPt.y<GRID_H)
r[int(cursorPt.x)+int(cursorPt.y)*GRID_W]+=10;
diffuse(r, GRID_W, GRID_H, 3, 0.01666);
advection(r, GRID_W, GRID_H, u, 0.0166);
diffuse(u, GRID_W, GRID_H, 10, 0.01666);
advection(u, GRID_W, GRID_H, u, 0.0166);
conserveMass(u, GRID_W, GRID_H);
view->setImage((float*)r, GRID_W, GRID_H, 1, false, false);
// view->redraw();
view->animate();
}
bool push(button_t btn) {
pushed = true;
return true;
}
bool release(button_t btn) {
pushed = false;
return true;
}
bool drag(const jm::vec2& pr) {
cursorPt = view->screenToImage(pr);
return true;
}
bool move(const jm::vec2& pr) {
auto pt = view->screenToImage(pr);
if( _JGL::eventMods(mod_t::SHIFT) ) {
auto d = pt - cursorPt;
if( cursorPt.x>=0 && cursorPt.y>=0 && cursorPt.x<GRID_W && cursorPt.y<GRID_H)
u[int(cursorPt.x)+int(cursorPt.y)*GRID_W]+=vec3(d*100, 0 );
}
cursorPt = pt;
return true;
}
bool key(int k) {
if (k == '0') {
init();
return true;
}
return false;
}
int main(int argc, const char * argv[]) {
Window* win = new Window(800,600,"IK");
view = new ImageViewer(0,0,800,600,"View");
view->keyCB(key);
view->drawGLCB(draw);
view->moveCB(move);
view->pushCB(push);
view->releaseCB(release);
view->dragCB(drag);
init();
win->show();
_JGL::run();
}