/*****************************************************************************/
/**
 * @file quaternion.h
 * @author FanHao (hao_fan_bit_ee@163.com)
 * @version 0.0
 * @date 2023-05-06
 *
 * @copyright Copyright(c) 2024 Beijing Institute of Technology.\n
 * Lab of Communication and Network
 *
 ******************************************************************************/

/**
 * @addtogroup LinkLayer
 * @{
 */

/**
 * @addtogroup LinkLayerGeometry
 * @{
 */

/**
 * @defgroup LinkLayerGeometry_Quaternion
 * @{
 */

#ifndef __QUATERNION_H
#define __QUATERNION_H

#include "coord.h"
#include "eulerangles.h"

/**
 * @struct QUATERNION
 * @brief Structure for standard quaternion
 */
typedef struct
{
	double s;
	double i;
	double j;
	double k;
} QUATERNION;

extern const QUATERNION QUATERNION_IDENTITY;
extern const QUATERNION QUATERNION_NIL;

/** @brief Subtracts two quaternions. */
QUATERNION Quaternion_Sub(QUATERNION q1, QUATERNION q2);

/** @brief Adds two quaternions. */
QUATERNION Quaternion_Add(QUATERNION q1, QUATERNION q2);

/** @brief Multiplies a quaternion by a real number. */
void Quaternion_ScalarMult(QUATERNION *q, double t);

/** @brief Multiplies a quaternion by a real number. */
QUATERNION Quaternion_GetScalarMult(QUATERNION a, double t);

/** @brief Divides a quaternion by a real number. */
void Quaternion_ScalarDiv(QUATERNION *q, double t);

/** @brief Divides a quaternion by a real number. */
QUATERNION Quaternion_GetScalarDiv(QUATERNION a, double t);

/** @brief Multiplies two quaternions. */
QUATERNION Quaternion_Mult(QUATERNION q1, QUATERNION q2);

/** @brief Divides two quaternions. */
QUATERNION Quaternion_Div(QUATERNION q1, QUATERNION q2);

/** @brief Dot product. */
double Quaternion_Dot(QUATERNION q1, QUATERNION q2);

/** @brief Calculate a quaternion length^2 (omits calling square root). */
double Quaternion_SquareLength(QUATERNION q);

/** @brief Calculate a quaternion length. */
double Quaternion_Length(QUATERNION q);

/** @brief Normalize quaternion. */
void Quaternion_Normalize(QUATERNION *q);

/** @brief Normalize quaternion. */
QUATERNION Quaternion_GetNormalized(QUATERNION q);

/** @brief Conjugated quaternion. */
void Quaternion_Conjugated(QUATERNION *q);

/** @brief Conjugated quaternion. */
QUATERNION Quaternion_GetConjugated(QUATERNION q);

/** @brief Inverse quaternion. */
void Quaternion_Inverse(QUATERNION *q);

/** @brief Inverse quaternion. */
QUATERNION Quaternion_GeInverse(QUATERNION q);

/** @brief Get rotate angle of quaternion. */
double Quaternion_GetRotationAngle(QUATERNION q);

/** @brief Get axis of quaternion. */
COORD Quaternion_GetRotationAxis(QUATERNION q);

/** @brief Get the axis and rotate angle of a unit Quaternion */
void Quaternion_GetRotationAxisAndAngle(QUATERNION q, COORD *axis, double *angle);

/** @brief Decompose the rotation on to 2 parts.
	1. Twist - rotation around the "direction" vector
	2. Swing - rotation around axis that is perpendicular to "direction" vector
	The rotation can be composed back by rotation = swing * twist

	has singularity in case of swing_rotation close to 180 degrees rotation.
	if the input quaternion is of non-unit length, the outputs are non-unit as well
	otherwise, outputs are both unit
*/
void Quaternion_GetSwingAndTwist(QUATERNION q, COORD axis, QUATERNION *swing, QUATERNION *twist);

/** @brief rotates v by this quaternion (quaternion must be unit) */
COORD Quaternion_Rotate(QUATERNION q, COORD v);

/** @brief returns the euler angles from a rotation Quaternion */
EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type);

/** @brief returns the rotation Quaternion from a euler angles */
QUATERNION Quaternion_FromeEulerAngles(EULERANGLES e, EULERANGLES_TYPE type);

/** @brief adapted from https://svn.code.sf.net/p/irrlicht/code/trunk/include/quaternion.h */
QUATERNION Quaternion_RotationFromTo(COORD from, COORD to);

/** @brief linear quaternion interpolation */
QUATERNION Quaternion_Lerp(QUATERNION q1, QUATERNION q2, double t);

/** @brief spherical linear interpolation */
QUATERNION Quaternion_Slerp(QUATERNION q1, QUATERNION q2, double t);

#endif

/** @} LinkLayerGeometry_Quaternion */

/** @} LinkLayerGeometry */

/** @} LinkLayer */