1212import de .robv .android .xposed .XposedBridge ;
1313import de .robv .android .xposed .XposedHelpers ;
1414import de .robv .android .xposed .callbacks .XC_LoadPackage ;
15+ import fr .frazew .virtualgyroscope .Util ;
1516import fr .frazew .virtualgyroscope .VirtualSensorListener ;
1617import fr .frazew .virtualgyroscope .XposedMod ;
1718
@@ -59,7 +60,7 @@ public static List<Object> changeSensorValues(Sensor s, float[] accelerometerVal
5960 float [] values = new float [3 ];
6061 float [] rotationMatrix = new float [9 ];
6162 SensorManager .getRotationMatrix (rotationMatrix , null , accelerometerValues , magneticValues );
62- float [] quaternion = rotationMatrixToQuaternion (rotationMatrix );
63+ float [] quaternion = Util . rotationMatrixToQuaternion (rotationMatrix );
6364
6465 values [0 ] = quaternion [1 ];
6566 values [1 ] = quaternion [2 ];
@@ -406,11 +407,11 @@ private static List<Object> filterValues(float[] values, float[][] lastFilterVal
406407 }
407408 newLastFilterValues [i ][9 ] = newValue ;
408409
409- /* float sum = 0F;
410+ float sum = 0F ;
410411 for (int j = 0 ; j < 10 ; j ++) {
411412 sum += lastFilterValues [i ][j ];
412413 }
413- newValue = sum/10;*/
414+ newValue = sum /10 ;
414415
415416 //The gyroscope is moving even after lowpass
416417 if (newValue != 0.0F ) {
@@ -432,143 +433,5 @@ private static List<Object> filterValues(float[] values, float[][] lastFilterVal
432433 private static float lowPass (float alpha , float value , float prev ) {
433434 return prev + alpha * (value - prev );
434435 }
435-
436- public static float [] normalizeQuaternion (float [] quaternion ) {
437- float [] returnQuat = new float [4 ];
438- float sqrt = (float )Math .sqrt (quaternion [0 ]*quaternion [0 ] + quaternion [1 ]*quaternion [1 ] + quaternion [2 ]*quaternion [2 ] + quaternion [3 ]*quaternion [3 ]);
439-
440- returnQuat [0 ] = quaternion [0 ] / sqrt ;
441- returnQuat [1 ] = quaternion [1 ] / sqrt ;
442- returnQuat [2 ] = quaternion [2 ] / sqrt ;
443- returnQuat [3 ] = quaternion [3 ] / sqrt ;
444-
445- return returnQuat ;
446- }
447-
448- public static float [] normalizeVector (float [] vector ) {
449- float [] newVec = new float [3 ];
450- float sqrt = (float )Math .sqrt (vector [0 ]*vector [0 ] + vector [1 ]*vector [1 ] + vector [2 ]*vector [2 ]);
451-
452- newVec [0 ] = vector [0 ] / sqrt ;
453- newVec [1 ] = vector [1 ] / sqrt ;
454- newVec [2 ] = vector [2 ] / sqrt ;
455-
456- return newVec ;
457- }
458-
459- /*
460- Subtracts a quaternion by another, this a very easy operation so nothing interesting
461- */
462- public static float [] subtractQuaternionbyQuaternion (float [] quat1 , float [] quat2 ) {
463- float [] quaternion = new float [4 ];
464-
465- quaternion [0 ] = quat1 [0 ] - quat2 [0 ];
466- quaternion [1 ] = quat1 [1 ] - quat2 [1 ];
467- quaternion [2 ] = quat1 [2 ] - quat2 [2 ];
468- quaternion [3 ] = quat1 [3 ] - quat2 [3 ];
469-
470- return quaternion ;
471- }
472-
473- /*
474- This uses the Hamilton product to multiply the vector converted to a quaternion with the rotation quaternion.
475- Returns a new quaternion which is the rotated vector.
476- Source: https://en.wikipedia.org/wiki/Quaternion#Hamilton_product
477- -- Not used, but keeping it just in case
478- */
479- public static float [] rotateVectorByQuaternion (float [] vector , float [] quaternion ) {
480- float a = vector [0 ];
481- float b = vector [1 ];
482- float c = vector [2 ];
483- float d = vector [3 ];
484-
485- float A = quaternion [0 ];
486- float B = quaternion [1 ];
487- float C = quaternion [2 ];
488- float D = quaternion [3 ];
489-
490- float newQuaternionReal = a *A - b *B - c *C - d *D ;
491- float newQuaternioni = a *B + b *A + c *D - d *C ;
492- float newQuaternionj = a *C - b *D + c *A + d *B ;
493- float newQuaternionk = a *D + b *C - c *B + d *A ;
494-
495- return new float [] {newQuaternionReal , newQuaternioni , newQuaternionj , newQuaternionk };
496- }
497-
498- private static float [] quaternionToRotationMatrix (float [] quaternion ) {
499- float [] rotationMatrix = new float [9 ];
500-
501- float w = quaternion [0 ];
502- float x = quaternion [1 ];
503- float y = quaternion [2 ];
504- float z = quaternion [3 ];
505-
506- float n = w * w + x * x + y * y + z * z ;
507- float s = n == 0 ? 0 : 2 /n ;
508- float wx = s * w * x , wy = s * w * y , wz = s * w * z ;
509- float xx = s * x * x , xy = s * x * y , xz = s * x * z ;
510- float yy = s * y * y , yz = s * y * z , zz = s * z * z ;
511-
512- rotationMatrix [0 ] = 1 - (yy + zz );
513- rotationMatrix [1 ] = xy - wz ;
514- rotationMatrix [2 ] = xz + wy ;
515- rotationMatrix [3 ] = xy + wz ;
516- rotationMatrix [4 ] = 1 - (xx + zz );
517- rotationMatrix [5 ] = yz - wx ;
518- rotationMatrix [6 ] = xz - wy ;
519- rotationMatrix [7 ] = yz + wx ;
520- rotationMatrix [8 ] = 1 - (xx + yy );
521-
522- return rotationMatrix ;
523- }
524-
525- /*
526- Credit for this code goes to http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/
527- Additional credit goes to https://en.wikipedia.org/wiki/Quaternion for helping me understand how quaternions work
528- */
529- private static float [] rotationMatrixToQuaternion (float [] rotationMatrix ) {
530- float m00 = rotationMatrix [0 ];
531- float m01 = rotationMatrix [1 ];
532- float m02 = rotationMatrix [2 ];
533- float m10 = rotationMatrix [3 ];
534- float m11 = rotationMatrix [4 ];
535- float m12 = rotationMatrix [5 ];
536- float m20 = rotationMatrix [6 ];
537- float m21 = rotationMatrix [7 ];
538- float m22 = rotationMatrix [8 ];
539-
540- float tr = m00 + m11 + m22 ;
541-
542- float qw ;
543- float qx ;
544- float qy ;
545- float qz ;
546- if (tr > 0 ) {
547- float S = (float )Math .sqrt (tr +1.0 ) * 2 ;
548- qw = 0.25F * S ;
549- qx = (m21 - m12 ) / S ;
550- qy = (m02 - m20 ) / S ;
551- qz = (m10 - m01 ) / S ;
552- } else if ((m00 > m11 )&(m00 > m22 )) {
553- float S = (float )Math .sqrt (1.0 + m00 - m11 - m22 ) * 2 ;
554- qw = (m21 - m12 ) / S ;
555- qx = 0.25F * S ;
556- qy = (m01 + m10 ) / S ;
557- qz = (m02 + m20 ) / S ;
558- } else if (m11 > m22 ) {
559- float S = (float )Math .sqrt (1.0 + m11 - m00 - m22 ) * 2 ;
560- qw = (m02 - m20 ) / S ;
561- qx = (m01 + m10 ) / S ;
562- qy = 0.25F * S ;
563- qz = (m12 + m21 ) / S ;
564- } else {
565- float S = (float )Math .sqrt (1.0 + m22 - m00 - m11 ) * 2 ;
566- qw = (m10 - m01 ) / S ;
567- qx = (m02 + m20 ) / S ;
568- qy = (m12 + m21 ) / S ;
569- qz = 0.25F * S ;
570- }
571- return new float [] {qw , qx , qy , qz };
572- }
573436}
574437
0 commit comments