MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SeriesBuilder.cpp
Go to the documentation of this file.
1#include "SeriesBuilder.hpp"
2
4
6
7// =============================================================================
8// Internal helpers
9// =============================================================================
10
11namespace {
12
14
15 /**
16 * @brief Collect all series from the container matching any role in @p mapping.
17 */
18 std::vector<std::span<const double>> series_for_mapping(
19 const Kakshya::PlotContainer& container,
20 const Series::AxisMapping& mapping)
21 {
22 std::vector<std::span<const double>> result;
23 for (const auto& role : mapping.roles) {
24 auto s = series_by_role(container, role);
25 result.insert(result.end(), s.begin(), s.end());
26 }
27 return result;
28 }
29
30 /**
31 * @brief Flatten all AxisMappings into a single merged AxisRange.
32 */
33 AxisRange merge_axis(std::vector<Series::AxisMapping>& mappings)
34 {
35 if (mappings.empty())
36 return {};
37 if (mappings.size() == 1)
38 return mappings[0].range;
39
40 AxisRange merged = mappings[0].range;
41 for (size_t i = 1; i < mappings.size(); ++i) {
42 const auto& r = mappings[i].range;
43 merged.min = std::min(merged.min, r.min);
44 merged.max = std::max(merged.max, r.max);
45 if (r.auto_scaling)
46 merged.auto_scaling = true;
47 if (!merged.scale_predicate && r.scale_predicate)
48 merged.scale_predicate = r.scale_predicate;
49 }
50 return merged;
51 }
52
53 AxisRange merge_axis_const(const std::vector<Series::AxisMapping>& mappings)
54 {
55 auto copy = mappings;
56 return merge_axis(copy);
57 }
58
59 /**
60 * @brief Resolve color for a series index within a mapping.
61 */
62 glm::vec3 resolve_color(
63 const Series::AxisMapping& mapping,
64 const std::vector<glm::vec3>& global_palette,
65 size_t series_index)
66 {
67 if (!mapping.palette.empty())
68 return palette_color(mapping.palette, series_index);
69 return palette_color(global_palette, series_index);
70 }
71
72 std::vector<float> uniform_x(size_t n, const AxisRange& x_range)
73 {
74 std::vector<float> xs(n);
75 if (n == 1) {
76 xs[0] = x_range.to_ndc((x_range.min + x_range.max) * 0.5F);
77 return xs;
78 }
79 for (size_t i = 0; i < n; ++i) {
80 const float t = static_cast<float>(i) / static_cast<float>(n - 1);
81 xs[i] = x_range.to_ndc(x_range.min + t * (x_range.max - x_range.min));
82 }
83 return xs;
84 }
85
86 /** Vertex field offsets — derived from VertexLayout, never hardcoded.
87 * All three vertex types share the 60-byte layout:
88 * offset 0 — position vec3 (12)
89 * offset 12 — color vec3 (12)
90 * offset 24 — scalar float (4) [size / thickness / weight]
91 * offset 28 — uv vec2 (8)
92 * offset 36 — normal vec3 (12)
93 * offset 48 — tangent vec3 (12)
94 **/
95
96 constexpr uint32_t k_stride = 60;
97 constexpr uint32_t k_off_pos = 0;
98 constexpr uint32_t k_off_color = 12;
99 constexpr uint32_t k_off_scalar = 24;
100
101 constexpr glm::vec3 k_default_normal { 0.F, 0.F, 1.F };
102 constexpr glm::vec3 k_default_tangent { 1.F, 0.F, 0.F };
103 constexpr glm::vec2 k_default_uv { 0.F, 0.F };
104
105 void write_vertex(
106 std::vector<uint8_t>& out,
107 glm::vec3 pos,
108 glm::vec3 color,
109 float scalar)
110 {
111 const size_t base = out.size();
112 out.resize(base + k_stride, 0);
113 uint8_t* v = out.data() + base;
114 std::memcpy(v + k_off_pos, &pos, 12);
115 std::memcpy(v + k_off_color, &color, 12);
116 std::memcpy(v + k_off_scalar, &scalar, 4);
117 std::memcpy(v + 28, &k_default_uv, 8);
118 std::memcpy(v + 36, &k_default_normal, 12);
119 std::memcpy(v + 48, &k_default_tangent, 12);
120 }
121
122} // namespace
123
124// =============================================================================
125// Series adornment resolution
126// =============================================================================
127
128std::vector<TickLabelsSpec> Series::resolved_tick_labels() const
129{
130 std::vector<TickLabelsSpec> resolved;
131 if (!m_plot_bounds)
132 return resolved;
133
134 resolved.reserve(m_ticks.size());
135
136 for (const auto& req : m_ticks) {
137 AxisRange range {};
138 if (req.range) {
139 range = *req.range;
140 } else {
141 switch (req.axis) {
142 case TickAxis::X:
143 range = merge_axis_const(m_x);
144 break;
145 case TickAxis::Y:
146 range = merge_axis_const(m_y);
147 break;
149 default:
150 range = {};
151 break;
152 }
153 }
154
155 resolved.push_back(TickLabelsSpec {
157 .range = std::move(range),
158 .count = req.count,
159 .edge = req.edge,
160 .color = req.color,
161 .decimal_places = req.decimal_places,
162 .label_h = req.label_h,
163 .label_w = req.label_w,
164 .name_prefix = req.name_prefix,
165 });
166 }
167
168 return resolved;
169}
170
171// =============================================================================
172// WaveformBuilder::done
173// =============================================================================
174
176{
177 auto x_mappings = m_state.x_mappings();
178 auto y_mappings = m_state.y_mappings();
179 auto z_mappings = m_state.z_mappings();
180 const auto global_palette = m_state.palette();
181 const float thickness = m_thickness;
182
183 return {
184 .fn = [x_mappings, y_mappings, z_mappings, global_palette, thickness](
185 const std::shared_ptr<Kakshya::PlotContainer>& container,
186 std::vector<uint8_t>& out,
187 Element&) mutable {
188 if (!container)
189 return;
190
191 container->process_default();
192
193 struct SeriesEntry {
194 std::span<const double> data;
195 size_t mapping_index;
196 size_t index_within_mapping;
197 };
198
199 std::vector<SeriesEntry> y_entries;
200 for (size_t mi = 0; mi < y_mappings.size(); ++mi) {
201 auto series = series_for_mapping(*container, y_mappings[mi]);
202 for (size_t si = 0; si < series.size(); ++si)
203 y_entries.push_back({ .data = series[si], .mapping_index = mi, .index_within_mapping = si });
204 }
205 if (y_entries.empty())
206 return;
207
208 AxisRange x_range = merge_axis(x_mappings);
209 AxisRange z_range = z_mappings.empty() ? AxisRange {} : merge_axis(z_mappings);
210
211 std::vector<std::span<const double>> all_x, all_z;
212 for (auto& m : x_mappings) {
213 auto s = series_for_mapping(*container, m);
214 all_x.insert(all_x.end(), s.begin(), s.end());
215 }
216 for (auto& m : z_mappings) {
217 auto s = series_for_mapping(*container, m);
218 all_z.insert(all_z.end(), s.begin(), s.end());
219 }
220
221 apply_auto_scale(x_range, all_x.empty() ? std::vector<std::span<const double>> { y_entries[0].data } : all_x);
222 apply_auto_scale(z_range, all_z);
223
224 out.clear();
225
226 for (size_t ei = 0; ei < y_entries.size(); ++ei) {
227 const auto& entry = y_entries[ei];
228 const auto& ys = entry.data;
229 const size_t n = ys.size();
230 if (n == 0)
231 continue;
232
233 const glm::vec3 color = resolve_color(
234 y_mappings[entry.mapping_index], global_palette, entry.index_within_mapping);
235
236 auto y_range = y_mappings[entry.mapping_index].range;
237 apply_auto_scale(y_range, { ys });
238
239 const bool has_x = ei < all_x.size() && all_x[ei].size() == n;
240 const bool has_z = ei < all_z.size() && all_z[ei].size() == n;
241
242 const std::vector<float> xs = has_x
243 ? [&] {
244 std::vector<float> v;
245 v.reserve(n);
246 for (double val : all_x[ei])
247 v.push_back(x_range.to_ndc(static_cast<float>(val)));
248 return v;
249 }()
250 : uniform_x(n, x_range);
251
252 for (size_t i = 0; i < n; ++i) {
253 write_vertex(out,
254 { xs[i],
255 y_range.to_ndc(static_cast<float>(ys[i])),
256 has_z ? z_range.to_ndc(static_cast<float>(all_z[ei][i])) : 0.F },
257 color,
258 thickness);
259 }
260
261 if (ei + 1 < y_entries.size()) {
262 const glm::vec3 sep_pos {
263 xs[n - 1],
264 y_range.to_ndc(static_cast<float>(ys[n - 1])),
265 has_z ? z_range.to_ndc(static_cast<float>(all_z[ei][n - 1])) : 0.F,
266 };
267 write_vertex(out, sep_pos, color, 0.F);
268 write_vertex(out, sep_pos, color, 0.F);
269 }
270 } },
272 .capacity_for = [](uint64_t n) { return (n + 2) * k_stride; },
273 .background_fn = m_state.has_background()
274 ? std::optional<GeometryFn<float>> { background(m_state.background_bounds(), m_state.background_color()) }
275 : std::nullopt,
276 .plot_bounds = m_state.plot_bounds(),
277 .labels = m_state.labels(),
278 .tick_labels = m_state.resolved_tick_labels(),
279 .legend = m_state.legend_spec(),
280 };
281}
282
283// =============================================================================
284// ScatterBuilder::done
285// =============================================================================
286
288{
289 auto x_mappings = m_state.x_mappings();
290 auto y_mappings = m_state.y_mappings();
291 auto z_mappings = m_state.z_mappings();
292 const auto global_palette = m_state.palette();
293 const float point_size = m_point_size;
294
295 return {
296 .fn = [x_mappings, y_mappings, z_mappings, global_palette, point_size](
297 const std::shared_ptr<Kakshya::PlotContainer>& container,
298 std::vector<uint8_t>& out,
299 Element&) mutable {
300 if (!container)
301 return;
302
303 container->process_default();
304
305 std::vector<std::span<const double>> all_x, all_z;
306 for (auto& m : x_mappings) {
307 auto s = series_for_mapping(*container, m);
308 all_x.insert(all_x.end(), s.begin(), s.end());
309 }
310 for (auto& m : z_mappings) {
311 auto s = series_for_mapping(*container, m);
312 all_z.insert(all_z.end(), s.begin(), s.end());
313 }
314
315 AxisRange x_range = merge_axis(x_mappings);
316 AxisRange z_range = z_mappings.empty() ? AxisRange {} : merge_axis(z_mappings);
317 apply_auto_scale(x_range, all_x);
318 apply_auto_scale(z_range, all_z);
319
320 out.clear();
321
322 size_t global_idx = 0;
323 for (auto& y_mapping : y_mappings) {
324 auto y_series = series_for_mapping(*container, y_mapping);
325 auto y_range = y_mapping.range;
326 apply_auto_scale(y_range, y_series);
327
328 for (size_t si = 0; si < y_series.size(); ++si, ++global_idx) {
329 const auto& ys = y_series[si];
330 const size_t n = ys.size();
331 if (n == 0)
332 continue;
333
334 const glm::vec3 color = resolve_color(y_mapping, global_palette, si);
335 const size_t x_idx = std::min(global_idx, all_x.empty() ? size_t(0) : all_x.size() - 1);
336 const bool has_x = !all_x.empty() && all_x[x_idx].size() == n;
337 const bool has_z = global_idx < all_z.size() && all_z[global_idx].size() == n;
338
339 for (size_t i = 0; i < n; ++i) {
340 const float px = has_x
341 ? x_range.to_ndc(static_cast<float>(all_x[x_idx][i]))
342 : x_range.to_ndc(x_range.min
343 + static_cast<float>(i) / static_cast<float>(std::max<size_t>(n - 1, 1))
344 * (x_range.max - x_range.min));
345
346 write_vertex(out,
347 { px,
348 y_range.to_ndc(static_cast<float>(ys[i])),
349 has_z ? z_range.to_ndc(static_cast<float>(all_z[global_idx][i])) : 0.F },
350 color,
351 point_size);
352 }
353 }
354 } },
356 .capacity_for = [](uint64_t n) { return n * k_stride; },
357 .background_fn = m_state.has_background()
358 ? std::optional<GeometryFn<float>> { background(m_state.background_bounds(), m_state.background_color()) }
359 : std::nullopt,
360 .plot_bounds = m_state.plot_bounds(),
361 .labels = m_state.labels(),
362 .tick_labels = m_state.resolved_tick_labels(),
363 .legend = m_state.legend_spec(),
364 };
365}
366
367// =============================================================================
368// BarsBuilder::done
369// =============================================================================
370
372{
373 auto x_mappings = m_state.x_mappings();
374 auto y_mappings = m_state.y_mappings();
375 const auto global_palette = m_state.palette();
376
377 return {
378 .fn = [x_mappings, y_mappings, global_palette](
379 const std::shared_ptr<Kakshya::PlotContainer>& container,
380 std::vector<uint8_t>& out,
381 Element&) mutable {
382 if (!container)
383 return;
384
385 container->process_default();
386
387 AxisRange x_range = x_mappings.empty() ? AxisRange {} : merge_axis(x_mappings);
388 out.clear();
389
390 for (auto& y_mapping : y_mappings) {
391 auto y_series = series_for_mapping(*container, y_mapping);
392 if (y_series.empty())
393 continue;
394
395 auto y_range = y_mapping.range;
396 apply_auto_scale(y_range, y_series);
397
398 for (size_t si = 0; si < y_series.size(); ++si) {
399 const auto& series = y_series[si];
400 const size_t n = series.size();
401 if (n == 0)
402 continue;
403
404 const glm::vec3 color = resolve_color(y_mapping, global_palette, si);
405 const float bar_w = (x_range.max - x_range.min) / static_cast<float>(n);
406 const float y_base = y_range.to_ndc(0.F);
407
408 for (size_t i = 0; i < n; ++i) {
409 const float x_left = x_range.to_ndc(x_range.min + static_cast<float>(i) * bar_w);
410 const float x_right = x_range.to_ndc(x_range.min + static_cast<float>(i + 1) * bar_w);
411 const float y_top = y_range.to_ndc(static_cast<float>(series[i]));
412
413 write_vertex(out, { x_left, y_base, 0.F }, color, 0.F);
414 write_vertex(out, { x_right, y_base, 0.F }, color, 0.F);
415 write_vertex(out, { x_left, y_top, 0.F }, color, 0.F);
416 write_vertex(out, { x_right, y_base, 0.F }, color, 0.F);
417 write_vertex(out, { x_right, y_top, 0.F }, color, 0.F);
418 write_vertex(out, { x_left, y_top, 0.F }, color, 0.F);
419 }
420 }
421 } },
423 .capacity_for = [](uint64_t n) { return n * 6 * k_stride; },
424 .background_fn = m_state.has_background()
425 ? std::optional<GeometryFn<float>> { background(m_state.background_bounds(), m_state.background_color()) }
426 : std::nullopt,
427 .plot_bounds = m_state.plot_bounds(),
428 .labels = m_state.labels(),
429 .tick_labels = m_state.resolved_tick_labels(),
430 .legend = m_state.legend_spec(),
431 };
432}
433
435{
436 auto x_mappings = m_state.x_mappings();
437 auto y_mappings = m_state.y_mappings();
438 const auto global_palette = m_state.palette();
439 const float baseline_data = m_baseline;
440
441 return {
442 .fn = [x_mappings, y_mappings, global_palette, baseline_data](
443 const std::shared_ptr<Kakshya::PlotContainer>& container,
444 std::vector<uint8_t>& out,
445 Element&) mutable {
446 if (!container)
447 return;
448
449 container->process_default();
450
451 struct SeriesEntry {
452 std::span<const double> data;
453 size_t mapping_index;
454 size_t index_within_mapping;
455 };
456 std::vector<SeriesEntry> y_entries;
457 for (size_t mi = 0; mi < y_mappings.size(); ++mi) {
458 auto sv = series_for_mapping(*container, y_mappings[mi]);
459 for (size_t si = 0; si < sv.size(); ++si)
460 y_entries.push_back({ .data = sv[si], .mapping_index = mi, .index_within_mapping = si });
461 }
462 if (y_entries.empty())
463 return;
464
465 AxisRange x_range = merge_axis(x_mappings);
466 AxisRange y_range = merge_axis(y_mappings);
467
468 std::vector<std::span<const double>> all_y;
469 all_y.reserve(y_entries.size());
470 for (const auto& e : y_entries)
471 all_y.push_back(e.data);
472 apply_auto_scale(y_range, all_y);
473
474 std::vector<std::span<const double>> all_x;
475 if (!x_mappings.empty()) {
476 all_x = series_for_mapping(*container, x_mappings[0]);
477 apply_auto_scale(x_range, all_x);
478 } else {
479 x_range = AxisRange {}.range(-1.F, 1.F);
480 }
481
482 const float baseline_ndc = y_range.to_ndc(baseline_data);
483
484 out.clear();
485
486 for (size_t ei = 0; ei < y_entries.size(); ++ei) {
487 const auto& entry = y_entries[ei];
488 const size_t n = entry.data.size();
489 if (n == 0)
490 continue;
491
492 const glm::vec3 color = resolve_color(
493 y_mappings[entry.mapping_index], global_palette, entry.index_within_mapping);
494
495 const size_t x_idx = std::min(ei, all_x.empty() ? size_t(0) : all_x.size() - 1);
496 const bool has_x = !all_x.empty() && all_x[x_idx].size() == n;
497
498 const size_t pre_size = out.size();
499
500 for (size_t i = 0; i < n; ++i) {
501 const float px = has_x
502 ? x_range.to_ndc(static_cast<float>(all_x[x_idx][i]))
503 : x_range.to_ndc(x_range.min
504 + static_cast<float>(i) / static_cast<float>(std::max<size_t>(n - 1, 1))
505 * (x_range.max - x_range.min));
506
507 const float py = y_range.to_ndc(static_cast<float>(entry.data[i]));
508
509 write_vertex(out, { px, py, 0.F }, color, 1.F);
510 write_vertex(out, { px, baseline_ndc, 0.F }, color, 0.F);
511 }
512
513 if (ei + 1 < y_entries.size() && out.size() > pre_size) {
514 const size_t last = out.size() - k_stride;
515 out.insert(out.end(), out.begin() + static_cast<ptrdiff_t>(last), out.end());
516 }
517 } },
519 .capacity_for = [](uint64_t n) { return n * 2 * k_stride + 128; },
520 .background_fn = m_state.has_background()
521 ? std::optional<GeometryFn<float>> { background(
523 : std::nullopt,
524 .plot_bounds = m_state.plot_bounds(),
525 .labels = m_state.labels(),
526 .tick_labels = m_state.resolved_tick_labels(),
527 .legend = m_state.legend_spec(),
528 };
529}
530
531} // namespace MayaFlux::Portal::Forma::Plot
ScatterBuilder & point_size(float s)
Point size in pixels.
const std::vector< LabelSpec > & labels() const
std::vector< TickLabelsSpec > resolved_tick_labels() const
Resolve pending fluent tick requests into concrete tick label specs.
Kinesis::AABB2D background_bounds() const
const std::vector< AxisMapping > & z_mappings() const
const std::vector< AxisMapping > & x_mappings() const
const std::optional< LegendSpec > & legend_spec() const
const std::optional< Kinesis::AABB2D > & plot_bounds() const
const std::vector< glm::vec3 > & palette() const
std::optional< Kinesis::AABB2D > m_plot_bounds
std::vector< TickRequest > m_ticks
const std::vector< AxisMapping > & y_mappings() const
WaveformBuilder & thickness(float t)
Line thickness in pixels.
glm::vec3 palette_color(const std::vector< glm::vec3 > &palette, size_t index) noexcept
Resolve a per-series color from a palette.
Definition PlotSpec.cpp:76
Series series()
Begin a Series chain.
Definition Plot.hpp:109
void apply_auto_scale(AxisRange &range, const std::vector< std::span< const double > > &series)
Apply auto-scaling to an AxisRange from a set of series.
Definition PlotSpec.cpp:53
std::vector< std::span< const double > > series_by_role(const Kakshya::PlotContainer &container, Kakshya::DataDimension::Role role)
Collect all series from processed_data whose DataDimension role matches role.
Definition PlotSpec.cpp:12
GeometryFn< float > background(Kinesis::AABB2D bounds, glm::vec3 color, const std::shared_ptr< Core::VKImage > &texture)
TRIANGLE_STRIP background quad for a plot area.
Definition PlotSpec.cpp:88
Role
Semantic role of the dimension.
Definition NDData.hpp:150
A bounded, renderable region on a window surface.
Definition Element.hpp:58
AxisRange & range(float lo, float hi)
Set the explicit [min, max] domain.
Definition AxisRange.hpp:39
float to_ndc(float v) const noexcept
Map a value into [-1, 1] NDC within this range.
Definition AxisRange.hpp:96
Scalar domain extent for one plot axis.
Definition AxisRange.hpp:22
Config for generating numeric tick labels on one plot edge.
Definition PlotSpec.hpp:75