MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ read_region()

template<typename Source, typename Access>
std::optional< std::function< bool(glm::vec2)> > MayaFlux::Portal::Forma::Detail::read_region ( const Source & node,
std::string & error )

Definition at line 91 of file ElementHit.hpp.

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
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 }
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
@ Kinesis
General mathematical and physics algorithns.
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)
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.

References MayaFlux::Kinesis::circular_bounds(), hit_fail(), MayaFlux::Kinesis::intersect_bounds(), MayaFlux::Portal::Forma::is_region_element(), only_hit_attributes(), MayaFlux::Kinesis::polygon_bounds(), read_hit_attribute(), read_region(), MayaFlux::Kinesis::stroke_bounds(), MayaFlux::Kinesis::subtract_bounds(), and MayaFlux::Kinesis::union_bounds().

Referenced by MayaFlux::Portal::Forma::read_hit_child(), and read_region().

Here is the call graph for this function:
Here is the caller graph for this function: