Head-tracking library for Immersive Audio

See README.md for details.

Bug: 188502620
Test: atest --host libheadtracking-test
Change-Id: I34201c4780e5e581cc96449bd89863bcbc250783
diff --git a/media/libheadtracking/Pose.cpp b/media/libheadtracking/Pose.cpp
new file mode 100644
index 0000000..9eeb2b1
--- /dev/null
+++ b/media/libheadtracking/Pose.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "media/Pose.h"
+#include "media/Twist.h"
+
+namespace android {
+namespace media {
+
+std::tuple<Pose3f, bool> moveWithRateLimit(const Pose3f& from, const Pose3f& to, float t,
+                                           float maxTranslationalVelocity,
+                                           float maxRotationalVelocity) {
+    // Never rate limit if both limits are set to infinity.
+    if (isinf(maxTranslationalVelocity) && isinf(maxRotationalVelocity)) {
+        return {to, false};
+    }
+    // Always rate limit if t is 0 (required to avoid division by 0).
+    if (t == 0) {
+        return {from, true};
+    }
+
+    Pose3f fromToTo = from.inverse() * to;
+    Twist3f twist = differentiate(fromToTo, t);
+    float angularRotationalRatio = twist.scalarRotationalVelocity() / maxRotationalVelocity;
+    float translationalVelocityRatio =
+            twist.scalarTranslationalVelocity() / maxTranslationalVelocity;
+    float maxRatio = std::max(angularRotationalRatio, translationalVelocityRatio);
+    if (maxRatio <= 1) {
+        return {to, false};
+    }
+    return {from * integrate(twist, t / maxRatio), true};
+}
+
+std::ostream& operator<<(std::ostream& os, const Pose3f& pose) {
+    os << "translation: " << pose.translation().transpose()
+       << " quaternion: " << pose.rotation().coeffs().transpose();
+    return os;
+}
+
+}  // namespace media
+}  // namespace android