EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Domain.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Robert Marskar <robert.marskar@sintef.no>
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
11#ifndef EBGEOMETRY_DOMAIN_HPP
12#define EBGEOMETRY_DOMAIN_HPP
13
14// Std includes
15#include <cstddef>
16#include <cstdint>
17#include <type_traits>
18
19// Our includes
21#include "EBGeometry_GPU.hpp"
22#include "EBGeometry_Macros.hpp"
23#include "EBGeometry_Math.hpp"
24#include "EBGeometry_Pool.hpp"
28#include "EBGeometry_Tape.hpp"
29
30namespace EBGeometry {
31
61template <class T, class... UserPrimitives>
62class Domain : public PoolHandle
63{
64public:
68 using TapeType = Tape<T, UserPrimitives...>;
69
73 static constexpr uint32_t Excluded = Subdomains::None;
74
84 Domain(Pool& a_tables, const TapeType& a_tape, const DomainSetup& a_setup) : m_tape(a_tape)
85 {
86 this->attach(a_tables, "Domain");
87
88 m_subdomains = a_setup.buildSubdomains(a_tables, a_tape.getNumRegions()).layout();
89 m_surfaceGroups = a_setup.buildSurfaceGroups(a_tables).layout();
90 }
91
96 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
97 inline const TapeType&
98 tape() const noexcept
99 {
100 return m_tape;
101 }
102
107 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
108 inline Subdomains
109 subdomains() const noexcept
110 {
111 return Subdomains(m_subdomains, this->location());
112 }
113
118 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
119 inline SurfaceGroups
120 surfaceGroups() const noexcept
121 {
122 return SurfaceGroups(m_surfaceGroups, this->location());
123 }
124
129 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
130 inline uint32_t
132 {
133 return m_subdomains.m_numSubdomains + 1U;
134 }
135
141 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
142 inline bool
143 computesIn(const uint32_t a_region) const noexcept
144 {
145 return this->subdomains().subdomainOf(a_region) != Subdomains::None;
146 }
147
154 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
155 inline bool
156 touches(const uint32_t a_first, const uint32_t a_second) const noexcept
157 {
158 return this->computesIn(a_first) || this->computesIn(a_second);
159 }
160
166 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
167 inline uint32_t
168 subdomainAt(const Vec3T<T>& a_point) const noexcept
169 {
170 return this->subdomains().subdomainOf(m_tape.region(a_point));
171 }
172
180 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
181 inline T
182 signedDistance(const Vec3T<T>& a_point, const uint32_t a_subdomain) const noexcept
183 {
184 return m_tape.signedDistance(a_point, this->subdomains(), a_subdomain);
185 }
186
197 inline void
198 signedDistances(const Vec3T<T>& a_point, T* a_values, const uint32_t a_capacity) const noexcept
199 {
200 const uint32_t excluded = m_subdomains.m_numSubdomains;
201
202#if defined(EBGEOMETRY_DEVICE_COMPILE)
203 EBGEOMETRY_EXPECT(a_capacity > excluded);
204#else
205 EBGEOMETRY_REQUIRE(a_capacity > excluded,
206 "Domain::signedDistances: room for %u values, the domain has %u functions",
207 unsigned(a_capacity),
208 unsigned(excluded + 1U));
209#endif
210
211 const Subdomains subdomains = this->subdomains();
212
213 // Each subdomain's smallest region function, the excluded regions' last.
214 for (uint32_t l = 0; l <= excluded; l++) {
215 a_values[l] = Math::Limits<T>::max();
216 }
217
218 m_tape.regionDistances(a_point, [&](const uint32_t a_region, const T a_value) {
219 const uint32_t subdomain = subdomains.subdomainOf(a_region);
220 const uint32_t slot = (subdomain == Subdomains::None) ? excluded : subdomain;
221
222 a_values[slot] = Math::min(a_values[slot], a_value);
223 });
224
225 // A function's other term is the smallest of the others': the smallest of all, or the second smallest for the one
226 // that has it.
227 T first = Math::Limits<T>::max();
228 T second = Math::Limits<T>::max();
229 uint32_t smallest = 0U;
230
231 for (uint32_t l = 0; l <= excluded; l++) {
232 if (a_values[l] < first) {
233 second = first;
234 first = a_values[l];
235 smallest = l;
236 }
237 else {
238 second = Math::min(second, a_values[l]);
239 }
240 }
241
242 for (uint32_t l = 0; l <= excluded; l++) {
243 a_values[l] = T(0.5) * (a_values[l] - ((l == smallest) ? second : first));
244 }
245 }
246
254 template <std::size_t N>
256 inline void
257 signedDistances(const Vec3T<T>& a_point, T (&a_values)[N]) const noexcept
258 {
259 this->signedDistances(a_point, a_values, static_cast<uint32_t>(N));
260 }
261
269 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
270 inline SurfaceRef<T>
271 closestSurface(const Vec3T<T>& a_point, const uint32_t a_subdomain) const noexcept
272 {
273 return m_tape.closestSurface(a_point, this->subdomains(), a_subdomain);
274 }
275
281 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
282 inline SurfaceRole
283 role(const SurfaceRef<T>& a_surface) const noexcept
284 {
285 return this->subdomains().role(a_surface);
286 }
287
293 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
294 inline uint32_t
295 surfaceGroupOf(const SurfaceRef<T>& a_surface) const noexcept
296 {
297 return this->surfaceGroups().surfaceGroupOf(a_surface);
298 }
299
305 [[nodiscard]] EBGEOMETRY_HOST
306 inline Domain
307 rebasedView(const Pool& a_pool) const
308 {
309 return this->rebasedView(a_pool, a_pool);
310 }
311
318 [[nodiscard]] EBGEOMETRY_HOST
319 inline Domain
320 rebasedView(const Pool& a_tapePool, const Pool& a_tablesPool) const
321 {
322 Domain view = *this;
323
324 view.m_tape = m_tape.rebasedView(a_tapePool);
325
326 const uint64_t endByte = Math::max(m_subdomains.m_table.endByte(), m_surfaceGroups.m_groupOf.endByte());
327
328 view.setLocation(this->rebasedLocation(a_tablesPool, endByte, "Domain"));
329
330 return view;
331 }
332
333private:
337 TapeType m_tape;
338
342 Subdomains::Layout m_subdomains;
343
347 SurfaceGroups::Layout m_surfaceGroups;
348};
349
353template <class T, class... UserPrimitives>
354Domain(Pool&, const Tape<T, UserPrimitives...>&, const DomainSetup&) -> Domain<T, UserPrimitives...>;
355
356} // namespace EBGeometry
357
358#endif
DomainSetup: how a domain is made, as an editable value on the host (see Domain).
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_HOST_DEVICE
Mark a function as callable from both host and device code (no-op on host compilers).
Definition EBGeometry_GPU.hpp:80
#define EBGEOMETRY_HOST
Mark a function as callable from host code only (no-op on host compilers).
Definition EBGeometry_GPU.hpp:83
Utility macros for EBGeometry.
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:200
#define EBGEOMETRY_EXPECT(cond)
Runtime precondition assertion for EBGeometry.
Definition EBGeometry_Macros.hpp:269
Scalar helpers and numeric limits that compile for both host and device.
Growable bump arena for the GPU memory foundation.
Subdomains: the subdomain of each region of a tape, read on the host or a device.
SurfaceSelector and SurfaceGroups: which surfaces a downstream code groups under one meaning.
SurfaceRef: which surface of a shape decides its value at a point, and the container protocol that na...
Declaration of Tape, a composed object (an Expr) compiled into a pool, and its evaluator.
How a domain is made: its subdomains (the regions each computes in) and its surface groups (the selec...
Definition EBGeometry_DomainSetup.hpp:39
SurfaceGroups buildSurfaceGroups(Pool &a_pool) const
Build the surface groups.
Definition EBGeometry_DomainSetup.hpp:371
Subdomains buildSubdomains(Pool &a_pool, const uint32_t a_numRegions) const
Build the subdomains for a tape of a number of regions.
Definition EBGeometry_DomainSetup.hpp:350
A computational domain over a tape, for a downstream code (a solver EBGeometry does not provide): the...
Definition EBGeometry_Domain.hpp:63
uint32_t subdomainAt(const Vec3T< T > &a_point) const noexcept
The subdomain a point is in: that of the region it is in, or Excluded.
Definition EBGeometry_Domain.hpp:168
T signedDistance(const Vec3T< T > &a_point, const uint32_t a_subdomain) const noexcept
A subdomain's function (Tape::signedDistance(p, subdomains, subdomain)), or Excluded's,...
Definition EBGeometry_Domain.hpp:182
Domain rebasedView(const Pool &a_tapePool, const Pool &a_tablesPool) const
A view resolving against mirrors, the tape and the tables in two pools.
Definition EBGeometry_Domain.hpp:320
Tape< T, UserPrimitives... > TapeType
The tape it is built for.
Definition EBGeometry_Domain.hpp:68
Subdomains subdomains() const noexcept
The subdomains, for the tape's own queries that take them (Tape::firstSurface(ray,...
Definition EBGeometry_Domain.hpp:109
bool computesIn(const uint32_t a_region) const noexcept
Whether it computes in a region.
Definition EBGeometry_Domain.hpp:143
Domain rebasedView(const Pool &a_pool) const
A view resolving against mirrors, the tape and the tables in one pool.
Definition EBGeometry_Domain.hpp:307
uint32_t surfaceGroupOf(const SurfaceRef< T > &a_surface) const noexcept
A surface's group (SurfaceGroups::surfaceGroupOf()).
Definition EBGeometry_Domain.hpp:295
void signedDistances(const Vec3T< T > &a_point, T *a_values, const uint32_t a_capacity) const noexcept
Every subdomain's function at a point, and Excluded's, each region's function evaluated once: what a ...
Definition EBGeometry_Domain.hpp:198
uint32_t getNumSubdomainFunctions() const noexcept
How many functions signedDistances() gives: one per subdomain, then Excluded's.
Definition EBGeometry_Domain.hpp:131
SurfaceRef< T > closestSurface(const Vec3T< T > &a_point, const uint32_t a_subdomain) const noexcept
The surface of a subdomain closest to a point, with what is on its other side (Tape::closestSurface(p...
Definition EBGeometry_Domain.hpp:271
static constexpr uint32_t Excluded
What subdomainAt() gives for a point in the regions of no subdomain.
Definition EBGeometry_Domain.hpp:73
Domain(Pool &a_tables, const TapeType &a_tape, const DomainSetup &a_setup)
Build a domain over a tape.
Definition EBGeometry_Domain.hpp:84
bool touches(const uint32_t a_first, const uint32_t a_second) const noexcept
Whether a surface between two regions is one it must give a condition: one side or both in its region...
Definition EBGeometry_Domain.hpp:156
SurfaceGroups surfaceGroups() const noexcept
The surface groups.
Definition EBGeometry_Domain.hpp:120
const TapeType & tape() const noexcept
The tape the domain was built for.
Definition EBGeometry_Domain.hpp:98
SurfaceRole role(const SurfaceRef< T > &a_surface) const noexcept
What a surface is to the domain (Subdomains::role()).
Definition EBGeometry_Domain.hpp:283
void signedDistances(const Vec3T< T > &a_point, T(&a_values)[N]) const noexcept
Every subdomain's function at a point, and Excluded's, into an array whose size is the capacity (see ...
Definition EBGeometry_Domain.hpp:257
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:563
PoolLocation location() const noexcept
Where this handle's arrays live.
Definition EBGeometry_Pool.hpp:571
PoolLocation rebasedLocation(const Pool &a_pool, uint64_t a_endByte, const char *a_who) const noexcept
The location of this handle's arrays in another pool of its mirror chain; see PoolLocation::rebasedOn...
Definition EBGeometry_Pool.hpp:680
void attach(const Pool &a_pool, const char *a_who, const bool a_deviceWritten=false) noexcept
Follow a_pool, as the first reservation does; see PoolLocation::attach().
Definition EBGeometry_Pool.hpp:665
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
The subdomains of a domain (see Domain), read on the host or a device: the subdomain of each region o...
Definition EBGeometry_Subdomains.hpp:51
static constexpr uint32_t None
No subdomain, or a region in no subdomain.
Definition EBGeometry_Subdomains.hpp:56
const Layout & layout() const noexcept
The arrays, as offsets.
Definition EBGeometry_Subdomains.hpp:121
uint32_t subdomainOf(const uint32_t a_region) const noexcept
The subdomain a region is in.
Definition EBGeometry_Subdomains.hpp:155
SurfaceRole role(const SurfaceRef< T > &a_surface) const noexcept
What a surface is to the subdomains at a point: from the regions on its two sides there,...
Definition EBGeometry_Subdomains.hpp:170
A domain's surface groups, read on the host or a device: each group a set of surfaces,...
Definition EBGeometry_SurfaceGroups.hpp:296
const Layout & layout() const noexcept
The arrays, as offsets.
Definition EBGeometry_SurfaceGroups.hpp:385
uint32_t surfaceGroupOf(const SurfaceRef< T > &a_surface) const noexcept
The surface group a surface point is in: that of the first selector it matches, those of the remainin...
Definition EBGeometry_SurfaceGroups.hpp:447
SurfaceRef< T > closestSurface(const Vec3T< T > &a_point, uint32_t a_region) const noexcept
The surface of a region that decides its function at a point: the region's own boundary,...
uint32_t region(const Vec3T< T > &a_point) const noexcept
The region a point is in: the last operand of the Overlays whose solid holds it (later operands are o...
void regionDistances(const Vec3T< T > &a_point, Visit &&a_visit) const noexcept
Every region's function at a point (signedDistance(point, region), the same values),...
T signedDistance(const Vec3T< T > &a_point) const noexcept
The object's value at a point: negative inside, positive outside, zero on the surface.
uint32_t getNumRegions() const noexcept
How many regions the object has: the background (region 0) and one per operand of its Overlays,...
Tape rebasedView(const Pool &a_pool) const noexcept
A view of this tape resolving against another pool of its mirror chain.
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
constexpr T max(const T &a_a, const T &a_b) noexcept
The larger of two values.
Definition EBGeometry_Math.hpp:61
constexpr T min(const T &a_a, const T &a_b) noexcept
The smaller of two values.
Definition EBGeometry_Math.hpp:44
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
SurfaceRole
What a surface is to a domain, at a point, from the regions on its two sides (see Subdomains::role())...
Definition EBGeometry_Subdomains.hpp:32
Numeric limits, with the meanings of the std::numeric_limits members of the same names.
Definition EBGeometry_Math.hpp:91
uint64_t endByte() const noexcept
One past the last byte this array occupies, measured from the pool base.
Definition EBGeometry_PODVector.hpp:264
The arrays, as offsets: what a Domain stores.
Definition EBGeometry_Subdomains.hpp:62
PODVector< uint32_t > m_table
The subdomain of each region, or None.
Definition EBGeometry_Subdomains.hpp:66
uint32_t m_numSubdomains
How many subdomains there are.
Definition EBGeometry_Subdomains.hpp:71
The arrays, as offsets: what a Domain stores.
Definition EBGeometry_SurfaceGroups.hpp:307
PODVector< uint32_t > m_groupOf
Each selector's surface group.
Definition EBGeometry_SurfaceGroups.hpp:316
The surface that decides a shape's value at a point, as closestSurface() reports it.
Definition EBGeometry_SurfaceRef.hpp:39