28 Fun3v sdfGrad =
nullptr);
40 auto const Eps = 1e-6;
41 Vec3 epsX{Eps, 0, 0}, epsY{0, Eps, 0}, epsZ{0, 0, Eps};
42 auto gx = (f(p + epsX) - f(p - epsX)) / 2;
43 auto gy = (f(p + epsY) - f(p - epsY)) / 2;
44 auto gz = (f(p + epsZ) - f(p - epsZ)) / 2;
51 auto const NumCubes = 50;
52 auto cubeSize = domain.
size * (1.0 / NumCubes);
65 sdfGrad = sdfGrad ==
nullptr
66 ? [&sdf](
Vec3 const &p)
67 {
return NumGrad(sdf, p); }
70 auto const NumX =
static_cast<int>(ceil(domain.
size.
x / cubeSize.
x));
71 auto const NumY =
static_cast<int>(ceil(domain.
size.
y / cubeSize.
y));
72 auto const NumZ =
static_cast<int>(ceil(domain.
size.
z / cubeSize.
z));
74 auto const HalfCubeDiag = cubeSize.
Norm() / 2.0;
75 auto const HalfCubeSize = cubeSize * 0.5;
79 for (
auto ix = 0; ix < NumX; ++ix)
81 auto x = domain.
min.
x + ix * cubeSize.
x;
83 for (
auto iy = 0; iy < NumY; ++iy)
85 auto y = domain.
min.
y + iy * cubeSize.
y;
87 for (
auto iz = 0; iz < NumZ; ++iz)
89 auto z = domain.
min.
z + iz * cubeSize.
z;
93 auto cubeCenter =
min + HalfCubeSize;
94 auto dist = abs(sdf(cubeCenter) - isoLevel);
95 if (dist > HalfCubeDiag)
98 Cube cube({
min, cubeSize}, sdf);
99 auto intersect = cube.
Intersect(isoLevel);
double min(double v1, double v2)
IntersectInfo Intersect(double isoLevel=0) const
void Triangulate(IntersectInfo const &intersect, Fun3v const &grad, Mesh &mesh)
Given a grid cube and an isolevel the triangles (5 max) required to represent the isosurface in the c...
Mesh MarchCube(Fun3s const &sdf, Rect3 const &domain)
Reconstructs a triangle mesh from a given signed distance function using Marching Cubes.
std::function< Vec3(Vec3 const &)> Fun3v
std::function< double(Vec3 const &)> Fun3s