2025-06-21 20:12:15 +03:00
|
|
|
#include <BH/Math/Vec3f.h>
|
2025-02-28 10:08:05 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fAdd(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
out[0] = a[0] + b[0];
|
|
|
|
|
out[1] = a[1] + b[1];
|
|
|
|
|
out[2] = a[2] + b[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fSub(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
out[0] = a[0] - b[0];
|
|
|
|
|
out[1] = a[1] - b[1];
|
|
|
|
|
out[2] = a[2] - b[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fMul(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
out[0] = a[0] * b[0];
|
|
|
|
|
out[1] = a[1] * b[1];
|
|
|
|
|
out[2] = a[2] * b[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fScale(const float a[3],
|
|
|
|
|
float b,
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
out[0] = a[0] * b;
|
|
|
|
|
out[1] = a[1] * b;
|
|
|
|
|
out[2] = a[2] * b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fMulAdd(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
const float c[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
out[0] = a[0] * b[0] + c[0];
|
|
|
|
|
out[1] = a[1] * b[1] + c[1];
|
|
|
|
|
out[2] = a[2] * b[2] + c[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fNegate(const float in[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
out[0] = -in[0];
|
|
|
|
|
out[1] = -in[1];
|
|
|
|
|
out[2] = -in[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
float BH_Vec3fDot(const float a[3],
|
|
|
|
|
const float b[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fCross(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
float tmp[3];
|
|
|
|
|
|
|
|
|
|
tmp[0] = a[1] * b[2] - a[2] * b[1];
|
|
|
|
|
tmp[1] = a[2] * b[0] - a[0] * b[2];
|
|
|
|
|
tmp[2] = a[0] * b[1] - a[1] * b[0];
|
|
|
|
|
memcpy(out, tmp, sizeof(tmp));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
float BH_Vec3fLength(const float in[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
return sqrtf(BH_Vec3fDot(in, in));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fNormal(const float in[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
BH_Vec3fScale(in, 1.0f / BH_Vec3fLength(in), out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
float BH_Vec3fNormalEx(const float in[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
float length;
|
|
|
|
|
|
|
|
|
|
length = BH_Vec3fLength(in);
|
|
|
|
|
BH_Vec3fScale(in, 1.0f / length, out);
|
|
|
|
|
return length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fMin(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
if (a[0] < b[0]) out[0] = a[0]; else out[0] = b[0];
|
|
|
|
|
if (a[1] < b[1]) out[1] = a[1]; else out[1] = b[1];
|
|
|
|
|
if (a[2] < b[2]) out[2] = a[2]; else out[2] = b[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fMax(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
if (a[0] > b[0]) out[0] = a[0]; else out[0] = b[0];
|
|
|
|
|
if (a[1] > b[1]) out[1] = a[1]; else out[1] = b[1];
|
|
|
|
|
if (a[2] > b[2]) out[2] = a[2]; else out[2] = b[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fLerp(const float a[3],
|
|
|
|
|
const float b[3],
|
2025-02-28 10:08:05 +03:00
|
|
|
float t,
|
2025-04-29 18:17:00 +03:00
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
float tmp[3];
|
|
|
|
|
|
|
|
|
|
BH_Vec3fSub(b, a, tmp);
|
|
|
|
|
BH_Vec3fScale(tmp, t, tmp);
|
|
|
|
|
BH_Vec3fAdd(a, tmp, out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fProject(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
float amount;
|
|
|
|
|
|
|
|
|
|
amount = BH_Vec3fDot(a, b) / BH_Vec3fDot(b, b);
|
|
|
|
|
BH_Vec3fScale(b, amount, out);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-04-29 18:17:00 +03:00
|
|
|
void BH_Vec3fBarycentric(const float a[3],
|
|
|
|
|
const float b[3],
|
|
|
|
|
const float c[3],
|
2025-02-28 10:08:05 +03:00
|
|
|
float v,
|
|
|
|
|
float w,
|
2025-04-29 18:17:00 +03:00
|
|
|
float out[3])
|
2025-02-28 10:08:05 +03:00
|
|
|
{
|
|
|
|
|
float tmp1[3], tmp2[3];
|
|
|
|
|
float u;
|
|
|
|
|
|
|
|
|
|
u = 1.0f - v - w;
|
|
|
|
|
tmp1[0] = tmp1[1] = tmp1[2] = u; BH_Vec3fMul(a, tmp1, tmp2);
|
|
|
|
|
tmp1[0] = tmp1[1] = tmp1[2] = v; BH_Vec3fMulAdd(b, tmp1, tmp2, tmp2);
|
|
|
|
|
tmp1[0] = tmp1[1] = tmp1[2] = w; BH_Vec3fMulAdd(c, tmp1, tmp2, out);
|
|
|
|
|
}
|