Debian Codegraph @

libs/gltfio/src/math.h

raw
1 /*
2 * Copyright (C) 2019 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 GLTFIO_MATH_H
18 #define GLTFIO_MATH_H
19
20 #include <math/quat.h>
21 #include <math/vec3.h>
22 #include <math/mat3.h>
23 #include <math/mat4.h>
24 #include <math/TVecHelpers.h>
25
26 namespace gltfio {
27
28 template <typename T>
29 T cubicSpline(const T& vert0, const T& tang0, const T& vert1, const T& tang1, float t) {
30 float tt = t * t, ttt = tt * t;
31 float s2 = -2 * ttt + 3 * tt, s3 = ttt - tt;
32 float s0 = 1 - s2, s1 = s3 - tt + t;
33 T p0 = vert0;
34 T m0 = tang0;
35 T p1 = vert1;
36 T m1 = tang1;
37 return s0 * p0 + s1 * m0 * t + s2 * p1 + s3 * m1 * t;
38 }
39
40 inline void decomposeMatrix(const filament::math::mat4f& mat, filament::math::float3* translation,
41 filament::math::quatf* rotation, filament::math::float3* scale) {
42 using namespace filament::math;
43
44 // Extract translation.
45 *translation = mat[3].xyz;
46
47 // Extract upper-left for determinant computation.
48 const float a = mat[0][0];
49 const float b = mat[0][1];
50 const float c = mat[0][2];
51 const float d = mat[1][0];
52 const float e = mat[1][1];
53 const float f = mat[1][2];
54 const float g = mat[2][0];
55 const float h = mat[2][1];
56 const float i = mat[2][2];
57 const float A = e * i - f * h;
58 const float B = f * g - d * i;
59 const float C = d * h - e * g;
60
61 // Extract scale.
62 const float det(a * A + b * B + c * C);
63 float scalex = length(float3({a, b, c}));
64 float scaley = length(float3({d, e, f}));
65 float scalez = length(float3({g, h, i}));
66 float3 s = { scalex, scaley, scalez };
67 if (det < 0) {
68 s = -s;
69 }
70 *scale = s;
71
72 // Remove scale from the matrix if it is not close to zero.
73 mat4f clone = mat;
74 if (std::abs(det) > std::numeric_limits<float>::epsilon()) {
75 clone[0] /= s.x;
76 clone[1] /= s.y;
77 clone[2] /= s.z;
78 // Extract rotation
79 *rotation = clone.toQuaternion();
80 } else {
81 // Set to identity if close to zero
82 *rotation = quatf(1);
83 }
84 }
85
86 inline filament::math::mat4f composeMatrix(const filament::math::float3& translation,
87 const filament::math::quatf& rotation, const filament::math::float3& scale) {
88 float tx = translation[0];
89 float ty = translation[1];
90 float tz = translation[2];
91 float qx = rotation[0];
92 float qy = rotation[1];
93 float qz = rotation[2];
94 float qw = rotation[3];
95 float sx = scale[0];
96 float sy = scale[1];
97 float sz = scale[2];
98 return filament::math::mat4f(
99 (1 - 2 * qy*qy - 2 * qz*qz) * sx,
100 (2 * qx*qy + 2 * qz*qw) * sx,
101 (2 * qx*qz - 2 * qy*qw) * sx,
102 0.f,
103 (2 * qx*qy - 2 * qz*qw) * sy,
104 (1 - 2 * qx*qx - 2 * qz*qz) * sy,
105 (2 * qy*qz + 2 * qx*qw) * sy,
106 0.f,
107 (2 * qx*qz + 2 * qy*qw) * sz,
108 (2 * qy*qz - 2 * qx*qw) * sz,
109 (1 - 2 * qx*qx - 2 * qy*qy) * sz,
110 0.f, tx, ty, tz, 1.f);
111 }
112
113 inline filament::math::mat3f matrixFromUvTransform(const float offset[2], float rotation,
114 const float scale[2]) {
115 float tx = offset[0];
116 float ty = offset[1];
117 float sx = scale[0];
118 float sy = scale[1];
119 float c = cos(rotation);
120 float s = sin(rotation);
121 return filament::math::mat3f(sx * c, sx * s, tx, -sy * s, sy * c, ty, 0.0f, 0.0f, 1.0f);
122 };
123
124 } // namespace gltfio
125
126 #endif // GLTFIO_MATH_H