2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 return (v < 0) ? 0.0 : v;
25 int getAngleChange(float *R, float *prevR, float *angleChange)
27 if (R == NULL || prevR == NULL || angleChange == NULL)
30 float rd1, rd4, rd6, rd7, rd8;
31 float ri0, ri1, ri2, ri3, ri4, ri5, ri6, ri7, ri8;
32 float pri0, pri1, pri2, pri3, pri4, pri5, pri6, pri7, pri8;
54 rd1 = pri0 * ri1 + pri3 * ri4 + pri6 * ri7;
55 rd4 = pri1 * ri1 + pri4 * ri4 + pri7 * ri7;
56 rd6 = pri2 * ri0 + pri5 * ri3 + pri8 * ri6;
57 rd7 = pri2 * ri1 + pri5 * ri4 + pri8 * ri7;
58 rd8 = pri2 * ri2 + pri5 * ri5 + pri8 * ri8;
60 angleChange[0] = atan2(rd1, rd4);
61 angleChange[1] = asin(-rd7);
62 angleChange[2] = atan2(-rd6, rd8);
66 int quatToMatrix(float *quat, float *R)
68 if (quat == NULL || R == NULL)
76 float sq_q1 = 2 * q1 * q1;
77 float sq_q2 = 2 * q2 * q2;
78 float sq_q3 = 2 * q3 * q3;
79 float q1_q2 = 2 * q1 * q2;
80 float q3_q0 = 2 * q3 * q0;
81 float q1_q3 = 2 * q1 * q3;
82 float q2_q0 = 2 * q2 * q0;
83 float q2_q3 = 2 * q2 * q3;
84 float q1_q0 = 2 * q1 * q0;
86 R[0] = 1 - sq_q2 - sq_q3;
90 R[4] = 1 - sq_q1 - sq_q3;
94 R[8] = 1 - sq_q1 - sq_q2;
99 int matrixToQuat(float *mat, float *q)
101 if (q == NULL || mat == NULL)
104 const float Hx = mat[0];
105 const float My = mat[4];
106 const float Az = mat[8];
107 q[0] = sqrtf(clamp(Hx - My - Az + 1) * 0.25f);
108 q[1] = sqrtf(clamp(-Hx + My - Az + 1) * 0.25f);
109 q[2] = sqrtf(clamp(-Hx - My + Az + 1) * 0.25f);
110 q[3] = sqrtf(clamp(Hx + My + Az + 1) * 0.25f);
111 q[0] = copysignf(q[0], mat[7] - mat[5]);
112 q[1] = copysignf(q[1], mat[2] - mat[6]);
113 q[2] = copysignf(q[2], mat[3] - mat[1]);
118 int getRotationMatrix(float *accel, float *geo, float *R, float *I)
120 if (accel == NULL || geo == NULL || R == NULL || I == NULL)
129 float Hx = Ey*Az - Ez*Ay;
130 float Hy = Ez*Ax - Ex*Az;
131 float Hz = Ex*Ay - Ey*Ax;
132 float normH = (float)sqrt(Hx*Hx + Hy*Hy + Hz*Hz);
136 float invH = 1.0f / normH;
140 float invA = 1.0f / (float)sqrt(Ax*Ax + Ay*Ay + Az*Az);
144 float Mx = Ay*Hz - Az*Hy;
145 float My = Az*Hx - Ax*Hz;
146 float Mz = Ax*Hy - Ay*Hx;
148 R[0] = Hx; R[1] = Hy; R[2] = Hz;
149 R[3] = Mx; R[4] = My; R[5] = Mz;
150 R[6] = Ax; R[7] = Ay; R[8] = Az;
152 float invE = 1.0 / (float)sqrt(Ex*Ex + Ey*Ey + Ez*Ez);
153 float c = (Ex*Mx + Ey*My + Ez*Mz) * invE;
154 float s = (Ex*Ax + Ey*Ay + Ez*Az) * invE;
156 I[0] = 1; I[1] = 0; I[2] = 0;
157 I[3] = 0; I[4] = c; I[5] = s;
158 I[6] = 0; I[7] = -s; I[8] = c;
164 int remapCoordinateSystem(float *inR, int X, int Y, float *outR)
166 if (inR == NULL || outR == NULL)
169 if ((X & 0x7C) != 0 || (Y & 0x7C) != 0)
170 return -1; /* invalid parameter */
171 if (((X & 0x3) == 0) || ((Y & 0x3) == 0))
172 return -1; /* no axis specified */
173 if ((X & 0x3) == (Y & 0x3))
174 return -1; /* same axis specified */
181 int axis_y = (z+1)%3;
182 int axis_z = (z+2)%3;
183 if (((x^axis_y)|(y^axis_z)) != 0)
186 char sx = (X >= 0x80) ? 1 : 0;
187 char sy = (Y >= 0x80) ? 1 : 0;
188 char sz = (Z >= 0x80) ? 1 : 0;
191 for (j = 0 ; j < 3 ; j++) {
193 for (i = 0 ; i < 3 ; i++) {
194 if (x == i) outR[offset+i] = sx ? -inR[offset+0] : inR[offset+0];
195 if (y == i) outR[offset+i] = sy ? -inR[offset+1] : inR[offset+1];
196 if (z == i) outR[offset+i] = sz ? -inR[offset+2] : inR[offset+2];