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 "device/vr/vr_math.h" | 5 #include "device/vr/vr_math.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 | 10 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 const float m31 = 2.0f * (xz - yw); | 170 const float m31 = 2.0f * (xz - yw); |
171 const float m32 = 2.0f * (yz + xw); | 171 const float m32 = 2.0f * (yz + xw); |
172 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; | 172 const float m33 = 1.0f - 2.0f * x2 - 2.0f * y2; |
173 | 173 |
174 *out = {{{{m11, m12, m13, 0.0f}}, | 174 *out = {{{{m11, m12, m13, 0.0f}}, |
175 {{m21, m22, m23, 0.0f}}, | 175 {{m21, m22, m23, 0.0f}}, |
176 {{m31, m32, m33, 0.0f}}, | 176 {{m31, m32, m33, 0.0f}}, |
177 {{0.0f, 0.0f, 0.0f, 1.0f}}}}; | 177 {{0.0f, 0.0f, 0.0f, 1.0f}}}}; |
178 } | 178 } |
179 | 179 |
| 180 vr::Quatf GetVectorRotation(const gfx::Vector3dF& from, |
| 181 const gfx::Vector3dF& to) { |
| 182 float dot = gfx::DotProduct(from, to); |
| 183 float norm = sqrt(from.LengthSquared() * to.LengthSquared()); |
| 184 float real = norm + dot; |
| 185 gfx::Vector3dF w; |
| 186 if (real < 1.e-6f * norm) { |
| 187 real = 0.0f; |
| 188 w = fabsf(from.x()) > fabsf(from.z()) |
| 189 ? gfx::Vector3dF{-from.y(), from.x(), 0.0f} |
| 190 : gfx::Vector3dF{0.0f, -from.z(), from.y()}; |
| 191 } else { |
| 192 w = gfx::CrossProduct(from, to); |
| 193 } |
| 194 vr::Quatf result{w.x(), w.y(), w.z(), real}; |
| 195 NormalizeQuat(&result); |
| 196 return result; |
| 197 } |
| 198 |
| 199 vr::Quatf QuatSum(const vr::Quatf& a, const vr::Quatf& b) { |
| 200 return {a.qx + b.qx, a.qy + b.qy, a.qz + b.qz, a.qw + b.qw}; |
| 201 } |
| 202 |
| 203 vr::Quatf QuatProduct(const vr::Quatf& a, const vr::Quatf& b) { |
| 204 return {a.qw * b.qx + a.qx * b.qw + a.qy * b.qz - a.qz * b.qy, |
| 205 a.qw * b.qy - a.qx * b.qz + a.qy * b.qw + a.qz * b.qx, |
| 206 a.qw * b.qz + a.qx * b.qy - a.qy * b.qx + a.qz * b.qw, |
| 207 a.qw * b.qw - a.qx * b.qx - a.qy * b.qy - a.qz * b.qz}; |
| 208 } |
| 209 |
| 210 vr::Quatf ScaleQuat(const vr::Quatf& q, float s) { |
| 211 return {q.qx * s, q.qy * s, q.qz * s, q.qw * s}; |
| 212 } |
| 213 |
| 214 vr::Quatf InvertQuat(const vr::Quatf& quat) { |
| 215 return {-quat.qx, -quat.qy, -quat.qz, quat.qw}; |
| 216 } |
| 217 |
| 218 float QuatAngleDegrees(const vr::Quatf& a, const vr::Quatf& b) { |
| 219 return QuatProduct(b, InvertQuat(a)).qw; |
| 220 } |
| 221 |
| 222 vr::Quatf QuatLerp(const vr::Quatf& a, const vr::Quatf& b, float t) { |
| 223 auto result = QuatSum(ScaleQuat(a, 1.0f - t), ScaleQuat(b, t)); |
| 224 NormalizeQuat(&result); |
| 225 return result; |
| 226 } |
| 227 |
| 228 gfx::Vector3dF QuatSlerp(const gfx::Vector3dF& v_start, |
| 229 const gfx::Vector3dF& v_end, |
| 230 float percent) { |
| 231 auto start = v_start; |
| 232 auto end = v_end; |
| 233 NormalizeVector(&start); |
| 234 NormalizeVector(&end); |
| 235 float dot = Clampf(gfx::DotProduct(start, end), -1.0f, 1.0f); |
| 236 float theta = acos(dot) * percent; |
| 237 auto relative_vec = end - gfx::ScaleVector3d(start, dot); |
| 238 NormalizeVector(&relative_vec); |
| 239 return gfx::ScaleVector3d(start, cos(theta)) + |
| 240 gfx::ScaleVector3d(relative_vec, sin(theta)); |
| 241 } |
| 242 |
180 gfx::Point3F GetRayPoint(const gfx::Point3F& rayOrigin, | 243 gfx::Point3F GetRayPoint(const gfx::Point3F& rayOrigin, |
181 const gfx::Vector3dF& rayVector, | 244 const gfx::Vector3dF& rayVector, |
182 float scale) { | 245 float scale) { |
183 return rayOrigin + gfx::ScaleVector3d(rayVector, scale); | 246 return rayOrigin + gfx::ScaleVector3d(rayVector, scale); |
184 } | 247 } |
185 | 248 |
186 float Distance(const gfx::Point3F& p1, const gfx::Point3F& p2) { | 249 float Distance(const gfx::Point3F& p1, const gfx::Point3F& p2) { |
187 return std::sqrt(p1.SquaredDistanceTo(p2)); | 250 return std::sqrt(p1.SquaredDistanceTo(p2)); |
188 } | 251 } |
189 | 252 |
190 bool XZAngle(const gfx::Vector3dF& vec1, | 253 bool XZAngle(const gfx::Vector3dF& vec1, |
191 const gfx::Vector3dF& vec2, | 254 const gfx::Vector3dF& vec2, |
192 float* angle) { | 255 float* angle) { |
193 float len1 = vec1.Length(); | 256 float len1 = vec1.Length(); |
194 float len2 = vec2.Length(); | 257 float len2 = vec2.Length(); |
195 if (len1 == 0 || len2 == 0) | 258 if (len1 == 0 || len2 == 0) |
196 return false; | 259 return false; |
197 float cross_p = vec1.x() * vec2.z() - vec1.z() * vec2.x(); | 260 float cross_p = vec1.x() * vec2.z() - vec1.z() * vec2.x(); |
198 *angle = asin(cross_p / (len1 * len2)); | 261 *angle = asin(cross_p / (len1 * len2)); |
199 return true; | 262 return true; |
200 } | 263 } |
201 | 264 |
| 265 gfx::Vector3dF ToVector(const gfx::Point3F& p) { |
| 266 return {p.x(), p.y(), p.z()}; |
| 267 } |
| 268 |
| 269 gfx::Point3F ToPoint(const gfx::Vector3dF& p) { |
| 270 return {p.x(), p.y(), p.z()}; |
| 271 } |
| 272 |
| 273 gfx::Point3F ScalePoint(const gfx::Point3F& p, const gfx::Vector3dF& s) { |
| 274 return gfx::ScalePoint(p, s.x(), s.y(), s.z()); |
| 275 } |
| 276 |
| 277 gfx::Vector3dF ScaleVector(const gfx::Vector3dF& v, const gfx::Vector3dF& s) { |
| 278 return gfx::ScaleVector3d(v, s.x(), s.y(), s.z()); |
| 279 } |
| 280 |
| 281 float Clampf(float value, float min, float max) { |
| 282 if (value < min) |
| 283 return min; |
| 284 if (value > max) |
| 285 return max; |
| 286 return value; |
| 287 } |
| 288 |
202 } // namespace vr | 289 } // namespace vr |
OLD | NEW |