1 /*
2  * Copyright 2020 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 
17 #ifndef SURROUND_VIEW_SERVICE_IMPL_MATH_HELP_H_
18 #define SURROUND_VIEW_SERVICE_IMPL_MATH_HELP_H_
19 
20 #include "Matrix4x4.h"
21 #include "core_lib.h"
22 
23 #include <android-base/logging.h>
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace sv {
28 namespace V1_0 {
29 namespace implementation {
30 
31 using android_auto::surround_view::Mat4x4;
32 
33 const int gMat4Size = 4 * 4 * sizeof(float);
34 
35 const Mat4x4 gMat4Identity = {1, 0, 0, /*tx=*/0.0, 0, 1, 0, /*ty=*/0,
36                               0, 0, 1, /*tz=*/0.0, 0, 0, 0, 1};
37 
degToRad(float angleInDegrees)38 inline float degToRad(float angleInDegrees) {
39     return 1.0f * angleInDegrees / 180 * M_PI;
40 }
41 
42 typedef std::array<float, 3> VectorT;
43 typedef std::array<float, 4> HomVectorT;
44 typedef Matrix4x4<float> HomMatrixT;
45 
46 // Create a Translation matrix.
translationMatrix(const VectorT & v)47 inline HomMatrixT translationMatrix(const VectorT& v) {
48     HomMatrixT m = HomMatrixT::identity();
49     m.setRow(3, HomVectorT{v[0], v[1], v[2], 1});
50     return m;
51 }
52 
53 // Create a Rotation matrix.
rotationMatrix(const VectorT & v,float angle,int orientation)54 inline HomMatrixT rotationMatrix(const VectorT& v, float angle, int orientation) {
55     const float c = cos(angle);
56     const float s = orientation * sin(angle);
57     const float t = 1 - c;
58     const float tx = t * v[0];
59     const float ty = t * v[1];
60     const float tz = t * v[2];
61     return HomMatrixT(tx * v[0] + c, tx * v[1] + s * v[2], tx * v[2] - s * v[1], 0,
62                       tx * v[1] - s * v[2], ty * v[1] + c, ty * v[2] + s * v[0], 0,
63                       tx * v[2] + s * v[1], ty * v[2] - s * v[0], tz * v[2] + c, 0, 0, 0, 0, 1);
64 }
65 
toMat4x4(const Matrix4x4F & matrix4x4F)66 inline Mat4x4 toMat4x4(const Matrix4x4F& matrix4x4F) {
67     Mat4x4 mat4x4;
68     memcpy(&mat4x4[0], matrix4x4F.transpose().data(), gMat4Size);
69     return mat4x4;
70 }
71 
toMatrix4x4F(const Mat4x4 & mat4x4)72 inline Matrix4x4F toMatrix4x4F(const Mat4x4& mat4x4) {
73     Matrix4x4F matrix4x4F;
74     memcpy(matrix4x4F.data(), &mat4x4[0], gMat4Size);
75 
76     for (int i = 0; i < 4; i++) {
77         for (int j = 0; j < 4; j++) {
78             if (matrix4x4F(i, j) != mat4x4[i * 4 + j]) {
79                 LOG(ERROR) << "Matrix error";
80             }
81         }
82     }
83     return matrix4x4F.transpose();
84 }
85 
86 // Create a Rotation Matrix, around a unit vector by a ccw angle.
rotationMatrix(float angleInDegrees,const VectorT & axis)87 inline Mat4x4 rotationMatrix(float angleInDegrees, const VectorT& axis) {
88     return toMat4x4(rotationMatrix(axis, degToRad(angleInDegrees), 1));
89 }
90 
appendRotation(float angleInDegrees,const VectorT & axis,const Mat4x4 & mat4)91 inline Mat4x4 appendRotation(float angleInDegrees, const VectorT& axis, const Mat4x4& mat4) {
92     return toMat4x4(toMatrix4x4F(mat4) * rotationMatrix(axis, degToRad(angleInDegrees), 1));
93 }
94 
95 // Append mat_l * mat_r;
appendMat(const Mat4x4 & matL,const Mat4x4 & matR)96 inline Mat4x4 appendMat(const Mat4x4& matL, const Mat4x4& matR) {
97     return toMat4x4(toMatrix4x4F(matL) * toMatrix4x4F(matR));
98 }
99 
100 // Rotate about a point about a unit vector.
rotationAboutPoint(float angleInDegrees,const VectorT & point,const VectorT & axis)101 inline Mat4x4 rotationAboutPoint(float angleInDegrees, const VectorT& point, const VectorT& axis) {
102     VectorT pointInv = point;
103     pointInv[0] *= -1;
104     pointInv[1] *= -1;
105     pointInv[2] *= -1;
106     return toMat4x4(translationMatrix(pointInv) *
107                     rotationMatrix(axis, degToRad(angleInDegrees), 1) * translationMatrix(point));
108 }
109 
translationMatrixToMat4x4(const VectorT & translation)110 inline Mat4x4 translationMatrixToMat4x4(const VectorT& translation) {
111     return toMat4x4(translationMatrix(translation));
112 }
113 
appendTranslation(const VectorT & translation,const Mat4x4 & mat4)114 inline Mat4x4 appendTranslation(const VectorT& translation, const Mat4x4& mat4) {
115     return toMat4x4(toMatrix4x4F(mat4) * translationMatrix(translation));
116 }
117 
appendMatrix(const Mat4x4 & deltaMatrix,const Mat4x4 & currentMatrix)118 inline Mat4x4 appendMatrix(const Mat4x4& deltaMatrix, const Mat4x4& currentMatrix) {
119     return toMat4x4(toMatrix4x4F(deltaMatrix) * toMatrix4x4F(currentMatrix));
120 }
121 
122 }  // namespace implementation
123 }  // namespace V1_0
124 }  // namespace sv
125 }  // namespace automotive
126 }  // namespace hardware
127 }  // namespace android
128 
129 #endif  // SURROUND_VIEW_SERVICE_IMPL_MATH_HELP_H_
130