-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmain.cpp
More file actions
386 lines (321 loc) · 12.1 KB
/
Copy pathmain.cpp
File metadata and controls
386 lines (321 loc) · 12.1 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// This code is based on Jet framework.
// Copyright (c) 2018 Doyub Kim
// CubbyFlow is voxel-based fluid simulation engine for computer games.
// Copyright (c) 2020 CubbyFlow Team
// Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
// AI Part: Dongheon Cho, Minseo Kim
// We are making my contributions/submissions to this project solely in our
// personal capacity and are not conveying any rights to any intellectual
// property of any third parties.
#include <../ClaraUtils.hpp>
#include <Core/Array/Array.hpp>
#include <Core/Emitter/VolumeGridEmitter3.hpp>
#include <Core/Geometry/Box.hpp>
#include <Core/Geometry/Cylinder3.hpp>
#include <Core/Geometry/ImplicitSurfaceSet.hpp>
#include <Core/Geometry/ImplicitTriangleMesh3.hpp>
#include <Core/Geometry/MarchingCubes.hpp>
#include <Core/Geometry/Plane.hpp>
#include <Core/Geometry/RigidBodyCollider.hpp>
#include <Core/Geometry/Sphere.hpp>
#include <Core/Geometry/TriangleMesh3.hpp>
#include <Core/Grid/ScalarGrid.hpp>
#include <Core/Solver/LevelSet/LevelSetLiquidSolver3.hpp>
#include <Core/Utils/Logging.hpp>
#include <pystring/pystring.h>
#include <clara.hpp>
#ifdef CUBBYFLOW_WINDOWS
#include <direct.h>
#else
#include <sys/stat.h>
#endif
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#define APP_NAME "LevelSetLiquidSim"
using namespace CubbyFlow;
void SaveTriangleMesh(const TriangleMesh3& mesh, const std::string& rootDir,
int frameCnt)
{
char baseName[256];
snprintf(baseName, sizeof(baseName), "frame_%06d.obj", frameCnt);
std::string fileName = pystring::os::path::join(rootDir, baseName);
std::ofstream file(fileName.c_str());
if (file)
{
printf("Writing %s...\n", fileName.c_str());
mesh.WriteObj(&file);
file.close();
}
}
void TriangulateAndSave(const ScalarGrid3Ptr& sdf, const std::string& rootDir,
int frameCnt)
{
TriangleMesh3 mesh;
const int flag = DIRECTION_ALL & ~DIRECTION_DOWN;
MarchingCubes(sdf->DataView(), sdf->GridSpacing(), sdf->DataOrigin(),
&mesh, 0.0, flag);
SaveTriangleMesh(mesh, rootDir, frameCnt);
}
void PrintInfo(const LevelSetLiquidSolver3Ptr& solver)
{
const auto grids = solver->GetGridSystemData();
const Vector3UZ resolution = grids->Resolution();
const BoundingBox3D domain = grids->GetBoundingBox();
const Vector3D gridSpacing = grids->GridSpacing();
printf("Resolution: %zu x %zu x %zu\n", resolution.x, resolution.y,
resolution.z);
printf("Domain: [%f, %f, %f] x [%f, %f, %f]\n", domain.lowerCorner.x,
domain.lowerCorner.y, domain.lowerCorner.z, domain.upperCorner.x,
domain.upperCorner.y, domain.upperCorner.z);
printf("Grid spacing: [%f, %f, %f]\n", gridSpacing.x, gridSpacing.y,
gridSpacing.z);
}
void RunSimulation(const std::string& rootDir,
const LevelSetLiquidSolver3Ptr& solver, int numberOfFrames,
double fps)
{
const auto sdf = solver->GetSignedDistanceField();
for (Frame frame(0, 1.0 / fps); frame.index < numberOfFrames; ++frame)
{
solver->Update(frame);
TriangulateAndSave(sdf, rootDir, frame.index);
}
}
// Water-drop example
void RunExample1(const std::string& rootDir, size_t resX, int numberOfFrames,
double fps)
{
// Build solver
auto solver = LevelSetLiquidSolver3::Builder()
.WithResolution({ resX, 2 * resX, resX })
.WithDomainSizeX(1.0)
.MakeShared();
const auto grids = solver->GetGridSystemData();
BoundingBox3D domain = grids->GetBoundingBox();
// Build emitter
const auto plane = Plane3::Builder()
.WithNormal({ 0, 1, 0 })
.WithPoint({ 0, 0.25 * domain.Height(), 0 })
.MakeShared();
const auto sphere = Sphere3::Builder()
.WithCenter(domain.MidPoint())
.WithRadius(0.15 * domain.Width())
.MakeShared();
const auto surfaceSet =
ImplicitSurfaceSet3::Builder()
.WithExplicitSurfaces(Array1<Surface3Ptr>{ plane, sphere })
.MakeShared();
auto emitter =
VolumeGridEmitter3::Builder().WithSourceRegion(surfaceSet).MakeShared();
solver->SetEmitter(emitter);
emitter->AddSignedDistanceTarget(solver->GetSignedDistanceField());
// Print simulation info
printf("Running example 1 (water-drop)\n");
PrintInfo(solver);
// Run simulation
RunSimulation(rootDir, solver, numberOfFrames, fps);
}
// Dam-breaking example
void RunExample2(const std::string& rootDir, size_t resX, int numberOfFrames,
double fps)
{
// Build solver
auto solver = LevelSetLiquidSolver3::Builder()
.WithResolution({ 3 * resX, 2 * resX, (3 * resX) / 2 })
.WithDomainSizeX(3.0)
.MakeShared();
solver->SetUseCompressedLinearSystem(true);
const auto grids = solver->GetGridSystemData();
BoundingBox3D domain = grids->GetBoundingBox();
const double lz = domain.Depth();
// Build emitter
const auto box1 = Box3::Builder()
.WithLowerCorner({ -0.5, -0.5, -0.5 * lz })
.WithUpperCorner({ 0.5, 0.75, 0.75 * lz })
.MakeShared();
const auto box2 = Box3::Builder()
.WithLowerCorner({ 2.5, -0.5, 0.25 * lz })
.WithUpperCorner({ 3.5, 0.75, 1.5 * lz })
.MakeShared();
const auto boxSet =
ImplicitSurfaceSet3::Builder()
.WithExplicitSurfaces(Array1<Surface3Ptr>{ box1, box2 })
.MakeShared();
auto emitter =
VolumeGridEmitter3::Builder().WithSourceRegion(boxSet).MakeShared();
solver->SetEmitter(emitter);
emitter->AddSignedDistanceTarget(solver->GetSignedDistanceField());
// Build collider
const auto cyl1 = Cylinder3::Builder()
.WithCenter({ 1, 0.375, 0.375 })
.WithRadius(0.1)
.WithHeight(0.75)
.MakeShared();
const auto cyl2 = Cylinder3::Builder()
.WithCenter({ 1.5, 0.375, 0.75 })
.WithRadius(0.1)
.WithHeight(0.75)
.MakeShared();
const auto cyl3 = Cylinder3::Builder()
.WithCenter({ 2, 0.375, 1.125 })
.WithRadius(0.1)
.WithHeight(0.75)
.MakeShared();
const auto cylSet =
ImplicitSurfaceSet3::Builder()
.WithExplicitSurfaces(Array1<Surface3Ptr>{ cyl1, cyl2, cyl3 })
.MakeShared();
const auto collider =
RigidBodyCollider3::Builder().WithSurface(cylSet).MakeShared();
solver->SetCollider(collider);
// Print simulation info
printf("Running example 2 (dam-breaking)\n");
PrintInfo(solver);
// Run simulation
RunSimulation(rootDir, solver, numberOfFrames, fps);
}
// High-viscosity example (bunny-drop)
void RunExample3(const std::string& rootDir, size_t resX, int numberOfFrames,
double fps)
{
// Build solver
auto solver = LevelSetLiquidSolver3::Builder()
.WithResolution({ resX, resX, resX })
.WithDomainSizeX(1.0)
.MakeShared();
solver->SetUseCompressedLinearSystem(true);
solver->SetViscosityCoefficient(1.0);
solver->SetIsGlobalCompensationEnabled(true);
auto grids = solver->GetGridSystemData();
// Build emitters
auto bunnyMesh = TriangleMesh3::Builder().MakeShared();
std::ifstream objFile(RESOURCES_DIR "/bunny.obj");
if (objFile)
{
[[maybe_unused]] bool isLoaded = bunnyMesh->ReadObj(&objFile);
}
else
{
fprintf(stderr, "Cannot open Resources/bunny.obj\n");
exit(EXIT_FAILURE);
}
const auto bunny = ImplicitTriangleMesh3::Builder()
.WithTriangleMesh(bunnyMesh)
.WithResolutionX(resX)
.MakeShared();
auto emitter =
VolumeGridEmitter3::Builder().WithSourceRegion(bunny).MakeShared();
solver->SetEmitter(emitter);
emitter->AddSignedDistanceTarget(solver->GetSignedDistanceField());
// Print simulation info
printf("Running example 3 (high-viscosity)\n");
PrintInfo(solver);
// Run simulation
RunSimulation(rootDir, solver, numberOfFrames, fps);
}
// Low-viscosity example (bunny-drop)
void RunExample4(const std::string& rootDir, size_t resX, int numberOfFrames,
double fps)
{
// Build solver
auto solver = LevelSetLiquidSolver3::Builder()
.WithResolution({ resX, resX, resX })
.WithDomainSizeX(1.0)
.MakeShared();
solver->SetUseCompressedLinearSystem(true);
solver->SetViscosityCoefficient(0.0);
solver->SetIsGlobalCompensationEnabled(true);
auto grids = solver->GetGridSystemData();
// Build emitters
auto bunnyMesh = TriangleMesh3::Builder().MakeShared();
std::ifstream objFile(RESOURCES_DIR "/bunny.obj");
if (objFile)
{
[[maybe_unused]] bool isLoaded = bunnyMesh->ReadObj(&objFile);
}
else
{
fprintf(stderr, "Cannot open Resources/bunny.obj\n");
exit(EXIT_FAILURE);
}
const auto bunny = ImplicitTriangleMesh3::Builder()
.WithTriangleMesh(bunnyMesh)
.WithResolutionX(resX)
.MakeShared();
auto emitter =
VolumeGridEmitter3::Builder().WithSourceRegion(bunny).MakeShared();
solver->SetEmitter(emitter);
emitter->AddSignedDistanceTarget(solver->GetSignedDistanceField());
// Print simulation info
printf("Running example 4 (low-viscosity)\n");
PrintInfo(solver);
// Run simulation
RunSimulation(rootDir, solver, numberOfFrames, fps);
}
int main(int argc, char* argv[])
{
bool showHelp = false;
size_t resX = 50;
int numberOfFrames = 100;
double fps = 60.0;
int exampleNum = 1;
std::string logFileName = APP_NAME ".log";
std::string outputDir = APP_NAME "_output";
// Parsing
auto parser =
clara::Help(showHelp) |
clara::Opt(resX, "resX")["-r"]["--resx"](
"grid resolution in x-axis (default is 50)") |
clara::Opt(numberOfFrames, "numberOfFrames")["-f"]["--frames"](
"total number of frames (default is 100)") |
clara::Opt(
fps, "fps")["-p"]["--fps"]("frames per second (default is 60.0)") |
clara::Opt(exampleNum, "exampleNum")["-e"]["--example"](
"example number (between 1 and 4, default is 1)") |
clara::Opt(logFileName, "logFileName")["-l"]["--log"](
"log file name (default is " APP_NAME ".log)") |
clara::Opt(outputDir, "outputDir")["-o"]["--output"](
"output directory name (default is " APP_NAME "_output)");
auto result = parser.parse(clara::Args(argc, argv));
if (!result)
{
std::cerr << "Error in command line: " << result.errorMessage() << '\n';
exit(EXIT_FAILURE);
}
if (showHelp)
{
std::cout << ToString(parser) << '\n';
exit(EXIT_SUCCESS);
}
#ifdef CUBBYFLOW_WINDOWS
_mkdir(outputDir.c_str());
#else
mkdir(outputDir.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
std::ofstream logFile(logFileName.c_str());
if (logFile)
{
Logging::SetAllStream(&logFile);
}
switch (exampleNum)
{
case 1:
RunExample1(outputDir, resX, numberOfFrames, fps);
break;
case 2:
RunExample2(outputDir, resX, numberOfFrames, fps);
break;
case 3:
RunExample3(outputDir, resX, numberOfFrames, fps);
break;
case 4:
RunExample4(outputDir, resX, numberOfFrames, fps);
break;
default:
std::cout << ToString(parser) << '\n';
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}