MayaFlux 0.5.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
220std::vector<GraphicsOperator::DirtyVertexRange> PathOperator::dirty_vertex_ranges() const
221{
222 std::vector<DirtyVertexRange> ranges;
223
224 uint32_t offset = 0;
225 uint32_t index = 0;
226 for (const auto& path : m_paths) {
227 const auto count = static_cast<uint32_t>(path->get_vertex_count());
228 if (path->needs_gpu_update()) {
229 ranges.push_back(DirtyVertexRange {
231 .vertex_offset = offset,
232 .vertex_count = count });
233 }
234 offset += count;
235 ++index;
236 }
237
238 return ranges;
239}
240
242{
243 size_t total = 0;
244 for (const auto& path : m_paths) {
245 total += path->get_all_vertex_count();
246 }
247 return total;
248}
249
250std::vector<uint32_t> PathOperator::build_cluster_ids() const
251{
252 std::vector<uint32_t> ids(get_vertex_count(), 0U);
253
254 if (m_paths.size() <= 1) {
255 return ids;
256 }
257
258 size_t offset = 0;
259 uint32_t cluster = 0;
260 for (const auto& path : m_paths) {
261 const size_t count = path->get_vertex_count();
262 for (size_t i = 0; i < count && offset + i < ids.size(); ++i) {
263 ids[offset + i] = cluster;
264 }
265 offset += count;
266 ++cluster;
267 }
268
269 return ids;
270}
271
272//-----------------------------------------------------------------------------
273// Parameter Control
274//-----------------------------------------------------------------------------
275
276void PathOperator::set_parameter(std::string_view param, double value)
277{
278 if (param == "tension") {
279 for (auto& path : m_paths) {
280 path->set_tension(value);
281 }
282 } else if (param == "samples_per_segment") {
283 m_default_samples_per_segment = static_cast<Eigen::Index>(value);
284 for (auto& path : m_paths) {
285 path->set_samples_per_segment(static_cast<Eigen::Index>(value));
286 }
287 } else if (param == "thickness") {
288 m_default_thickness = static_cast<float>(value);
289 for (auto& path : m_paths) {
290 path->set_path_thickness(m_default_thickness);
291 }
292 }
293}
294
295std::optional<double> PathOperator::query_state(std::string_view query) const
296{
297 if (query == "control_point_count") {
298 return static_cast<double>(get_point_count());
299 }
300 if (query == "vertex_count") {
301 return static_cast<double>(get_vertex_count());
302 }
303 if (query == "path_count") {
304 return static_cast<double>(m_paths.size());
305 }
306 return std::nullopt;
307}
308
309//-----------------------------------------------------------------------------
310// Path Configuration
311//-----------------------------------------------------------------------------
312
314{
316 for (auto& path : m_paths) {
317 path->set_samples_per_segment(samples);
318 }
319}
320
321void PathOperator::set_tension(double tension)
322{
323 for (auto& path : m_paths) {
324 path->set_tension(tension);
325 }
326}
327
329{
330 m_default_thickness = thickness;
331 for (auto& path : m_paths) {
332 path->set_path_thickness(thickness);
333 }
334}
335
336void PathOperator::set_global_color(const glm::vec3& color)
337{
338 for (auto& path : m_paths) {
339 path->set_path_color(color);
340 }
341}
342
343void* PathOperator::get_data_at(size_t global_index)
344{
345 size_t offset = 0;
346 for (auto& group : m_paths) {
347 const auto& vertices = group->get_all_vertices();
348 if (global_index < offset + vertices.size()) {
349 size_t local_index = global_index - offset;
350 return const_cast<LineVertex*>(&vertices[local_index]);
351 }
352 offset += vertices.size();
353 }
354 return nullptr;
355}
356
357} // namespace MayaFlux::Nodes::Network
#define MF_ERROR(comp, ctx,...)
#define MF_WARN(comp, ctx,...)
#define MF_DEBUG(comp, ctx,...)
std::vector< glm::vec2 > * points
uint32_t index
Definition VKDevice.cpp:142
size_t count
float value
float offset
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
std::vector< DirtyVertexRange > dirty_vertex_ranges() const override
One DirtyVertexRange per path whose PathGeneratorNode needs a GPU update, so add_control_point() on o...
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.
std::vector< uint32_t > build_cluster_ids() const override
Overrides GraphicsOperator::build_cluster_ids() for real multi-path scenes.
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.
A contiguous span of vertex records that changed since the last mark_vertex_data_clean().