MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
StaticGeometry.cpp
Go to the documentation of this file.
1#include "StaticGeometry.hpp"
2
4
6
7namespace {
8
9 std::vector<uint8_t> polygon_bytes(std::span<const glm::vec2> ndc,
10 std::span<const uint32_t> starts,
11 std::span<const std::array<uint32_t, 3>> triangles,
12 std::span<const glm::vec4> colors, float thickness)
13 {
14 std::vector<uint8_t> out;
15
16 if (thickness > 0.F) {
17 std::vector<size_t> bounds { 0 };
18 bounds.insert(bounds.end(), starts.begin(), starts.end());
19 bounds.push_back(ndc.size());
20
21 std::vector<Kakshya::LineVertex> verts;
22 verts.reserve(ndc.size() * 2);
23 for (size_t ring = 0; ring + 1 < bounds.size(); ++ring) {
24 const size_t begin = bounds[ring];
25 const size_t count = bounds[ring + 1] - begin;
26 for (size_t k = 0; k < count; ++k) {
27 const size_t from = begin + k;
28 const size_t to = begin + (k + 1) % count;
29 verts.push_back(Kakshya::LineVertex {
30 .position = { ndc[from].x, ndc[from].y, 0.F },
31 .color = colors[from],
32 .thickness = thickness,
33 });
34 verts.push_back(Kakshya::LineVertex {
35 .position = { ndc[to].x, ndc[to].y, 0.F },
36 .color = colors[to],
37 .thickness = thickness,
38 });
39 }
40 }
41 Geometry::write_verts(out, verts);
42 return out;
43 }
44
45 std::vector<Kakshya::Vertex> verts;
46 verts.reserve(triangles.size() * 3);
47 for (const auto& triangle : triangles) {
48 for (const uint32_t k : triangle) {
49 verts.push_back(Kakshya::Vertex {
50 .position = { ndc[k].x, ndc[k].y, 0.F },
51 .color = colors[k],
52 });
53 }
54 }
56 return out;
57 }
58
59}
60
62{
63 std::vector<glm::vec2> positions;
64 positions.reserve(input.vertices.size());
65 for (const Kakshya::Vertex& vertex : input.vertices)
66 positions.emplace_back(vertex.position.x, vertex.position.y);
67
68 const Containment hit = containment_of(positions, input.topology);
69 const size_t capacity = input.vertices.size() * sizeof(Kakshya::Vertex);
71 [vertices = input.vertices](const std::monostate&, std::vector<uint8_t>& out) {
72 Geometry::write_verts(out, vertices);
73 },
74 hit);
75 return Geometry::Form<std::monostate> { std::move(fn), input.topology, capacity };
76}
77
78std::optional<Geometry::Form<std::monostate>> vertex_geometry_form(
79 const VertexGeometryInputs& input, const RectPlacementInputs& placement, std::string& error)
80{
81 const auto region = region_of(placement, error);
82 if (!region)
83 return std::nullopt;
84
85 VertexGeometryInputs mapped { .topology = input.topology, .vertices = input.vertices };
86 for (Kakshya::Vertex& vertex : mapped.vertices) {
87 const glm::vec2 ndc = region->point_at({ vertex.position.x, vertex.position.y });
88 vertex.position = { ndc.x, ndc.y, vertex.position.z };
89 }
90 return vertex_form(mapped);
91}
92
93std::optional<Geometry::Form<bool>> PolygonFactory::operator()(
94 const PolygonInputs& input, const RectPlacementInputs& placement, std::string& error) const
95{
96 const bool per_vertex = !input.colors.empty();
97 if (input.points.size() < 3) {
98 error = "points needs at least three x y pairs";
99 return std::nullopt;
100 }
101 if (per_vertex && input.colors.size() != input.points.size()) {
102 error = "colors needs one r g b a per point";
103 return std::nullopt;
104 }
105 if (!(input.thickness >= 0.F)) {
106 error = "thickness may not be negative";
107 return std::nullopt;
108 }
109
110 std::vector<glm::vec2> unit = input.points;
111 std::vector<glm::vec4> vertex_colors = input.colors;
112 std::vector<uint32_t> starts;
113 for (const PolygonHole& hole : input.holes) {
114 if (hole.points.size() < 3) {
115 error = "a hole needs at least three x y pairs";
116 return std::nullopt;
117 }
118 if (per_vertex ? hole.colors.size() != hole.points.size() : !hole.colors.empty()) {
119 error = "a hole needs one r g b a per point exactly when the outline has colors";
120 return std::nullopt;
121 }
122 starts.push_back(static_cast<uint32_t>(unit.size()));
123 unit.insert(unit.end(), hole.points.begin(), hole.points.end());
124 vertex_colors.insert(vertex_colors.end(), hole.colors.begin(), hole.colors.end());
125 }
126 if (!per_vertex)
127 vertex_colors.assign(unit.size(), input.color);
128
129 const auto region = region_of(placement, error);
130 if (!region)
131 return std::nullopt;
132
133 std::vector<glm::vec2> ndc;
134 ndc.reserve(unit.size());
135 for (const glm::vec2 point : unit)
136 ndc.push_back(region->point_at(point));
137
138 const bool stroke = input.thickness > 0.F;
139 std::vector<std::array<uint32_t, 3>> triangles;
140 if (!stroke) {
141 triangles = Kinesis::triangulate_polygon(unit, starts);
142 if (triangles.empty()) {
143 error = "the outline and holes do not form a valid region";
144 return std::nullopt;
145 }
146 }
147
148 const std::vector<uint8_t> base = polygon_bytes(ndc, starts, triangles, vertex_colors, input.thickness);
149 std::vector<uint8_t> active = base;
150 if (input.active) {
151 const std::vector<glm::vec4> active_colors(unit.size(), *input.active);
152 active = polygon_bytes(ndc, starts, triangles, active_colors, input.thickness);
153 }
154
155 using Topology = Graphics::PrimitiveTopology;
156 const Topology topology = stroke ? Topology::LINE_LIST : Topology::TRIANGLE_LIST;
157 std::vector<glm::vec2> drawn;
158 if (stroke) {
159 drawn = ndc;
160 } else {
161 drawn.reserve(triangles.size() * 3);
162 for (const auto& triangle : triangles) {
163 for (const uint32_t k : triangle)
164 drawn.push_back(ndc[k]);
165 }
166 }
167
168 const auto hit = containments.resolve_or(input.containment,
169 ContainmentSource { .positions = drawn, .topology = topology, .placement = region },
170 containment_of(drawn, topology), error);
171 if (!hit)
172 return std::nullopt;
173
174 const size_t capacity = stroke
175 ? ndc.size() * 2 * sizeof(Kakshya::LineVertex)
176 : triangles.size() * 3 * sizeof(Kakshya::MeshVertex);
178 [base, active](const bool& on, std::vector<uint8_t>& out) { out = on ? active : base; },
179 *hit);
180
181 Geometry::Form<bool> form { std::move(fn), topology, capacity };
182 if (!interactions.attach(form, input.interaction, region, error))
183 return std::nullopt;
184 return form;
185}
186
187}
std::vector< MeshVertex > to_mesh_vertices(std::span< const Vertex > vertices, glm::vec2 weight_range)
Batch-project raw Vertex vector to MeshVertex.
std::vector< double > count(std::span< const double > data, size_t n_windows, uint32_t hop_size, uint32_t window_size)
Sample count per window (as double for pipeline uniformity).
Definition Analysis.cpp:506
std::vector< std::array< uint32_t, 3 > > triangulate_polygon(std::span< const glm::vec2 > pts, std::span< const uint32_t > hole_starts)
Ear-clipping triangulation of a polygon with optional holes, as index triples.
void write_verts(std::vector< uint8_t > &out, const std::vector< V > &verts)
Write a vertex array into a GeometryFn output buffer.
Definition Geometry.hpp:35
std::optional< Kinesis::AABB2D > region_of(const RectPlacementInputs &placement, std::string &error)
The surface region a rect placement covers.
Geometry::Form< std::monostate > vertex_form(const VertexGeometryInputs &input)
A Form with no state that draws ordered vertices exactly as written.
GeometryFn< T > geometry_with_containment(std::function< void(const T &, std::vector< uint8_t > &)> draw, Containment region)
A geometry function that draws a value and claims a fixed containment.
std::optional< Geometry::Form< std::monostate > > vertex_geometry_form(const VertexGeometryInputs &input, const RectPlacementInputs &placement, std::string &error)
A Form with no state that draws ordered vertices.
Kinesis::HitRegion2D Containment
The spatial fields of an Element as one value: Kinesis::HitRegion2D.
Containment containment_of(std::span< const glm::vec2 > positions, Graphics::PrimitiveTopology topology)
Hit region derived from placed vertex positions.
PrimitiveTopology
Vertex assembly primitive topology.
Vertex type for line primitives (LINE_LIST / LINE_STRIP topology).
Vertex type for indexed triangle mesh primitives (TRIANGLE_LIST topology).
Type-neutral vertex carrying the universal 68-byte attribute layout.
What a hit region provider may use: the placed geometry and, when the placement has one,...
A geometry function plus everything its realization requires.
Definition Geometry.hpp:83
std::optional< Geometry::Form< bool > > operator()(const PolygonInputs &input, const RectPlacementInputs &placement, std::string &error) const
One inner ring cut out of an outline, with optional per point colors.
An outline in unit space, filled or stroked, with a bool state.
A placement by origin and size: x, y, width, height.
A topology and ordered vertices in the Kakshya::Vertex field format.