/*****************************************************************************/
/**
 * @file coord.c
 * @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
 * @{
 */

/**
 * @addtogroup LinkLayerGeometry_Coord
 * @{
 */

#include <math.h>
#include "coord.h"

const COORD COORD_NIL = {NAN, NAN, NAN};
const COORD COORD_ZERO = {0.0, 0.0, 0.0};
const COORD COORD_ONE = {1.0, 1.0, 1.0};
const COORD COORD_X_AXIS = {1.0, 0.0, 0.0};
const COORD COORD_Y_AXIS = {0.0, 1.0, 0.0};
const COORD COORD_Z_AXIS = {0.0, 0.0, 1.0};

/** @brief Subtracts two coordinate vectors. */
COORD Coord_Sub(COORD a, COORD b)
{
	COORD ret;
	ret.x = a.x - b.x;
	ret.y = a.y - b.y;
	ret.z = a.z - b.z;
	return ret;
}

/** @brief Adds two coordinate vectors. */
COORD Coord_Add(COORD a, COORD b)
{
	COORD ret;
	ret.x = a.x + b.x;
	ret.y = a.y + b.y;
	ret.z = a.z + b.z;
	return ret;
}

/** @brief Multiplies a coordinate vector by a real number. */
void Coord_ScalarMult(COORD *a, double t)
{
	a->x *= t;
	a->y *= t;
	a->z *= t;
}

/** @brief Multiplies a coordinate vector by a real number. */
COORD Coord_GetScalarMult(COORD a, double t)
{
	COORD ret;
	ret.x = a.x * t;
	ret.y = a.y * t;
	ret.z = a.z * t;
	return ret;
}

/** @brief Divides a coordinate vector by a real number. */
void Coord_ScalarDiv(COORD *a, double t)
{
	a->x /= t;
	a->y /= t;
	a->z /= t;
}

/** @brief Divides a coordinate vector by a real number. */
COORD Coord_GetScalarDiv(COORD a, double t)
{
	COORD ret;
	ret.x = a.x / t;
	ret.y = a.y / t;
	ret.z = a.z / t;
	return ret;
}

/** @brief Dot product. */
inline double Coord_Dot(COORD a, COORD b)
{
	return (a.x * b.x + a.y * b.y + a.z * b.z);
}

/** @brief Cross product. */
COORD Coord_Cross(COORD a, COORD b)
{
	COORD ret;
	ret.x = a.y * b.z - a.z * b.y;
	ret.y = a.z * b.x - a.x * b.z;
	ret.z = a.x * b.y - a.y * b.x;
	return ret;
}

/** @brief Multiplies two coordinate vector in elementwise. */
COORD Coord_ElementwiseMult(COORD a, COORD b)
{
	COORD ret;
	ret.x = a.x * b.x;
	ret.y = a.y * b.y;
	ret.z = a.z * b.z;
	return ret;
}

/** @brief Divides two coordinate vector in elementwise. */
COORD Coord_ElementwiseDiv(COORD a, COORD b)
{
	COORD ret;
	ret.x = a.x / b.x;
	ret.y = a.y / b.y;
	ret.z = a.z / b.z;
	return ret;
}

/** @brief Calculate a coordinate vector length^2 (omits calling square root). */
double Coord_SquareLength(COORD a)
{
	return (a.x * a.x + a.y * a.y + a.z * a.z);
}

/** @brief Calculate a coordinate vector length. */
double Coord_Length(COORD a)
{
	return sqrt(Coord_SquareLength(a));
}

/** @brief Calculate two coordinates' distance. */
double Coord_Distance(COORD a, COORD b)
{
	return Coord_Length(Coord_Sub(a, b));
}

/** @brief Normalize coordinate vector. */
void Coord_Normalize(COORD *a)
{
	Coord_ScalarDiv(a, Coord_Length(*a));
}

/** @brief Normalize coordinate vector. */
COORD Coord_GetNormalized(COORD a)
{
	Coord_Normalize(&a);
	return a;
}

/** @brief Returns the angle between the two vectors. */
double Coord_Angle(COORD a, COORD b)
{
	Coord_Normalize(&a);
	Coord_Normalize(&b);
	return acos(Coord_Dot(a, b));
}

// Additional Coordinates convert
// TODO: Confirm correctness
/** @brief Convert coordinate from Launch coordinate system to ECEF. */
COORD Coord_Launch2Ecef(COORD o, COORD p, double A, double B, double lambda)
{
	COORD ret;
	double sinA, cosA, sinB, cosB, sinL, cosL;
	double S11, S12, S13, S21, S22, S23, S31, S32, S33;
	sinA = sin(A);
	cosA = cos(A);
	sinB = sin(B);
	cosB = cos(B);
	sinL = sin(lambda);
	cosL = cos(lambda);

	S11 = -sinA * sinL - cosA * sinB * cosL;
	S12 = cosB * cosL;
	S13 = -cosA * sinL + sinA * sinB * cosL;

	S21 = sinA * cosL - cosA * sinB * sinL;
	S22 = cosB * sinL;
	S23 = cosA * cosL + sinA * sinB * sinL;

	S31 = cosA * cosB;
	S32 = sinB;
	S33 = -sinA * cosB;

	ret.x = p.x * (S11 + S21 + S31) + o.x;
	ret.y = p.y * (S12 + S22 + S32) + o.y;
	ret.z = p.z * (S13 + S23 + S33) + o.z;

	return ret;
}

/** @brief Convert coordinate from launch coordinate system to Inertia coordinate system. */
COORD Coord_Launch2Inertia(COORD o, COORD p, double A, double B, double we, double t)
{
	COORD ret;
	double OmgX, OmgY, OmgZ, sinwet, coswet;
	double M11, M12, M13, M21, M22, M23, M31, M32, M33;
	OmgX = cos(A) * cos(B);
	OmgY = sin(B);
	OmgZ = -sin(A) * cos(B);
	sinwet = sin(we * t);
	coswet = cos(we * t);

	M11 = 1 - (1 - OmgX * OmgX) * (1 - coswet);
	M12 = OmgX * OmgY * (1 - coswet) + OmgZ * sinwet;
	M13 = OmgX * OmgZ * (1 - coswet) - OmgY * sinwet;

	M21 = OmgY * OmgX * (1 - coswet) - OmgZ * sinwet;
	M22 = 1 - (1 - OmgY * OmgY) * (1 - coswet);
	M23 = OmgY * OmgZ * (1 - coswet) + OmgX * sinwet;

	M31 = OmgZ * OmgX * (1 - coswet) + OmgY * sinwet;
	M32 = OmgZ * OmgY * (1 - coswet) - OmgX * sinwet;
	M33 = 1 - (1 - OmgZ * OmgZ) * (1 - coswet);

	ret.x = p.x * (M11 + M21 + M31) + o.x;
	ret.y = p.y * (M12 + M22 + M32) + o.y;
	ret.z = p.z * (M13 + M23 + M33) + o.z;

	return ret;
}

/** @} LinkLayerGeometry_Coord */

/** @} LinkLayerGeometry */

/** @} LinkLayer */