EBGeometry
Compact, header-only C++ library for fast evaluation of signed distance functions
Loading...
Searching...
No Matches
EBGeometry_Profile.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_PROFILE_HPP
13#define EBGEOMETRY_PROFILE_HPP
14
15// Std includes
16#include <cmath>
17#include <cstdint>
18#include <type_traits>
19#include <vector>
20
21// Our includes
22#include "EBGeometry_Array.hpp"
25#include "EBGeometry_GPU.hpp"
26#include "EBGeometry_Macros.hpp"
27#include "EBGeometry_Math.hpp"
28#include "EBGeometry_Vec.hpp"
29
30namespace EBGeometry {
31
35enum class ProfileEdge : uint8_t
36{
37 Line,
38 ArcLeft,
39 ArcRight,
40 Quad
41};
42
57template <class T>
59{
60 static_assert(std::is_floating_point_v<T>, "Profile<T>: T must be a floating-point type");
61
62public:
67 static constexpr uint32_t MaxEdges = 96U;
68
94
99 explicit Profile(const Vec2T<T>& a_start);
100
106 Profile&
107 lineTo(const Vec2T<T>& a_point);
108
116 Profile&
117 arcTo(const Vec2T<T>& a_point, T a_radius);
118
126 Profile&
127 quadTo(const Vec2T<T>& a_control, const Vec2T<T>& a_point);
128
141 Profile&
142 cubicTo(const Vec2T<T>& a_control1, const Vec2T<T>& a_control2, const Vec2T<T>& a_point, T a_tolerance);
143
148 [[nodiscard]] std::vector<Edge>
149 closedEdges() const;
150
155 [[nodiscard]] const Vec2T<T>&
156 getCurrentPoint() const noexcept
157 {
158 return m_current;
159 }
160
161protected:
168 void
169 checkEnd(const Vec2T<T>& a_point, const char* a_who, uint32_t a_count) const;
170
175
180
184 std::vector<Edge> m_edges;
185
189 uint32_t m_parts = 0U;
190};
191
212template <class T>
214{
215 static_assert(std::is_floating_point_v<T>, "ProfileSDF<T>: T must be a floating-point type");
216
217public:
222
226 static constexpr uint32_t MaxEdges = Profile<T>::MaxEdges;
227
233
239 [[nodiscard]] static ProfileSDF
240 revolve(const Profile<T>& a_profile);
241
248 [[nodiscard]] static ProfileSDF
249 extrude(const Profile<T>& a_profile, T a_height);
250
254 ProfileSDF(const ProfileSDF&) = default;
255
259 ProfileSDF(ProfileSDF&&) = default;
260
266 operator=(const ProfileSDF&) = default;
267
273 operator=(ProfileSDF&&) = default;
274
279 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
280 bool
281 isRevolved() const noexcept
282 {
283 return m_revolved != 0U;
284 }
285
290 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
291 T
292 getHeight() const noexcept
293 {
294 return m_height;
295 }
296
301 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
302 uint32_t
303 getNumEdges() const noexcept
304 {
305 return m_numEdges;
306 }
307
313 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
315 getEdgeKind(const uint32_t a_edge) const noexcept
316 {
318
319 return static_cast<ProfileEdge>(m_kinds[a_edge] & uint8_t(~OnAxis));
320 }
321
327 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
328 const Vec2T<T>&
329 getEdgeStart(const uint32_t a_edge) const noexcept
330 {
332
333 return m_points[a_edge];
334 }
335
341 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
342 const Vec2T<T>&
343 getEdgeEnd(const uint32_t a_edge) const noexcept
344 {
346
347 return m_points[(a_edge + 1U < m_numEdges) ? a_edge + 1U : 0U];
348 }
349
355 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
356 const Vec2T<T>&
357 getEdgeControl(const uint32_t a_edge) const noexcept
358 {
360
361 return m_controls[a_edge];
362 }
363
369 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
370 uint32_t
371 getEdgePart(const uint32_t a_edge) const noexcept
372 {
374
375 return m_parts[a_edge];
376 }
377
384 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
385 bool
386 isOnAxis(const uint32_t a_edge) const noexcept
387 {
389
390 return (m_kinds[a_edge] & OnAxis) != 0U;
391 }
392
398 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
399 bool
400 isCounterclockwise() const noexcept
401 {
402 return m_counterclockwise != 0U;
403 }
404
413 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
415 T
416 profileDistance(const Vec2T<T>& a_point) const noexcept;
417
426 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
428 T
429 signedDistance(const Vec3T<T>& a_point) const noexcept;
430
435 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
436 uint32_t
437 getNumParts() const noexcept
438 {
439 return m_numParts + ((m_revolved != 0U) ? 0U : 2U);
440 }
441
449 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
451 uint32_t
452 closestPart(const Vec3T<T>& a_point) const noexcept;
453
458 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
460 computeBoundingVolume() const noexcept;
461
467 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
468 Vec2T<T>
469 inPlane(const Vec3T<T>& a_point) const noexcept
470 {
471 return (m_revolved != 0U) ? Vec2T<T>(std::sqrt(a_point[0] * a_point[0] + a_point[2] * a_point[2]), a_point[1])
472 : Vec2T<T>(a_point[0], a_point[2]);
473 }
474
475protected:
483 void
484 build(const Profile<T>& a_profile, bool a_revolved, T a_height, const char* a_who);
485
492 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
493 uint32_t
494 nearestEdge(const Vec2T<T>& a_point, T& a_squared) const noexcept;
495
502 [[nodiscard]] EBGEOMETRY_HOST_DEVICE
503 bool
504 insideOutline(const Vec2T<T>& a_point) const noexcept;
505
509 static constexpr uint8_t OnAxis = 0x80U;
510
514 uint32_t m_numEdges = 0U;
515
519 uint32_t m_numParts = 0U;
520
524 uint32_t m_revolved = 1U;
525
529 uint32_t m_counterclockwise = 1U;
530
534 T m_height = T(0);
535
540
545
550
555
560
565};
566
567static_assert(std::is_trivially_copyable_v<ProfileSDF<float>>, "ProfileSDF<float> must be trivially copyable");
568static_assert(std::is_trivially_copyable_v<ProfileSDF<double>>, "ProfileSDF<double> must be trivially copyable");
569
570} // namespace EBGeometry
571
572#include "EBGeometry_ProfileImplem.hpp"
573
574#endif
A fixed-size array that compiles for both host and device.
Declarations of bounding volume types used in bounding volume hierarchies.
How far a function's value can be trusted as a distance: DistanceQuality, and the distanceQualityOf t...
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
Utility macros for EBGeometry.
#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.
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
Signed distance field for a profile turned about the y axis (a solid of revolution,...
Definition EBGeometry_Profile.hpp:214
void build(const Profile< T > &a_profile, bool a_revolved, T a_height, const char *a_who)
Sets the shape from a profile.
bool isOnAxis(const uint32_t a_edge) const noexcept
Whether an edge lies on the axis of a turned profile: it turns into a line, which is no surface,...
Definition EBGeometry_Profile.hpp:386
T signedDistance(const Vec3T< T > &a_point) const noexcept
Signed distance function for the solid.
bool isCounterclockwise() const noexcept
Whether the outline runs counterclockwise in its plane (its inside on the left of each edge,...
Definition EBGeometry_Profile.hpp:400
ProfileSDF(ProfileSDF &&)=default
Move constructor.
T m_height
An extrusion's height.
Definition EBGeometry_Profile.hpp:534
uint32_t getNumEdges() const noexcept
How many edges the outline has.
Definition EBGeometry_Profile.hpp:303
const Vec2T< T > & getEdgeControl(const uint32_t a_edge) const noexcept
An arc's center, or a Bezier curve's control point (unused for a straight edge).
Definition EBGeometry_Profile.hpp:357
T getHeight() const noexcept
How far an extruded profile is extruded (0 for a turned one).
Definition EBGeometry_Profile.hpp:292
uint32_t m_counterclockwise
1 if the outline runs counterclockwise in its plane, 0 if clockwise: which side of each edge is outsi...
Definition EBGeometry_Profile.hpp:529
uint32_t m_revolved
1 if turned, 0 if extruded.
Definition EBGeometry_Profile.hpp:524
bool isRevolved() const noexcept
Whether the profile is turned about the y axis (otherwise it is extruded).
Definition EBGeometry_Profile.hpp:281
bool insideOutline(const Vec2T< T > &a_point) const noexcept
Whether a point in the plane is inside the outline: the ray from it towards +u crosses the outline an...
ProfileEdge getEdgeKind(const uint32_t a_edge) const noexcept
An edge's kind.
Definition EBGeometry_Profile.hpp:315
Vec2T< T > m_hi
The high corner of the outline's box in its plane.
Definition EBGeometry_Profile.hpp:544
static ProfileSDF revolve(const Profile< T > &a_profile)
A profile turned about the y axis: its point (r, h) sweeps the circle of radius r at height y = h.
static constexpr uint32_t MaxEdges
The most edges a profile can have.
Definition EBGeometry_Profile.hpp:226
ProfileSDF & operator=(ProfileSDF &&)=default
Move assignment.
uint32_t m_numParts
Number of the profile's parts.
Definition EBGeometry_Profile.hpp:519
BoundingVolumes::AABBT< T > computeBoundingVolume() const noexcept
The shape's axis-aligned bounding box: that of the outline, turned or extruded.
const Vec2T< T > & getEdgeEnd(const uint32_t a_edge) const noexcept
Where an edge ends: where the next one starts.
Definition EBGeometry_Profile.hpp:343
T profileDistance(const Vec2T< T > &a_point) const noexcept
The signed distance from a point to the outline, in its plane; negative inside.
Array< uint8_t, MaxEdges > m_kinds
Each edge's kind (a ProfileEdge).
Definition EBGeometry_Profile.hpp:559
uint32_t getNumParts() const noexcept
How many parts the shape has (see hasParts): the profile's, and an extrusion's two ends.
Definition EBGeometry_Profile.hpp:437
uint32_t m_numEdges
Number of edges.
Definition EBGeometry_Profile.hpp:514
Array< Vec2T< T >, MaxEdges > m_points
Where each edge starts.
Definition EBGeometry_Profile.hpp:549
uint32_t getEdgePart(const uint32_t a_edge) const noexcept
The part an edge belongs to.
Definition EBGeometry_Profile.hpp:371
Array< Vec2T< T >, MaxEdges > m_controls
Each arc's center, or each Bezier curve's control point.
Definition EBGeometry_Profile.hpp:554
static ProfileSDF extrude(const Profile< T > &a_profile, T a_height)
A profile extruded along y: its point (u, v) is (x, z) = (u, v), from y = 0 to the height.
ProfileSDF & operator=(const ProfileSDF &)=default
Copy assignment.
static constexpr DistanceQuality distanceQuality
How far signedDistance() can be trusted as a distance: the Euclidean distance to the surface.
Definition EBGeometry_Profile.hpp:221
Array< uint8_t, MaxEdges > m_parts
Each edge's part.
Definition EBGeometry_Profile.hpp:564
Vec2T< T > inPlane(const Vec3T< T > &a_point) const noexcept
Where a point is in the outline's plane: (sqrt(x^2 + z^2), y) turned, (x, z) extruded.
Definition EBGeometry_Profile.hpp:469
ProfileSDF(const ProfileSDF &)=default
Copy constructor.
ProfileSDF()
Default constructor: the square from (0, 0) to (1, 1), turned, which is the cylinder of radius 1 from...
static constexpr uint8_t OnAxis
The bit of m_kinds that marks a straight edge on the axis of a turned profile, which is no surface.
Definition EBGeometry_Profile.hpp:509
const Vec2T< T > & getEdgeStart(const uint32_t a_edge) const noexcept
Where an edge starts.
Definition EBGeometry_Profile.hpp:329
Vec2T< T > m_lo
The low corner of the outline's box in its plane.
Definition EBGeometry_Profile.hpp:539
uint32_t closestPart(const Vec3T< T > &a_point) const noexcept
The part nearest a point (see hasParts): that of the nearest edge (the first on a tie),...
uint32_t nearestEdge(const Vec2T< T > &a_point, T &a_squared) const noexcept
The nearest edge to a point in the plane, and the square of its distance.
A closed outline in a plane, of straight edges, circular arcs and quadratic Bezier curves,...
Definition EBGeometry_Profile.hpp:59
Profile & quadTo(const Vec2T< T > &a_control, const Vec2T< T > &a_point)
Draws a quadratic Bezier curve. One whose control point lies on the line between its ends,...
Profile & arcTo(const Vec2T< T > &a_point, T a_radius)
Draws a circular arc, the shorter of the two of its radius between its ends (at most a half circle).
static constexpr uint32_t MaxEdges
The most edges a profile can have, a cubic's pieces and the closing edge included....
Definition EBGeometry_Profile.hpp:67
Profile(const Vec2T< T > &a_start)
Starts an outline at a point.
Vec2T< T > m_start
The first point.
Definition EBGeometry_Profile.hpp:174
std::vector< Edge > m_edges
The edges drawn.
Definition EBGeometry_Profile.hpp:184
Profile & lineTo(const Vec2T< T > &a_point)
Draws a straight edge.
Profile & cubicTo(const Vec2T< T > &a_control1, const Vec2T< T > &a_control2, const Vec2T< T > &a_point, T a_tolerance)
Draws a cubic Bezier curve, as quadratic ones that stay within a distance of it.
void checkEnd(const Vec2T< T > &a_point, const char *a_who, uint32_t a_count) const
Checks a point an edge ends at, and that the outline has room for more edges.
Vec2T< T > m_current
Where the last edge ended.
Definition EBGeometry_Profile.hpp:179
uint32_t m_parts
How many parts the edges drawn belong to.
Definition EBGeometry_Profile.hpp:189
std::vector< Edge > closedEdges() const
The outline's edges, closed: with the closing edge, when the outline does not end where it started.
const Vec2T< T > & getCurrentPoint() const noexcept
Where the outline is now: where the last edge ended.
Definition EBGeometry_Profile.hpp:156
Two-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:37
Three-dimensional vector class with arithmetic operators.
Definition EBGeometry_Vec.hpp:258
Namespace containing all of EBGeometry's functionality.
Definition EBGeometry_Affine.hpp:26
ProfileEdge
The kinds of a profile's edges.
Definition EBGeometry_Profile.hpp:36
@ ArcRight
A circular arc that turns right (clockwise about its center), at most a half circle.
@ ArcLeft
A circular arc that turns left (counterclockwise about its center), at most a half circle.
@ Line
A straight edge.
@ Quad
A quadratic Bezier curve.
DistanceQuality
How far the value of a signed distance function can be trusted, from strongest to weakest.
Definition EBGeometry_DistanceQuality.hpp:25
@ Exact
The value's magnitude is the Euclidean distance to the surface.
Fixed-size array with host- and device-callable members.
Definition EBGeometry_Array.hpp:33
One edge of the outline.
Definition EBGeometry_Profile.hpp:73
uint32_t m_part
The part it belongs to.
Definition EBGeometry_Profile.hpp:92
Vec2T< T > m_control
An arc's center, or a Bezier curve's control point; unused for a straight edge.
Definition EBGeometry_Profile.hpp:87
ProfileEdge m_kind
Its kind.
Definition EBGeometry_Profile.hpp:77
Vec2T< T > m_start
Where it starts; it ends where the next edge starts (the first one, after the last).
Definition EBGeometry_Profile.hpp:82