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) {
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
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 },
171 if (!hit)
172 return std::nullopt;
173
174 const size_t capacity = stroke
175 ? ndc.size() * 2 * sizeof(Kakshya::LineVertex)
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}
void error(Component component, Context context, std::source_location location, std::string_view message)
Log an error message and optionally throw an exception.
@ Kakshya
Containers[Signalsource, Stream, File], Regions, DataProcessors.
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.
Topology
Defines the structural relationships between nodes in the network.
Kakshya::MeshVertex MeshVertex
PrimitiveTopology
Vertex assembly primitive topology.