OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/vr_math.h" | 5 #include "chrome/browser/android/vr_shell/vr_math.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 namespace vr_shell { | 9 namespace vr_shell { |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... | |
39 for (int j = 0; j < 4; ++j) { | 39 for (int j = 0; j < 4; ++j) { |
40 tmat.m[i][j] = mat.m[i][j]; | 40 tmat.m[i][j] = mat.m[i][j]; |
41 } | 41 } |
42 } | 42 } |
43 } | 43 } |
44 tmat.m[0][3] += x; | 44 tmat.m[0][3] += x; |
45 tmat.m[1][3] += y; | 45 tmat.m[1][3] += y; |
46 tmat.m[2][3] += z; | 46 tmat.m[2][3] += z; |
47 } | 47 } |
48 | 48 |
49 // Right multiply a translation matrix. | |
50 void TranslateMRight(gvr::Mat4f& tmat, | |
tiborg
2017/03/06 17:51:43
Should we delete all these matrix operations? Mayb
cjgrant
2017/03/06 18:41:02
We should add the methods only when we need them.
| |
51 gvr::Mat4f& mat, | |
52 float x, | |
53 float y, | |
54 float z) { | |
55 if (&tmat != &mat) { | |
56 for (int i = 0; i < 4; ++i) { | |
57 for (int j = 0; j < 3; ++j) { | |
58 tmat.m[i][j] = mat.m[i][j]; | |
59 } | |
60 } | |
61 } | |
62 | |
63 for (int i = 0; i < 4; i++) { | |
64 tmat.m[i][3] = | |
65 mat.m[i][0] * x + mat.m[i][1] * y + mat.m[i][2] * z + mat.m[i][3]; | |
66 } | |
67 } | |
68 | |
69 // Left multiply a scale matrix. | 49 // Left multiply a scale matrix. |
70 void ScaleM(gvr::Mat4f& tmat, | 50 void ScaleM(gvr::Mat4f& tmat, |
71 const gvr::Mat4f& mat, | 51 const gvr::Mat4f& mat, |
72 float x, | 52 float x, |
73 float y, | 53 float y, |
74 float z) { | 54 float z) { |
75 if (&tmat != &mat) { | 55 if (&tmat != &mat) { |
76 for (int i = 0; i < 4; ++i) { | 56 for (int i = 0; i < 4; ++i) { |
77 for (int j = 0; j < 3; ++j) { | 57 for (int j = 0; j < 3; ++j) { |
78 tmat.m[i][j] = mat.m[i][j]; | 58 tmat.m[i][j] = mat.m[i][j]; |
79 } | 59 } |
80 } | 60 } |
81 } | 61 } |
82 // Multiply all rows including translation components. | 62 // Multiply all rows including translation components. |
83 for (int j = 0; j < 4; ++j) { | 63 for (int j = 0; j < 4; ++j) { |
84 tmat.m[0][j] *= x; | 64 tmat.m[0][j] *= x; |
85 tmat.m[1][j] *= y; | 65 tmat.m[1][j] *= y; |
86 tmat.m[2][j] *= z; | 66 tmat.m[2][j] *= z; |
87 } | 67 } |
88 } | 68 } |
89 | 69 |
90 // Right multiply a scale matrix. | |
91 void ScaleMRight(gvr::Mat4f& tmat, | |
92 const gvr::Mat4f& mat, | |
93 float x, | |
94 float y, | |
95 float z) { | |
96 if (&tmat != &mat) { | |
97 for (int i = 0; i < 4; ++i) { | |
98 for (int j = 0; j < 3; ++j) { | |
99 tmat.m[i][j] = mat.m[i][j]; | |
100 } | |
101 } | |
102 } | |
103 // Multiply columns, don't change translation components. | |
104 for (int i = 0; i < 3; ++i) { | |
105 tmat.m[i][0] *= x; | |
106 tmat.m[i][1] *= y; | |
107 tmat.m[i][2] *= z; | |
108 } | |
109 } | |
110 | |
111 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { | |
112 gvr::Mat4f result; | |
113 for (int i = 0; i < 4; ++i) { | |
114 for (int k = 0; k < 4; ++k) { | |
115 result.m[i][k] = mat.m[k][i]; | |
116 } | |
117 } | |
118 return result; | |
119 } | |
120 | |
121 std::array<float, 4> MatrixVectorMul(const gvr::Mat4f& matrix, | |
122 const std::array<float, 4>& vec) { | |
123 std::array<float, 4> result; | |
124 for (int i = 0; i < 4; ++i) { | |
125 result[i] = 0; | |
126 for (int k = 0; k < 4; ++k) { | |
127 result[i] += matrix.m[i][k] * vec[k]; | |
128 } | |
129 } | |
130 return result; | |
131 } | |
132 | |
133 std::array<float, 3> MatrixVectorMul(const gvr::Mat4f& matrix, | |
134 const std::array<float, 3>& vec) { | |
135 // Use homogeneous coordinates for the multiplication. | |
136 std::array<float, 4> vec_h = {{vec[0], vec[1], vec[2], 1.0f}}; | |
137 std::array<float, 4> result; | |
138 for (int i = 0; i < 4; ++i) { | |
139 result[i] = 0; | |
140 for (int k = 0; k < 4; ++k) { | |
141 result[i] += matrix.m[i][k] * vec_h[k]; | |
142 } | |
143 } | |
144 // Convert back from homogeneous coordinates. | |
145 float rw = 1.0f / result[3]; | |
146 return {{rw * result[0], rw * result[1], rw * result[2]}}; | |
147 } | |
148 | |
149 gvr::Vec3f MatrixVectorMul(const gvr::Mat4f& m, const gvr::Vec3f& v) { | 70 gvr::Vec3f MatrixVectorMul(const gvr::Mat4f& m, const gvr::Vec3f& v) { |
150 gvr::Vec3f res; | 71 gvr::Vec3f res; |
151 res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3]; | 72 res.x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3]; |
152 res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3]; | 73 res.y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3]; |
153 res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3]; | 74 res.z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3]; |
154 return res; | 75 return res; |
155 } | 76 } |
156 | 77 |
157 // Rotation only, ignore translation components. | 78 // Rotation only, ignore translation components. |
158 gvr::Vec3f MatrixVectorRotate(const gvr::Mat4f& m, const gvr::Vec3f& v) { | 79 gvr::Vec3f MatrixVectorRotate(const gvr::Mat4f& m, const gvr::Vec3f& v) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 } | 138 } |
218 | 139 |
219 gvr::Vec3f GetTranslation(const gvr::Mat4f& matrix) { | 140 gvr::Vec3f GetTranslation(const gvr::Mat4f& matrix) { |
220 return {matrix.m[0][3], matrix.m[1][3], matrix.m[2][3]}; | 141 return {matrix.m[0][3], matrix.m[1][3], matrix.m[2][3]}; |
221 } | 142 } |
222 | 143 |
223 float VectorLength(const gvr::Vec3f& vec) { | 144 float VectorLength(const gvr::Vec3f& vec) { |
224 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); | 145 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); |
225 } | 146 } |
226 | 147 |
148 gvr::Vec3f VectorSubtract(const gvr::Vec3f& a, const gvr::Vec3f& b) { | |
149 return {a.x - b.x, a.y - b.y, a.z - b.z}; | |
150 } | |
151 | |
227 float NormalizeVector(gvr::Vec3f& vec) { | 152 float NormalizeVector(gvr::Vec3f& vec) { |
228 float len = VectorLength(vec); | 153 float len = VectorLength(vec); |
229 vec.x /= len; | 154 vec.x /= len; |
230 vec.y /= len; | 155 vec.y /= len; |
231 vec.z /= len; | 156 vec.z /= len; |
232 return len; | 157 return len; |
233 } | 158 } |
234 | 159 |
235 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) { | 160 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) { |
236 return a.x * b.x + a.y * b.y + a.z * b.z; | 161 return a.x * b.x + a.y * b.y + a.z * b.z; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
300 const gvr::Vec3f& rayVector, | 225 const gvr::Vec3f& rayVector, |
301 float scale) { | 226 float scale) { |
302 gvr::Vec3f v; | 227 gvr::Vec3f v; |
303 v.x = rayOrigin.x + scale * rayVector.x; | 228 v.x = rayOrigin.x + scale * rayVector.x; |
304 v.y = rayOrigin.y + scale * rayVector.y; | 229 v.y = rayOrigin.y + scale * rayVector.y; |
305 v.z = rayOrigin.z + scale * rayVector.z; | 230 v.z = rayOrigin.z + scale * rayVector.z; |
306 return v; | 231 return v; |
307 } | 232 } |
308 | 233 |
309 } // namespace vr_shell | 234 } // namespace vr_shell |
OLD | NEW |