1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"){}
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <android-base/stringprintf.h>
17
18 #include "PoseRateLimiter.h"
19
20 namespace android {
21 namespace media {
22 using android::base::StringAppendF;
23
PoseRateLimiter(const Options & options)24 PoseRateLimiter::PoseRateLimiter(const Options& options) : mOptions(options), mLimiting(false) {}
25
enable()26 void PoseRateLimiter::enable() {
27 mLimiting = true;
28 }
29
reset(const Pose3f & target)30 void PoseRateLimiter::reset(const Pose3f& target) {
31 mLimiting = false;
32 mTargetPose = target;
33 }
34
setTarget(const Pose3f & target)35 void PoseRateLimiter::setTarget(const Pose3f& target) {
36 mTargetPose = target;
37 }
38
calculatePose(int64_t timestamp)39 Pose3f PoseRateLimiter::calculatePose(int64_t timestamp) {
40 assert(mTargetPose.has_value());
41 Pose3f pose;
42 if (mLimiting && mOutput.has_value()) {
43 std::tie(pose, mLimiting) = moveWithRateLimit(
44 mOutput->pose, mTargetPose.value(), timestamp - mOutput->timestamp,
45 mOptions.maxTranslationalVelocity, mOptions.maxRotationalVelocity);
46 } else {
47 pose = mTargetPose.value();
48 }
49 mOutput = Point{pose, timestamp};
50 return pose;
51 }
52
toString(unsigned level) const53 std::string PoseRateLimiter::toString(unsigned level) const {
54 std::string ss(level, ' ');
55 if (mLimiting) {
56 StringAppendF(&ss, "PoseRateLimiter: enabled with target: %s\n",
57 mTargetPose.has_value() ? mTargetPose.value().toString().c_str() : "NULL");
58 } else {
59 StringAppendF(&ss, "PoseRateLimiter: disabled\n");
60 }
61 return ss;
62 }
63 } // namespace media
64 } // namespace android
65