Avara3D 0.2.0
C++ API reference
Transients.h
1//
2// Transients.h
3// avara3d
4//
5// Created by Morgan Davis on 8/11/26.
6// Copyright © 2026 Morgan K Davis. All rights reserved.
7//
8
9#ifndef AVARA3D_EXTENSION_TRANSIENTS_H
10#define AVARA3D_EXTENSION_TRANSIENTS_H
11
12#include <cstddef>
13#include <cstdint>
14#include <memory>
15#include <optional>
16#include <string>
17#include <unordered_map>
18#include <vector>
19
20#include "a3d/Math.h"
21#include "a3d/scene/Scene.h"
22
23namespace a3d {
24
25 class Node;
26
27}
28
29namespace a3d::ext {
30
42 class Transients {
43
44 public:
45 // [Public Types]
46
49
52
54 float radius {0.0f};
55 };
56
58 struct Policy {
59
65 std::optional<std::size_t> maxCount {};
66
72 std::optional<double> maxAge {};
73
79 std::optional<DistanceLimit> distanceLimit {};
80 };
81
83 struct SweepPolicy {
84
86 enum class Mode {
90 };
91
94
96 std::uint64_t updateInterval {1};
97
99 double timeInterval {0.0};
100
107
116 static SweepPolicy EveryNUpdates(std::uint64_t updates);
117
126 static SweepPolicy EveryInterval(double seconds);
127 };
128
129 // [Public Lifecycle Functions]
130
139
140 Transients(const Transients&) = delete;
141 Transients& operator=(const Transients&) = delete;
142
143 Transients(Transients&&) = delete;
144 Transients& operator=(Transients&&) = delete;
145
147 ~Transients() = default;
148
149 // [Public Member Functions]
150
163 void track(const std::shared_ptr<Node>& node, const std::string& group = {});
164
174 void track(const std::vector<std::shared_ptr<Node>>& nodes, const std::string& group = {});
175
181 void untrack(const Node& node);
182
184 const Policy& policy() const;
185
195 void policy(const Policy& policy);
196
208 void groupPolicy(const std::string& group, const Policy& policy);
209
211 const SweepPolicy& sweepPolicy() const;
212
223
233 void update(const Scene::StepInfo& info);
234
243 void sweep(const Scene::StepInfo& info);
244
250 void clear();
251
257 void removeAll();
258
259 private:
260 // [Private Types]
261
262 struct Entry {
263 std::weak_ptr<Node> node;
264 std::string group;
265 double creationTime {0.0};
266 };
267
268 // [Private Static Member Functions]
269
270 static void validatePolicy(const Policy& policy);
271 static void validateSweepPolicy(const SweepPolicy& policy);
272 static void detachNodes(const std::vector<std::shared_ptr<Node>>& nodes);
273
274 // [Private Member Functions]
275
276 void observeStepInfo(const Scene::StepInfo& info);
277 bool sweepDue(const Scene::StepInfo& info) const;
278 void performSweep(double simulationTime);
279
280 bool shouldRemove(const Entry& entry, const Node& node, double simulationTime) const;
281 bool policyRemoves(const Policy& policy,
282 const Entry& entry,
283 const Node& node,
284 double simulationTime) const;
285
286 void pruneInactiveEntries();
287 void enforceCountLimits(const std::string& group);
288 void enforceMaxCount(std::size_t maxCount, const std::string* group);
289
290 void resetSweepSchedule();
291 void resetRuntimeState();
292
293 // [Private Member Variables]
294
295 std::vector<Entry> _entries;
296 Policy _policy;
297 std::unordered_map<std::string, Policy> _groupPolicies;
298 SweepPolicy _sweepPolicy;
299 std::uint64_t _updatesSinceSweep;
300 std::optional<double> _simulationTime;
301 std::optional<double> _nextSweepTime;
302 };
303
304}
305
306#endif // AVARA3D_EXTENSION_TRANSIENTS_H
A transformable object in a Scene hierarchy.
Definition: Node.h:45
Tracks transient scene nodes and removes them according to configurable policies.
Definition: Transients.h:42
Transients(SweepPolicy sweepPolicy=SweepPolicy::EveryUpdate())
Creates an empty registry with the supplied sweep policy.
void sweep(const Scene::StepInfo &info)
Immediately evaluates all swept removal policies.
~Transients()=default
Destroys the registry without removing tracked nodes.
void policy(const Policy &policy)
Replaces the global removal policy.
void track(const std::shared_ptr< Node > &node, const std::string &group={})
Tracks a transient node and immediately enforces applicable count limits.
void update(const Scene::StepInfo &info)
Advances registry scheduling and sweeps when the configured policy is due.
const Policy & policy() const
Returns the global removal policy.
void clear()
Forgets every tracked node and resets runtime scheduling state.
void groupPolicy(const std::string &group, const Policy &policy)
Replaces the removal policy for a named group.
void track(const std::vector< std::shared_ptr< Node > > &nodes, const std::string &group={})
Tracks a collection of nodes as members of the same transient group.
void sweepPolicy(const SweepPolicy &policy)
Replaces the scheduling policy used by update().
const SweepPolicy & sweepPolicy() const
Returns the scheduling policy used by update().
void untrack(const Node &node)
Stops tracking a node without removing it from its parent.
void removeAll()
Removes every live tracked node from its parent and clears the registry.
Timing information for one simulation step.
Definition: Scene.h:87
Maximum permitted world-space distance from a fixed point.
Definition: Transients.h:48
math::vec3 center
World-space center used for the distance test.
Definition: Transients.h:51
float radius
Maximum permitted distance from center.
Definition: Transients.h:54
Removal limits applied globally or to a named group.
Definition: Transients.h:58
std::optional< double > maxAge
Maximum node age in seconds of simulation time.
Definition: Transients.h:72
std::optional< std::size_t > maxCount
Maximum number of matching tracked nodes.
Definition: Transients.h:65
std::optional< DistanceLimit > distanceLimit
Maximum world-space distance from a fixed point.
Definition: Transients.h:79
Scheduling policy controlling when update() performs a sweep.
Definition: Transients.h:83
std::uint64_t updateInterval
Number of update() calls between sweeps in Mode::EveryNUpdates.
Definition: Transients.h:96
static SweepPolicy EveryNUpdates(std::uint64_t updates)
Creates a policy that sweeps after a fixed number of update() calls.
double timeInterval
Seconds of simulation time between sweeps in Mode::EveryInterval.
Definition: Transients.h:99
static SweepPolicy EveryUpdate()
Creates a policy that sweeps on every call to update().
static SweepPolicy EveryInterval(double seconds)
Creates a policy that sweeps at a fixed simulation-time interval.
Mode
Available sweep scheduling modes.
Definition: Transients.h:86
@ EveryUpdate
Sweep on every call to update().
Mode mode
Selected scheduling mode.
Definition: Transients.h:93