1 | /* |
2 | Copyright 2018 Google Inc. All Rights Reserved. |
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 | |
17 | #include "platforms/common/utils.h" |
18 | |
19 | namespace vraudio { |
20 | |
21 | void FlipZAxis(Eigen::Matrix4f* matrix) { |
22 | // This operation is equivalent to: |
23 | // matrix = flipZ * matrix * flipZ |
24 | // where flipZ is the scale(1, 1, -1) matrix. |
25 | (*matrix)(0, 2) *= -1.0f; |
26 | (*matrix)(1, 2) *= -1.0f; |
27 | (*matrix)(2, 0) *= -1.0f; |
28 | (*matrix)(2, 1) *= -1.0f; |
29 | (*matrix)(2, 3) *= -1.0f; |
30 | (*matrix)(3, 2) *= -1.0f; |
31 | } |
32 | |
33 | Eigen::Quaternionf GetQuaternion(const Eigen::Matrix4f& matrix) { |
34 | const Eigen::Matrix3f rotation_matrix = matrix.block(startRow: 0, startCol: 0, blockRows: 3, blockCols: 3); |
35 | Eigen::Quaternionf quaternion(rotation_matrix); |
36 | return quaternion.normalized(); |
37 | } |
38 | |
39 | Eigen::Vector3f GetPosition(const Eigen::Matrix4f& matrix) { |
40 | return Eigen::Vector3f(matrix.col(i: 3).head<3>()); |
41 | } |
42 | |
43 | Eigen::Matrix4f GetTransformMatrix(const Eigen::Vector3f& position, |
44 | const Eigen::Vector3f& forward, |
45 | const Eigen::Vector3f& up) { |
46 | Eigen::Matrix4f transform_matrix; |
47 | // Compose the homogeneous vectors for the transformation matrix. |
48 | const Eigen::Vector3f right = up.cross(other: forward); |
49 | const Eigen::Vector4f position_4(position.x(), position.y(), position.z(), |
50 | 1.0f); |
51 | const Eigen::Vector4f forward_4(forward.x(), forward.y(), forward.z(), 0.0f); |
52 | const Eigen::Vector4f up_4(up.x(), up.y(), up.z(), 0.0f); |
53 | const Eigen::Vector4f right_4(right.x(), right.y(), right.z(), 0.0f); |
54 | // Fill in the transformation matrix. |
55 | transform_matrix << right_4, up_4, forward_4, position_4; |
56 | return transform_matrix; |
57 | } |
58 | |
59 | } // namespace vraudio |
60 | |