/**********************************************************************************************
*
* raymath v2.0 - 用于处理 Vector2、Vector3、矩阵和四元数的数学函数
*
* CONVENTIONS:
* - 矩阵结构按行优先（内存布局）定义，但参数命名以及库执行的所有
* 数学运算均按列优先方式处理矩阵结构
* 这相当于所有数学运算都使用矩阵的转置版本
* 这使得某些函数更利于缓存，并且避免了 OpenGL 有时需要的矩阵转置
* 例如：按内存顺序，第 0 行是 [m0 m4 m8 m12]，但在语义数学中，第 0 行是 [m0 m1 m2 m3]
* - 函数始终是自包含的，任何函数内部都不会使用其他 raymath 函数，
* 所需的代码在函数内部直接重新实现
* - 函数的输入参数始终按值接收（2 （不可避免的例外情况）
* - 函数始终使用“result”变量作为返回值（C++ 运算符除外）
* - 函数始终以内联方式定义
* - 角度始终以弧度为单位（为方便起见，提供了 DEG2RAD/RAD2DEG 宏）
* - 不使用复合字面量，以确保库与 C++ 兼容
*
* CONFIGURATION:
* #define RAYMATH_IMPLEMENTATION
* 将库的实现生成到包含的文件中
* 如果未定义，则库处于仅头文件模式，可以包含在其他头文件中
* 或源文件中，而不会出现问题。但实现应该只存在于一个文件中
*
* #define RAYMATH_STATIC_INLINE
* 定义静态内联函数代码，因此只需包含头文件即可使用
* 这可能会占用大量内存
*
* #define RAYMATH_DISABLE_CPP_OPERATORS
* 禁用 raymath 类型的 C++ 运算符重载。
*
* #define RAYMATH_USE_SIMD_INTRINSICS
* 尝试为 MatrixMultiply() 启用 SIMD 内联指令
* 请注意，启用此功能的用户必须了解应用程序运行的目标平台
* 以支持所选的 SIMD 内联指令，目前仅支持 SSE
*
* LICENSE: zlib/libpng
*
* 版权所有 (c) 2015-2026 Ramon Santamaria (@raysan5)
*
* 本软件按“原样”提供，不提供任何明示或暗示的担保。在任何情况下，
* 作者均不对因使用本软件而造成的任何损失承担责任。
*
* 任何人均可出于任何目的使用本软件，包括商业用途，
* 并可对其进行修改和自由分发，但须遵守以下限制：
*
* 1. 不得歪曲本软件的来源；您不得声称您
* 编写了原始软件。如果您在产品中使用本软件，我们感谢您在产品文档中注明出处，
* 但这不是强制性的。
*
* 2. 修改后的源代码版本必须明确标明，且不得歪曲为
* 原始软件。
* 
* 3. 不得从任何源代码分发包中删除或更改此声明。
*
**************************************************************************************************/

#ifndef RAYMATH_H
#define RAYMATH_H

#if defined(RAYMATH_IMPLEMENTATION) && defined(RAYMATH_STATIC_INLINE)
    #error "Specifying both RAYMATH_IMPLEMENTATION and RAYMATH_STATIC_INLINE is contradictory"
#endif

// 函数说明符定义
#if defined(RAYMATH_IMPLEMENTATION)
    #if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED)
        #define RMAPI __declspec(dllexport) extern inline    // 将 raylib 构建为 Win32 共享库 (.dll)
    #elif defined(BUILD_LIBTYPE_SHARED)
        #define RMAPI __attribute__((visibility("default"))) // 将 raylib 构建为 Unix 共享库 (.so/.dylib)
    #elif defined(_WIN32) && defined(USE_LIBTYPE_SHARED)
        #define RMAPI __declspec(dllimport)                  // 使用 Win32 共享库 (.dll) 中的 raylib
    #else
        #define RMAPI extern inline // 提供外部定义
    #endif
#elif defined(RAYMATH_STATIC_INLINE)
    #define RMAPI static inline // 函数可以内联，不允许外部内联定义
#else
    #if defined(__TINYC__)
        #define RMAPI static inline // tinycc 不支持纯内联函数（参见问题 #435）
    #else
        #define RMAPI inline        // 函数可以内联，也可以使用外部定义。
    #endif
#endif

//----------------------------------------------------------------------------------
// 宏定义和宏
//----------------------------------------------------------------------------------
#ifndef PI
    #define PI 3.14159265358979323846f
#endif

#ifndef EPSILON
    #define EPSILON 0.000001f
#endif

#ifndef DEG2RAD
    #define DEG2RAD (PI/180.0f)
#endif

#ifndef RAD2DEG
    #define RAD2DEG (180.0f/PI)
#endif

// 获取矩阵的浮点向量
#ifndef MatrixToFloat
    #define MatrixToFloat(mat) (RLMatrixToFloatV(mat).v)
#endif

// 获取 Vector3 的浮点向量
#ifndef Vector3ToFloat
    #define Vector3ToFloat(vec) (RLVector3ToFloatV(vec).v)
#endif

//----------------------------------------------------------------------------------
// 类型和结构定义
//----------------------------------------------------------------------------------
#if !defined(RL_VECTOR2_TYPE)
// Vector2 类型
typedef struct RLVector2 {
    float x;
    float y;
} RLVector2;
#define RL_VECTOR2_TYPE
#endif

#if !defined(RL_VECTOR3_TYPE)
// Vector3 类型
typedef struct RLVector3 {
    float x;
    float y;
    float z;
} RLVector3;
#define RL_VECTOR3_TYPE
#endif

#if !defined(RL_VECTOR4_TYPE)
// Vector4 类型
typedef struct RLVector4 {
    float x;
    float y;
    float z;
    float w;
} RLVector4;
#define RL_VECTOR4_TYPE
#endif

#if !defined(RL_QUATERNION_TYPE)
// 四元数类型
typedef RLVector4 RLQuaternion;
#define RL_QUATERNION_TYPE
#endif

#if !defined(RL_MATRIX_TYPE)
// 矩阵类型（OpenGL 风格 4x4 - 右手，列优先）
typedef struct RLMatrix {
    float m0, m4, m8, m12;     // 矩阵第一行（4 个分量）
    float m1, m5, m9, m13;     // 矩阵第二行（4 个分量）
    float m2, m6, m10, m14;    // 矩阵第三行（4 个分量）
    float m3, m7, m11, m15;    // 矩阵第四行（4 个分量）
} RLMatrix;
#define RL_MATRIX_TYPE
#endif

// 注意：*ToFloat 函数应使用辅助类型而不是数组返回类型
#if !defined(RL_FLOAT3_TYPE)
typedef struct float3 {
    float v[3];
} float3;
#define RL_FLOAT3_TYPE
#endif

#if !defined(RL_FLOAT16_TYPE)
typedef struct float16 {
    float v[16];
} float16;
#define RL_FLOAT16_TYPE
#endif

#include <math.h>       // Required for: sinf(), cosf(), tan(), atan2f(), sqrtf(), floor(), fminf(), fmaxf(), fabsf()

#if defined(RAYMATH_USE_SIMD_INTRINSICS)
    // SIMD 用于开销最大的光线计算函数 MatrixMultiply()
    // 注意：仅实现了对 SSE 指令集的支持
    // TODO：考虑支持其他 SIMD 指令集：
    // - SSEx、AVX、AVX2、FMA、NEON、RVV
    /*
    #if defined(__SSE4_2__)
        #include <nmmintrin.h>
        #define RAYMATH_SSE42_ENABLED
    #elif defined(__SSE4_1__)
        #include <smmintrin.h>
        #define RAYMATH_SSE41_ENABLED
    #elif defined(__SSSE3__)
        #include <tmmintrin.h>
        #define RAYMATH_SSSE3_ENABLED
    #elif defined(__SSE3__)
        #include <pmmintrin.h>
        #define RAYMATH_SSE3_ENABLED
    #elif defined(__SSE2__) || (defined(_M_AMD64) || defined(_M_X64)) // SSE2 x64
        #include <emmintrin.h>
        #define RAYMATH_SSE2_ENABLED
    #endif
    */
    #if defined(__SSE__) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP >= 1))
        #include <xmmintrin.h>
        #define RAYMATH_SSE_ENABLED
    #endif
#endif

//----------------------------------------------------------------------------------
// 模块函数定义 - 实用数学
//----------------------------------------------------------------------------------

// 限制浮点值
RMAPI float RLClamp(float value, float min, float max)
{
    float result = (value < min)? min : value;

    if (result > max) result = max;

    return result;
}

// 计算两个浮点数之间的线性插值
RMAPI float RLLerp(float start, float end, float amount)
{
    float result = start + amount*(end - start);

    return result;
}

// 将输入值归一化到输入范围内
RMAPI float RLNormalize(float value, float start, float end)
{
    float result = (value - start)/(end - start);

    return result;
}

// 将输入范围内的输入值重新映射到输出范围
RMAPI float RLRemap(float value, float inputStart, float inputEnd, float outputStart, float outputEnd)
{
    float result = (value - inputStart)/(inputEnd - inputStart)*(outputEnd - outputStart) + outputStart;

    return result;
}

// 将输入值从最小值到最大值进行换行
RMAPI float RLWrap(float value, float min, float max)
{
    float result = value - (max - min)*floorf((value - min)/(max - min));

    return result;
}

// 检查两个给定的浮点数是否几乎相等
RMAPI int RLFloatEquals(float x, float y)
{
#if !defined(EPSILON)
    #define EPSILON 0.000001f
#endif

    int result = (fabsf(x - y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(x), fabsf(y))));

    return result;
}

//----------------------------------------------------------------------------------
// 模块函数定义 - Vector2 数学
//----------------------------------------------------------------------------------

// 分量值为 0.0f 的向量
RMAPI RLVector2 RLVector2Zero(void)
{
    RLVector2 result = { 0.0f, 0.0f };

    return result;
}

// 分量值为 1.0f 的向量
RMAPI RLVector2 RLVector2One(void)
{
    RLVector2 result = { 1.0f, 1.0f };

    return result;
}

// 将两个向量相加 (v1 + v2)
RMAPI RLVector2 RLVector2Add(RLVector2 v1, RLVector2 v2)
{
    RLVector2 result = { v1.x + v2.x, v1.y + v2.y };

    return result;
}

// 将向量和浮点值相加
RMAPI RLVector2 RLVector2AddValue(RLVector2 v, float add)
{
    RLVector2 result = { v.x + add, v.y + add };

    return result;
}

// 计算两个向量之差 (v1 - v2)
RMAPI RLVector2 RLVector2Subtract(RLVector2 v1, RLVector2 v2)
{
    RLVector2 result = { v1.x - v2.x, v1.y - v2.y };

    return result;
}

// 用浮点值减去向量
RMAPI RLVector2 RLVector2SubtractValue(RLVector2 v, float sub)
{
    RLVector2 result = { v.x - sub, v.y - sub };

    return result;
}

// 计算向量长度
RMAPI float RLVector2Length(RLVector2 v)
{
    float result = sqrtf((v.x*v.x) + (v.y*v.y));

    return result;
}

// 计算向量的平方长度
RMAPI float RLVector2LengthSqr(RLVector2 v)
{
    float result = (v.x*v.x) + (v.y*v.y);

    return result;
}

// 计算两个向量的点积
RMAPI float RLVector2DotProduct(RLVector2 v1, RLVector2 v2)
{
    float result = (v1.x*v2.x + v1.y*v2.y);

    return result;
}

// 计算两个向量的叉积
RMAPI float RLVector2CrossProduct(RLVector2 v1, RLVector2 v2)
{
    float result = (v1.x*v2.y - v1.y*v2.x);

    return result;
}

// 计算两个向量之间的距离
RMAPI float RLVector2Distance(RLVector2 v1, RLVector2 v2)
{
    float result = sqrtf((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));

    return result;
}

// 计算两个向量之间的平方距离
RMAPI float RLVector2DistanceSqr(RLVector2 v1, RLVector2 v2)
{
    float result = ((v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y));

    return result;
}

// 计算 v1 到 v2 相对于原点 (0, 0) 的有符号角度
// 注意：坐标系约定：X 轴正方向向右，Y 轴正方向向下
// 正角度表示顺时针方向，负角度表示逆时针方向
RMAPI float RLVector2Angle(RLVector2 v1, RLVector2 v2)
{
    float result = 0.0f;

    float dot = v1.x*v2.x + v1.y*v2.y;
    float det = v1.x*v2.y - v1.y*v2.x;

    result = atan2f(det, dot);

    return result;
}

// 计算由两个向量构成的直线所定义的角度
// 注意：参数需要归一化
// 当前实现应与 glm::angle 保持一致
RMAPI float RLVector2LineAngle(RLVector2 start, RLVector2 end)
{
    float result = 0.0f;

    // TODO(2023年10月9日): 目前角度是顺时针旋转的，请确定这是否是预期行为
    result = -atan2f(end.y - start.y, end.x - start.x);

    return result;
}

// 缩放向量（乘以值）
RMAPI RLVector2 RLVector2Scale(RLVector2 v, float scale)
{
    RLVector2 result = { v.x*scale, v.y*scale };

    return result;
}

// 向量乘以向量
RMAPI RLVector2 RLVector2Multiply(RLVector2 v1, RLVector2 v2)
{
    RLVector2 result = { v1.x*v2.x, v1.y*v2.y };

    return result;
}

// 取反向量
RMAPI RLVector2 RLVector2Negate(RLVector2 v)
{
    RLVector2 result = { -v.x, -v.y };

    return result;
}

// 向量除以向量
RMAPI RLVector2 RLVector2Divide(RLVector2 v1, RLVector2 v2)
{
    RLVector2 result = { v1.x/v2.x, v1.y/v2.y };

    return result;
}

// 将提供的向量归一化
RMAPI RLVector2 RLVector2Normalize(RLVector2 v)
{
    RLVector2 result = { 0 };
    float length = sqrtf((v.x*v.x) + (v.y*v.y));

    if (length > 0)
    {
        float ilength = 1.0f/length;
        result.x = v.x*ilength;
        result.y = v.y*ilength;
    }

    return result;
}

// 将 Vector2 转换为给定的矩阵
RMAPI RLVector2 RLVector2Transform(RLVector2 v, RLMatrix mat)
{
    RLVector2 result = { 0 };

    float x = v.x;
    float y = v.y;
    float z = 0;

    result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
    result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;

    return result;
}

// 计算两个向量之间的线性插值
RMAPI RLVector2 RLVector2Lerp(RLVector2 v1, RLVector2 v2, float amount)
{
    RLVector2 result = { 0 };

    result.x = v1.x + amount*(v2.x - v1.x);
    result.y = v1.y + amount*(v2.y - v1.y);

    return result;
}

// 计算反射矢量到法线的距离
RMAPI RLVector2 RLVector2Reflect(RLVector2 v, RLVector2 normal)
{
    RLVector2 result = { 0 };

    float dotProduct = (v.x*normal.x + v.y*normal.y); // 点积

    result.x = v.x - (2.0f*normal.x)*dotProduct;
    result.y = v.y - (2.0f*normal.y)*dotProduct;

    return result;
}

// 获取每对组件的最小值
RMAPI RLVector2 RLVector2Min(RLVector2 v1, RLVector2 v2)
{
    RLVector2 result = { 0 };

    result.x = fminf(v1.x, v2.x);
    result.y = fminf(v1.y, v2.y);

    return result;
}

// 获取每对组件的最大值
RMAPI RLVector2 RLVector2Max(RLVector2 v1, RLVector2 v2)
{
    RLVector2 result = { 0 };

    result.x = fmaxf(v1.x, v2.x);
    result.y = fmaxf(v1.y, v2.y);

    return result;
}

// 按角度旋转向量
RMAPI RLVector2 RLVector2Rotate(RLVector2 v, float angle)
{
    RLVector2 result = { 0 };

    float cosres = cosf(angle);
    float sinres = sinf(angle);

    result.x = v.x*cosres - v.y*sinres;
    result.y = v.x*sinres + v.y*cosres;

    return result;
}

// 将向量移动到目标位置
RMAPI RLVector2 RLVector2MoveTowards(RLVector2 v, RLVector2 target, float maxDistance)
{
    RLVector2 result = { 0 };

    float dx = target.x - v.x;
    float dy = target.y - v.y;
    float value = (dx*dx) + (dy*dy);

    if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;

    float dist = sqrtf(value);

    result.x = v.x + dx/dist*maxDistance;
    result.y = v.y + dy/dist*maxDistance;

    return result;
}

// 反转给定的向量
RMAPI RLVector2 RLVector2Invert(RLVector2 v)
{
    RLVector2 result = { 1.0f/v.x, 1.0f/v.y };

    return result;
}

// 将向量的分量限制在以下范围内：
// 由给定向量指定的最小值和最大值。
RMAPI RLVector2 RLVector2Clamp(RLVector2 v, RLVector2 min, RLVector2 max)
{
    RLVector2 result = { 0 };

    result.x = fminf(max.x, fmaxf(min.x, v.x));
    result.y = fminf(max.y, fmaxf(min.y, v.y));

    return result;
}

// 将向量的模限制在两个最小值和最大值之间
RMAPI RLVector2 RLVector2ClampValue(RLVector2 v, float min, float max)
{
    RLVector2 result = v;

    float length = (v.x*v.x) + (v.y*v.y);
    if (length > 0.0f)
    {
        length = sqrtf(length);

        float scale = 1;    // 默认值为 1，表示中性元素。
        if (length < min)
        {
            scale = min/length;
        }
        else if (length > max)
        {
            scale = max/length;
        }

        result.x = v.x*scale;
        result.y = v.y*scale;
    }

    return result;
}

// 检查两个给定的向量是否几乎相等
RMAPI int RLVector2Equals(RLVector2 p, RLVector2 q)
{
#if !defined(EPSILON)
    #define EPSILON 0.000001f
#endif

    int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
                  ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y)))));

    return result;
}

// 计算折射光线的方向
// v：入射光线的归一化方向
// n：两种光学介质界面的归一化法向量
// r：光线来源介质的折射率比
// 与表面另一侧介质的折射率有关
RMAPI RLVector2 RLVector2Refract(RLVector2 v, RLVector2 n, float r)
{
    RLVector2 result = { 0 };

    float dot = v.x*n.x + v.y*n.y;
    float d = 1.0f - r*r*(1.0f - dot*dot);

    if (d >= 0.0f)
    {
        d = sqrtf(d);
        v.x = r*v.x - (r*dot + d)*n.x;
        v.y = r*v.y - (r*dot + d)*n.y;

        result = v;
    }

    return result;
}


//----------------------------------------------------------------------------------
// 模块函数定义 - Vector3 数学
//----------------------------------------------------------------------------------

// 分量值为 0.0f 的向量
RMAPI RLVector3 RLVector3Zero(void)
{
    RLVector3 result = { 0.0f, 0.0f, 0.0f };

    return result;
}

// 分量值为 1.0f 的向量
RMAPI RLVector3 RLVector3One(void)
{
    RLVector3 result = { 1.0f, 1.0f, 1.0f };

    return result;
}

// 将两个向量相加
RMAPI RLVector3 RLVector3Add(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z };

    return result;
}

// 将向量和浮点值相加
RMAPI RLVector3 RLVector3AddValue(RLVector3 v, float add)
{
    RLVector3 result = { v.x + add, v.y + add, v.z + add };

    return result;
}

// 计算两个向量的差
RMAPI RLVector3 RLVector3Subtract(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { v1.x - v2.x, v1.y - v2.y, v1.z - v2.z };

    return result;
}

// 用浮点值减去向量
RMAPI RLVector3 RLVector3SubtractValue(RLVector3 v, float sub)
{
    RLVector3 result = { v.x - sub, v.y - sub, v.z - sub };

    return result;
}

// 将向量乘以标量
RMAPI RLVector3 RLVector3Scale(RLVector3 v, float scalar)
{
    RLVector3 result = { v.x*scalar, v.y*scalar, v.z*scalar };

    return result;
}

// 向量乘以向量
RMAPI RLVector3 RLVector3Multiply(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z };

    return result;
}

// 计算两个向量的叉积
RMAPI RLVector3 RLVector3CrossProduct(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };

    return result;
}

// 计算一个垂直向量
RMAPI RLVector3 RLVector3Perpendicular(RLVector3 v)
{
    RLVector3 result = { 0 };

    float min = fabsf(v.x);
    RLVector3 cardinalAxis = {1.0f, 0.0f, 0.0f};

    if (fabsf(v.y) < min)
    {
        min = fabsf(v.y);
        RLVector3 tmp = {0.0f, 1.0f, 0.0f};
        cardinalAxis = tmp;
    }

    if (fabsf(v.z) < min)
    {
        RLVector3 tmp = {0.0f, 0.0f, 1.0f};
        cardinalAxis = tmp;
    }

    // 向量的叉积
    result.x = v.y*cardinalAxis.z - v.z*cardinalAxis.y;
    result.y = v.z*cardinalAxis.x - v.x*cardinalAxis.z;
    result.z = v.x*cardinalAxis.y - v.y*cardinalAxis.x;

    return result;
}

// 计算向量长度
RMAPI float RLVector3Length(const RLVector3 v)
{
    float result = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);

    return result;
}

// 计算向量的平方长度
RMAPI float RLVector3LengthSqr(const RLVector3 v)
{
    float result = v.x*v.x + v.y*v.y + v.z*v.z;

    return result;
}

// 计算两个向量的点积
RMAPI float RLVector3DotProduct(RLVector3 v1, RLVector3 v2)
{
    float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);

    return result;
}

// 计算两个向量之间的距离
RMAPI float RLVector3Distance(RLVector3 v1, RLVector3 v2)
{
    float result = 0.0f;

    float dx = v2.x - v1.x;
    float dy = v2.y - v1.y;
    float dz = v2.z - v1.z;
    result = sqrtf(dx*dx + dy*dy + dz*dz);

    return result;
}

// 计算两个向量之间的平方距离
RMAPI float RLVector3DistanceSqr(RLVector3 v1, RLVector3 v2)
{
    float result = 0.0f;

    float dx = v2.x - v1.x;
    float dy = v2.y - v1.y;
    float dz = v2.z - v1.z;
    result = dx*dx + dy*dy + dz*dz;

    return result;
}

// 计算两个向量之间的夹角
RMAPI float RLVector3Angle(RLVector3 v1, RLVector3 v2)
{
    float result = 0.0f;

    RLVector3 cross = { v1.y*v2.z - v1.z*v2.y, v1.z*v2.x - v1.x*v2.z, v1.x*v2.y - v1.y*v2.x };
    float len = sqrtf(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z);
    float dot = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
    result = atan2f(len, dot);

    return result;
}

// 对提供的向量取反（反转方向）
RMAPI RLVector3 RLVector3Negate(RLVector3 v)
{
    RLVector3 result = { -v.x, -v.y, -v.z };

    return result;
}

// 向量除以向量
RMAPI RLVector3 RLVector3Divide(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z };

    return result;
}

// 将提供的向量归一化
RMAPI RLVector3 RLVector3Normalize(RLVector3 v)
{
    RLVector3 result = v;

    float length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
    if (length != 0.0f)
    {
        float ilength = 1.0f/length;

        result.x *= ilength;
        result.y *= ilength;
        result.z *= ilength;
    }

    return result;
}

//计算向量 v1 在向量 v2 上的投影
RMAPI RLVector3 RLVector3Project(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { 0 };

    float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
    float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);

    float mag = v1dv2/v2dv2;

    result.x = v2.x*mag;
    result.y = v2.y*mag;
    result.z = v2.z*mag;

    return result;
}

//计算向量 v1 对 v2 的拒绝度
RMAPI RLVector3 RLVector3Reject(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { 0 };

    float v1dv2 = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z);
    float v2dv2 = (v2.x*v2.x + v2.y*v2.y + v2.z*v2.z);

    float mag = v1dv2/v2dv2;

    result.x = v1.x - (v2.x*mag);
    result.y = v1.y - (v2.y*mag);
    result.z = v1.z - (v2.z*mag);

    return result;
}

// 对提供的向量进行正交归一化
// 使向量归一化并彼此正交
// 格拉姆-施密特正交化函数实现
RMAPI void RLVector3OrthoNormalize(RLVector3 *v1, RLVector3 *v2)
{
    float length = 0.0f;
    float ilength = 0.0f;

    // Vector3Normalize(*v1);
    RLVector3 v = *v1;
    length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
    if (length == 0.0f) length = 1.0f;
    ilength = 1.0f/length;
    v1->x *= ilength;
    v1->y *= ilength;
    v1->z *= ilength;

    // Vector3CrossProduct(*v1, *v2)
    RLVector3 vn1 = { v1->y*v2->z - v1->z*v2->y, v1->z*v2->x - v1->x*v2->z, v1->x*v2->y - v1->y*v2->x };

    // Vector3Normalize(vn1);
    v = vn1;
    length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
    if (length == 0.0f) length = 1.0f;
    ilength = 1.0f/length;
    vn1.x *= ilength;
    vn1.y *= ilength;
    vn1.z *= ilength;

    // Vector3CrossProduct(vn1, *v1)
    RLVector3 vn2 = { vn1.y*v1->z - vn1.z*v1->y, vn1.z*v1->x - vn1.x*v1->z, vn1.x*v1->y - vn1.y*v1->x };

    *v2 = vn2;
}

// 将 Vector3 乘以给定的矩阵
RMAPI RLVector3 RLVector3Transform(RLVector3 v, RLMatrix mat)
{
    RLVector3 result = { 0 };

    float x = v.x;
    float y = v.y;
    float z = v.z;

    result.x = mat.m0*x + mat.m4*y + mat.m8*z + mat.m12;
    result.y = mat.m1*x + mat.m5*y + mat.m9*z + mat.m13;
    result.z = mat.m2*x + mat.m6*y + mat.m10*z + mat.m14;

    return result;
}

// 通过四元数旋转变换向量
RMAPI RLVector3 RLVector3RotateByQuaternion(RLVector3 v, RLQuaternion q)
{
    RLVector3 result = { 0 };

    result.x = v.x*(q.x*q.x + q.w*q.w - q.y*q.y - q.z*q.z) + v.y*(2*q.x*q.y - 2*q.w*q.z) + v.z*(2*q.x*q.z + 2*q.w*q.y);
    result.y = v.x*(2*q.w*q.z + 2*q.x*q.y) + v.y*(q.w*q.w - q.x*q.x + q.y*q.y - q.z*q.z) + v.z*(-2*q.w*q.x + 2*q.y*q.z);
    result.z = v.x*(-2*q.w*q.y + 2*q.x*q.z) + v.y*(2*q.w*q.x + 2*q.y*q.z)+ v.z*(q.w*q.w - q.x*q.x - q.y*q.y + q.z*q.z);

    return result;
}

// 将向量绕轴旋转
RMAPI RLVector3 RLVector3RotateByAxisAngle(RLVector3 v, RLVector3 axis, float angle)
{
    // 使用欧拉-罗德里格斯公式
    // 参考：https://en.wikipedia.org/w/index.php?title=Euler%E2%80%93Rodrigues_formula

    RLVector3 result = v;

    // Vector3Normalize(axis);
    float length = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);
    if (length == 0.0f) length = 1.0f;
    float ilength = 1.0f/length;
    axis.x *= ilength;
    axis.y *= ilength;
    axis.z *= ilength;

    angle /= 2.0f;
    float a = sinf(angle);
    float b = axis.x*a;
    float c = axis.y*a;
    float d = axis.z*a;
    a = cosf(angle);
    RLVector3 w = { b, c, d };

    // Vector3CrossProduct(w, v)
    RLVector3 wv = { w.y*v.z - w.z*v.y, w.z*v.x - w.x*v.z, w.x*v.y - w.y*v.x };

    // Vector3CrossProduct(w, wv)
    RLVector3 wwv = { w.y*wv.z - w.z*wv.y, w.z*wv.x - w.x*wv.z, w.x*wv.y - w.y*wv.x };

    // Vector3Scale(wv, 2*a)
    a *= 2;
    wv.x *= a;
    wv.y *= a;
    wv.z *= a;

    // Vector3Scale(wwv, 2)
    wwv.x *= 2;
    wwv.y *= 2;
    wwv.z *= 2;

    result.x += wv.x;
    result.y += wv.y;
    result.z += wv.z;

    result.x += wwv.x;
    result.y += wwv.y;
    result.z += wwv.z;

    return result;
}

// 将向量移动到目标位置
RMAPI RLVector3 RLVector3MoveTowards(RLVector3 v, RLVector3 target, float maxDistance)
{
    RLVector3 result = { 0 };

    float dx = target.x - v.x;
    float dy = target.y - v.y;
    float dz = target.z - v.z;
    float value = (dx*dx) + (dy*dy) + (dz*dz);

    if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;

    float dist = sqrtf(value);

    result.x = v.x + dx/dist*maxDistance;
    result.y = v.y + dy/dist*maxDistance;
    result.z = v.z + dz/dist*maxDistance;

    return result;
}

// 计算两个向量之间的线性插值
RMAPI RLVector3 RLVector3Lerp(RLVector3 v1, RLVector3 v2, float amount)
{
    RLVector3 result = { 0 };

    result.x = v1.x + amount*(v2.x - v1.x);
    result.y = v1.y + amount*(v2.y - v1.y);
    result.z = v1.z + amount*(v2.z - v1.z);

    return result;
}

// 计算两个向量及其切线之间的三次埃尔米特插值
// 如 GLTF 2.0 规范中所述：https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#interpolation-cubic
RMAPI RLVector3 RLVector3CubicHermite(RLVector3 v1, RLVector3 tangent1, RLVector3 v2, RLVector3 tangent2, float amount)
{
    RLVector3 result = { 0 };

    float amountPow2 = amount*amount;
    float amountPow3 = amount*amount*amount;

    result.x = (2*amountPow3 - 3*amountPow2 + 1)*v1.x + (amountPow3 - 2*amountPow2 + amount)*tangent1.x + (-2*amountPow3 + 3*amountPow2)*v2.x + (amountPow3 - amountPow2)*tangent2.x;
    result.y = (2*amountPow3 - 3*amountPow2 + 1)*v1.y + (amountPow3 - 2*amountPow2 + amount)*tangent1.y + (-2*amountPow3 + 3*amountPow2)*v2.y + (amountPow3 - amountPow2)*tangent2.y;
    result.z = (2*amountPow3 - 3*amountPow2 + 1)*v1.z + (amountPow3 - 2*amountPow2 + amount)*tangent1.z + (-2*amountPow3 + 3*amountPow2)*v2.z + (amountPow3 - amountPow2)*tangent2.z;

    return result;
}

// 计算反射矢量到法线的距离
RMAPI RLVector3 RLVector3Reflect(RLVector3 v, RLVector3 normal)
{
    RLVector3 result = { 0 };

    // I 是原始向量
    // N 是入射平面的法线
    // R = I - (2*N*(DotProduct[I, N]))

    float dotProduct = (v.x*normal.x + v.y*normal.y + v.z*normal.z);

    result.x = v.x - (2.0f*normal.x)*dotProduct;
    result.y = v.y - (2.0f*normal.y)*dotProduct;
    result.z = v.z - (2.0f*normal.z)*dotProduct;

    return result;
}

// 获取每对组件的最小值
RMAPI RLVector3 RLVector3Min(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { 0 };

    result.x = fminf(v1.x, v2.x);
    result.y = fminf(v1.y, v2.y);
    result.z = fminf(v1.z, v2.z);

    return result;
}

// 获取每对组件的最大值
RMAPI RLVector3 RLVector3Max(RLVector3 v1, RLVector3 v2)
{
    RLVector3 result = { 0 };

    result.x = fmaxf(v1.x, v2.x);
    result.y = fmaxf(v1.y, v2.y);
    result.z = fmaxf(v1.z, v2.z);

    return result;
}

// 计算点 p 相对于三角形 (a, b, c) 的重心坐标 (u, v, w) // 注意：假设 P 点位于三角形所在的平面上
RMAPI RLVector3 RLVector3Barycenter(RLVector3 p, RLVector3 a, RLVector3 b, RLVector3 c)
{
    RLVector3 result = { 0 };

    RLVector3 v0 = { b.x - a.x, b.y - a.y, b.z - a.z };   // Vector3Subtract(b, a)
    RLVector3 v1 = { c.x - a.x, c.y - a.y, c.z - a.z };   // Vector3Subtract(c, a)
    RLVector3 v2 = { p.x - a.x, p.y - a.y, p.z - a.z };   // Vector3Subtract(p, a)
    float d00 = (v0.x*v0.x + v0.y*v0.y + v0.z*v0.z);    // Vector3DotProduct(v0, v0)
    float d01 = (v0.x*v1.x + v0.y*v1.y + v0.z*v1.z);    // Vector3DotProduct(v0, v1)
    float d11 = (v1.x*v1.x + v1.y*v1.y + v1.z*v1.z);    // Vector3DotProduct(v1, v1)
    float d20 = (v2.x*v0.x + v2.y*v0.y + v2.z*v0.z);    // Vector3DotProduct(v2, v0)
    float d21 = (v2.x*v1.x + v2.y*v1.y + v2.z*v1.z);    // Vector3DotProduct(v2, v1)

    float denom = d00*d11 - d01*d01;

    result.y = (d11*d20 - d01*d21)/denom;
    result.z = (d00*d21 - d01*d20)/denom;
    result.x = 1.0f - (result.z + result.y);

    return result;
}

// 将屏幕空间中的 Vector3 投影到物体空间
// 注意：这是一个独立的函数，不会调用其他光线计算函数。
RMAPI RLVector3 RLVector3Unproject(RLVector3 source, RLMatrix projection, RLMatrix view)
{
    RLVector3 result = { 0 };

    // 计算未投影矩阵（视图矩阵乘以投影矩阵）并求其逆矩阵
    RLMatrix matViewProj = {      // MatrixMultiply(view, projection);
        view.m0*projection.m0 + view.m1*projection.m4 + view.m2*projection.m8 + view.m3*projection.m12,
        view.m0*projection.m1 + view.m1*projection.m5 + view.m2*projection.m9 + view.m3*projection.m13,
        view.m0*projection.m2 + view.m1*projection.m6 + view.m2*projection.m10 + view.m3*projection.m14,
        view.m0*projection.m3 + view.m1*projection.m7 + view.m2*projection.m11 + view.m3*projection.m15,
        view.m4*projection.m0 + view.m5*projection.m4 + view.m6*projection.m8 + view.m7*projection.m12,
        view.m4*projection.m1 + view.m5*projection.m5 + view.m6*projection.m9 + view.m7*projection.m13,
        view.m4*projection.m2 + view.m5*projection.m6 + view.m6*projection.m10 + view.m7*projection.m14,
        view.m4*projection.m3 + view.m5*projection.m7 + view.m6*projection.m11 + view.m7*projection.m15,
        view.m8*projection.m0 + view.m9*projection.m4 + view.m10*projection.m8 + view.m11*projection.m12,
        view.m8*projection.m1 + view.m9*projection.m5 + view.m10*projection.m9 + view.m11*projection.m13,
        view.m8*projection.m2 + view.m9*projection.m6 + view.m10*projection.m10 + view.m11*projection.m14,
        view.m8*projection.m3 + view.m9*projection.m7 + view.m10*projection.m11 + view.m11*projection.m15,
        view.m12*projection.m0 + view.m13*projection.m4 + view.m14*projection.m8 + view.m15*projection.m12,
        view.m12*projection.m1 + view.m13*projection.m5 + view.m14*projection.m9 + view.m15*projection.m13,
        view.m12*projection.m2 + view.m13*projection.m6 + view.m14*projection.m10 + view.m15*projection.m14,
        view.m12*projection.m3 + view.m13*projection.m7 + view.m14*projection.m11 + view.m15*projection.m15 };

    // 计算逆矩阵 -> MatrixInvert(matViewProj);
    // 缓存矩阵值（速度优化）
    float a00 = matViewProj.m0, a01 = matViewProj.m1, a02 = matViewProj.m2, a03 = matViewProj.m3;
    float a10 = matViewProj.m4, a11 = matViewProj.m5, a12 = matViewProj.m6, a13 = matViewProj.m7;
    float a20 = matViewProj.m8, a21 = matViewProj.m9, a22 = matViewProj.m10, a23 = matViewProj.m11;
    float a30 = matViewProj.m12, a31 = matViewProj.m13, a32 = matViewProj.m14, a33 = matViewProj.m15;

    float b00 = a00*a11 - a01*a10;
    float b01 = a00*a12 - a02*a10;
    float b02 = a00*a13 - a03*a10;
    float b03 = a01*a12 - a02*a11;
    float b04 = a01*a13 - a03*a11;
    float b05 = a02*a13 - a03*a12;
    float b06 = a20*a31 - a21*a30;
    float b07 = a20*a32 - a22*a30;
    float b08 = a20*a33 - a23*a30;
    float b09 = a21*a32 - a22*a31;
    float b10 = a21*a33 - a23*a31;
    float b11 = a22*a33 - a23*a32;

    // 计算行列式的逆矩阵（内联执行以避免重复缓存）
    float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);

    RLMatrix matViewProjInv = {
        (a11*b11 - a12*b10 + a13*b09)*invDet,
        (-a01*b11 + a02*b10 - a03*b09)*invDet,
        (a31*b05 - a32*b04 + a33*b03)*invDet,
        (-a21*b05 + a22*b04 - a23*b03)*invDet,
        (-a10*b11 + a12*b08 - a13*b07)*invDet,
        (a00*b11 - a02*b08 + a03*b07)*invDet,
        (-a30*b05 + a32*b02 - a33*b01)*invDet,
        (a20*b05 - a22*b02 + a23*b01)*invDet,
        (a10*b10 - a11*b08 + a13*b06)*invDet,
        (-a00*b10 + a01*b08 - a03*b06)*invDet,
        (a30*b04 - a31*b02 + a33*b00)*invDet,
        (-a20*b04 + a21*b02 - a23*b00)*invDet,
        (-a10*b09 + a11*b07 - a12*b06)*invDet,
        (a00*b09 - a01*b07 + a02*b06)*invDet,
        (-a30*b03 + a31*b01 - a32*b00)*invDet,
        (a20*b03 - a21*b01 + a22*b00)*invDet };

    // 从源点创建四元数
    RLQuaternion quat = { source.x, source.y, source.z, 1.0f };

    // 将四点乘以未投影矩阵的逆矩阵
    RLQuaternion qtransformed = {     // QuaternionTransform(quat, matViewProjInv)
        matViewProjInv.m0*quat.x + matViewProjInv.m4*quat.y + matViewProjInv.m8*quat.z + matViewProjInv.m12*quat.w,
        matViewProjInv.m1*quat.x + matViewProjInv.m5*quat.y + matViewProjInv.m9*quat.z + matViewProjInv.m13*quat.w,
        matViewProjInv.m2*quat.x + matViewProjInv.m6*quat.y + matViewProjInv.m10*quat.z + matViewProjInv.m14*quat.w,
        matViewProjInv.m3*quat.x + matViewProjInv.m7*quat.y + matViewProjInv.m11*quat.z + matViewProjInv.m15*quat.w };

    // 归一化世界点向量
    result.x = qtransformed.x/qtransformed.w;
    result.y = qtransformed.y/qtransformed.w;
    result.z = qtransformed.z/qtransformed.w;

    return result;
}

// 将 Vector3 转换为浮点数组
RMAPI float3 RLVector3ToFloatV(RLVector3 v)
{
    float3 buffer = { 0 };

    buffer.v[0] = v.x;
    buffer.v[1] = v.y;
    buffer.v[2] = v.z;

    return buffer;
}

// 反转给定的向量
RMAPI RLVector3 RLVector3Invert(RLVector3 v)
{
    RLVector3 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z };

    return result;
}

// 将向量的分量限制在以下范围内：
// 由给定向量指定的最小值和最大值。
RMAPI RLVector3 RLVector3Clamp(RLVector3 v, RLVector3 min, RLVector3 max)
{
    RLVector3 result = { 0 };

    result.x = fminf(max.x, fmaxf(min.x, v.x));
    result.y = fminf(max.y, fmaxf(min.y, v.y));
    result.z = fminf(max.z, fmaxf(min.z, v.z));

    return result;
}

// 将向量的大小限制在两个值之间
RMAPI RLVector3 RLVector3ClampValue(RLVector3 v, float min, float max)
{
    RLVector3 result = v;

    float length = (v.x*v.x) + (v.y*v.y) + (v.z*v.z);
    if (length > 0.0f)
    {
        length = sqrtf(length);

        float scale = 1;    // 默认值为 1，表示中性元素。
        if (length < min)
        {
            scale = min/length;
        }
        else if (length > max)
        {
            scale = max/length;
        }

        result.x = v.x*scale;
        result.y = v.y*scale;
        result.z = v.z*scale;
    }

    return result;
}

// 检查两个给定的向量是否几乎相等
RMAPI int RLVector3Equals(RLVector3 p, RLVector3 q)
{
#if !defined(EPSILON)
    #define EPSILON 0.000001f
#endif

    int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
                 ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
                 ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z)))));

    return result;
}

// 计算折射光线的方向
// v：入射光线的归一化方向
// n：两种光学介质界面的归一化法向量
// r：光线入射介质的折射率与折射介质另一侧的折射率之比
// 与表面另一侧介质的折射率有关
RMAPI RLVector3 RLVector3Refract(RLVector3 v, RLVector3 n, float r)
{
    RLVector3 result = { 0 };

    float dot = v.x*n.x + v.y*n.y + v.z*n.z;
    float d = 1.0f - r*r*(1.0f - dot*dot);

    if (d >= 0.0f)
    {
        d = sqrtf(d);
        v.x = r*v.x - (r*dot + d)*n.x;
        v.y = r*v.y - (r*dot + d)*n.y;
        v.z = r*v.z - (r*dot + d)*n.z;

        result = v;
    }

    return result;
}


//----------------------------------------------------------------------------------
// 模块函数定义 - Vector4 数学
//----------------------------------------------------------------------------------

RMAPI RLVector4 RLVector4Zero(void)
{
    RLVector4 result = { 0.0f, 0.0f, 0.0f, 0.0f };
    return result;
}

RMAPI RLVector4 RLVector4One(void)
{
    RLVector4 result = { 1.0f, 1.0f, 1.0f, 1.0f };
    return result;
}

RMAPI RLVector4 RLVector4Add(RLVector4 v1, RLVector4 v2)
{
    RLVector4 result = {
        v1.x + v2.x,
        v1.y + v2.y,
        v1.z + v2.z,
        v1.w + v2.w
    };
    return result;
}

RMAPI RLVector4 RLVector4AddValue(RLVector4 v, float add)
{
    RLVector4 result = {
        v.x + add,
        v.y + add,
        v.z + add,
        v.w + add
    };
    return result;
}

RMAPI RLVector4 RLVector4Subtract(RLVector4 v1, RLVector4 v2)
{
    RLVector4 result = {
        v1.x - v2.x,
        v1.y - v2.y,
        v1.z - v2.z,
        v1.w - v2.w
    };
    return result;
}

RMAPI RLVector4 RLVector4SubtractValue(RLVector4 v, float add)
{
    RLVector4 result = {
        v.x - add,
        v.y - add,
        v.z - add,
        v.w - add
    };
    return result;
}

RMAPI float RLVector4Length(RLVector4 v)
{
    float result = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));
    return result;
}

RMAPI float RLVector4LengthSqr(RLVector4 v)
{
    float result = (v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w);
    return result;
}

RMAPI float RLVector4DotProduct(RLVector4 v1, RLVector4 v2)
{
    float result = (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w);
    return result;
}

// 计算两个向量之间的距离
RMAPI float RLVector4Distance(RLVector4 v1, RLVector4 v2)
{
    float result = sqrtf(
        (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) +
        (v1.z - v2.z)*(v1.z - v2.z) + (v1.w - v2.w)*(v1.w - v2.w));
    return result;
}

// 计算两个向量之间的平方距离
RMAPI float RLVector4DistanceSqr(RLVector4 v1, RLVector4 v2)
{
    float result =
        (v1.x - v2.x)*(v1.x - v2.x) + (v1.y - v2.y)*(v1.y - v2.y) +
        (v1.z - v2.z)*(v1.z - v2.z) + (v1.w - v2.w)*(v1.w - v2.w);

    return result;
}

RMAPI RLVector4 RLVector4Scale(RLVector4 v, float scale)
{
    RLVector4 result = { v.x*scale, v.y*scale, v.z*scale, v.w*scale };
    return result;
}

// 向量乘以向量
RMAPI RLVector4 RLVector4Multiply(RLVector4 v1, RLVector4 v2)
{
    RLVector4 result = { v1.x*v2.x, v1.y*v2.y, v1.z*v2.z, v1.w*v2.w };
    return result;
}

// 取反向量
RMAPI RLVector4 RLVector4Negate(RLVector4 v)
{
    RLVector4 result = { -v.x, -v.y, -v.z, -v.w };
    return result;
}

// 向量除以向量
RMAPI RLVector4 RLVector4Divide(RLVector4 v1, RLVector4 v2)
{
    RLVector4 result = { v1.x/v2.x, v1.y/v2.y, v1.z/v2.z, v1.w/v2.w };
    return result;
}

// 将提供的向量归一化
RMAPI RLVector4 RLVector4Normalize(RLVector4 v)
{
    RLVector4 result = { 0 };
    float length = sqrtf((v.x*v.x) + (v.y*v.y) + (v.z*v.z) + (v.w*v.w));

    if (length > 0)
    {
        float ilength = 1.0f/length;
        result.x = v.x*ilength;
        result.y = v.y*ilength;
        result.z = v.z*ilength;
        result.w = v.w*ilength;
    }

    return result;
}

// 获取每对组件的最小值
RMAPI RLVector4 RLVector4Min(RLVector4 v1, RLVector4 v2)
{
    RLVector4 result = { 0 };

    result.x = fminf(v1.x, v2.x);
    result.y = fminf(v1.y, v2.y);
    result.z = fminf(v1.z, v2.z);
    result.w = fminf(v1.w, v2.w);

    return result;
}

// 获取每对组件的最大值
RMAPI RLVector4 RLVector4Max(RLVector4 v1, RLVector4 v2)
{
    RLVector4 result = { 0 };

    result.x = fmaxf(v1.x, v2.x);
    result.y = fmaxf(v1.y, v2.y);
    result.z = fmaxf(v1.z, v2.z);
    result.w = fmaxf(v1.w, v2.w);

    return result;
}

// 计算两个向量之间的线性插值
RMAPI RLVector4 RLVector4Lerp(RLVector4 v1, RLVector4 v2, float amount)
{
    RLVector4 result = { 0 };

    result.x = v1.x + amount*(v2.x - v1.x);
    result.y = v1.y + amount*(v2.y - v1.y);
    result.z = v1.z + amount*(v2.z - v1.z);
    result.w = v1.w + amount*(v2.w - v1.w);

    return result;
}

// 将向量移动到目标位置
RMAPI RLVector4 RLVector4MoveTowards(RLVector4 v, RLVector4 target, float maxDistance)
{
    RLVector4 result = { 0 };

    float dx = target.x - v.x;
    float dy = target.y - v.y;
    float dz = target.z - v.z;
    float dw = target.w - v.w;
    float value = (dx*dx) + (dy*dy) + (dz*dz) + (dw*dw);

    if ((value == 0) || ((maxDistance >= 0) && (value <= maxDistance*maxDistance))) return target;

    float dist = sqrtf(value);

    result.x = v.x + dx/dist*maxDistance;
    result.y = v.y + dy/dist*maxDistance;
    result.z = v.z + dz/dist*maxDistance;
    result.w = v.w + dw/dist*maxDistance;

    return result;
}

// 反转给定的向量
RMAPI RLVector4 RLVector4Invert(RLVector4 v)
{
    RLVector4 result = { 1.0f/v.x, 1.0f/v.y, 1.0f/v.z, 1.0f/v.w };
    return result;
}

// 检查两个给定的向量是否几乎相等
RMAPI int RLVector4Equals(RLVector4 p, RLVector4 q)
{
#if !defined(EPSILON)
    #define EPSILON 0.000001f
#endif

    int result = ((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
                  ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
                  ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
                  ((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))));
    return result;
}


//----------------------------------------------------------------------------------
// 模块函数定义 - 矩阵运算
//----------------------------------------------------------------------------------

// 计算矩阵行列式
RMAPI float RLMatrixDeterminant(RLMatrix mat)
{
    float result = 0.0f;
/*
    // Cache the matrix values (speed optimization)
    float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
    float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
    float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
    float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;

    // NOTE: It takes 72 multiplication to calculate 4x4 matrix determinant
    result = a30*a21*a12*a03 - a20*a31*a12*a03 - a30*a11*a22*a03 + a10*a31*a22*a03 +
             a20*a11*a32*a03 - a10*a21*a32*a03 - a30*a21*a02*a13 + a20*a31*a02*a13 +
             a30*a01*a22*a13 - a00*a31*a22*a13 - a20*a01*a32*a13 + a00*a21*a32*a13 +
             a30*a11*a02*a23 - a10*a31*a02*a23 - a30*a01*a12*a23 + a00*a31*a12*a23 +
             a10*a01*a32*a23 - a00*a11*a32*a23 - a20*a11*a02*a33 + a10*a21*a02*a33 +
             a20*a01*a12*a33 - a00*a21*a12*a33 - a10*a01*a22*a33 + a00*a11*a22*a33;
*/
    // 使用拉普拉斯展开（https://en.wikipedia.org/wiki/Laplace_expansion），
    // 之前的操作可以简化为 40 次乘法，从而减小矩阵大小
    // 使用子式将矩阵大小从 4x4 减小到 2x2

    // 缓存矩阵值（速度优化）
    float m0 = mat.m0, m1 = mat.m1, m2 = mat.m2, m3 = mat.m3;
    float m4 = mat.m4, m5 = mat.m5, m6 = mat.m6, m7 = mat.m7;
    float m8 = mat.m8, m9 = mat.m9, m10 = mat.m10, m11 = mat.m11;
    float m12 = mat.m12, m13 = mat.m13, m14 = mat.m14, m15 = mat.m15;

    result = (m0*((m5*(m10*m15 - m11*m14) - m9*(m6*m15 - m7*m14) + m13*(m6*m11 - m7*m10))) -
        m4*((m1*(m10*m15 - m11*m14) - m9*(m2*m15 - m3*m14) + m13*(m2*m11 - m3*m10))) +
        m8*((m1*(m6*m15 - m7*m14) - m5*(m2*m15 - m3*m14) + m13*(m2*m7 - m3*m6))) -
        m12*((m1*(m6*m11 - m7*m10) - m5*(m2*m11 - m3*m10) + m9*(m2*m7 - m3*m6))));

    return result;
}

// 获取矩阵的迹（对角线上所有元素之和）
RMAPI float RLMatrixTrace(RLMatrix mat)
{
    float result = (mat.m0 + mat.m5 + mat.m10 + mat.m15);

    return result;
}

// 转置提供的矩阵
RMAPI RLMatrix RLMatrixTranspose(RLMatrix mat)
{
    RLMatrix result = { 0 };

    result.m0 = mat.m0;
    result.m1 = mat.m4;
    result.m2 = mat.m8;
    result.m3 = mat.m12;
    result.m4 = mat.m1;
    result.m5 = mat.m5;
    result.m6 = mat.m9;
    result.m7 = mat.m13;
    result.m8 = mat.m2;
    result.m9 = mat.m6;
    result.m10 = mat.m10;
    result.m11 = mat.m14;
    result.m12 = mat.m3;
    result.m13 = mat.m7;
    result.m14 = mat.m11;
    result.m15 = mat.m15;

    return result;
}

// 对提供的矩阵求逆
RMAPI RLMatrix RLMatrixInvert(RLMatrix mat)
{
    RLMatrix result = { 0 };

    // 缓存矩阵值（速度优化）
    float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3;
    float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7;
    float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11;
    float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15;

    float b00 = a00*a11 - a01*a10;
    float b01 = a00*a12 - a02*a10;
    float b02 = a00*a13 - a03*a10;
    float b03 = a01*a12 - a02*a11;
    float b04 = a01*a13 - a03*a11;
    float b05 = a02*a13 - a03*a12;
    float b06 = a20*a31 - a21*a30;
    float b07 = a20*a32 - a22*a30;
    float b08 = a20*a33 - a23*a30;
    float b09 = a21*a32 - a22*a31;
    float b10 = a21*a33 - a23*a31;
    float b11 = a22*a33 - a23*a32;

    // 计算行列式的逆矩阵（内联执行以避免重复缓存）
    float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06);

    result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet;
    result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet;
    result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet;
    result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet;
    result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet;
    result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet;
    result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet;
    result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet;
    result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet;
    result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet;
    result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet;
    result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet;
    result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet;
    result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet;
    result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet;
    result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet;

    return result;
}

// 获取单位矩阵
RMAPI RLMatrix RLMatrixIdentity(void)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
                      0.0f, 1.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f };

    return result;
}

// 将两个矩阵相加
RMAPI RLMatrix RLMatrixAdd(RLMatrix left, RLMatrix right)
{
    RLMatrix result = { 0 };

    result.m0 = left.m0 + right.m0;
    result.m1 = left.m1 + right.m1;
    result.m2 = left.m2 + right.m2;
    result.m3 = left.m3 + right.m3;
    result.m4 = left.m4 + right.m4;
    result.m5 = left.m5 + right.m5;
    result.m6 = left.m6 + right.m6;
    result.m7 = left.m7 + right.m7;
    result.m8 = left.m8 + right.m8;
    result.m9 = left.m9 + right.m9;
    result.m10 = left.m10 + right.m10;
    result.m11 = left.m11 + right.m11;
    result.m12 = left.m12 + right.m12;
    result.m13 = left.m13 + right.m13;
    result.m14 = left.m14 + right.m14;
    result.m15 = left.m15 + right.m15;

    return result;
}

// 计算两个矩阵的差（左矩阵 - 右矩阵）
RMAPI RLMatrix RLMatrixSubtract(RLMatrix left, RLMatrix right)
{
    RLMatrix result = { 0 };

    result.m0 = left.m0 - right.m0;
    result.m1 = left.m1 - right.m1;
    result.m2 = left.m2 - right.m2;
    result.m3 = left.m3 - right.m3;
    result.m4 = left.m4 - right.m4;
    result.m5 = left.m5 - right.m5;
    result.m6 = left.m6 - right.m6;
    result.m7 = left.m7 - right.m7;
    result.m8 = left.m8 - right.m8;
    result.m9 = left.m9 - right.m9;
    result.m10 = left.m10 - right.m10;
    result.m11 = left.m11 - right.m11;
    result.m12 = left.m12 - right.m12;
    result.m13 = left.m13 - right.m13;
    result.m14 = left.m14 - right.m14;
    result.m15 = left.m15 - right.m15;

    return result;
}

// 获取两个矩阵的乘法结果
// 注意：矩阵乘法时……顺序很重要！
RMAPI RLMatrix RLMatrixMultiply(RLMatrix left, RLMatrix right)
{
    RLMatrix result = { 0 };
    
#if defined(RAYMATH_SSE_ENABLED)
    // 加载左侧和右侧
    __m128 c0 = _mm_set_ps(right.m12, right.m8,  right.m4,  right.m0);
    __m128 c1 = _mm_set_ps(right.m13, right.m9,  right.m5,  right.m1);
    __m128 c2 = _mm_set_ps(right.m14, right.m10, right.m6,  right.m2);
    __m128 c3 = _mm_set_ps(right.m15, right.m11, right.m7,  right.m3);
    
    // 转置，使 c0..c3 按语义顺序成为右矩阵的*行*
    _MM_TRANSPOSE4_PS(c0, c1, c2, c3);

    float tmp[4] = { 0 };
    __m128 row;
    
    // 结果的第 0 行：[m0, m1, m2, m3]
    row  = _mm_mul_ps(_mm_set1_ps(left.m0),  c0);
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m1),  c1));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m2),  c2));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m3),  c3));
    _mm_storeu_ps(tmp, row);
    result.m0 = tmp[0];
    result.m1 = tmp[1];
    result.m2 = tmp[2];
    result.m3 = tmp[3];

    // 结果第 1 行：[m4, m5, m6, m7]
    row  = _mm_mul_ps(_mm_set1_ps(left.m4),  c0);
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m5),  c1));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m6),  c2));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m7),  c3));
    _mm_storeu_ps(tmp, row);
    result.m4 = tmp[0];
    result.m5 = tmp[1];
    result.m6 = tmp[2];
    result.m7 = tmp[3];

    // 结果第 2 行：[m8, m9, m10, m11]
    row  = _mm_mul_ps(_mm_set1_ps(left.m8),  c0);
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m9),  c1));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m10), c2));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m11), c3));
    _mm_storeu_ps(tmp, row);
    result.m8  = tmp[0];
    result.m9  = tmp[1];
    result.m10 = tmp[2];
    result.m11 = tmp[3];

    // 结果第 3 行：[m12, m13, m14, m15]
    row  = _mm_mul_ps(_mm_set1_ps(left.m12), c0);
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m13), c1));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m14), c2));
    row  = _mm_add_ps(row, _mm_mul_ps(_mm_set1_ps(left.m15), c3));
    _mm_storeu_ps(tmp, row);
    result.m12 = tmp[0];
    result.m13 = tmp[1];
    result.m14 = tmp[2];
    result.m15 = tmp[3];
#else
    result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
    result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
    result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
    result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
    result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
    result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
    result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
    result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
    result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
    result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
    result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
    result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
    result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
    result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
    result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
    result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
#endif

    return result;
}

// 获取平移矩阵
RMAPI RLMatrix RLMatrixTranslate(float x, float y, float z)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, x,
                      0.0f, 1.0f, 0.0f, y,
                      0.0f, 0.0f, 1.0f, z,
                      0.0f, 0.0f, 0.0f, 1.0f };

    return result;
}

// 根据轴和角度创建旋转矩阵
// 注意：角度应以弧度为单位
RMAPI RLMatrix RLMatrixRotate(RLVector3 axis, float angle)
{
    RLMatrix result = { 0 };

    float x = axis.x, y = axis.y, z = axis.z;

    float lengthSquared = x*x + y*y + z*z;

    if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f))
    {
        float ilength = 1.0f/sqrtf(lengthSquared);
        x *= ilength;
        y *= ilength;
        z *= ilength;
    }

    float sinres = sinf(angle);
    float cosres = cosf(angle);
    float t = 1.0f - cosres;

    result.m0 = x*x*t + cosres;
    result.m1 = y*x*t + z*sinres;
    result.m2 = z*x*t - y*sinres;
    result.m3 = 0.0f;

    result.m4 = x*y*t - z*sinres;
    result.m5 = y*y*t + cosres;
    result.m6 = z*y*t + x*sinres;
    result.m7 = 0.0f;

    result.m8 = x*z*t + y*sinres;
    result.m9 = y*z*t - x*sinres;
    result.m10 = z*z*t + cosres;
    result.m11 = 0.0f;

    result.m12 = 0.0f;
    result.m13 = 0.0f;
    result.m14 = 0.0f;
    result.m15 = 1.0f;

    return result;
}

// 获取 x 轴旋转矩阵
// 注意：角度必须以弧度为单位
RMAPI RLMatrix RLMatrixRotateX(float angle)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
                      0.0f, 1.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()

    float cosres = cosf(angle);
    float sinres = sinf(angle);

    result.m5 = cosres;
    result.m6 = sinres;
    result.m9 = -sinres;
    result.m10 = cosres;

    return result;
}

// 获取 y 轴旋转矩阵
// 注意：角度必须以弧度为单位
RMAPI RLMatrix RLMatrixRotateY(float angle)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
                      0.0f, 1.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()

    float cosres = cosf(angle);
    float sinres = sinf(angle);

    result.m0 = cosres;
    result.m2 = -sinres;
    result.m8 = sinres;
    result.m10 = cosres;

    return result;
}

// 获取 z 轴旋转矩阵
// 注意：角度必须以弧度为单位
RMAPI RLMatrix RLMatrixRotateZ(float angle)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
                      0.0f, 1.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()

    float cosres = cosf(angle);
    float sinres = sinf(angle);

    result.m0 = cosres;
    result.m1 = sinres;
    result.m4 = -sinres;
    result.m5 = cosres;

    return result;
}


// 获取 xyz 旋转矩阵
// 注意：角度必须以弧度为单位
RMAPI RLMatrix RLMatrixRotateXYZ(RLVector3 angle)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
                      0.0f, 1.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()

    float cosz = cosf(-angle.z);
    float sinz = sinf(-angle.z);
    float cosy = cosf(-angle.y);
    float siny = sinf(-angle.y);
    float cosx = cosf(-angle.x);
    float sinx = sinf(-angle.x);

    result.m0 = cosz*cosy;
    result.m1 = (cosz*siny*sinx) - (sinz*cosx);
    result.m2 = (cosz*siny*cosx) + (sinz*sinx);

    result.m4 = sinz*cosy;
    result.m5 = (sinz*siny*sinx) + (cosz*cosx);
    result.m6 = (sinz*siny*cosx) - (cosz*sinx);

    result.m8 = -siny;
    result.m9 = cosy*sinx;
    result.m10= cosy*cosx;

    return result;
}

// 获取 zyx 旋转矩阵
// 注意：角度必须以弧度为单位
RMAPI RLMatrix RLMatrixRotateZYX(RLVector3 angle)
{
    RLMatrix result = { 0 };

    float cz = cosf(angle.z);
    float sz = sinf(angle.z);
    float cy = cosf(angle.y);
    float sy = sinf(angle.y);
    float cx = cosf(angle.x);
    float sx = sinf(angle.x);

    result.m0 = cz*cy;
    result.m4 = cz*sy*sx - cx*sz;
    result.m8 = sz*sx + cz*cx*sy;
    result.m12 = 0;

    result.m1 = cy*sz;
    result.m5 = cz*cx + sz*sy*sx;
    result.m9 = cx*sz*sy - cz*sx;
    result.m13 = 0;

    result.m2 = -sy;
    result.m6 = cy*sx;
    result.m10 = cy*cx;
    result.m14 = 0;

    result.m3 = 0;
    result.m7 = 0;
    result.m11 = 0;
    result.m15 = 1;

    return result;
}

// 获取缩放矩阵
RMAPI RLMatrix RLMatrixScale(float x, float y, float z)
{
    RLMatrix result = { x, 0.0f, 0.0f, 0.0f,
                      0.0f, y, 0.0f, 0.0f,
                      0.0f, 0.0f, z, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f };

    return result;
}

// 获取透视投影矩阵
RMAPI RLMatrix RLMatrixFrustum(double left, double right, double bottom, double top, double nearPlane, double farPlane)
{
    RLMatrix result = { 0 };

    float rl = (float)(right - left);
    float tb = (float)(top - bottom);
    float fn = (float)(farPlane - nearPlane);

    result.m0 = ((float)nearPlane*2.0f)/rl;
    result.m1 = 0.0f;
    result.m2 = 0.0f;
    result.m3 = 0.0f;

    result.m4 = 0.0f;
    result.m5 = ((float)nearPlane*2.0f)/tb;
    result.m6 = 0.0f;
    result.m7 = 0.0f;

    result.m8 = ((float)right + (float)left)/rl;
    result.m9 = ((float)top + (float)bottom)/tb;
    result.m10 = -((float)farPlane + (float)nearPlane)/fn;
    result.m11 = -1.0f;

    result.m12 = 0.0f;
    result.m13 = 0.0f;
    result.m14 = -((float)farPlane*(float)nearPlane*2.0f)/fn;
    result.m15 = 0.0f;

    return result;
}

// 获取透视投影矩阵
// 注意：视点角度必须以弧度为单位
RMAPI RLMatrix RLMatrixPerspective(double fovY, double aspect, double nearPlane, double farPlane)
{
    RLMatrix result = { 0 };

    double top = nearPlane*tan(fovY*0.5);
    double bottom = -top;
    double right = top*aspect;
    double left = -right;

    // MatrixFrustum(-right, right, -top, top, near, far);
    float rl = (float)(right - left);
    float tb = (float)(top - bottom);
    float fn = (float)(farPlane - nearPlane);

    result.m0 = ((float)nearPlane*2.0f)/rl;
    result.m5 = ((float)nearPlane*2.0f)/tb;
    result.m8 = ((float)right + (float)left)/rl;
    result.m9 = ((float)top + (float)bottom)/tb;
    result.m10 = -((float)farPlane + (float)nearPlane)/fn;
    result.m11 = -1.0f;
    result.m14 = -((float)farPlane*(float)nearPlane*2.0f)/fn;

    return result;
}

// 获取正交投影矩阵
RMAPI RLMatrix RLMatrixOrtho(double left, double right, double bottom, double top, double nearPlane, double farPlane)
{
    RLMatrix result = { 0 };

    float rl = (float)(right - left);
    float tb = (float)(top - bottom);
    float fn = (float)(farPlane - nearPlane);

    result.m0 = 2.0f/rl;
    result.m1 = 0.0f;
    result.m2 = 0.0f;
    result.m3 = 0.0f;
    result.m4 = 0.0f;
    result.m5 = 2.0f/tb;
    result.m6 = 0.0f;
    result.m7 = 0.0f;
    result.m8 = 0.0f;
    result.m9 = 0.0f;
    result.m10 = -2.0f/fn;
    result.m11 = 0.0f;
    result.m12 = -((float)left + (float)right)/rl;
    result.m13 = -((float)top + (float)bottom)/tb;
    result.m14 = -((float)farPlane + (float)nearPlane)/fn;
    result.m15 = 1.0f;

    return result;
}

// 获取相机注视矩阵（视图矩阵）
RMAPI RLMatrix RLMatrixLookAt(RLVector3 eye, RLVector3 target, RLVector3 up)
{
    RLMatrix result = { 0 };

    float length = 0.0f;
    float ilength = 0.0f;

    // Vector3Subtract(eye, target)
    RLVector3 vz = { eye.x - target.x, eye.y - target.y, eye.z - target.z };

    // Vector3Normalize(vz)
    RLVector3 v = vz;
    length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
    if (length == 0.0f) length = 1.0f;
    ilength = 1.0f/length;
    vz.x *= ilength;
    vz.y *= ilength;
    vz.z *= ilength;

    // Vector3CrossProduct(up, vz)
    RLVector3 vx = { up.y*vz.z - up.z*vz.y, up.z*vz.x - up.x*vz.z, up.x*vz.y - up.y*vz.x };

    // Vector3Normalize(x)
    v = vx;
    length = sqrtf(v.x*v.x + v.y*v.y + v.z*v.z);
    if (length == 0.0f) length = 1.0f;
    ilength = 1.0f/length;
    vx.x *= ilength;
    vx.y *= ilength;
    vx.z *= ilength;

    // Vector3CrossProduct(vz, vx)
    RLVector3 vy = { vz.y*vx.z - vz.z*vx.y, vz.z*vx.x - vz.x*vx.z, vz.x*vx.y - vz.y*vx.x };

    result.m0 = vx.x;
    result.m1 = vy.x;
    result.m2 = vz.x;
    result.m3 = 0.0f;
    result.m4 = vx.y;
    result.m5 = vy.y;
    result.m6 = vz.y;
    result.m7 = 0.0f;
    result.m8 = vx.z;
    result.m9 = vy.z;
    result.m10 = vz.z;
    result.m11 = 0.0f;
    result.m12 = -(vx.x*eye.x + vx.y*eye.y + vx.z*eye.z);   // Vector3DotProduct(vx, eye)
    result.m13 = -(vy.x*eye.x + vy.y*eye.y + vy.z*eye.z);   // Vector3DotProduct(vy, eye)
    result.m14 = -(vz.x*eye.x + vz.y*eye.y + vz.z*eye.z);   // Vector3DotProduct(vz, eye)
    result.m15 = 1.0f;

    return result;
}

// 获取矩阵数据的浮点数组
RMAPI float16 RLMatrixToFloatV(RLMatrix mat)
{
    float16 result = { 0 };

    result.v[0] = mat.m0;
    result.v[1] = mat.m1;
    result.v[2] = mat.m2;
    result.v[3] = mat.m3;
    result.v[4] = mat.m4;
    result.v[5] = mat.m5;
    result.v[6] = mat.m6;
    result.v[7] = mat.m7;
    result.v[8] = mat.m8;
    result.v[9] = mat.m9;
    result.v[10] = mat.m10;
    result.v[11] = mat.m11;
    result.v[12] = mat.m12;
    result.v[13] = mat.m13;
    result.v[14] = mat.m14;
    result.v[15] = mat.m15;

    return result;
}

//----------------------------------------------------------------------------------
// 模块函数定义 - 四元数运算
//----------------------------------------------------------------------------------

// 将两个四元数相加
RMAPI RLQuaternion RLQuaternionAdd(RLQuaternion q1, RLQuaternion q2)
{
    RLQuaternion result = {q1.x + q2.x, q1.y + q2.y, q1.z + q2.z, q1.w + q2.w};

    return result;
}

// 将四元数和浮点数相加
RMAPI RLQuaternion RLQuaternionAddValue(RLQuaternion q, float add)
{
    RLQuaternion result = {q.x + add, q.y + add, q.z + add, q.w + add};

    return result;
}

// 减去两个四元数
RMAPI RLQuaternion RLQuaternionSubtract(RLQuaternion q1, RLQuaternion q2)
{
    RLQuaternion result = {q1.x - q2.x, q1.y - q2.y, q1.z - q2.z, q1.w - q2.w};

    return result;
}

// 四元数与浮点数相减
RMAPI RLQuaternion RLQuaternionSubtractValue(RLQuaternion q, float sub)
{
    RLQuaternion result = {q.x - sub, q.y - sub, q.z - sub, q.w - sub};

    return result;
}

// 获取标识四元数
RMAPI RLQuaternion RLQuaternionIdentity(void)
{
    RLQuaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };

    return result;
}

// 计算四元数的长度
RMAPI float RLQuaternionLength(RLQuaternion q)
{
    float result = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);

    return result;
}

// 规范化提供的四元数
RMAPI RLQuaternion RLQuaternionNormalize(RLQuaternion q)
{
    RLQuaternion result = { 0 };

    float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
    if (length == 0.0f) length = 1.0f;
    float ilength = 1.0f/length;

    result.x = q.x*ilength;
    result.y = q.y*ilength;
    result.z = q.z*ilength;
    result.w = q.w*ilength;

    return result;
}

// 反转提供的四元数
RMAPI RLQuaternion RLQuaternionInvert(RLQuaternion q)
{
    RLQuaternion result = q;

    float lengthSq = q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w;

    if (lengthSq != 0.0f)
    {
        float invLength = 1.0f/lengthSq;

        result.x *= -invLength;
        result.y *= -invLength;
        result.z *= -invLength;
        result.w *= invLength;
    }

    return result;
}

// 计算两个四元数的乘法
RMAPI RLQuaternion RLQuaternionMultiply(RLQuaternion q1, RLQuaternion q2)
{
    RLQuaternion result = { 0 };

    float qax = q1.x, qay = q1.y, qaz = q1.z, qaw = q1.w;
    float qbx = q2.x, qby = q2.y, qbz = q2.z, qbw = q2.w;

    result.x = qax*qbw + qaw*qbx + qay*qbz - qaz*qby;
    result.y = qay*qbw + qaw*qby + qaz*qbx - qax*qbz;
    result.z = qaz*qbw + qaw*qbz + qax*qby - qay*qbx;
    result.w = qaw*qbw - qax*qbx - qay*qby - qaz*qbz;

    return result;
}

// 按浮点值缩放四元数
RMAPI RLQuaternion RLQuaternionScale(RLQuaternion q, float mul)
{
    RLQuaternion result = { 0 };

    result.x = q.x*mul;
    result.y = q.y*mul;
    result.z = q.z*mul;
    result.w = q.w*mul;

    return result;
}

// 将两个四元数相除
RMAPI RLQuaternion RLQuaternionDivide(RLQuaternion q1, RLQuaternion q2)
{
    RLQuaternion result = { q1.x/q2.x, q1.y/q2.y, q1.z/q2.z, q1.w/q2.w };

    return result;
}

// 计算两个四元数之间的线性插值
RMAPI RLQuaternion RLQuaternionLerp(RLQuaternion q1, RLQuaternion q2, float amount)
{
    RLQuaternion result = { 0 };

    result.x = q1.x + amount*(q2.x - q1.x);
    result.y = q1.y + amount*(q2.y - q1.y);
    result.z = q1.z + amount*(q2.z - q1.z);
    result.w = q1.w + amount*(q2.w - q1.w);

    return result;
}

// 计算两个四元数之间的 SLERP 优化插值
RMAPI RLQuaternion RLQuaternionNlerp(RLQuaternion q1, RLQuaternion q2, float amount)
{
    RLQuaternion result = { 0 };

    // QuaternionLerp(q1, q2, amount)
    result.x = q1.x + amount*(q2.x - q1.x);
    result.y = q1.y + amount*(q2.y - q1.y);
    result.z = q1.z + amount*(q2.z - q1.z);
    result.w = q1.w + amount*(q2.w - q1.w);

    // QuaternionNormalize(q);
    RLQuaternion q = result;
    float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
    if (length == 0.0f) length = 1.0f;
    float ilength = 1.0f/length;

    result.x = q.x*ilength;
    result.y = q.y*ilength;
    result.z = q.z*ilength;
    result.w = q.w*ilength;

    return result;
}

// 计算两个四元数之间的球面线性插值
RMAPI RLQuaternion RLQuaternionSlerp(RLQuaternion q1, RLQuaternion q2, float amount)
{
    RLQuaternion result = { 0 };

#if !defined(EPSILON)
    #define EPSILON 0.000001f
#endif

    float cosHalfTheta = q1.x*q2.x + q1.y*q2.y + q1.z*q2.z + q1.w*q2.w;

    if (cosHalfTheta < 0)
    {
        q2.x = -q2.x; q2.y = -q2.y; q2.z = -q2.z; q2.w = -q2.w;
        cosHalfTheta = -cosHalfTheta;
    }

    if (fabsf(cosHalfTheta) >= 1.0f) result = q1;
    else if (cosHalfTheta > 0.95f) result = RLQuaternionNlerp(q1, q2, amount);
    else
    {
        float halfTheta = acosf(cosHalfTheta);
        float sinHalfTheta = sqrtf(1.0f - cosHalfTheta*cosHalfTheta);

        if (fabsf(sinHalfTheta) < EPSILON)
        {
            result.x = (q1.x*0.5f + q2.x*0.5f);
            result.y = (q1.y*0.5f + q2.y*0.5f);
            result.z = (q1.z*0.5f + q2.z*0.5f);
            result.w = (q1.w*0.5f + q2.w*0.5f);
        }
        else
        {
            float ratioA = sinf((1 - amount)*halfTheta)/sinHalfTheta;
            float ratioB = sinf(amount*halfTheta)/sinHalfTheta;

            result.x = (q1.x*ratioA + q2.x*ratioB);
            result.y = (q1.y*ratioA + q2.y*ratioB);
            result.z = (q1.z*ratioA + q2.z*ratioB);
            result.w = (q1.w*ratioA + q2.w*ratioB);
        }
    }

    return result;
}

// 使用三次埃尔米特样条算法计算四元数三次样条插值
// 如 GLTF 2.0 规范中所述：https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#interpolation-cubic
RMAPI RLQuaternion RLQuaternionCubicHermiteSpline(RLQuaternion q1, RLQuaternion outTangent1, RLQuaternion q2, RLQuaternion inTangent2, float t)
{
    float t2 = t*t;
    float t3 = t2*t;
    float h00 = 2*t3 - 3*t2 + 1;
    float h10 = t3 - 2*t2 + t;
    float h01 = -2*t3 + 3*t2;
    float h11 = t3 - t2;

    RLQuaternion p0 = RLQuaternionScale(q1, h00);
    RLQuaternion m0 = RLQuaternionScale(outTangent1, h10);
    RLQuaternion p1 = RLQuaternionScale(q2, h01);
    RLQuaternion m1 = RLQuaternionScale(inTangent2, h11);

    RLQuaternion result = { 0 };

    result = RLQuaternionAdd(p0, m0);
    result = RLQuaternionAdd(result, p1);
    result = RLQuaternionAdd(result, m1);
    result = RLQuaternionNormalize(result);

    return result;
}

// 根据从一个向量到另一个向量的旋转计算四元数
RMAPI RLQuaternion RLQuaternionFromVector3ToVector3(RLVector3 from, RLVector3 to)
{
    RLQuaternion result = { 0 };

    float cos2Theta = (from.x*to.x + from.y*to.y + from.z*to.z); // Vector3DotProduct(from, to)
    RLVector3 cross = { from.y*to.z - from.z*to.y, from.z*to.x - from.x*to.z, from.x*to.y - from.y*to.x }; // Vector3CrossProduct(from, to)

    result.x = cross.x;
    result.y = cross.y;
    result.z = cross.z;
    result.w = sqrtf(cross.x*cross.x + cross.y*cross.y + cross.z*cross.z + cos2Theta*cos2Theta) + cos2Theta;

    // QuaternionNormalize(q);
    // 注意：归一化本质上是对原始值进行非线性插值，并将恒等值设为 0.5
    RLQuaternion q = result;
    float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
    if (length == 0.0f) length = 1.0f;
    float ilength = 1.0f/length;

    result.x = q.x*ilength;
    result.y = q.y*ilength;
    result.z = q.z*ilength;
    result.w = q.w*ilength;

    return result;
}

// 获取给定旋转矩阵的四元数
RMAPI RLQuaternion RLQuaternionFromMatrix(RLMatrix mat)
{
    RLQuaternion result = { 0 };

    float fourWSquaredMinus1 = mat.m0  + mat.m5 + mat.m10;
    float fourXSquaredMinus1 = mat.m0  - mat.m5 - mat.m10;
    float fourYSquaredMinus1 = mat.m5  - mat.m0 - mat.m10;
    float fourZSquaredMinus1 = mat.m10 - mat.m0 - mat.m5;

    int biggestIndex = 0;
    float fourBiggestSquaredMinus1 = fourWSquaredMinus1;
    if (fourXSquaredMinus1 > fourBiggestSquaredMinus1)
    {
        fourBiggestSquaredMinus1 = fourXSquaredMinus1;
        biggestIndex = 1;
    }

    if (fourYSquaredMinus1 > fourBiggestSquaredMinus1)
    {
        fourBiggestSquaredMinus1 = fourYSquaredMinus1;
        biggestIndex = 2;
    }

    if (fourZSquaredMinus1 > fourBiggestSquaredMinus1)
    {
        fourBiggestSquaredMinus1 = fourZSquaredMinus1;
        biggestIndex = 3;
    }

    float biggestVal = sqrtf(fourBiggestSquaredMinus1 + 1.0f)*0.5f;
    float mult = 0.25f/biggestVal;

    switch (biggestIndex)
    {
        case 0:
            result.w = biggestVal;
            result.x = (mat.m6 - mat.m9)*mult;
            result.y = (mat.m8 - mat.m2)*mult;
            result.z = (mat.m1 - mat.m4)*mult;
            break;
        case 1:
            result.x = biggestVal;
            result.w = (mat.m6 - mat.m9)*mult;
            result.y = (mat.m1 + mat.m4)*mult;
            result.z = (mat.m8 + mat.m2)*mult;
            break;
        case 2:
            result.y = biggestVal;
            result.w = (mat.m8 - mat.m2)*mult;
            result.x = (mat.m1 + mat.m4)*mult;
            result.z = (mat.m6 + mat.m9)*mult;
            break;
        case 3:
            result.z = biggestVal;
            result.w = (mat.m1 - mat.m4)*mult;
            result.x = (mat.m8 + mat.m2)*mult;
            result.y = (mat.m6 + mat.m9)*mult;
            break;
    }

    return result;
}

// Get a matrix for a given quaternion
RMAPI RLMatrix RLQuaternionToMatrix(RLQuaternion q)
{
    RLMatrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
                      0.0f, 1.0f, 0.0f, 0.0f,
                      0.0f, 0.0f, 1.0f, 0.0f,
                      0.0f, 0.0f, 0.0f, 1.0f }; // MatrixIdentity()

    float a2 = q.x*q.x;
    float b2 = q.y*q.y;
    float c2 = q.z*q.z;
    float ac = q.x*q.z;
    float ab = q.x*q.y;
    float bc = q.y*q.z;
    float ad = q.w*q.x;
    float bd = q.w*q.y;
    float cd = q.w*q.z;

    result.m0 = 1 - 2*(b2 + c2);
    result.m1 = 2*(ab + cd);
    result.m2 = 2*(ac - bd);

    result.m4 = 2*(ab - cd);
    result.m5 = 1 - 2*(a2 + c2);
    result.m6 = 2*(bc + ad);

    result.m8 = 2*(ac + bd);
    result.m9 = 2*(bc - ad);
    result.m10 = 1 - 2*(a2 + b2);

    return result;
}

// Get rotation quaternion for an angle and axis
// NOTE: Angle must be provided in radians
RMAPI RLQuaternion RLQuaternionFromAxisAngle(RLVector3 axis, float angle)
{
    RLQuaternion result = { 0.0f, 0.0f, 0.0f, 1.0f };

    float axisLength = sqrtf(axis.x*axis.x + axis.y*axis.y + axis.z*axis.z);

    if (axisLength != 0.0f)
    {
        angle *= 0.5f;

        float length = 0.0f;
        float ilength = 0.0f;

        // Vector3Normalize(axis)
        length = axisLength;
        if (length == 0.0f) length = 1.0f;
        ilength = 1.0f/length;
        axis.x *= ilength;
        axis.y *= ilength;
        axis.z *= ilength;

        float sinres = sinf(angle);
        float cosres = cosf(angle);

        result.x = axis.x*sinres;
        result.y = axis.y*sinres;
        result.z = axis.z*sinres;
        result.w = cosres;

        // QuaternionNormalize(q);
        RLQuaternion q = result;
        length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
        if (length == 0.0f) length = 1.0f;
        ilength = 1.0f/length;
        result.x = q.x*ilength;
        result.y = q.y*ilength;
        result.z = q.z*ilength;
        result.w = q.w*ilength;
    }

    return result;
}

// Get the rotation angle and axis for a given quaternion
RMAPI void RLQuaternionToAxisAngle(RLQuaternion q, RLVector3 *outAxis, float *outAngle)
{
    if (fabsf(q.w) > 1.0f)
    {
        // QuaternionNormalize(q);
        float length = sqrtf(q.x*q.x + q.y*q.y + q.z*q.z + q.w*q.w);
        if (length == 0.0f) length = 1.0f;
        float ilength = 1.0f/length;

        q.x = q.x*ilength;
        q.y = q.y*ilength;
        q.z = q.z*ilength;
        q.w = q.w*ilength;
    }

    RLVector3 resAxis = { 0.0f, 0.0f, 0.0f };
    float resAngle = 2.0f*acosf(q.w);
    float den = sqrtf(1.0f - q.w*q.w);

    if (den > EPSILON)
    {
        resAxis.x = q.x/den;
        resAxis.y = q.y/den;
        resAxis.z = q.z/den;
    }
    else
    {
        // This occurs when the angle is zero.
        // Not a problem: just set an arbitrary normalized axis.
        resAxis.x = 1.0f;
    }

    *outAxis = resAxis;
    *outAngle = resAngle;
}

// Get the quaternion equivalent to Euler angles
// NOTE: Rotation order is ZYX
RMAPI RLQuaternion RLQuaternionFromEuler(float pitch, float yaw, float roll)
{
    RLQuaternion result = { 0 };

    float x0 = cosf(pitch*0.5f);
    float x1 = sinf(pitch*0.5f);
    float y0 = cosf(yaw*0.5f);
    float y1 = sinf(yaw*0.5f);
    float z0 = cosf(roll*0.5f);
    float z1 = sinf(roll*0.5f);

    result.x = x1*y0*z0 - x0*y1*z1;
    result.y = x0*y1*z0 + x1*y0*z1;
    result.z = x0*y0*z1 - x1*y1*z0;
    result.w = x0*y0*z0 + x1*y1*z1;

    return result;
}

// Get the Euler angles equivalent to quaternion (roll, pitch, yaw)
// NOTE: Angles are returned in a Vector3 struct in radians
RMAPI RLVector3 RLQuaternionToEuler(RLQuaternion q)
{
    RLVector3 result = { 0 };

    // Roll (x-axis rotation)
    float x0 = 2.0f*(q.w*q.x + q.y*q.z);
    float x1 = 1.0f - 2.0f*(q.x*q.x + q.y*q.y);
    result.x = atan2f(x0, x1);

    // Pitch (y-axis rotation)
    float y0 = 2.0f*(q.w*q.y - q.z*q.x);
    y0 = y0 > 1.0f ? 1.0f : y0;
    y0 = y0 < -1.0f ? -1.0f : y0;
    result.y = asinf(y0);

    // Yaw (z-axis rotation)
    float z0 = 2.0f*(q.w*q.z + q.x*q.y);
    float z1 = 1.0f - 2.0f*(q.y*q.y + q.z*q.z);
    result.z = atan2f(z0, z1);

    return result;
}

// Transform a quaternion given a transformation matrix
RMAPI RLQuaternion RLQuaternionTransform(RLQuaternion q, RLMatrix mat)
{
    RLQuaternion result = { 0 };

    result.x = mat.m0*q.x + mat.m4*q.y + mat.m8*q.z + mat.m12*q.w;
    result.y = mat.m1*q.x + mat.m5*q.y + mat.m9*q.z + mat.m13*q.w;
    result.z = mat.m2*q.x + mat.m6*q.y + mat.m10*q.z + mat.m14*q.w;
    result.w = mat.m3*q.x + mat.m7*q.y + mat.m11*q.z + mat.m15*q.w;

    return result;
}

// Check whether two given quaternions are almost equal
RMAPI int RLQuaternionEquals(RLQuaternion p, RLQuaternion q)
{
#if !defined(EPSILON)
    #define EPSILON 0.000001f
#endif

    int result = (((fabsf(p.x - q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
                  ((fabsf(p.y - q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
                  ((fabsf(p.z - q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
                  ((fabsf(p.w - q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w)))))) ||
                 (((fabsf(p.x + q.x)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.x), fabsf(q.x))))) &&
                  ((fabsf(p.y + q.y)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.y), fabsf(q.y))))) &&
                  ((fabsf(p.z + q.z)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.z), fabsf(q.z))))) &&
                  ((fabsf(p.w + q.w)) <= (EPSILON*fmaxf(1.0f, fmaxf(fabsf(p.w), fabsf(q.w))))));

    return result;
}

// Compose a transformation matrix from rotational, translational and scaling components
// TODO: This function is not following raymath conventions defined in header: NOT self-contained
RMAPI RLMatrix RLMatrixCompose(RLVector3 translation, RLQuaternion rotation, RLVector3 scale)
{
    // Initialize vectors
    RLVector3 right = { 1.0f, 0.0f, 0.0f };
    RLVector3 up = { 0.0f, 1.0f, 0.0f };
    RLVector3 forward = { 0.0f, 0.0f, 1.0f };

    // Scale vectors
    right = RLVector3Scale(right, scale.x);
    up = RLVector3Scale(up, scale.y);
    forward = RLVector3Scale(forward , scale.z);

    // Rotate vectors
    right = RLVector3RotateByQuaternion(right, rotation);
    up = RLVector3RotateByQuaternion(up, rotation);
    forward = RLVector3RotateByQuaternion(forward, rotation);
    
    // Set result matrix output
    RLMatrix result = {
        right.x, up.x, forward.x, translation.x,
        right.y, up.y, forward.y, translation.y,
        right.z, up.z, forward.z, translation.z,
        0.0f, 0.0f, 0.0f, 1.0f
    };

    return result;
}

// Decompose a transformation matrix into its rotational, translational and scaling components and remove shear
// TODO: This function is not following raymath conventions defined in header: NOT self-contained
RMAPI void RLMatrixDecompose(RLMatrix mat, RLVector3 *translation, RLQuaternion *rotation, RLVector3 *scale)
{
    float eps = (float)1e-9;

    // Extract Translation
    translation->x = mat.m12;
    translation->y = mat.m13;
    translation->z = mat.m14;

    // Matrix Columns - Rotation will be extracted into here.
    RLVector3 matColumns[3] = { { mat.m0, mat.m4, mat.m8 },
                             { mat.m1, mat.m5, mat.m9 },
                             { mat.m2, mat.m6, mat.m10 } };

    // Shear Parameters XY, XZ, and YZ (extract and ignored)
    float shear[3] = { 0 };

    // Normalized Scale Parameters
    RLVector3 scl = { 0 };

    // Max-Normalizing helps numerical stability
    float stabilizer = eps;
    for (int i = 0; i < 3; i++)
    {
        stabilizer = fmaxf(stabilizer, fabsf(matColumns[i].x));
        stabilizer = fmaxf(stabilizer, fabsf(matColumns[i].y));
        stabilizer = fmaxf(stabilizer, fabsf(matColumns[i].z));
    };
    matColumns[0] = RLVector3Scale(matColumns[0], 1.0f / stabilizer);
    matColumns[1] = RLVector3Scale(matColumns[1], 1.0f / stabilizer);
    matColumns[2] = RLVector3Scale(matColumns[2], 1.0f / stabilizer);

    // X Scale
    scl.x = RLVector3Length(matColumns[0]);
    if (scl.x > eps) matColumns[0] = RLVector3Scale(matColumns[0], 1.0f / scl.x);

    // Compute XY shear and make col2 orthogonal
    shear[0] = RLVector3DotProduct(matColumns[0], matColumns[1]);
    matColumns[1] = RLVector3Subtract(matColumns[1], RLVector3Scale(matColumns[0], shear[0]));

    // Y Scale
    scl.y = RLVector3Length(matColumns[1]);
    if (scl.y > eps)
    {
        matColumns[1] = RLVector3Scale(matColumns[1], 1.0f / scl.y);
        shear[0] /= scl.y; // Correct XY shear
    }

    // Compute XZ and YZ shears and make col3 orthogonal
    shear[1] = RLVector3DotProduct(matColumns[0], matColumns[2]);
    matColumns[2] = RLVector3Subtract(matColumns[2], RLVector3Scale(matColumns[0], shear[1]));
    shear[2] = RLVector3DotProduct(matColumns[1], matColumns[2]);
    matColumns[2] = RLVector3Subtract(matColumns[2], RLVector3Scale(matColumns[1], shear[2]));

    // Z Scale
    scl.z = RLVector3Length(matColumns[2]);
    if (scl.z > eps)
    {
        matColumns[2] = RLVector3Scale(matColumns[2], 1.0f / scl.z);
        shear[1] /= scl.z; // Correct XZ shear
        shear[2] /= scl.z; // Correct YZ shear
    }

    // matColumns are now orthonormal in O(3). Now ensure its in SO(3) by enforcing det = 1.
    if (RLVector3DotProduct(matColumns[0], RLVector3CrossProduct(matColumns[1], matColumns[2])) < 0)
    {
        scl = RLVector3Negate(scl);
        matColumns[0] = RLVector3Negate(matColumns[0]);
        matColumns[1] = RLVector3Negate(matColumns[1]);
        matColumns[2] = RLVector3Negate(matColumns[2]);
    }

    // Set Scale
    *scale = RLVector3Scale(scl, stabilizer);

    // Extract Rotation
    RLMatrix rotationMatrix = { matColumns[0].x, matColumns[0].y, matColumns[0].z, 0,
                             matColumns[1].x, matColumns[1].y, matColumns[1].z, 0,
                             matColumns[2].x, matColumns[2].y, matColumns[2].z, 0,
                             0, 0, 0, 1 };
    *rotation = RLQuaternionFromMatrix(rotationMatrix);
}

#if defined(__cplusplus) && !defined(RAYMATH_DISABLE_CPP_OPERATORS)

// Optional C++ math operators
//-------------------------------------------------------------------------------

// Vector2 operators
static constexpr RLVector2 Vector2Zeros = { 0, 0 };
static constexpr RLVector2 Vector2Ones = { 1, 1 };
static constexpr RLVector2 Vector2UnitX = { 1, 0 };
static constexpr RLVector2 Vector2UnitY = { 0, 1 };

inline RLVector2 operator + (const RLVector2& lhs, const RLVector2& rhs)
{
    return RLVector2Add(lhs, rhs);
}

inline const RLVector2& operator += (RLVector2& lhs, const RLVector2& rhs)
{
    lhs = RLVector2Add(lhs, rhs);
    return lhs;
}

inline RLVector2 operator - (const RLVector2& lhs, const RLVector2& rhs)
{
    return RLVector2Subtract(lhs, rhs);
}

inline const RLVector2& operator -= (RLVector2& lhs, const RLVector2& rhs)
{
    lhs = RLVector2Subtract(lhs, rhs);
    return lhs;
}

inline RLVector2 operator * (const RLVector2& lhs, const float& rhs)
{
    return RLVector2Scale(lhs, rhs);
}

inline const RLVector2& operator *= (RLVector2& lhs, const float& rhs)
{
    lhs = RLVector2Scale(lhs, rhs);
    return lhs;
}

inline RLVector2 operator * (const RLVector2& lhs, const RLVector2& rhs)
{
    return RLVector2Multiply(lhs, rhs);
}

inline const RLVector2& operator *= (RLVector2& lhs, const RLVector2& rhs)
{
    lhs = RLVector2Multiply(lhs, rhs);
    return lhs;
}

inline RLVector2 operator * (const RLVector2& lhs, const RLMatrix& rhs)
{
    return RLVector2Transform(lhs, rhs);
}

inline const RLVector2& operator *= (RLVector2& lhs, const RLMatrix& rhs)
{
    lhs = RLVector2Transform(lhs, rhs);
    return lhs;
}

inline RLVector2 operator / (const RLVector2& lhs, const float& rhs)
{
    return RLVector2Scale(lhs, 1.0f/rhs);
}

inline const RLVector2& operator /= (RLVector2& lhs, const float& rhs)
{
    lhs = RLVector2Scale(lhs, 1.0f/rhs);
    return lhs;
}

inline RLVector2 operator / (const RLVector2& lhs, const RLVector2& rhs)
{
    return RLVector2Divide(lhs, rhs);
}

inline const RLVector2& operator /= (RLVector2& lhs, const RLVector2& rhs)
{
    lhs = RLVector2Divide(lhs, rhs);
    return lhs;
}

inline bool operator == (const RLVector2& lhs, const RLVector2& rhs)
{
    return RLFloatEquals(lhs.x, rhs.x) && RLFloatEquals(lhs.y, rhs.y);
}

inline bool operator != (const RLVector2& lhs, const RLVector2& rhs)
{
    return !RLFloatEquals(lhs.x, rhs.x) || !RLFloatEquals(lhs.y, rhs.y);
}

// Vector3 operators
static constexpr RLVector3 Vector3Zeros = { 0, 0, 0 };
static constexpr RLVector3 Vector3Ones = { 1, 1, 1 };
static constexpr RLVector3 Vector3UnitX = { 1, 0, 0 };
static constexpr RLVector3 Vector3UnitY = { 0, 1, 0 };
static constexpr RLVector3 Vector3UnitZ = { 0, 0, 1 };

inline RLVector3 operator + (const RLVector3& lhs, const RLVector3& rhs)
{
    return RLVector3Add(lhs, rhs);
}

inline const RLVector3& operator += (RLVector3& lhs, const RLVector3& rhs)
{
    lhs = RLVector3Add(lhs, rhs);
    return lhs;
}

inline RLVector3 operator - (const RLVector3& lhs, const RLVector3& rhs)
{
    return RLVector3Subtract(lhs, rhs);
}

inline const RLVector3& operator -= (RLVector3& lhs, const RLVector3& rhs)
{
    lhs = RLVector3Subtract(lhs, rhs);
    return lhs;
}

inline RLVector3 operator * (const RLVector3& lhs, const float& rhs)
{
    return RLVector3Scale(lhs, rhs);
}

inline const RLVector3& operator *= (RLVector3& lhs, const float& rhs)
{
    lhs = RLVector3Scale(lhs, rhs);
    return lhs;
}

inline RLVector3 operator * (const RLVector3& lhs, const RLVector3& rhs)
{
    return RLVector3Multiply(lhs, rhs);
}

inline const RLVector3& operator *= (RLVector3& lhs, const RLVector3& rhs)
{
    lhs = RLVector3Multiply(lhs, rhs);
    return lhs;
}

inline RLVector3 operator * (const RLVector3& lhs, const RLMatrix& rhs)
{
    return RLVector3Transform(lhs, rhs);
}

inline const RLVector3& operator *= (RLVector3& lhs, const RLMatrix& rhs)
{
    lhs = RLVector3Transform(lhs, rhs);
    return lhs;
}

inline RLVector3 operator / (const RLVector3& lhs, const float& rhs)
{
    return RLVector3Scale(lhs, 1.0f/rhs);
}

inline const RLVector3& operator /= (RLVector3& lhs, const float& rhs)
{
    lhs = RLVector3Scale(lhs, 1.0f/rhs);
    return lhs;
}

inline RLVector3 operator / (const RLVector3& lhs, const RLVector3& rhs)
{
    return RLVector3Divide(lhs, rhs);
}

inline const RLVector3& operator /= (RLVector3& lhs, const RLVector3& rhs)
{
    lhs = RLVector3Divide(lhs, rhs);
    return lhs;
}

inline bool operator == (const RLVector3& lhs, const RLVector3& rhs)
{
    return RLFloatEquals(lhs.x, rhs.x) && RLFloatEquals(lhs.y, rhs.y) && RLFloatEquals(lhs.z, rhs.z);
}

inline bool operator != (const RLVector3& lhs, const RLVector3& rhs)
{
    return !RLFloatEquals(lhs.x, rhs.x) || !RLFloatEquals(lhs.y, rhs.y) || !RLFloatEquals(lhs.z, rhs.z);
}

// Vector4 operators
static constexpr RLVector4 Vector4Zeros = { 0, 0, 0, 0 };
static constexpr RLVector4 Vector4Ones = { 1, 1, 1, 1 };
static constexpr RLVector4 Vector4UnitX = { 1, 0, 0, 0 };
static constexpr RLVector4 Vector4UnitY = { 0, 1, 0, 0 };
static constexpr RLVector4 Vector4UnitZ = { 0, 0, 1, 0 };
static constexpr RLVector4 Vector4UnitW = { 0, 0, 0, 1 };

inline RLVector4 operator + (const RLVector4& lhs, const RLVector4& rhs)
{
    return RLVector4Add(lhs, rhs);
}

inline const RLVector4& operator += (RLVector4& lhs, const RLVector4& rhs)
{
    lhs = RLVector4Add(lhs, rhs);
    return lhs;
}

inline RLVector4 operator - (const RLVector4& lhs, const RLVector4& rhs)
{
    return RLVector4Subtract(lhs, rhs);
}

inline const RLVector4& operator -= (RLVector4& lhs, const RLVector4& rhs)
{
    lhs = RLVector4Subtract(lhs, rhs);
    return lhs;
}

inline RLVector4 operator * (const RLVector4& lhs, const float& rhs)
{
    return RLVector4Scale(lhs, rhs);
}

inline const RLVector4& operator *= (RLVector4& lhs, const float& rhs)
{
    lhs = RLVector4Scale(lhs, rhs);
    return lhs;
}

inline RLVector4 operator * (const RLVector4& lhs, const RLVector4& rhs)
{
    return RLVector4Multiply(lhs, rhs);
}

inline const RLVector4& operator *= (RLVector4& lhs, const RLVector4& rhs)
{
    lhs = RLVector4Multiply(lhs, rhs);
    return lhs;
}

inline RLVector4 operator / (const RLVector4& lhs, const float& rhs)
{
    return RLVector4Scale(lhs, 1.0f/rhs);
}

inline const RLVector4& operator /= (RLVector4& lhs, const float& rhs)
{
    lhs = RLVector4Scale(lhs, 1.0f/rhs);
    return lhs;
}

inline RLVector4 operator / (const RLVector4& lhs, const RLVector4& rhs)
{
    return RLVector4Divide(lhs, rhs);
}

inline const RLVector4& operator /= (RLVector4& lhs, const RLVector4& rhs)
{
    lhs = RLVector4Divide(lhs, rhs);
    return lhs;
}

inline bool operator == (const RLVector4& lhs, const RLVector4& rhs)
{
    return RLFloatEquals(lhs.x, rhs.x) && RLFloatEquals(lhs.y, rhs.y) && RLFloatEquals(lhs.z, rhs.z) && RLFloatEquals(lhs.w, rhs.w);
}

inline bool operator != (const RLVector4& lhs, const RLVector4& rhs)
{
    return !RLFloatEquals(lhs.x, rhs.x) || !RLFloatEquals(lhs.y, rhs.y) || !RLFloatEquals(lhs.z, rhs.z) || !RLFloatEquals(lhs.w, rhs.w);
}

// Quaternion operators
static constexpr RLQuaternion QuaternionZeros = { 0, 0, 0, 0 };
static constexpr RLQuaternion QuaternionOnes = { 1, 1, 1, 1 };
static constexpr RLQuaternion QuaternionUnitX = { 0, 0, 0, 1 };

inline RLQuaternion operator + (const RLQuaternion& lhs, const float& rhs)
{
    return RLQuaternionAddValue(lhs, rhs);
}

inline const RLQuaternion& operator += (RLQuaternion& lhs, const float& rhs)
{
    lhs = RLQuaternionAddValue(lhs, rhs);
    return lhs;
}

inline RLQuaternion operator - (const RLQuaternion& lhs, const float& rhs)
{
    return RLQuaternionSubtractValue(lhs, rhs);
}

inline const RLQuaternion& operator -= (RLQuaternion& lhs, const float& rhs)
{
    lhs = RLQuaternionSubtractValue(lhs, rhs);
    return lhs;
}

inline RLQuaternion operator * (const RLQuaternion& lhs, const RLMatrix& rhs)
{
    return RLQuaternionTransform(lhs, rhs);
}

inline const RLQuaternion& operator *= (RLQuaternion& lhs, const RLMatrix& rhs)
{
    lhs = RLQuaternionTransform(lhs, rhs);
    return lhs;
}

// Matrix operators
inline RLMatrix operator + (const RLMatrix& lhs, const RLMatrix& rhs)
{
    return RLMatrixAdd(lhs, rhs);
}

inline const RLMatrix& operator += (RLMatrix& lhs, const RLMatrix& rhs)
{
    lhs = RLMatrixAdd(lhs, rhs);
    return lhs;
}

inline RLMatrix operator - (const RLMatrix& lhs, const RLMatrix& rhs)
{
    return RLMatrixSubtract(lhs, rhs);
}

inline const RLMatrix& operator -= (RLMatrix& lhs, const RLMatrix& rhs)
{
    lhs = RLMatrixSubtract(lhs, rhs);
    return lhs;
}

inline RLMatrix operator * (const RLMatrix& lhs, const RLMatrix& rhs)
{
    return RLMatrixMultiply(lhs, rhs);
}

inline const RLMatrix& operator *= (RLMatrix& lhs, const RLMatrix& rhs)
{
    lhs = RLMatrixMultiply(lhs, rhs);
    return lhs;
}
//-------------------------------------------------------------------------------
#endif  // C++ operators

#endif  // RAYMATH_H
