EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_ExplodedView.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
12#ifndef EBGEOMETRY_EXPLODEDVIEW_HPP
13#define EBGEOMETRY_EXPLODEDVIEW_HPP
14
15// Std includes
16#include <cmath>
17#include <cstdint>
18#include <vector>
19
20// Our includes
21#include "EBGeometry_Array.hpp"
23#include "EBGeometry_Expr.hpp"
24#include "EBGeometry_GPU.hpp"
26#include "EBGeometry_Math.hpp"
30#include "EBGeometry_Vec.hpp"
31
32namespace EBGeometry::Render::ViewerDetail {
33
57template <class V>
59{
60public:
64 using T = typename V::T;
65
69 using Box = RegionNeighborhoodDetail::Box<T>;
70
74 static constexpr uint32_t MaxRegions = 64;
75
85 ExplodedView(const V& a_view, const std::vector<Box>& a_boxes, const T a_factor)
86 : ExplodedView(a_view, a_boxes, offsetsOf(a_boxes, a_factor))
87 {}
88
96 ExplodedView(const V& a_view, const std::vector<Box>& a_boxes, const std::vector<Vec3T<T>>& a_offsets)
97 : m_view(a_view), m_rest(a_view)
98 {
99 const uint32_t numRegions = a_view.getNumRegions();
100
101 m_count = Math::min(numRegions, MaxRegions);
102
104
105 bool any = false;
106
107 // The regions moved: visible ones with a box (an unbounded one is moved too: its box is all of space).
108 for (uint32_t k = 0; k < MaxRegions; k++) {
109 m_offset[k] = Vec3T<T>::zeros();
110 m_low[k] = Vec3T<T>::zeros();
111 m_high[k] = Vec3T<T>::zeros();
112 }
113
114 m_moved = 0U;
115
116 for (uint32_t k = 1; k < m_count; k++) {
117 if (!a_view.isVisible(k)) {
118 continue;
119 }
120
121 // A region the clipping above it leaves nothing of has nothing to draw.
122 if (k < a_boxes.size() && !a_boxes[k].m_some) {
123 continue;
124 }
125
126 const bool hasBox = k < a_boxes.size();
127
128 m_offset[k] = (k < a_offsets.size()) ? a_offsets[k] : Vec3T<T>::zeros();
129 m_low[k] = hasBox ? a_boxes[k].m_lo : -Vec3T<T>::infinity();
130 m_high[k] = hasBox ? a_boxes[k].m_hi : Vec3T<T>::infinity();
131 m_moved |= uint64_t(1U) << k;
132
133 const BoundingVolumes::AABBT<T> moved(m_low[k] + m_offset[k], m_high[k] + m_offset[k]);
134
135 box = any ? BoundingVolumes::AABBT<T>(min(box.getLowCorner(), moved.getLowCorner()),
136 max(box.getHighCorner(), moved.getHighCorner()))
137 : moved;
138 any = true;
139 }
140
141 // The regions from MaxRegions on, through the view with the moved ones hidden.
142 m_hasRest = numRegions > MaxRegions;
143
144 if (m_hasRest) {
145 RegionSettings rest = a_view.getSettings();
146
147 rest.m_hidden = ~uint64_t(0U);
148 m_rest = a_view.withSettings(rest);
149
150 const BoundingVolumes::AABBT<T> whole = a_view.computeBoundingVolume();
151
152 box = any ? BoundingVolumes::AABBT<T>(min(box.getLowCorner(), whole.getLowCorner()),
153 max(box.getHighCorner(), whole.getHighCorner()))
154 : whole;
155 any = true;
156 }
157
159 }
160
166 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
168 inline T
169 signedDistance(const Vec3T<T>& a_point) const noexcept
170 {
171 Vec3T<T> back;
172
173 return this->decide(a_point, back).m_value;
174 }
175
180 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
181 inline DistanceQuality
182 getDistanceQuality() const noexcept
183 {
184 return m_view.getDistanceQuality();
185 }
186
191 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
193 computeBoundingVolume() const noexcept
194 {
195 return m_box;
196 }
197
204 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
205 inline uint32_t
206 regionAt(const Vec3T<T>& a_point, Vec3T<T>& a_back) const noexcept
207 {
208 const Decided decided = this->decide(a_point, a_back);
209
210 return (decided.m_region != RegionSettings::None) ? decided.m_region : m_view.getTape().region(a_point);
211 }
212
219 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
221 inline LeafRef<T>
222 closestLeaf(const Vec3T<T>& a_point) const noexcept
223 {
224 Vec3T<T> back;
225 const Decided decided = this->decide(a_point, back);
226
227 if (decided.m_region == RegionSettings::None) {
228 return m_rest.closestLeaf(a_point);
229 }
230
231 LeafRef<T> ref;
232
233 ref.m_leaf = this->keyOf(m_view.getTape().closestSurface(back, decided.m_region), decided.m_region);
234 ref.m_localPoint = a_point;
235
236 return ref;
237 }
238
245 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
246 inline uint32_t
247 sectionKey(const Vec3T<T>& a_point) const noexcept
248 {
249 Vec3T<T> back;
250 const Decided decided = this->decide(a_point, back);
251
252 if (decided.m_region == RegionSettings::None) {
253 return m_rest.sectionKey(a_point);
254 }
255
256 return m_view.getTape().getMaterialOf((decided.m_value <= T(0)) ? decided.m_region : 0U).value();
257 }
258
266 [[nodiscard]] EBGEOMETRY_HOST
267 inline ExplodedView
268 withView(const V& a_view) const noexcept
269 {
270 ExplodedView exploded = *this;
271
272 exploded.m_view = a_view;
273 exploded.m_rest = m_hasRest ? a_view.withSettings(m_rest.getSettings()) : a_view;
274
275 return exploded;
276 }
277
282 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
283 inline const V&
284 getView() const noexcept
285 {
286 return m_view;
287 }
288
296 [[nodiscard]] EBGEOMETRY_HOST
297 static inline std::vector<Vec3T<T>>
298 offsetsOf(const std::vector<Box>& a_boxes, const T a_factor)
299 {
300 const auto bounded = [](const Box& a_box) {
301 const Vec3T<T>& lo = a_box.m_lo;
302 const Vec3T<T>& hi = a_box.m_hi;
303
304 bool finite = a_box.m_some;
305
306 for (int d = 0; d < 3; d++) {
307 finite = finite && std::isfinite(lo[d]) && std::isfinite(hi[d]) && lo[d] <= hi[d];
308 }
309
310 return finite;
311 };
312
315
316 for (std::size_t k = 1; k < a_boxes.size(); k++) {
317 if (bounded(a_boxes[k])) {
318 lo = min(lo, a_boxes[k].m_lo);
319 hi = max(hi, a_boxes[k].m_hi);
320 }
321 }
322
323 std::vector<Vec3T<T>> offsets(a_boxes.size(), Vec3T<T>::zeros());
324
325 const Vec3T<T> middle = T(0.5) * (lo + hi);
326
327 for (std::size_t k = 1; k < a_boxes.size(); k++) {
328 if (bounded(a_boxes[k])) {
329 offsets[k] = a_factor * (T(0.5) * (a_boxes[k].m_lo + a_boxes[k].m_hi) - middle);
330 }
331 }
332
333 return offsets;
334 }
335
336private:
340 struct Decided
341 {
345 T m_value;
346
350 uint32_t m_region;
351 };
352
360 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
361 inline Decided
362 decide(const Vec3T<T>& a_point, Vec3T<T>& a_back) const noexcept
363 {
365
366 a_back = a_point;
367
368 for (uint32_t k = 1; k < m_count; k++) {
369 if (((m_moved >> k) & 1U) == 0U) {
370 continue;
371 }
372
373 const Vec3T<T> q = a_point - m_offset[k];
374
375 // The distance from q to the region's box: nothing nearer than that can come from it.
376 T outside = T(0);
377
378 for (int d = 0; d < 3; d++) {
379 const T beyond = Math::max(m_low[k][d] - q[d], q[d] - m_high[k][d]);
380
381 outside += (beyond > T(0)) ? beyond * beyond : T(0);
382 }
383
384 if ((best.m_value > T(0)) ? (outside >= best.m_value * best.m_value) : (outside > T(0))) {
385 continue;
386 }
387
388 const T value = m_view.getTape().signedDistance(q, k);
389
390 if (value < best.m_value) {
391 best = Decided{value, k};
392 a_back = q;
393 }
394 }
395
396 if (m_hasRest) {
397 const T value = m_rest.signedDistance(a_point);
398
399 if (value < best.m_value) {
400 best = Decided{value, RegionSettings::None};
401 a_back = a_point;
402 }
403 }
404
405 return best;
406 }
407
414 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
415 inline uint32_t
416 keyOf(const SurfaceRef<T>& a_surface, const uint32_t a_region) const noexcept
417 {
418 const RegionSettings& settings = m_view.getSettings();
419
420 switch (settings.m_coloring) {
422 return RegionSettings::interfaceKey(a_surface.m_ownerRegion, a_surface.m_neighborRegion);
423 }
425 return settings.m_surfaceGroups.surfaceGroupOf(a_surface);
426 }
428 return m_view.getTape().getMaterialOf(a_region).value();
429 }
431 return a_surface.m_surfaceLabel.value();
432 }
434 return a_surface.m_surface;
435 }
437 return a_surface.m_part;
438 }
439 default: {
440 return a_region;
441 }
442 }
443 }
444
448 V m_view;
449
453 V m_rest;
454
458 bool m_hasRest = false;
459
463 uint32_t m_count = 0;
464
468 uint64_t m_moved = 0U;
469
473 Array<Vec3T<T>, MaxRegions> m_offset;
474
478 Array<Vec3T<T>, MaxRegions> m_low;
479
483 Array<Vec3T<T>, MaxRegions> m_high;
484
488 BoundingVolumes::AABBT<T> m_box;
489};
490
499template <class T>
500[[nodiscard]] inline std::vector<RegionNeighborhoodDetail::Box<T>>
501regionBoxes(const Expr<T>& a_expr)
502{
503 std::vector<RegionNeighborhoodDetail::Box<T>> boxes(
504 1, RegionNeighborhoodDetail::Box<T>{Vec3T<T>::zeros(), Vec3T<T>::zeros(), false});
505
506 RegionNeighborhoodDetail::regionBoxes(a_expr, boxes);
507
508 return boxes;
509}
510
511} // namespace EBGeometry::Render::ViewerDetail
512
513#endif
A fixed-size array that compiles for both host and device.
Declarations of bounding volume types used in bounding volume hierarchies.
std::vector< RegionNeighborhoodDetail::Box< T > > regionBoxes(const Expr< T > &a_expr)
Each region's box, by region number, from the expression a tape was compiled from (the background's,...
Definition EBGeometry_ExplodedView.hpp:501
The tape's front end: Expr, a host-side value that describes a composed object, and the free function...
GPU portability layer: host/device decoration macros and backend detection.
#define EBGEOMETRY_DEVICE_NOINLINE
Keep a function out of line in the device pass (CUDA/HIP); nothing on the host.
Definition EBGeometry_GPU.hpp:110
#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
LeafRef: which leaf of a composition decides its value at a point.
Scalar helpers and numeric limits that compile for both host and device.
RegionNeighborhood: some regions of an expression with their neighbors, as a smaller expression (host...
RegionSettings: how the regions of an object are shown (visibility, coloring, a domain's surface grou...
RegionView: the visible regions of a tape (see Overlay()) as a shape to look at, its surfaces those o...
Declaration of 2D and 3D point/vector classes with templated precision, and free functions on them.
Axis-aligned bounding box (AABB) enclosing a set of 3D points.
Definition EBGeometry_BoundingVolumes.hpp:37
Vec3T< T > & getLowCorner() noexcept
Get a modifiable reference to the low corner of the AABB.
Vec3T< T > & getHighCorner() noexcept
Get a modifiable reference to the high corner of the AABB.
A composed object: an immutable tree of shapes and operations, built on the host and compiled by a Ta...
Definition EBGeometry_Expr.hpp:452
A view of regions drawn exploded, as in an assembly drawing: each region moved by its own offset....
Definition EBGeometry_ExplodedView.hpp:59
ExplodedView(const V &a_view, const std::vector< Box > &a_boxes, const T a_factor)
Each region moved away from the middle: by a_factor times its box's center less the center of the box...
Definition EBGeometry_ExplodedView.hpp:85
const V & getView() const noexcept
The view drawn exploded.
Definition EBGeometry_ExplodedView.hpp:284
ExplodedView(const V &a_view, const std::vector< Box > &a_boxes, const std::vector< Vec3T< T > > &a_offsets)
Each region moved by its own offset.
Definition EBGeometry_ExplodedView.hpp:96
DistanceQuality getDistanceQuality() const noexcept
The distance quality: the view's, since the value is made of its regions' functions.
Definition EBGeometry_ExplodedView.hpp:182
uint32_t regionAt(const Vec3T< T > &a_point, Vec3T< T > &a_back) const noexcept
The region that decides the value at a point, and the point moved back into the view's frame.
Definition EBGeometry_ExplodedView.hpp:206
ExplodedView withView(const V &a_view) const noexcept
The same exploded view of another view of the same regions: the view on a mirror of its pool (whose m...
Definition EBGeometry_ExplodedView.hpp:268
BoundingVolumes::AABBT< T > computeBoundingVolume() const noexcept
The box of the moved regions (and of the view, with regions that stay where they are).
Definition EBGeometry_ExplodedView.hpp:193
T signedDistance(const Vec3T< T > &a_point) const noexcept
The value: the least function of a visible region at the point moved back by the region's offset.
Definition EBGeometry_ExplodedView.hpp:169
static constexpr uint32_t MaxRegions
How many regions (the background included) can be moved; the later ones stay where they are.
Definition EBGeometry_ExplodedView.hpp:74
LeafRef< T > closestLeaf(const Vec3T< T > &a_point) const noexcept
The key of the surface that decides the value at a point, as the view names it, as a leaf reference (...
Definition EBGeometry_ExplodedView.hpp:222
uint32_t sectionKey(const Vec3T< T > &a_point) const noexcept
What a point is in, for a section plane's fill: the material of the region the point is in,...
Definition EBGeometry_ExplodedView.hpp:247
RegionNeighborhoodDetail::Box< T > Box
A region's box, or none.
Definition EBGeometry_ExplodedView.hpp:69
typename V::T T
Floating-point precision.
Definition EBGeometry_ExplodedView.hpp:64
static std::vector< Vec3T< T > > offsetsOf(const std::vector< Box > &a_boxes, const T a_factor)
Each region's offset for a factor: a_factor times its box's center less the center of all boxes but t...
Definition EBGeometry_ExplodedView.hpp:298
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
static constexpr Vec3T< T > infinity() noexcept
Return a vector with infinite components.
static constexpr Vec3T< T > zeros() noexcept
Return a vector with x = y = z = 0.
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
Vec2T< T > min(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise minimum of two vectors.
@ SurfaceGroup
The surface group the surface is in (RegionSettings::m_surfaceGroups), or none.
@ Interface
The pair of regions on its two sides there (see RegionSettings::interfaceKey()).
@ Part
The surface's part: a box's face, a mesh's face.
@ Surface
The surface itself (SurfaceRef::m_surface): each shape, each member of a collection.
@ SurfaceLabel
The surface's label (SurfaceLabel()).
@ Material
That region's material.
DistanceQuality
How far the value of a signed distance function can be trusted, from strongest to weakest.
Definition EBGeometry_DistanceQuality.hpp:25
Vec2T< T > max(const Vec2T< T > &u, const Vec2T< T > &v) noexcept
Component-wise maximum of two vectors.
The deciding leaf of a composition at a point: the primitive whose surface the composed value comes f...
Definition EBGeometry_LeafRef.hpp:42
Vec3T< T > m_localPoint
The point in the primitive's own frame (after the composition's maps), at which it can be queried its...
Definition EBGeometry_LeafRef.hpp:68
uint32_t m_leaf
The leaf: its number in expression order, depth first, each occurrence counted.
Definition EBGeometry_LeafRef.hpp:51
Numeric limits, with the meanings of the std::numeric_limits members of the same names.
Definition EBGeometry_Math.hpp:91
How the regions of an object are shown (see RegionView): which regions are visible,...
Definition EBGeometry_RegionSettings.hpp:66
uint64_t m_hidden
The hidden regions, a bit each (bit k for region k); regions from 64 on are always shown.
Definition EBGeometry_RegionSettings.hpp:86
static constexpr uint32_t None
No key.
Definition EBGeometry_RegionSettings.hpp:70
static uint32_t interfaceKey(const uint32_t a_first, const uint32_t a_second) noexcept
The key of the interface between two regions, the same whichever side it is seen from.
Definition EBGeometry_RegionSettings.hpp:292