xref: /aosp_15_r20/frameworks/av/media/libheadtracking/PoseRateLimiter.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker  * Copyright (C) 2021 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker  *
4*ec779b8eSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker  *
8*ec779b8eSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker  *
10*ec779b8eSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker  * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker  */
16*ec779b8eSAndroid Build Coastguard Worker #pragma once
17*ec779b8eSAndroid Build Coastguard Worker 
18*ec779b8eSAndroid Build Coastguard Worker #include <optional>
19*ec779b8eSAndroid Build Coastguard Worker 
20*ec779b8eSAndroid Build Coastguard Worker #include "media/Pose.h"
21*ec779b8eSAndroid Build Coastguard Worker 
22*ec779b8eSAndroid Build Coastguard Worker namespace android {
23*ec779b8eSAndroid Build Coastguard Worker namespace media {
24*ec779b8eSAndroid Build Coastguard Worker 
25*ec779b8eSAndroid Build Coastguard Worker /**
26*ec779b8eSAndroid Build Coastguard Worker  * Limits a stream of poses to a given maximum translational and rotational velocities.
27*ec779b8eSAndroid Build Coastguard Worker  *
28*ec779b8eSAndroid Build Coastguard Worker  * Normal operation:
29*ec779b8eSAndroid Build Coastguard Worker  *
30*ec779b8eSAndroid Build Coastguard Worker  * Pose3f output;
31*ec779b8eSAndroid Build Coastguard Worker  * PoseRateLimiter limiter(...);
32*ec779b8eSAndroid Build Coastguard Worker  *
33*ec779b8eSAndroid Build Coastguard Worker  * // Limiting is disabled. Output will be the same as last input.
34*ec779b8eSAndroid Build Coastguard Worker  * limiter.setTarget(...);
35*ec779b8eSAndroid Build Coastguard Worker  * output = limiter.calculatePose(...);
36*ec779b8eSAndroid Build Coastguard Worker  * limiter.setTarget(...);
37*ec779b8eSAndroid Build Coastguard Worker  * output = limiter.calculatePose(...);
38*ec779b8eSAndroid Build Coastguard Worker  *
39*ec779b8eSAndroid Build Coastguard Worker  * // Enable limiting. Output will no longer be necessarily the same as last input.
40*ec779b8eSAndroid Build Coastguard Worker  * limiter.enable();
41*ec779b8eSAndroid Build Coastguard Worker  * limiter.setTarget(...);
42*ec779b8eSAndroid Build Coastguard Worker  * output = limiter.calculatePose(...);
43*ec779b8eSAndroid Build Coastguard Worker  * limiter.setTarget(...);
44*ec779b8eSAndroid Build Coastguard Worker  * output = limiter.calculatePose(...);
45*ec779b8eSAndroid Build Coastguard Worker  *
46*ec779b8eSAndroid Build Coastguard Worker  * // When eventually the output has been able to catch up with the last input, the limited will be
47*ec779b8eSAndroid Build Coastguard Worker  * // automatically disabled again and the output will match the input again.
48*ec779b8eSAndroid Build Coastguard Worker  * limiter.setTarget(...);
49*ec779b8eSAndroid Build Coastguard Worker  * output = limiter.calculatePose(...);
50*ec779b8eSAndroid Build Coastguard Worker  *
51*ec779b8eSAndroid Build Coastguard Worker  * As shown above, the limiter is turned on manually via enable(), but turns off automatically as
52*ec779b8eSAndroid Build Coastguard Worker  * soon as the output is able to catch up to the input. The intention is that rate limiting will be
53*ec779b8eSAndroid Build Coastguard Worker  * turned on at specific times to smooth out any artificial discontinuities introduced to the pose
54*ec779b8eSAndroid Build Coastguard Worker  * stream, but the rest of the time will be a simple passthrough.
55*ec779b8eSAndroid Build Coastguard Worker 
56*ec779b8eSAndroid Build Coastguard Worker  * setTarget(...) and calculatePose(...) don't have to be ordered in any particular way. However,
57*ec779b8eSAndroid Build Coastguard Worker  * setTarget or reset() must be called at least once prior to the first calculatePose().
58*ec779b8eSAndroid Build Coastguard Worker  *
59*ec779b8eSAndroid Build Coastguard Worker  * Calling reset() instead of setTarget() forces the output to the given pose and disables rate
60*ec779b8eSAndroid Build Coastguard Worker  * limiting.
61*ec779b8eSAndroid Build Coastguard Worker  *
62*ec779b8eSAndroid Build Coastguard Worker  * This implementation is thread-compatible, but not thread-safe.
63*ec779b8eSAndroid Build Coastguard Worker  */
64*ec779b8eSAndroid Build Coastguard Worker class PoseRateLimiter {
65*ec779b8eSAndroid Build Coastguard Worker   public:
66*ec779b8eSAndroid Build Coastguard Worker     struct Options {
67*ec779b8eSAndroid Build Coastguard Worker         float maxTranslationalVelocity = std::numeric_limits<float>::infinity();
68*ec779b8eSAndroid Build Coastguard Worker         float maxRotationalVelocity = std::numeric_limits<float>::infinity();
69*ec779b8eSAndroid Build Coastguard Worker     };
70*ec779b8eSAndroid Build Coastguard Worker 
71*ec779b8eSAndroid Build Coastguard Worker     explicit PoseRateLimiter(const Options& options);
72*ec779b8eSAndroid Build Coastguard Worker 
73*ec779b8eSAndroid Build Coastguard Worker     void enable();
74*ec779b8eSAndroid Build Coastguard Worker 
75*ec779b8eSAndroid Build Coastguard Worker     void reset(const Pose3f& target);
76*ec779b8eSAndroid Build Coastguard Worker     void setTarget(const Pose3f& target);
77*ec779b8eSAndroid Build Coastguard Worker 
78*ec779b8eSAndroid Build Coastguard Worker     Pose3f calculatePose(int64_t timestamp);
79*ec779b8eSAndroid Build Coastguard Worker 
80*ec779b8eSAndroid Build Coastguard Worker     std::string toString(unsigned level) const;
81*ec779b8eSAndroid Build Coastguard Worker 
82*ec779b8eSAndroid Build Coastguard Worker   private:
83*ec779b8eSAndroid Build Coastguard Worker     struct Point {
84*ec779b8eSAndroid Build Coastguard Worker         Pose3f pose;
85*ec779b8eSAndroid Build Coastguard Worker         int64_t timestamp;
86*ec779b8eSAndroid Build Coastguard Worker     };
87*ec779b8eSAndroid Build Coastguard Worker 
88*ec779b8eSAndroid Build Coastguard Worker     const Options mOptions;
89*ec779b8eSAndroid Build Coastguard Worker     bool mLimiting;
90*ec779b8eSAndroid Build Coastguard Worker     std::optional<Pose3f> mTargetPose;
91*ec779b8eSAndroid Build Coastguard Worker     std::optional<Point> mOutput;
92*ec779b8eSAndroid Build Coastguard Worker };
93*ec779b8eSAndroid Build Coastguard Worker 
94*ec779b8eSAndroid Build Coastguard Worker }  // namespace media
95*ec779b8eSAndroid Build Coastguard Worker }  // namespace android
96