-
Notifications
You must be signed in to change notification settings - Fork 1
/
MetaBall.cpp
111 lines (101 loc) · 2.19 KB
/
MetaBall.cpp
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
#include "MetaBall.h"
#include <numeric>
#include <execution>
#include "MyModel.h"
#include <Eigen/src/Core/util/ForwardDeclarations.h>
#include "GLUtils.h"
double MetaBall::getIsoLevel(Vector3d pos)
{
return std::transform_reduce(
std::execution::par_unseq,
spheres.begin(),
spheres.end(),
0.0,
[](const double a, const double b) {return a + b;},
[&](const Sphere& sphere)
{
return pow(sphere.r, 3) / (pow(pos.x() - sphere.p.x(), 2) + pow(pos.y() - sphere.p.y(), 2) + pow(pos.z() - sphere.p.z(), 2));
}
);
}
void MetaBall::initializeGrids()
{
const auto gs = getGridSize();
const auto xSteps = unsigned int((domainXMax - domainXMin) / gs);
const auto ySteps = unsigned int((domainYMax - domainYMin) / gs);
const auto zSteps = unsigned int((domainZMax - domainZMin) / gs);
if (grids.size() == xSteps * ySteps * zSteps) return;
grids.clear();
for (auto zi = 0u; zi < zSteps; zi++)
{
auto z = domainZMin + zi * gs;
for (auto yi = 0u; yi < ySteps; yi++)
{
auto y = domainYMin + yi * gs;
for (auto xi = 0u; xi < xSteps; xi++)
{
auto x = domainXMin + xi * gs;
grids.push_back(MarchingCubes::Grid{
{
Vector3d(x, y, z + gs),
Vector3d(x + gs, y, z + gs),
Vector3d(x + gs, y, z),
Vector3d(x, y, z),
Vector3d(x, y + gs, z + gs),
Vector3d(x + gs, y + gs, z + gs),
Vector3d(x + gs, y + gs, z),
Vector3d(x, y + gs, z)
},
{}
});
}
}
}
}
void MetaBall::drawMetaBalls()
{
initializeGrids();
std::for_each(
std::execution::seq,
grids.begin(),
grids.end(),
[&](MarchingCubes::Grid& grid)
{
for (auto i = 0u; i < 8; i++)
{
grid.isoLevels[i] = getIsoLevel(grid.cornerPositions[i]);
}
MarchingCubes::polygonizeDraw(grid, threshold);
}
);
}
double MetaBall::getGridSize()
{
double gridSize;
switch (ModelerDrawState::Instance()->m_quality)
{
case HIGH:
gridSize = 0.25;
break;
case MEDIUM:
default:
gridSize = 0.5;
break;
case LOW:
gridSize = 1;
break;
case POOR:
gridSize = 2;
break;
}
return gridSize;
}
MetaBall* MetaBall::getInstancePtr()
{
if (instancePtr == nullptr)
{
instancePtr = new MetaBall();
instancePtr->initializeGrids();
}
return instancePtr;
}