/*****************************************************************************
 * $Id: game_base_math.cc,v 1.4 2006/06/02 01:00:41 jimb Exp $
 * @file
 * @author	Jim E. Brooks  http://www.jimbrooks.org
 * @brief	Math functions.
 *//*
 * LEGAL:	COPYRIGHT (C) 2006 JIM E. BROOKS
 *          THIS SOURCE CODE IS RELEASED UNDER THE TERMS
 *          OF THE GNU GENERAL PUBLIC LICENSE (GPL).
 *****************************************************************************/

#define GAME_BASE_MATH_CC 1
#include "game_base_common.hh"
#include "game_base_math.hh"
#include "gfx_stream.hh"

////////////////////////////////////////////////////////////////////////////////
// Orienting a matrix towards a vertex (target):
//
// Idea is to yaw and pitch a matrix to orient its Z axis to point towards a target.
//
// The trick is that the calculations of the angles to yaw/pitch are asymmetric.
// The yaw angle is calculated from the X coordinate vs. the 2D distance of the X,Z coordinates.
// The pitch angle is calculated from the Y coordinate vs. the full 3D distance.
//
// When target is at (1,1,1), this is the result.
// 0.5777^2 = 0.333  (1,1,1), when normalized, is (0.333,0.333,0.333)
// Xxyzw:   0.707107  -0.408248  0.577350
// Yxyzw:   0.000000   0.816497  0.577350
// Zxyzw:  -0.707107  -0.408248  0.577350
// Oxyzw:   0.000000   0.000000  0.000000
// yaw, pitch = 45.000000, 35.264389
// CheckAngle3D() = 0.000000
//
// Note: If there was a Matrix class, these functions would be its methods.
////////////////////////////////////////////////////////////////////////////////

/*****************************************************************************
 * Calculate degree to yaw a matrix towards a target vertex.
 * @param	m
 *          matrix (in/out)
 * @param	targetLocal
 *			Target translated relative to matrix space.
 *****************************************************************************/
static fp
OrientMatrix_CalcAngleYaw( Matrix& m /*IN/OUT*/, const Vector3& targetLocal )
{
	// Calculate angle between YZ plane and X coordinate.
	fp dist = sqrtf( SQUARE(targetLocal.x)
                   + SQUARE(targetLocal.z) );
	if ( dist < 0.1 ) return 0.0;  // prevent divide error
	fp norm = targetLocal.x / dist;
	fp rad = asin( norm );
	fp deg = rad2deg( rad );

	// Change sign of degree according to X,Z coordinate.
	// This affects and depends on the way RotateMatrixLocal() rotates.
	if ( targetLocal.z < 0 )
		deg += 180.0;
	if ( (targetLocal.x < 0 && targetLocal.z < 0) || (targetLocal.x > 0 && targetLocal.z < 0) )
		deg = -deg;

	return deg;
}

/*****************************************************************************
 * Calculate degree to pitch a matrix towards a target vertex.
 * @param	m
 *          matrix (in/out)
 * @param	targetLocal
 *			Target translated relative to matrix space.
 *****************************************************************************/
static fp
OrientMatrix_CalcAnglePitch( Matrix& m /*IN/OUT*/, const Vector3& targetLocal )
{
	// Calculate angle between Y coordinate and 3D distance.
	// Visualize the 3D distance as point the Y axis,
	// and the target is some 3D point elsewhere.
	// The angle is between the target's Y coordinate and the point on the Y axis.
	fp dist = sqrtf( SQUARE(targetLocal.x)
                   + SQUARE(targetLocal.y)
                   + SQUARE(targetLocal.z) );
	if ( dist < 0.1 ) return 0.0;  // prevent divide error
	fp norm = targetLocal.y / dist;
	fp rad = asin( norm );
	fp deg = rad2deg( rad );
	return deg;
}

/*****************************************************************************
 * Check 3D angle between matrix Z axis and target.
 *****************************************************************************/
#if DEBUG
static bool
OrientMatrix_Check( const Matrix& m, const Vector3& targetLocal )
{
	// Z axis coordinates are taken from Xz/Yz/Zz column (not Zx/Zy/Zx row).
	// For example, for target at (1,1,1), the right column corresponds to (1,1,1).
	// BTW 0.5777350^2 = 0.33333333
	// Xxyzw:  0.707107  -0.408248  0.577350
	// Yxyzw:  0.000000   0.816497  0.577350
	// Zxyzw:  -0.707107 -0.408248  0.577350
	Vector3 z;
	z.x = m[Xz];
	z.y = m[Yz];
	z.z = m[Zz];
	fp rad = Angle( z, targetLocal );
	fp deg = rad2deg( rad );
	if ( EX( ABS(deg) < 0.1 ) )
	{
		return true;
	}
	else
	{
		*gStreamError << "ERROR[OrientMatrix]: angle > 0.1" << endl;
		return false;
	}
}
#endif // DEBUG

/*****************************************************************************
 * Orient a matrix so that the matrix -Z axis is pointing at a 3D point (target).
 * Only the matrix axis coefficients are changed (not the matrix origin).
 * Orient means a combination of rotations.
 *****************************************************************************/
void
OrientMatrix( Matrix& m /*IN/OUT*/, const WorldVertex& targetWorld )
{
	// Change only the matrix axis coefficients (not the matrix origin).
	m[Xx] = 1.0; m[Xy] = 0.0; m[Xz] = 0.0;
	m[Yx] = 0.0; m[Yy] = 1.0; m[Yz] = 0.0;
	m[Zx] = 0.0; m[Zy] = 0.0; m[Zz] = 1.0;

	// Translate target to matrix local space.
	Vector3 targetLocal;
	targetLocal.x = targetWorld.x - m[Ox];
	targetLocal.y = targetWorld.y - m[Oy];
	targetLocal.z = targetWorld.z - m[Oz];

	// A combination of yaw and pitch can orient
	// a matrix's Z axis towards a 3D vertex (target).
	fp degYaw   = OrientMatrix_CalcAngleYaw(   m, targetLocal );
	fp degPitch = OrientMatrix_CalcAnglePitch( m, targetLocal );
	RotateMatrixLocal( AXIS_Y, degYaw,   m );
	RotateMatrixLocal( AXIS_X, degPitch, m );

	DEBUG_CODE( OrientMatrix_Check( m, targetLocal ); )
}
