MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
HitRegion.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "Bounds.hpp"
4
5namespace MayaFlux::Kinesis {
6
7/**
8 * @struct HitRegion
9 * @brief A region a point can hit: fast reject bounds, an authoritative
10 * containment test, and an optional clip.
11 *
12 * The three parts are independent, so any bounds function composes with any
13 * box. hits() applies them in one fixed order:
14 * - a point outside clip never hits;
15 * - a point outside bounds never hits, so bounds must circumscribe contains;
16 * - contains, when set, decides;
17 * - otherwise bounds alone decides, and a region with neither never hits.
18 *
19 * Dimension aliases follow the box family: HitRegion1D with AABB1D over a
20 * scalar coordinate, HitRegion2D with AABB2D, HitRegion3D with AABB3D.
21 *
22 * Usage:
23 * @code
24 * HitRegion2D disc {
25 * .bounds = AABB2D::from_ndc({ 0.0F, 0.0F }, { 0.3F, 0.3F }),
26 * .contains = circular_bounds({ 0.0F, 0.0F }, 0.3F),
27 * };
28 * bool inside = disc.hits({ 0.1F, 0.1F });
29 * @endcode
30 *
31 * @tparam Point Coordinate type: double, glm::vec2 or glm::vec3.
32 * @tparam Box Box type with contains(Point), as AABB1D, AABB2D and AABB3D have.
33 */
34template <typename Point, typename Box>
35struct HitRegion {
36 /// @brief Fast reject box; must circumscribe contains when both are set.
37 std::optional<Box> bounds;
38
39 /// @brief Authoritative containment test.
40 std::function<bool(Point)> contains;
41
42 /// @brief Region outside which nothing hits, regardless of the other parts.
43 std::optional<Box> clip;
44
45 /** @brief Whether a point hits this region. */
46 [[nodiscard]] bool hits(Point p) const
47 {
48 return test(clip, bounds, contains, p);
49 }
50
51 /**
52 * @brief hits() over three parts held elsewhere, without copying them.
53 *
54 * For owners that keep the clip, bounds and containment test as separate
55 * fields and cannot build a HitRegion per query.
56 */
57 [[nodiscard]] static bool test(const std::optional<Box>& clip,
58 const std::optional<Box>& bounds, const std::function<bool(Point)>& contains, Point p)
59 {
60 if (clip && !clip->contains(p))
61 return false;
62 if (bounds && !bounds->contains(p))
63 return false;
64 if (contains)
65 return contains(p);
66 return bounds.has_value();
67 }
68};
69
73
74}
HitRegion< double, AABB1D > HitRegion1D
Definition HitRegion.hpp:70
HitRegion< glm::vec3, AABB3D > HitRegion3D
Definition HitRegion.hpp:72
HitRegion< glm::vec2, AABB2D > HitRegion2D
Definition HitRegion.hpp:71
std::function< bool(double)> contains
Definition HitRegion.hpp:40
static bool test(const std::optional< Box > &clip, const std::optional< Box > &bounds, const std::function< bool(Point)> &contains, Point p)
hits() over three parts held elsewhere, without copying them.
Definition HitRegion.hpp:57
bool hits(Point p) const
Whether a point hits this region.
Definition HitRegion.hpp:46
A region a point can hit: fast reject bounds, an authoritative containment test, and an optional clip...
Definition HitRegion.hpp:35