Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/PersonWaitingPolicyTests.cpp

Source at documentation-generation time. See Test catalog for fixture associations.

2
3#include <cmath>
4#include <cstdio>
5#include <cstdlib>
6#include <limits>
7#include <set>
8#include <vector>
9
10using namespace PersonWaiting;
11
12static int checks = 0;
13static void Check(bool value, const char* description)
14{
15 ++checks;
16 if (!value)
17 {
18 std::fprintf(stderr, "FAIL: %s\n", description);
19 std::exit(1);
20 }
21}
22
23static Configuration Single(Gesture gesture)
24{
25 Configuration result;
26 result.available.fill(false);
27 result.available[static_cast<int>(gesture)] = true;
29 result.minIntervalSeconds = result.maxIntervalSeconds = 0.1;
30 return result;
31}
32
33static Command Update(Scheduler& scheduler, double dt = 0.0,
34 Movement movement = Movement::None, bool finished = false, bool eligible = true)
35{
36 return scheduler.Update({dt, eligible, movement, finished});
37}
38
39static void CheckInterrupts()
40{
42 for (const auto movement : {Movement::Forward, Movement::TurnLeft, Movement::TurnRight})
43 {
44 Scheduler scheduler(100, 200, Single(gesture));
45 auto command = Update(scheduler, 0.1);
46 Check(command.kind == CommandKind::PlayClip, "gesture starts");
47 command = Update(scheduler, 0.01, movement);
48 Check(command.kind == CommandKind::BlendToStanding, "one-shot and hip-entry interrupt immediately");
49 Check(command.blendSeconds == 0.35, "shared neutral blend duration");
50 Check(Update(scheduler, 0.1, movement).kind == CommandKind::None, "blend cannot repeatedly restart");
51 command = Update(scheduler, 0.25, movement, true);
52 Check(command.kind == CommandKind::BeginMovement && command.movement == movement,
53 "requested movement starts after neutral is reached");
54 }
55
56 Scheduler hips(1, 2, Single(Gesture::HandOnHip));
57 Check(Update(hips, 0.1).clip == Clip::HipEntry, "hip sequence enters");
58 Check(Update(hips, 0.65, Movement::None, true).clip == Clip::HipHold, "hip entry completes into hold");
59 auto command = Update(hips, 0.1, Movement::Forward);
60 Check(command.kind == CommandKind::PlayClip && command.clip == Clip::HipExit,
61 "movement from hip hold uses authored exit");
62 Check(command.blendSeconds == 0.35, "arbitrary hold phase blends into authored exit");
63 Check(Update(hips, 0.1, Movement::Forward).kind == CommandKind::None, "exit cannot repeatedly restart");
64 command = Update(hips, 0.55, Movement::Forward, true);
65 Check(command.kind == CommandKind::BeginMovement, "exit reaches neutral before walking starts");
66
67 Scheduler canceled(3, 4, Single(Gesture::CheckWatch));
68 Update(canceled, 0.1);
69 Update(canceled, 0.1, Movement::Forward);
70 Check(Update(canceled, 0.35, Movement::None, true).kind == CommandKind::None,
71 "releasing input while exiting must not start stale movement");
72
73 Scheduler changed(3, 4, Single(Gesture::CheckWatch));
74 Update(changed, 0.1);
75 Update(changed, 0.1, Movement::Forward);
76 command = Update(changed, 0.35, Movement::TurnLeft, true);
77 Check(command.kind == CommandKind::BeginMovement && command.movement == Movement::TurnLeft,
78 "latest input wins during the exit blend");
79
80 Scheduler finished(5, 6, Single(Gesture::WeightShiftLeft));
81 Update(finished, 0.1);
82 command = Update(finished, 2.0, Movement::Forward, true);
83 Check(command.kind == CommandKind::BeginMovement, "completed neutral one-shot needs no redundant blend");
84}
85
86static void CheckNaturalHipExit()
87{
88 auto settings = Single(Gesture::HandOnHip);
89 settings.minHipHoldSeconds = 3.0;
90 settings.maxHipHoldSeconds = 7.0;
91 std::set<int> holds;
92 for (std::uint64_t avatar = 0; avatar != 128; ++avatar)
93 {
94 Scheduler scheduler(avatar, 999, settings);
95 Update(scheduler, 0.1);
96 Update(scheduler, 0.65, Movement::None, true);
97 int ticks = 0;
98 Command command;
99 while (++ticks < 1000)
100 {
101 command = Update(scheduler, 0.01);
102 if (command.kind != CommandKind::None) break;
103 }
104 Check(command.clip == Clip::HipExit && command.blendSeconds == 0.35,
105 "natural hold completion blends arbitrary breathing phase into authored exit");
106 Check(ticks >= 300 && ticks <= 701, "hold timing stays inside configured range");
107 holds.insert(ticks);
108 Check(Update(scheduler, 0.65, Movement::None, true).kind == CommandKind::None,
109 "completed hip exit returns to a fresh neutral delay");
110 }
111 Check(holds.size() > 90, "hip holds vary independently between avatars");
112}
113
114static void CheckHipExitHostProtocol()
115{
116 // The adapter owns clip sampling and must report completion after a fixed
117 // first-frame preblend PLUS the full exit. Exercise that event contract for
118 // both current profile durations; the scheduler must not begin walking early.
119 for (const double authoredDuration : {0.65, 0.75})
120 {
121 Scheduler scheduler(88, 99, Single(Gesture::HandOnHip));
122 Update(scheduler, 0.1);
123 Update(scheduler, 1.0, Movement::None, true);
124 const auto exit = Update(scheduler, 0.0, Movement::Forward);
125 Check(exit.clip == Clip::HipExit && exit.blendSeconds == 0.35, "hip exit has explicit preblend");
126 const int blendTicks = static_cast<int>(std::lround(exit.blendSeconds * 100.0));
127 const int clipTicks = static_cast<int>(std::lround(authoredDuration * 100.0));
128 for (int tick = 1; tick < blendTicks + clipTicks; ++tick)
129 Check(Update(scheduler, 0.01, Movement::Forward).kind == CommandKind::None,
130 "walking waits for first-frame preblend and the entire exit clip");
131 const auto command = Update(scheduler, 0.01, Movement::Forward, true);
132 Check(command.kind == CommandKind::BeginMovement && command.movement == Movement::Forward,
133 "movement begins only after host completes both exit phases");
134 }
135}
136
137static void CheckTimingAndAvailability()
138{
139 std::set<int> delays;
140 Configuration available;
141 available.available.fill(true);
142 for (std::uint64_t avatar = 0; avatar != 512; ++avatar)
143 {
144 Scheduler scheduler(avatar, 12345, available);
145 Scheduler duplicate(avatar, 12345, available);
146 int ticks = 0;
147 while (++ticks < 3000)
148 {
149 const auto first = Update(scheduler, 0.01);
150 const auto second = Update(duplicate, 0.01);
151 Check(first.kind == second.kind && first.clip == second.clip, "same avatar seed is reproducible");
152 if (first.kind == CommandKind::PlayClip) break;
153 }
154 Check(ticks >= 1200 && ticks <= 2401, "first delay remains 12 to 24 seconds");
155 delays.insert(ticks);
156 }
157 Check(delays.size() > 350, "avatars do not synchronize their first gesture");
158
159 Scheduler empty(11, 22);
160 for (int t = 0; t != 100; ++t)
161 Check(Update(empty, 60.0).kind == CommandKind::None, "missing clips remain neutral");
162 auto move = Update(empty, 0.1, Movement::Forward);
163 Check(move.kind == CommandKind::BeginMovement, "missing waiting data never blocks movement");
164
166 {
167 auto single = Single(gesture);
168 Scheduler scheduler(7, 8, single);
169 const auto command = Update(scheduler, 0.1);
170 const auto expected = gesture == Gesture::WeightShiftLeft ? Clip::WeightShiftLeft :
173 Check(command.clip == expected, "selection respects the available profile clips");
174 }
175
176 Scheduler ineligible(99, 55);
177 for (int t = 0; t != 100; ++t)
178 Check(Update(ineligible, 60.0, Movement::None, false, false).kind == CommandKind::None,
179 "seated or moving avatars cannot acquire a waiting gesture");
180 Check(Update(ineligible, 0.1).kind == CommandKind::None, "eligibility starts a new initial delay");
181}
182
183static void CheckCooldownAndVariation()
184{
185 auto settings = Single(Gesture::CheckWatch);
186 settings.cooldownSeconds[static_cast<int>(Gesture::CheckWatch)] = 45.0;
187 Scheduler watch(1, 1, settings);
188 Check(Update(watch, 0.1).clip == Clip::CheckWatch, "watch starts");
189 Update(watch, 2.0, Movement::None, true);
190 for (int second = 0; second != 42; ++second)
191 Check(Update(watch, 1.0).kind == CommandKind::None, "watch cannot recur before its cooldown");
192 Check(Update(watch, 1.0).clip == Clip::CheckWatch, "watch becomes eligible after cooldown");
193
194 Configuration varied;
195 varied.available.fill(true);
197 varied.minIntervalSeconds = varied.maxIntervalSeconds = 0.1;
198 varied.minHipHoldSeconds = varied.maxHipHoldSeconds = 0.1;
199 varied.cooldownSeconds.fill(0.0);
200 Scheduler scheduler(333, 444, varied);
201 Clip previous = Clip::None;
202 std::array<int, GestureCount> counts = {};
203 bool finished = false;
204 for (int tick = 0; tick != 10000; ++tick)
205 {
206 const auto command = Update(scheduler, 0.1, Movement::None, finished);
207 finished = false;
208 if (command.kind != CommandKind::PlayClip) continue;
209 if (command.clip == Clip::HipHold) continue;
210 finished = true;
211 if (command.clip == Clip::HipExit) continue;
212 Check(command.clip != previous, "available alternatives prevent immediate gesture repetition");
213 previous = command.clip;
214 ++counts[command.clip == Clip::WeightShiftLeft ? 0 : command.clip == Clip::WeightShiftRight ? 1 :
215 command.clip == Clip::CheckWatch ? 2 : 3];
216 }
217 Check(counts[0] + counts[1] > counts[3] && counts[3] > counts[2], "configured weights favor subtle weight shifts");
218 Check(counts[0] > 0 && counts[1] > 0, "both directional weight shifts remain independently reachable");
219 Check(std::abs(counts[0] - counts[1]) < 0.15 * (counts[0] + counts[1]),
220 "left and right shifts retain balanced configured weights");
221 Check(counts[2] > 0, "watch remains reachable with a lower weight");
222}
223
224static void CheckDirectionalCooldowns()
225{
226 auto settings = Single(Gesture::WeightShiftLeft);
227 settings.available[static_cast<int>(Gesture::WeightShiftRight)] = true;
228 // Deliberately different periods catch an accidental shared weight-shift
229 // timer even though the shipped left/right defaults are both 12 seconds.
230 settings.cooldownSeconds[static_cast<int>(Gesture::WeightShiftLeft)] = 60.0;
231 settings.cooldownSeconds[static_cast<int>(Gesture::WeightShiftRight)] = 12.0;
232 Scheduler scheduler(42, 43, settings);
233 const auto first = Update(scheduler, 0.1);
234 Check(first.clip == Clip::WeightShiftLeft || first.clip == Clip::WeightShiftRight,
235 "directional-only availability selects a directional shift");
236 Update(scheduler, 0.1, Movement::None, true);
237 const auto second = Update(scheduler, 0.1);
238 Check(second.kind == CommandKind::PlayClip && second.clip != first.clip &&
239 (second.clip == Clip::WeightShiftLeft || second.clip == Clip::WeightShiftRight),
240 "cooldown of one side does not block the other side");
241 Update(scheduler, 0.1, Movement::None, true);
242 const auto third = Update(scheduler, 13.0);
243 Check(third.clip == Clip::WeightShiftRight,
244 "right cooldown expires independently while left is still cooling down");
245 Update(scheduler, 0.1, Movement::None, true);
246 Check(Update(scheduler, 0.1).kind == CommandKind::None,
247 "both individual cooldowns prevent a premature directional replay");
248 Check(Update(scheduler, 13.0).clip == Clip::WeightShiftRight,
249 "a ready direction remains playable when its opposite is unavailable");
250}
251
252static void CheckPoseBlends()
253{
254 Pose from;
255 Pose to;
256 from.anglesDegrees[0] = {{179.0, -179.0, 60.0}};
257 to.anglesDegrees[0] = {{-179.0, 179.0, -20.0}};
258 from.translationMeters = {{0.02, -0.01, -0.03}};
259 to.translationMeters = {{0.0, 0.0, 0.0}};
260 from.cameraHeightOffsetMeters = -0.025;
261 to.cameraHeightOffsetMeters = 0.005;
262 const auto start = BlendPose(from, to, 0.0, 0.35);
263 const auto middle = BlendPose(from, to, 0.175, 0.35);
264 const auto end = BlendPose(from, to, 0.35, 0.35);
265 Check(start.anglesDegrees == from.anglesDegrees && start.translationMeters == from.translationMeters &&
266 start.cameraHeightOffsetMeters == from.cameraHeightOffsetMeters,
267 "first blended frame preserves every sampled pose component");
268 Check(end.anglesDegrees == to.anglesDegrees && end.translationMeters == to.translationMeters &&
269 end.cameraHeightOffsetMeters == to.cameraHeightOffsetMeters,
270 "last blended frame exactly matches the target pose");
271 Check(std::abs(middle.anglesDegrees[0][0] - 180.0) < 1.0e-12 &&
272 std::abs(middle.anglesDegrees[0][1] + 180.0) < 1.0e-12,
273 "joint angle wrapping takes the shortest rotational arc");
274 Check(std::abs(middle.translationMeters[2] + 0.015) < 1.0e-12 &&
275 std::abs(middle.cameraHeightOffsetMeters + 0.01) < 1.0e-12,
276 "grounding and first-person camera use the same blend as the skeleton");
277 double previous = 0.0;
278 for (int tick = 0; tick <= 1000; ++tick)
279 {
280 const double value = SmoothBlendProgress(tick * 0.00035, 0.35);
281 Check(value >= previous && value >= 0.0 && value <= 1.0, "blend remains bounded and monotonic");
282 previous = value;
283 }
284 Check(SmoothBlendProgress(0.000001, 0.35) < 1.0e-12, "blend starts with a flat tangent");
285 Check(1.0 - SmoothBlendProgress(0.349999, 0.35) < 1.0e-12, "blend ends with a flat tangent");
286 Check(SmoothBlendProgress(std::numeric_limits<double>::quiet_NaN(), 0.35) == 0.0,
287 "invalid elapsed time cannot contaminate a pose");
288}
289
290int main()
291{
292 CheckInterrupts();
293 CheckNaturalHipExit();
294 CheckHipExitHostProtocol();
295 CheckTimingAndAvailability();
296 CheckCooldownAndVariation();
297 CheckDirectionalCooldowns();
298 CheckPoseBlends();
299 std::printf("PersonWaitingPolicy: %d checks passed.\n", checks);
300}
Command Update(const Input &input)
float Update(Component *components, std::size_t count, bool viewerInside)
double SmoothBlendProgress(double elapsedSeconds, double durationSeconds)
Pose BlendPose(const Pose &from, const Pose &to, double elapsedSeconds, double durationSeconds)
std::array< double, GestureCount > cooldownSeconds
std::array< bool, GestureCount > available
std::array< std::array< double, 3 >, 16 > anglesDegrees
std::array< double, 3 > translationMeters