OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/vr/vr_transform_util.h" | |
6 | |
7 #include <math.h> | |
8 | |
9 namespace content { | |
10 | |
11 // Source: | |
12 // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQu
aternion/ | |
13 VRVector4Ptr MatrixToOrientationQuaternion( | |
14 const float m0[3], const float m1[3], const float m2[3]) { | |
15 VRVector4Ptr out = VRVector4::New(); | |
16 float trace = m0[0] + m1[1] + m2[2]; | |
17 float root; | |
18 if (trace > 0.0f) { | |
19 root = sqrtf(1.0f + trace) * 2.0f; | |
20 out->x = (m2[1] - m1[2]) / root; | |
21 out->y = (m0[2] - m2[0]) / root; | |
22 out->z = (m1[0] - m0[1]) / root; | |
23 out->w = 0.25f * root; | |
24 } else if ((m0[0] > m1[1]) && (m0[0] > m2[2])) { | |
25 root = sqrtf(1.0f + m0[0] - m1[1] - m2[2]) * 2.0f; | |
26 out->x = 0.25f * root; | |
27 out->y = (m0[1] + m1[0]) / root; | |
28 out->z = (m0[2] + m2[0]) / root; | |
29 out->w = (m2[1] - m1[2]) / root; | |
30 } else if (m1[1] > m2[2]) { | |
31 root = sqrtf(1.0f + m1[1] - m0[0] - m2[2]) * 2.0f; | |
32 out->x = (m0[1] + m1[0]) / root; | |
33 out->y = 0.25f * root; | |
34 out->z = (m1[2] + m2[1]) / root; | |
35 out->w = (m0[2] - m2[0]) / root; | |
36 } else { | |
37 root = sqrtf(1.0f + m2[2] - m0[0] - m1[1]) * 2.0f; | |
38 out->x = (m0[2] + m2[0]) / root; | |
39 out->y = (m1[2] + m2[1]) / root; | |
40 out->z = 0.25f * root; | |
41 out->w = (m1[0] - m0[1]) / root; | |
42 } | |
43 return out.Pass(); | |
44 } | |
45 | |
46 } // namespace content | |
OLD | NEW |