MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ElementHit.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "BoundsText.hpp"
4
8
10
11/**
12 * @struct ElementHit
13 * @brief The spatial fields a document gives an element, as Element carries them.
14 *
15 * bounds is Element::bounds_hint, the fast reject box, and contains is Element::contains, the
16 * authoritative test. Either may be empty, and an empty one leaves what the form wrote.
17 */
18struct ElementHit {
19 std::optional<Kinesis::AABB2D> bounds;
20 std::function<bool(glm::vec2)> contains;
21};
22
23/** @brief Whether an element of this name is a region: circle, polygon, stroke or a combination of two. */
24[[nodiscard]] inline bool is_region_element(std::string_view name)
25{
26 return name == "circle" || name == "polygon" || name == "stroke" || name == "union_bounds"
27 || name == "intersect_bounds" || name == "subtract_bounds";
28}
29
30/** @brief Whether an element of this name sets spatial fields: bounds, or a region. */
31[[nodiscard]] inline bool is_hit_child(std::string_view name)
32{
33 return name == "bounds" || is_region_element(name);
34}
35
36/**
37 * @brief Write a document's spatial fields after a form's own.
38 *
39 * A form's geometry function writes bounds_hint and contains on every draw, so the document's
40 * are applied after it, as Forma::geometry_with_containment does for a form's own.
41 */
42template <typename T>
43void apply_hit(Geometry::Form<T>& form, const ElementHit& hit)
44{
45 if (!hit.bounds && !hit.contains)
46 return;
47 form.geometry = [inner = std::move(form.geometry), hit](T value, std::vector<uint8_t>& out, Element& element) {
48 inner(std::move(value), out, element);
49 if (hit.bounds)
50 element.bounds_hint = hit.bounds;
51 if (hit.contains)
52 element.contains = hit.contains;
53 };
54}
55
56namespace Detail {
57
58 template <typename Source, typename Access>
59 bool hit_fail(const Source& at, const std::string& message, std::string& error)
60 {
61 error = Reflect::located_message(Access::location(at),
62 "<" + std::string(Access::name(at)) + "> " + message);
63 return false;
64 }
65
66 template <typename Source, typename Access, typename T>
67 bool read_hit_attribute(const Source& node, std::string_view name, T& value, std::string& error)
68 {
69 const auto text = Access::attribute(node, name);
70 if (!text)
71 return hit_fail<Source, Access>(node, "needs a " + std::string(name) + " attribute", error);
73 if (!Reflect::TextCodec<T>::decode(*text, value, reason)) {
74 return hit_fail<Source, Access>(node,
75 "has an invalid " + std::string(name) + " of " + std::string(*text), error);
76 }
77 return true;
78 }
79
80 template <typename Source, typename Access>
81 bool only_hit_attributes(const Source& node, std::initializer_list<std::string_view> allowed, std::string& error)
82 {
83 return Access::for_each_attribute(node, [&](std::string_view name, std::string_view) {
84 if (std::ranges::find(allowed, name) != allowed.end())
85 return true;
86 return hit_fail<Source, Access>(node, "has an unsupported attribute " + std::string(name), error);
87 });
88 }
89
90 template <typename Source, typename Access>
91 std::optional<std::function<bool(glm::vec2)>> read_region(const Source& node, std::string& error)
92 {
93 using Region = std::function<bool(glm::vec2)>;
94 const std::string_view kind = Access::name(node);
95
96 if (kind == "circle") {
97 glm::vec2 center {};
98 float radius {};
99 if (!only_hit_attributes<Source, Access>(node, { "center", "radius" }, error)
100 || !read_hit_attribute<Source, Access>(node, "center", center, error)
101 || !read_hit_attribute<Source, Access>(node, "radius", radius, error)) {
102 return std::nullopt;
103 }
104 if (!(radius > 0.F)) {
105 hit_fail<Source, Access>(node, "needs a positive radius", error);
106 return std::nullopt;
107 }
108 return Kinesis::circular_bounds(center, radius);
109 }
110
111 if (kind == "polygon" || kind == "stroke") {
112 const bool stroke = kind == "stroke";
113 std::vector<glm::vec2> points;
114 float half_thickness {};
115 const bool read = stroke
116 ? only_hit_attributes<Source, Access>(node, { "points", "half_thickness" }, error)
117 && read_hit_attribute<Source, Access>(node, "half_thickness", half_thickness, error)
118 : only_hit_attributes<Source, Access>(node, { "points" }, error);
119 if (!read || !read_hit_attribute<Source, Access>(node, "points", points, error))
120 return std::nullopt;
121 if (points.size() < (stroke ? 2U : 3U)) {
123 stroke ? "needs at least two x y pairs" : "needs at least three x y pairs", error);
124 return std::nullopt;
125 }
126 if (stroke && !(half_thickness > 0.F)) {
127 hit_fail<Source, Access>(node, "needs a positive half_thickness", error);
128 return std::nullopt;
129 }
130 return stroke ? Kinesis::stroke_bounds(points, half_thickness) : Kinesis::polygon_bounds(points);
131 }
132
133 if (!only_hit_attributes<Source, Access>(node, {}, error))
134 return std::nullopt;
135 std::vector<Region> operands;
136 const bool traversed = Access::for_each_child(node, [&](const Source& child) {
137 if (!is_region_element(Access::name(child))) {
138 return hit_fail<Source, Access>(child, "is not a region, and <" + std::string(kind)
139 + "> combines two regions", error);
140 }
141 auto region = read_region<Source, Access>(child, error);
142 if (!region)
143 return false;
144 operands.push_back(std::move(*region));
145 return true;
146 });
147 if (!traversed)
148 return std::nullopt;
149 if (operands.size() != 2) {
150 hit_fail<Source, Access>(node, "needs exactly two regions", error);
151 return std::nullopt;
152 }
153 if (kind == "union_bounds")
154 return Kinesis::union_bounds(std::move(operands[0]), std::move(operands[1]));
155 if (kind == "intersect_bounds")
156 return Kinesis::intersect_bounds(std::move(operands[0]), std::move(operands[1]));
157 return Kinesis::subtract_bounds(std::move(operands[0]), std::move(operands[1]));
158 }
159
160}
161
162/**
163 * @brief Read one spatial child of an element into @p hit.
164 *
165 * bounds is Element::with_bounds, with min and max. A region is Element::with_contains over the
166 * Kinesis function of the same name: circle (center, radius), polygon (points), stroke (points,
167 * half_thickness), and union_bounds, intersect_bounds and subtract_bounds, each of two
168 * regions. An element has at most one bounds and one region.
169 *
170 * @param error Receives a located message when false is returned.
171 */
172template <typename Source, typename Access = Reflect::RecordAccess<Source>>
173[[nodiscard]] bool read_hit_child(const Source& child, ElementHit& hit, std::string& error)
174{
175 if (Access::name(child) == "bounds") {
176 if (hit.bounds)
177 return Detail::hit_fail<Source, Access>(child, "may appear only once in an <element>", error);
178 Kinesis::AABB2D box {};
179 if (!Detail::only_hit_attributes<Source, Access>(child, { "min", "max" }, error)
180 || !Detail::read_hit_attribute<Source, Access>(child, "min", box.min, error)
181 || !Detail::read_hit_attribute<Source, Access>(child, "max", box.max, error)) {
182 return false;
183 }
184 if (!(box.max.x > box.min.x) || !(box.max.y > box.min.y))
185 return Detail::hit_fail<Source, Access>(child, "needs a max above its min", error);
186 hit.bounds = box;
187 return true;
188 }
189
190 if (hit.contains) {
192 "is a second region, and an <element> has only one", error);
193 }
194 auto region = Detail::read_region<Source, Access>(child, error);
195 if (!region)
196 return false;
197 hit.contains = std::move(*region);
198 return true;
199}
200
201}
Illustrative geometry functions for common Mapped use cases.
std::function< bool(glm::vec2)> intersect_bounds(std::function< bool(glm::vec2)> a, std::function< bool(glm::vec2)> b)
Intersection of two containment tests.
Definition Bounds.hpp:485
std::function< bool(glm::vec2)> polygon_bounds(std::span< const glm::vec2 > vertices)
Containment test for a convex or concave polygon.
Definition Bounds.hpp:302
std::function< bool(glm::vec2)> subtract_bounds(std::function< bool(glm::vec2)> a, std::function< bool(glm::vec2)> b)
Difference of two containment tests.
Definition Bounds.hpp:499
std::function< bool(glm::vec2)> stroke_bounds(std::span< const glm::vec2 > points, float half_thickness)
Containment test for a polyline with a uniform half-thickness.
Definition Bounds.hpp:342
std::function< bool(glm::vec2)> circular_bounds(glm::vec2 center, float radius) noexcept
Containment test for a circle.
Definition Bounds.hpp:283
std::function< bool(glm::vec2)> union_bounds(std::function< bool(glm::vec2)> a, std::function< bool(glm::vec2)> b)
Union of two containment tests.
Definition Bounds.hpp:472
std::optional< std::function< bool(glm::vec2)> > read_region(const Source &node, std::string &error)
bool hit_fail(const Source &at, const std::string &message, std::string &error)
bool only_hit_attributes(const Source &node, std::initializer_list< std::string_view > allowed, std::string &error)
bool read_hit_attribute(const Source &node, std::string_view name, T &value, std::string &error)
void apply_hit(Geometry::Form< T > &form, const ElementHit &hit)
Write a document's spatial fields after a form's own.
bool read_hit_child(const Source &child, ElementHit &hit, std::string &error)
Read one spatial child of an element into hit.
bool is_region_element(std::string_view name)
Whether an element of this name is a region: circle, polygon, stroke or a combination of two.
bool is_hit_child(std::string_view name)
Whether an element of this name sets spatial fields: bounds, or a region.
std::string located_message(uint64_t location, std::string_view message)
A diagnostic message prefixed with the location of the element it concerns.
Axis-aligned bounding rectangle in a 2D coordinate space.
Definition Bounds.hpp:63
std::optional< Kinesis::AABB2D > bounds
std::function< bool(glm::vec2)> contains
The spatial fields a document gives an element, as Element carries them.
A bounded, renderable region on a window surface.
Definition Element.hpp:62
GeometryFn< T > geometry
Writes vertex bytes for the current value.
Definition Geometry.hpp:85
A geometry function plus everything its realization requires.
Definition Geometry.hpp:83
Conversion of authored text to one value type.
Definition TextCodec.hpp:81