v0.3.0 API Reference
TransientsTracker.h
1//
2// TransientsTracker.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_TRANSIENTTRACKER_H
10#define AVARA3D_EXTENSION_TRANSIENTTRACKER_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
25class Node;
26
27} // namespace a3d
28
29namespace a3d::ext {
30
43
44public:
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 TransientsTracker(const TransientsTracker&) = delete;
141 TransientsTracker& operator=(const TransientsTracker&) = delete;
142
144 TransientsTracker& operator=(TransientsTracker&&) = delete;
145
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
259private:
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, const Entry& entry, const Node& node, double simulationTime) const;
282
283 void pruneInactiveEntries();
284 void enforceCountLimits(const std::string& group);
285 void enforceMaxCount(std::size_t maxCount, const std::string* group);
286
287 void resetSweepSchedule();
288 void resetRuntimeState();
289
290 // [Private Member Variables]
291
292 std::vector<Entry> _entries;
293 Policy _policy;
294 std::unordered_map<std::string, Policy> _groupPolicies;
295 SweepPolicy _sweepPolicy;
296 std::uint64_t _updatesSinceSweep;
297 std::optional<double> _simulationTime;
298 std::optional<double> _nextSweepTime;
299};
300
301} // namespace a3d::ext
302
303#endif // AVARA3D_EXTENSION_TRANSIENTTRACKER_H
A transformable object in a Scene hierarchy.
Definition: Node.h:45
Tracks transient scene nodes and removes them according to configurable policies.
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 sweep(const Scene::StepInfo &info)
Immediately evaluates all swept removal policies.
const SweepPolicy & sweepPolicy() const
Returns the scheduling policy used by update().
const Policy & policy() const
Returns the global removal policy.
void removeAll()
Removes every live tracked node from its parent and clears the registry.
void groupPolicy(const std::string &group, const Policy &policy)
Replaces the removal policy for a named group.
TransientsTracker(SweepPolicy sweepPolicy=SweepPolicy::EveryUpdate())
Creates an empty registry with the supplied sweep policy.
void policy(const Policy &policy)
Replaces the global removal policy.
void update(const Scene::StepInfo &info)
Advances registry scheduling and sweeps when the configured policy is due.
void track(const std::shared_ptr< Node > &node, const std::string &group={})
Tracks a transient node and immediately enforces applicable count limits.
void untrack(const Node &node)
Stops tracking a node without removing it from its parent.
void clear()
Forgets every tracked node and resets runtime scheduling state.
~TransientsTracker()=default
Destroys the registry without removing tracked nodes.
void sweepPolicy(const SweepPolicy &policy)
Replaces the scheduling policy used by update().
Timing information for one simulation step.
Definition: Scene.h:87
Maximum permitted world-space distance from a fixed point.
float radius
Maximum permitted distance from center.
math::vec3 center
World-space center used for the distance test.
Removal limits applied globally or to a named group.
std::optional< DistanceLimit > distanceLimit
Maximum world-space distance from a fixed point.
std::optional< double > maxAge
Maximum node age in seconds of simulation time.
std::optional< std::size_t > maxCount
Maximum number of matching tracked nodes.
Scheduling policy controlling when update() performs a sweep.
static SweepPolicy EveryNUpdates(std::uint64_t updates)
Creates a policy that sweeps after a fixed number of update() calls.
Mode
Available sweep scheduling modes.
@ EveryUpdate
Sweep on every call to update().
double timeInterval
Seconds of simulation time between sweeps in Mode::EveryInterval.
std::uint64_t updateInterval
Number of update() calls between sweeps in Mode::EveryNUpdates.
static SweepPolicy EveryInterval(double seconds)
Creates a policy that sweeps at a fixed simulation-time interval.
Mode mode
Selected scheduling mode.
static SweepPolicy EveryUpdate()
Creates a policy that sweeps on every call to update().