in Networked Physics/Assets/Scripts/Snapshot.cs [67:180]
public static void QuaternionToSmallestThree( Quaternion quaternion, out uint largest, out uint integer_a, out uint integer_b, out uint integer_c )
{
const float minimum = - 1.0f / 1.414214f; // 1.0f / sqrt(2)
const float maximum = + 1.0f / 1.414214f;
const float scale = (float) ( ( 1 << Constants.RotationBits ) - 1 );
float x = quaternion.x;
float y = quaternion.y;
float z = quaternion.z;
float w = quaternion.w;
float abs_x = Math.Abs( x );
float abs_y = Math.Abs( y );
float abs_z = Math.Abs( z );
float abs_w = Math.Abs( w );
float largest_value = abs_x;
largest = 0;
if ( abs_y > largest_value )
{
largest = 1;
largest_value = abs_y;
}
if ( abs_z > largest_value )
{
largest = 2;
largest_value = abs_z;
}
if ( abs_w > largest_value )
{
largest = 3;
largest_value = abs_w;
}
float a = 0;
float b = 0;
float c = 0;
switch ( largest )
{
case 0:
if ( x >= 0 )
{
a = y;
b = z;
c = w;
}
else
{
a = -y;
b = -z;
c = -w;
}
break;
case 1:
if ( y >= 0 )
{
a = x;
b = z;
c = w;
}
else
{
a = -x;
b = -z;
c = -w;
}
break;
case 2:
if ( z >= 0 )
{
a = x;
b = y;
c = w;
}
else
{
a = -x;
b = -y;
c = -w;
}
break;
case 3:
if ( w >= 0 )
{
a = x;
b = y;
c = z;
}
else
{
a = -x;
b = -y;
c = -z;
}
break;
}
float normal_a = ( a - minimum ) / ( maximum - minimum );
float normal_b = ( b - minimum ) / ( maximum - minimum );
float normal_c = ( c - minimum ) / ( maximum - minimum );
integer_a = (uint) Math.Floor( normal_a * scale + 0.5f );
integer_b = (uint) Math.Floor( normal_b * scale + 0.5f );
integer_c = (uint) Math.Floor( normal_c * scale + 0.5f );
}