EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_SurfaceGroups.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_SURFACEGROUPS_HPP
12#define EBGEOMETRY_SURFACEGROUPS_HPP
13
14// Std includes
15#include <cstdint>
16#include <type_traits>
17#include <vector>
18
19// Our includes
20#include "EBGeometry_GPU.hpp"
22#include "EBGeometry_Macros.hpp"
24#include "EBGeometry_Pool.hpp"
26
27namespace EBGeometry {
28
40{
44 static constexpr uint32_t Any = 0xFFFFFFFFU;
45
49 uint32_t m_surface = Any;
50
54 uint32_t m_part = Any;
55
59 uint32_t m_regionA = Any;
60
64 uint32_t m_regionB = Any;
65
69 uint32_t m_label = Any;
70
74 uint32_t m_materialA = Any;
75
79 uint32_t m_materialB = Any;
80
84 bool m_rest = false;
85
89 uint32_t m_numSurfaces = 1;
90
98 template <class T>
99 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
100 static inline SurfaceSelector
101 piece(const SurfaceRef<T>& a_surface) noexcept
102 {
103 SurfaceSelector selector;
104
105 selector.m_surface = a_surface.m_surface;
106 selector.m_regionA = a_surface.m_ownerRegion;
107 selector.m_regionB = a_surface.m_neighborRegion;
108
109 return selector;
110 }
111
117 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
118 static inline SurfaceSelector
119 rest() noexcept
120 {
121 SurfaceSelector selector;
122
123 selector.m_rest = true;
124
125 return selector;
126 }
127
133 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
134 static inline SurfaceSelector
135 surface(const uint32_t a_surface) noexcept
136 {
137 SurfaceSelector selector;
138
139 selector.m_surface = a_surface;
140
141 return selector;
142 }
143
151 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
152 static inline SurfaceSelector
153 surfaces(const uint32_t a_first, const uint32_t a_count) noexcept
154 {
155 SurfaceSelector selector;
156
157 selector.m_surface = a_first;
158 selector.m_numSurfaces = a_count;
159
160 return selector;
161 }
162
168 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
169 static inline SurfaceSelector
170 label(const SurfaceLabelId a_label) noexcept
171 {
172 SurfaceSelector selector;
173
174 selector.m_label = a_label.value();
175
176 return selector;
177 }
178
185 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
186 static inline SurfaceSelector
187 interface(const uint32_t a_first, const uint32_t a_second) noexcept
188 {
189 SurfaceSelector selector;
190
191 selector.m_regionA = a_first;
192 selector.m_regionB = a_second;
193
194 return selector;
195 }
196
202 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
203 static inline SurfaceSelector
204 region(const uint32_t a_region) noexcept
205 {
206 SurfaceSelector selector;
207
208 selector.m_regionA = a_region;
209
210 return selector;
211 }
212
219 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
220 static inline SurfaceSelector
221 materials(const MaterialLabelId a_first, const MaterialLabelId a_second = MaterialLabelId::none()) noexcept
222 {
223 SurfaceSelector selector;
224
225 selector.m_materialA = a_first.value();
226 selector.m_materialB = a_second.value();
227
228 return selector;
229 }
230
236 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
237 inline SurfaceSelector
238 withPart(const uint32_t a_part) const noexcept
239 {
240 SurfaceSelector selector = *this;
241
242 selector.m_part = a_part;
243
244 return selector;
245 }
246
253 template <class T>
254 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
255 inline bool
256 matches(const SurfaceRef<T>& a_surface) const noexcept
257 {
258 const auto is = [](const uint32_t a_condition, const uint32_t a_value) {
259 return a_condition == Any || a_condition == a_value;
260 };
261
262 // A pair of conditions on the two sides, either way round; one alone: on either side.
263 const auto sides =
264 [&is](const uint32_t a_first, const uint32_t a_second, const uint32_t a_minus, const uint32_t a_plus) {
265 if (a_first == Any && a_second == Any) {
266 return true;
267 }
268 if (a_second == Any) {
269 return a_first == a_minus || a_first == a_plus;
270 }
271
272 return (is(a_first, a_minus) && is(a_second, a_plus)) || (is(a_first, a_plus) && is(a_second, a_minus));
273 };
274
275 // A range of surfaces: below its first, the difference wraps round to a large number.
276 const bool surface = m_surface == Any || a_surface.m_surface - m_surface < m_numSurfaces;
277
278 return surface && is(m_part, a_surface.m_part) && is(m_label, a_surface.m_surfaceLabel.value()) &&
279 sides(m_regionA, m_regionB, a_surface.m_ownerRegion, a_surface.m_neighborRegion) &&
280 sides(m_materialA, m_materialB, a_surface.m_ownerMaterial.value(), a_surface.m_neighborMaterial.value());
281 }
282};
283
296{
297public:
301 static constexpr uint32_t None = 0xFFFFFFFFU;
302
323
327 SurfaceGroups() noexcept = default;
328
340 const std::vector<SurfaceSelector>& a_selectors,
341 const std::vector<uint32_t>& a_surfaceGroups,
342 const uint32_t a_numSurfaceGroups)
343 {
344 EBGEOMETRY_REQUIRE(a_selectors.size() == a_surfaceGroups.size(),
345 "SurfaceGroups: %zu selectors and %zu surface groups of them",
346 a_selectors.size(),
347 a_surfaceGroups.size());
348
349 this->attach(a_pool, "SurfaceGroups");
350
351 const uint32_t n = static_cast<uint32_t>(a_selectors.size());
352
353 m_layout.m_selectors.reserveSizedFrom(a_pool, n);
354 m_layout.m_groupOf.reserveSizedFrom(a_pool, n);
355 m_layout.m_numSurfaceGroups = a_numSurfaceGroups;
356
357 for (uint32_t i = 0; i < n; i++) {
358 EBGEOMETRY_REQUIRE(a_surfaceGroups[i] < a_numSurfaceGroups,
359 "SurfaceGroups: selector %u is in surface group %u of %u",
360 unsigned(i),
361 unsigned(a_surfaceGroups[i]),
362 unsigned(a_numSurfaceGroups));
363
364 m_layout.m_selectors.at(this->base(), i) = a_selectors[i];
365 m_layout.m_groupOf.at(this->base(), i) = a_surfaceGroups[i];
366 }
367 }
368
375 SurfaceGroups(const Layout& a_layout, const PoolLocation& a_location) noexcept
376 : PoolHandle(a_location), m_layout(a_layout)
377 {}
378
383 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
384 inline const Layout&
385 layout() const noexcept
386 {
387 return m_layout;
388 }
389
394 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
395 inline uint32_t
396 getNumSurfaceGroups() const noexcept
397 {
398 return m_layout.m_numSurfaceGroups;
399 }
400
405 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
406 inline uint32_t
407 getNumSelectors() const noexcept
408 {
409 return m_layout.m_selectors.size();
410 }
411
417 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
418 inline const SurfaceSelector&
419 getSelector(const uint32_t a_selector) const noexcept
420 {
421 return m_layout.m_selectors.at(this->base(), a_selector);
422 }
423
429 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
430 inline uint32_t
431 getSurfaceGroupOfSelector(const uint32_t a_selector) const noexcept
432 {
433 return m_layout.m_groupOf.at(this->base(), a_selector);
434 }
435
444 template <class T>
445 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
446 inline uint32_t
447 surfaceGroupOf(const SurfaceRef<T>& a_surface) const noexcept
448 {
449 const uint32_t n = m_layout.m_selectors.size();
450
451 if (n == 0U) {
452 return None;
453 }
454
455 const SurfaceSelector* selectors = m_layout.m_selectors.data(this->base());
456 const uint32_t* groups = m_layout.m_groupOf.data(this->base());
457
458 // The remaining surfaces' selectors last.
459 for (int pass = 0; pass < 2; pass++) {
460 for (uint32_t i = 0; i < n; i++) {
461 if (selectors[i].m_rest == (pass == 1) && selectors[i].matches(a_surface)) {
462 return groups[i];
463 }
464 }
465 }
466
467 return None;
468 }
469
475 [[nodiscard]] EBGEOMETRY_HOST
476 inline SurfaceGroups
477 rebasedView(const Pool& a_pool) const noexcept
478 {
479 SurfaceGroups view = *this;
480
481 view.setLocation(this->rebasedLocation(a_pool, m_layout.m_groupOf.endByte(), "SurfaceGroups"));
482
483 return view;
484 }
485
486private:
490 Layout m_layout;
491};
492
493static_assert(std::is_trivially_copyable_v<SurfaceGroups>,
494 "SurfaceGroups must be trivially copyable, for a kernel to take it");
495
496} // namespace EBGeometry
497
498#endif
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
LabelId: the number of a label, typed by what it labels (SurfaceLabelId, MaterialLabelId).
Utility macros for EBGeometry.
#define EBGEOMETRY_REQUIRE(cond,...)
Always-on check of user input and one-time host setup.
Definition EBGeometry_Macros.hpp:200
Placement-independent POD array views for the GPU memory foundation.
Growable bump arena for the GPU memory foundation.
SurfaceRef: which surface of a shape decides its value at a point, and the container protocol that na...
Base of every pool-resident handle: the one PoolLocation its arrays resolve against.
Definition EBGeometry_Pool.hpp:563
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
PoolHandle() noexcept=default
An unset location; the first reservation attaches it.
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
void * base() noexcept
The base address this handle's offsets resolve against; see PoolLocation::base().
Definition EBGeometry_Pool.hpp:639
Growable bump arena over a MemoryResource.
Definition EBGeometry_Pool.hpp:205
A domain's surface groups, read on the host or a device: each group a set of surfaces,...
Definition EBGeometry_SurfaceGroups.hpp:296
uint32_t getNumSelectors() const noexcept
How many selectors there are, over all surface groups.
Definition EBGeometry_SurfaceGroups.hpp:407
uint32_t getSurfaceGroupOfSelector(const uint32_t a_selector) const noexcept
The surface group of a selector.
Definition EBGeometry_SurfaceGroups.hpp:431
SurfaceGroups() noexcept=default
No surface groups.
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
uint32_t getNumSurfaceGroups() const noexcept
How many surface groups there are.
Definition EBGeometry_SurfaceGroups.hpp:396
SurfaceGroups(const Layout &a_layout, const PoolLocation &a_location) noexcept
Rebuild a view from a stored layout and the location of the pool that holds it (as a Domain does).
Definition EBGeometry_SurfaceGroups.hpp:375
SurfaceGroups rebasedView(const Pool &a_pool) const noexcept
A view resolving against another pool of the selectors' mirror chain (see PoolHandle).
Definition EBGeometry_SurfaceGroups.hpp:477
static constexpr uint32_t None
No surface group: a surface no selector matches.
Definition EBGeometry_SurfaceGroups.hpp:301
const SurfaceSelector & getSelector(const uint32_t a_selector) const noexcept
A selector.
Definition EBGeometry_SurfaceGroups.hpp:419
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
constexpr uint32_t value() const noexcept
The number, for indexing an array of your own.
Definition EBGeometry_LabelId.hpp:74
static constexpr LabelId none() noexcept
No label.
Definition EBGeometry_LabelId.hpp:63
Placement-independent descriptor of a POD array inside a Pool.
Definition EBGeometry_PODVector.hpp:131
void reserveSizedFrom(Pool &a_pool, uint32_t a_count)
Reserve a_count slots from a_pool and count them all as elements.
Definition EBGeometry_PODVector.hpp:181
uint64_t endByte() const noexcept
One past the last byte this array occupies, measured from the pool base.
Definition EBGeometry_PODVector.hpp:264
T & at(void *a_base, uint32_t a_i) const noexcept
Mutable element access against a_base.
Definition EBGeometry_PODVector.hpp:220
T * data(void *a_base) const noexcept
Mutable pointer to element 0 against a_base.
Definition EBGeometry_PODVector.hpp:195
Where a pool-resident descriptor resolves its offsets, and the rules for moving it.
Definition EBGeometry_Pool.hpp:99
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
uint32_t m_numSurfaceGroups
How many surface groups there are.
Definition EBGeometry_SurfaceGroups.hpp:321
PODVector< SurfaceSelector > m_selectors
The selectors, in the order added.
Definition EBGeometry_SurfaceGroups.hpp:311
The surface that decides a shape's value at a point, as closestSurface() reports it.
Definition EBGeometry_SurfaceRef.hpp:39
Conditions on a point of a surface, all of which must hold; an unset one (Any) matches anything.
Definition EBGeometry_SurfaceGroups.hpp:40
uint32_t m_regionB
The region on the other side, or Any (with m_regionA set: m_regionA on either side,...
Definition EBGeometry_SurfaceGroups.hpp:64
static SurfaceSelector piece(const SurfaceRef< T > &a_surface) noexcept
The piece a surface point belongs to: its surface between the two regions on its sides,...
Definition EBGeometry_SurfaceGroups.hpp:101
static SurfaceSelector label(const SurfaceLabelId a_label) noexcept
Every surface with a surface label.
Definition EBGeometry_SurfaceGroups.hpp:170
static SurfaceSelector interface(const uint32_t a_first, const uint32_t a_second) noexcept
Every surface between two regions, either way round: an interface.
Definition EBGeometry_SurfaceGroups.hpp:187
SurfaceSelector withPart(const uint32_t a_part) const noexcept
The same selector restricted to one part (a cylinder's cap, a box's face).
Definition EBGeometry_SurfaceGroups.hpp:238
uint32_t m_materialB
The material on the other side, or Any (as m_regionB).
Definition EBGeometry_SurfaceGroups.hpp:79
bool m_rest
Whether the selector holds the remaining surfaces (rest()): surface groups try it after every other.
Definition EBGeometry_SurfaceGroups.hpp:84
bool matches(const SurfaceRef< T > &a_surface) const noexcept
Whether a surface point matches every condition set.
Definition EBGeometry_SurfaceGroups.hpp:256
static SurfaceSelector rest() noexcept
The remaining surfaces: every surface no other selector of the surface groups holds,...
Definition EBGeometry_SurfaceGroups.hpp:119
uint32_t m_numSurfaces
How many surfaces from m_surface it holds (1: one surface); unused when m_surface is Any.
Definition EBGeometry_SurfaceGroups.hpp:89
uint32_t m_materialA
A material on one side, or Any.
Definition EBGeometry_SurfaceGroups.hpp:74
static SurfaceSelector region(const uint32_t a_region) noexcept
Every surface of a region: those with the region on either side.
Definition EBGeometry_SurfaceGroups.hpp:204
static SurfaceSelector surfaces(const uint32_t a_first, const uint32_t a_count) noexcept
Every point of a range of surfaces, whatever their parts, labels and sides: a node of an expression a...
Definition EBGeometry_SurfaceGroups.hpp:153
static constexpr uint32_t Any
An unset condition: matches anything.
Definition EBGeometry_SurfaceGroups.hpp:44
uint32_t m_label
The surface label (SurfaceRef::m_surfaceLabel), or Any.
Definition EBGeometry_SurfaceGroups.hpp:69
uint32_t m_surface
The surface (SurfaceRef::m_surface), or the first of a range of them (see m_numSurfaces); or Any.
Definition EBGeometry_SurfaceGroups.hpp:49
static SurfaceSelector surface(const uint32_t a_surface) noexcept
Every point of a surface, whatever its part and sides.
Definition EBGeometry_SurfaceGroups.hpp:135
uint32_t m_regionA
A region on one side, or Any.
Definition EBGeometry_SurfaceGroups.hpp:59
static SurfaceSelector materials(const MaterialLabelId a_first, const MaterialLabelId a_second=MaterialLabelId::none()) noexcept
Every surface between two materials, either way round (one material alone: on either side).
Definition EBGeometry_SurfaceGroups.hpp:221
uint32_t m_part
The part (SurfaceRef::m_part), or Any.
Definition EBGeometry_SurfaceGroups.hpp:54