29inline std::vector<float>
generateSdf(
int rx,
int ry,
int rz,
F3 dmin,
F3 dmax,
int numReal,
31 Kokkos::View<const ShapeDesc*, CpMem> shapes,
bool px,
33 float globalScale = 1.0f) {
39 const long total = (long)rx * ry * rz;
40 const F3 origin = dmin;
41 const F3 vox{(dmax.
x - dmin.
x) / rx, (dmax.
y - dmin.
y) / ry, (dmax.
z - dmin.
z) / rz};
43 Kokkos::View<float*, CpMem> grid(
"sdf_grid", total);
44 Kokkos::View<int*, CpMem> state(
"sdf_state", total);
45 Kokkos::deep_copy(grid, 100.0f);
46 Kokkos::deep_copy(state, 0);
50 "peclet::dem::sdf::splat", Kokkos::RangePolicy<CpExec>(space, 0, numReal),
51 KOKKOS_LAMBDA(
int i) {
52 const F3 pw =
loadF3(pos, i);
53 const F4 q =
loadF4(quat, i);
58 const float sc = scale(i) * globalScale;
59 const ShapeDesc shp = shapes(shapeId(i));
62 const float r = shp.params.x, h = shp.params.y;
63 rbound = Kokkos::sqrt(r * r + (h * 0.5f) * (h * 0.5f));
64 }
else if (shp.type ==
SPHERE)
65 rbound = shp.params.x;
67 rbound = shp.params.x;
71 int minx = (int)Kokkos::floor((pw.x - rbound - origin.x) / vox.x),
72 maxx = (int)Kokkos::ceil((pw.x + rbound - origin.x) / vox.x);
73 int miny = (int)Kokkos::floor((pw.y - rbound - origin.y) / vox.y),
74 maxy = (int)Kokkos::ceil((pw.y + rbound - origin.y) / vox.y);
75 int minz = (int)Kokkos::floor((pw.z - rbound - origin.z) / vox.z),
76 maxz = (int)Kokkos::ceil((pw.z + rbound - origin.z) / vox.z);
78 minx = minx < 0 ? 0 : minx;
79 maxx = maxx > rx - 1 ? rx - 1 : maxx;
82 miny = miny < 0 ? 0 : miny;
83 maxy = maxy > ry - 1 ? ry - 1 : maxy;
86 minz = minz < 0 ? 0 : minz;
87 maxz = maxz > rz - 1 ? rz - 1 : maxz;
90 for (
int z = minz; z <= maxz; ++z)
91 for (
int y = miny; y <= maxy; ++y)
92 for (
int x = minx; x <= maxx; ++x) {
93 const F3 vp{origin.x + (x + 0.5f) * vox.x, origin.y + (y + 0.5f) * vox.y,
94 origin.z + (z + 0.5f) * vox.z};
95 const F3 prel =
sub3(vp, pw);
100 else if (shp.type ==
SPHERE)
106 const float dist = distc * sc;
107 const int wx = (x % rx + rx) % rx, wy = (y % ry + ry) % ry, wz = (z % rz + rz) % rz;
108 if (!px && (x < 0 || x >= rx))
110 if (!py && (y < 0 || y >= ry))
112 if (!pz && (z < 0 || z >= rz))
114 const long vi = (long)wz * rx * ry + (
long)wy * rx + wx;
115 Kokkos::atomic_min(&grid(vi), dist);
123 Kokkos::View<float*, CpMem> grid2(
"sdf_grid2", total);
124 const int iters = Kokkos::max(Kokkos::max(rx, ry), rz) * 4;
125 const float h = vox.x;
126 Kokkos::View<float*, CpMem> in = grid, out = grid2;
127 for (
int it = 0; it < iters; ++it) {
128 Kokkos::View<float*, CpMem> din = in, dout = out;
129 Kokkos::View<int*, CpMem> st = state;
130 Kokkos::parallel_for(
131 "peclet::dem::sdf::eikonal", Kokkos::RangePolicy<CpExec>(space, 0, total),
132 KOKKOS_LAMBDA(
long idx) {
134 dout(idx) = din(idx);
137 const long sy = rx, sz = (long)rx * ry;
138 const int z = (int)(idx / sz);
139 const long rem = idx % sz;
140 const int y = (int)(rem / sy), x = (int)(rem % sy);
141 float xm = 100.0f, xp = 100.0f, ym = 100.0f, yp = 100.0f, zm = 100.0f, zp = 100.0f;
145 xm = din(idx - 1 + rx);
149 xp = din(idx + 1 - rx);
153 ym = din((
long)z * sz + (
long)(ry - 1) * sy + x);
157 yp = din((
long)z * sz + x);
161 zm = din((
long)(rz - 1) * sz + (
long)y * sy + x);
165 zp = din((
long)y * sy + x);
166 float a = Kokkos::fmin(xm, xp), b = Kokkos::fmin(ym, yp), c = Kokkos::fmin(zm, zp);
182 float u = 100.0f, u1 = a + h;
186 const float d2 = 2.0f * h * h - (a - b) * (a - b);
187 float u2 = (d2 < 0.0f) ? 100.0f : (a + b + Kokkos::sqrt(d2)) * 0.5f;
191 const float s = a + b + c, qd = s * s - 3.0f * (a * a + b * b + c * c - h * h);
192 u = (qd < 0.0f) ? 100.0f : (s + Kokkos::sqrt(qd)) / 3.0f;
203 std::vector<float> h_grid(total);
204 auto hv = Kokkos::create_mirror_view(in);
205 Kokkos::deep_copy(hv, in);
206 for (
long i = 0; i < total; ++i)
std::vector< float > generateSdf(int rx, int ry, int rz, F3 dmin, F3 dmax, int numReal, const BodyState &bodies, Kokkos::View< const ShapeDesc *, CpMem > shapes, bool px, bool py, bool pz, GridView sdfGrid=GridView{}, float globalScale=1.0f)
globalScale multiplies every per-particle scale, exactly as the narrow-phase does (narrowphase....
A body's state, one row per particle slot (Particles::bodyState()).