MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
PathOperator.cpp
Go to the documentation of this file.
1#include "PathOperator.hpp"
3
5
6//-----------------------------------------------------------------------------
7// Construction
8//-----------------------------------------------------------------------------
9
12 Eigen::Index samples_per_segment)
13 : m_default_mode(mode)
14 , m_default_samples_per_segment(samples_per_segment)
15{
17 "PathOperator created with mode {}, {} samples per segment",
18 static_cast<int>(mode), samples_per_segment);
19}
20
21//-----------------------------------------------------------------------------
22// GraphicsOperator Interface (Simple Initialization)
23//-----------------------------------------------------------------------------
24
25void PathOperator::initialize(const std::vector<LineVertex>& vertices)
26{
27 if (vertices.empty()) {
29 "Cannot initialize PathOperator with zero vertices");
30 return;
31 }
32
33 m_paths.clear();
35
37 "PathOperator initialized with {} control vertices", vertices.size());
38}
39
40//-----------------------------------------------------------------------------
41// Advanced Initialization (Multiple Paths)
42//-----------------------------------------------------------------------------
43
45 const std::vector<std::vector<LineVertex>>& paths,
47{
48 for (const auto& path : paths) {
50 }
51
53 "PathOperator initialized with {} paths",
54 paths.size());
55}
56
58 const std::vector<LineVertex>& control_vertices,
59 Kinesis::InterpolationMode mode, uint32_t default_samples_per_segment, size_t max_control_points, double tension)
60{
61 if (control_vertices.empty()) {
63 "Cannot add path with zero control vertices");
64 return;
65 }
66
67 auto path = std::make_shared<GpuSync::PathGeneratorNode>(
68 mode,
69 default_samples_per_segment,
70 max_control_points,
71 tension);
72
73 path->set_control_points(control_vertices);
74 path->set_path_thickness(m_default_thickness);
75 path->compute_frame();
76
77 uint32_t expected = 0;
78 while (!m_access_token.compare_exchange_weak(expected, 1,
79 std::memory_order_acquire, std::memory_order_relaxed)) {
80 if (m_shutdown.load(std::memory_order_relaxed))
81 return;
82 expected = 0;
83 }
84
85 m_paths.push_back(std::move(path));
86
87 m_access_token.store(0, std::memory_order_release);
88
90 "Added path #{} with {} control vertices, {} generated vertices",
91 m_paths.size(), control_vertices.size(),
92 m_paths.back()->get_generated_vertex_count());
93}
94
95void PathOperator::add_node(std::shared_ptr<GpuSync::PathGeneratorNode> node)
96{
97 if (!node) {
99 "PathOperator::add_node: null node ignored");
100 return;
101 }
102
103 node->compute_frame();
104
105 uint32_t expected = 0;
106 while (!m_access_token.compare_exchange_weak(expected, 1,
107 std::memory_order_acquire, std::memory_order_relaxed)) {
108 if (m_shutdown.load(std::memory_order_relaxed))
109 return;
110 expected = 0;
111 }
112
113 m_paths.push_back(std::move(node));
114
115 m_access_token.store(0, std::memory_order_release);
116
118 "PathOperator: added node #{} ({} vertices)",
119 m_paths.size(), m_paths.back()->get_vertex_count());
120}
121
122//-----------------------------------------------------------------------------
123// Processing
124//-----------------------------------------------------------------------------
125
126void PathOperator::process(float /*dt*/)
127{
128 uint32_t expected = 0;
129 while (!m_access_token.compare_exchange_weak(expected, 1,
130 std::memory_order_acquire, std::memory_order_relaxed)) {
131 if (m_shutdown.load(std::memory_order_relaxed))
132 return;
133 expected = 0;
134 }
135
136 for (auto& path : m_paths) {
137 path->compute_frame();
138 }
139
140 m_access_token.store(0, std::memory_order_release);
141}
142
143//-----------------------------------------------------------------------------
144// GraphicsOperator Interface (Data Extraction)
145//-----------------------------------------------------------------------------
146
147std::vector<LineVertex> PathOperator::extract_vertices() const
148{
149 std::vector<LineVertex> positions;
150
151 for (const auto& path : m_paths) {
152 auto points = path->get_all_vertices();
153 for (const auto& pt : points) {
154 positions.push_back(pt);
155 }
156 }
157
158 return positions;
159}
160
161std::span<const uint8_t> PathOperator::get_vertex_data_for_collection(uint32_t idx) const
162{
163 if (m_paths.empty() || idx >= m_paths.size()) {
164 return {};
165 }
166 return m_paths[idx]->get_vertex_data();
167}
168
169std::span<const uint8_t> PathOperator::get_vertex_data() const
170{
172 for (const auto& group : m_paths) {
173 auto span = group->get_vertex_data();
176 span.begin(), span.end());
177 }
178 return { m_vertex_data_aggregate.data(), m_vertex_data_aggregate.size() };
179}
180
182{
183 if (m_paths.empty()) {
184 return {};
185 }
186
187 auto layout_opt = m_paths[0]->get_vertex_layout();
188 if (!layout_opt) {
189 return {};
190 }
191
192 auto layout = *layout_opt;
193 layout.vertex_count = static_cast<uint32_t>(get_vertex_count());
194 return layout;
195}
196
198{
199 size_t total = 0;
200 for (const auto& path : m_paths) {
201 total += path->get_vertex_count();
202 }
203 return total;
204}
205
207{
208 return std::ranges::any_of(
209 m_paths,
210 [](const auto& group) { return group->needs_gpu_update(); });
211}
212
214{
215 for (auto& path : m_paths) {
216 path->clear_gpu_update_flag();
217 }
218}
219
221{
222 size_t total = 0;
223 for (const auto& path : m_paths) {
224 total += path->get_all_vertex_count();
225 }
226 return total;
227}
228
229//-----------------------------------------------------------------------------
230// Parameter Control
231//-----------------------------------------------------------------------------
232
233void PathOperator::set_parameter(std::string_view param, double value)
234{
235 if (param == "tension") {
236 for (auto& path : m_paths) {
237 path->set_tension(value);
238 }
239 } else if (param == "samples_per_segment") {
240 m_default_samples_per_segment = static_cast<Eigen::Index>(value);
241 for (auto& path : m_paths) {
242 path->set_samples_per_segment(static_cast<Eigen::Index>(value));
243 }
244 } else if (param == "thickness") {
245 m_default_thickness = static_cast<float>(value);
246 for (auto& path : m_paths) {
247 path->set_path_thickness(m_default_thickness);
248 }
249 }
250}
251
252std::optional<double> PathOperator::query_state(std::string_view query) const
253{
254 if (query == "control_point_count") {
255 return static_cast<double>(get_point_count());
256 }
257 if (query == "vertex_count") {
258 return static_cast<double>(get_vertex_count());
259 }
260 if (query == "path_count") {
261 return static_cast<double>(m_paths.size());
262 }
263 return std::nullopt;
264}
265
266//-----------------------------------------------------------------------------
267// Path Configuration
268//-----------------------------------------------------------------------------
269
271{
273 for (auto& path : m_paths) {
274 path->set_samples_per_segment(samples);
275 }
276}
277
278void PathOperator::set_tension(double tension)
279{
280 for (auto& path : m_paths) {
281 path->set_tension(tension);
282 }
283}
284
286{
287 m_default_thickness = thickness;
288 for (auto& path : m_paths) {
289 path->set_path_thickness(thickness);
290 }
291}
292
293void PathOperator::set_global_color(const glm::vec3& color)
294{
295 for (auto& path : m_paths) {
296 path->set_path_color(color);
297 }
298}
299
300void* PathOperator::get_data_at(size_t global_index)
301{
302 size_t offset = 0;
303 for (auto& group : m_paths) {
304 const auto& vertices = group->get_all_vertices();
305 if (global_index < offset + vertices.size()) {
306 size_t local_index = global_index - offset;
307 return const_cast<LineVertex*>(&vertices[local_index]);
308 }
309 offset += vertices.size();
310 }
311 return nullptr;
312}
313
314} // namespace MayaFlux::Nodes::Network
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::vector< glm::vec2 > * points
void set_parameter(std::string_view param, double value) override
Set operator parameter.
void * get_data_at(size_t global_index) override
Get mutable access to point at global index.
std::vector< uint8_t > m_vertex_data_aggregate
Kinesis::InterpolationMode m_default_mode
void add_path(const std::vector< LineVertex > &control_vertices, Kinesis::InterpolationMode mode, uint32_t default_samples_per_segment=32, size_t max_control_points=64, double tension=0.5)
Add a new path with given control points and properties.
bool is_vertex_data_dirty() const override
Check if geometry changed this frame.
void initialize_paths(const std::vector< std::vector< LineVertex > > &paths, Kinesis::InterpolationMode mode)
Initialize multiple paths with given control points and properties.
std::vector< std::shared_ptr< GpuSync::PathGeneratorNode > > m_paths
void add_node(std::shared_ptr< GpuSync::PathGeneratorNode > node)
Add an externally constructed PathGeneratorNode subtype to the operator.
void set_tension(double tension)
Set the tension parameter for all paths (if supported by mode).
size_t get_vertex_count() const override
Get number of vertices (may differ from point count for topology/path)
PathOperator(Kinesis::InterpolationMode mode=Kinesis::InterpolationMode::CATMULL_ROM, Eigen::Index samples_per_segment=32)
size_t get_point_count() const override
Get source point count (before topology expansion)
Kakshya::VertexLayout get_vertex_layout() const override
Get vertex layout describing vertex structure.
void set_samples_per_segment(Eigen::Index samples)
Set the number of samples per segment for all paths.
void set_global_color(const glm::vec3 &color)
Set the global color tint for all paths.
void initialize(const std::vector< LineVertex > &vertices)
Initialize a single path with given control points and properties.
void process(float dt) override
Process for one batch cycle.
void mark_vertex_data_clean() override
Clear dirty flag after GPU upload.
std::span< const uint8_t > get_vertex_data_for_collection(uint32_t idx) const override
Get vertex data for specific collection (if multiple)
std::vector< LineVertex > extract_vertices() const
Extract current vertex data as LineVertex array.
void set_global_thickness(float thickness)
Set the global thickness for all paths.
std::span< const uint8_t > get_vertex_data() const override
Get vertex data for GPU upload.
std::optional< double > query_state(std::string_view query) const override
Query operator internal state.
@ NodeProcessing
Node graph processing (Nodes::NodeGraphManager)
@ Nodes
DSP Generator and Filter Nodes, graph pipeline, node management.
InterpolationMode
Mathematical interpolation methods.
Vertex type for line primitives (LINE_LIST / LINE_STRIP topology)
uint32_t vertex_count
Total number of vertices in this buffer.
Complete description of vertex data layout in a buffer.