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, | |
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) { | 70 gvr::Mat4f MatrixTranspose(const gvr::Mat4f& mat) { |
112 gvr::Mat4f result; | 71 gvr::Mat4f result; |
113 for (int i = 0; i < 4; ++i) { | 72 for (int i = 0; i < 4; ++i) { |
114 for (int k = 0; k < 4; ++k) { | 73 for (int k = 0; k < 4; ++k) { |
115 result.m[i][k] = mat.m[k][i]; | 74 result.m[i][k] = mat.m[k][i]; |
116 } | 75 } |
117 } | 76 } |
118 return result; | 77 return result; |
119 } | 78 } |
120 | 79 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 } | 176 } |
218 | 177 |
219 gvr::Vec3f GetTranslation(const gvr::Mat4f& matrix) { | 178 gvr::Vec3f GetTranslation(const gvr::Mat4f& matrix) { |
220 return {matrix.m[0][3], matrix.m[1][3], matrix.m[2][3]}; | 179 return {matrix.m[0][3], matrix.m[1][3], matrix.m[2][3]}; |
221 } | 180 } |
222 | 181 |
223 float VectorLength(const gvr::Vec3f& vec) { | 182 float VectorLength(const gvr::Vec3f& vec) { |
224 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); | 183 return sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z); |
225 } | 184 } |
226 | 185 |
| 186 gvr::Vec3f VectorSubtract(const gvr::Vec3f& a, const gvr::Vec3f& b) { |
| 187 return {a.x - b.x, a.y - b.y, a.z - b.z}; |
| 188 } |
| 189 |
227 float NormalizeVector(gvr::Vec3f& vec) { | 190 float NormalizeVector(gvr::Vec3f& vec) { |
228 float len = VectorLength(vec); | 191 float len = VectorLength(vec); |
229 vec.x /= len; | 192 vec.x /= len; |
230 vec.y /= len; | 193 vec.y /= len; |
231 vec.z /= len; | 194 vec.z /= len; |
232 return len; | 195 return len; |
233 } | 196 } |
234 | 197 |
235 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) { | 198 float VectorDot(const gvr::Vec3f& a, const gvr::Vec3f& b) { |
236 return a.x * b.x + a.y * b.y + a.z * b.z; | 199 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, | 263 const gvr::Vec3f& rayVector, |
301 float scale) { | 264 float scale) { |
302 gvr::Vec3f v; | 265 gvr::Vec3f v; |
303 v.x = rayOrigin.x + scale * rayVector.x; | 266 v.x = rayOrigin.x + scale * rayVector.x; |
304 v.y = rayOrigin.y + scale * rayVector.y; | 267 v.y = rayOrigin.y + scale * rayVector.y; |
305 v.z = rayOrigin.z + scale * rayVector.z; | 268 v.z = rayOrigin.z + scale * rayVector.z; |
306 return v; | 269 return v; |
307 } | 270 } |
308 | 271 |
309 } // namespace vr_shell | 272 } // namespace vr_shell |
OLD | NEW |