core 1.5.0
Shared MPI block decomposition, asynchronous ghost-layer exchange, SDF geometry, load balancing (header-only C++20)
Loading...
Searching...
No Matches
grid_sdf.hpp
Go to the documentation of this file.
1
9#ifndef PECLET_CORE_GEOM_GRID_SDF_HPP
10#define PECLET_CORE_GEOM_GRID_SDF_HPP
11
12#include <algorithm>
13#include <vector>
14
17
18namespace peclet::core::geom {
19
24struct GridSdf {
25 std::vector<float> values;
28 Vec<3> spacing{1, 1, 1};
29
31 double at(Index i, Index j, Index k) const {
32 return static_cast<double>(values[i + dims[0] * (j + dims[1] * k)]);
33 }
34
40 d.nx = static_cast<int>(dims[0]);
41 d.ny = static_cast<int>(dims[1]);
42 d.nz = static_cast<int>(dims[2]);
43 d.offset = 0;
44 d.origin = Vec3<double>{origin[0], origin[1], origin[2]};
45 d.invSpacing = Vec3<double>{spacing[0] != 0.0 ? 1.0 / spacing[0] : 0.0,
46 spacing[1] != 0.0 ? 1.0 / spacing[1] : 0.0,
47 spacing[2] != 0.0 ? 1.0 / spacing[2] : 0.0};
48 d.extension = GridExtension::kClamp; // flat at the box face -- this type's documented contract
49 return d;
50 }
51
60 double eval(const Vec<3>& p) const {
61 return sampleGrid(Vec3<double>{p[0], p[1], p[2]}, desc(), PoolPtr<float>{values.data()});
62 }
63};
64
66template <typename S>
67GridSdf sample(const S& shape, IVec<3> dims, Vec<3> origin, Vec<3> spacing) {
68 GridSdf g;
69 g.dims = dims;
70 g.origin = origin;
71 g.spacing = spacing;
72 g.values.resize(static_cast<std::size_t>(dims[0]) * dims[1] * dims[2]);
73 for (Index k = 0; k < dims[2]; ++k)
74 for (Index j = 0; j < dims[1]; ++j)
75 for (Index i = 0; i < dims[0]; ++i) {
76 Vec<3> p{origin[0] + i * spacing[0], origin[1] + j * spacing[1],
77 origin[2] + k * spacing[2]};
78 g.values[i + dims[0] * (j + dims[1] * k)] = static_cast<float>(shape.eval(p));
79 }
80 return g;
81}
82
83} // namespace peclet::core::geom
84
85#endif // PECLET_CORE_GEOM_GRID_SDF_HPP
GridSdf sample(const S &shape, IVec< 3 > dims, Vec< 3 > origin, Vec< 3 > spacing)
Sample any analytic SDF onto a grid, producing a GridSdf (e.g. to bake geometry for a solver).
Definition grid_sdf.hpp:67
PECLET_HD Real sampleGrid(Vec3< Real > p, const GridDesc< Real > &d, const Pool &pool)
Trilinear sample of a grid field, with the clamp-plus-residual off-grid extension selected by d....
Definition scene.hpp:127
Kokkos::View< T *, MemSpace > View
1D device array.
Definition view.hpp:26
std::int64_t Index
Signed index type for grids and particles (supersedes block_decomposer's long int IndxT).
Definition types.hpp:15
A signed-distance field sampled on a regular axis-aligned grid (negative inside solid).
Definition grid_sdf.hpp:24
double at(Index i, Index j, Index k) const
Raw sample lookup at integer grid index (i,j,k); no bounds checking.
Definition grid_sdf.hpp:31
Vec< 3 > origin
World position of sample (0,0,0).
Definition grid_sdf.hpp:27
std::vector< float > values
Sample values, x-fastest: idx = i + j*nx + k*nx*ny.
Definition grid_sdf.hpp:25
GridDesc< double > desc() const
Descriptor form of this field, for the shared sampler.
Definition grid_sdf.hpp:38
Vec< 3 > spacing
World-space distance between samples per axis.
Definition grid_sdf.hpp:28
double eval(const Vec< 3 > &p) const
Trilinearly-interpolated signed distance at world point p.
Definition grid_sdf.hpp:60
IVec< 3 > dims
Sample count per axis (nx, ny, nz).
Definition grid_sdf.hpp:26