vn/code/core/core_math.cpp

739 lines
16 KiB
C++
Raw Permalink Normal View History

2023-06-21 16:59:36 +00:00
//- sixten: V2 R32
inline v2_r32 V2R32(r32 x, r32 y)
{
v2_r32 Result = {x, y};
return(Result);
}
inline v2_r32 operator+(v2_r32 A, v2_r32 B)
{
v2_r32 Result = {A.x + B.x, A.y + B.y};
return(Result);
}
inline v2_r32 operator-(v2_r32 A, v2_r32 B)
{
v2_r32 Result = {A.x - B.x, A.y - B.y};
return(Result);
}
inline v2_r32 operator*(v2_r32 A, v2_r32 B)
{
v2_r32 Result = {A.x * B.x, A.y * B.y};
return(Result);
}
inline v2_r32 operator/(v2_r32 A, v2_r32 B)
{
v2_r32 Result = {A.x / B.x, A.y / B.y};
return(Result);
}
inline v2_r32 operator*(v2_r32 A, r32 B)
{
v2_r32 Result = {A.x * B, A.y * B};
return(Result);
}
inline v2_r32 operator+=(v2_r32 &A, v2_r32 B) { return A = A + B; }
inline v2_r32 operator-=(v2_r32 &A, v2_r32 B) { return A = A - B; }
inline v2_r32 operator*=(v2_r32 &A, v2_r32 B) { return A = A * B; }
inline v2_r32 operator/=(v2_r32 &A, v2_r32 B) { return A = A / B; }
inline v2_r32 operator*=(v2_r32 &A, r32 B) { return A = A * B; }
inline b32 operator==(v2_r32 A, v2_r32 B)
{
b32 Result = A.x == B.x && A.y == B.y;
return(Result);
}
inline b32 operator!=(v2_r32 A, v2_r32 B)
{
b32 Result = A.x != B.x || A.y != B.y;
return(Result);
}
2023-06-21 16:59:36 +00:00
inline r32 Inner(v2_r32 A, v2_r32 B)
{
r32 Result = A.x*B.x + A.y*B.y;
return(Result);
}
inline r32 LengthSquared(v2_r32 V)
{
r32 Result = Inner(V, V);
return(Result);
}
inline r32 Length(v2_r32 V)
{
r32 Result = SquareRoot(LengthSquared(V));
return(Result);
}
inline v2_r32 NOZ(v2_r32 V)
{
v2_r32 Result = {};
r32 Len = Length(V);
if(Len != 0)
{
Result = V / V2R32(Len, Len);
}
return(Result);
}
inline v2_r32 Hadamard(v2_r32 A, v2_r32 B)
{
v2_r32 Result = {A.x*B.x, A.y*B.y};
return(Result);
}
2023-06-21 16:59:36 +00:00
inline v2_r32 LinearBlend(v2_r32 A, v2_r32 B, r32 x)
{
v2_r32 Result = A+(B-A)*x;
return(Result);
}
inline v2_r32 Min(v2_r32 A, v2_r32 B)
{
v2_r32 Result = V2R32(Min(A.x, B.x), Min(A.y, B.y));
return(Result);
}
inline v2_r32 Max(v2_r32 A, v2_r32 B)
{
v2_r32 Result = V2R32(Max(A.x, B.x), Max(A.y, B.y));
return(Result);
}
//- sixten: V2 S32
inline v2_s32 V2S32(s32 x, s32 y)
{
v2_s32 Result = {x, y};
return(Result);
}
inline v2_s32 operator+(v2_s32 A, v2_s32 B)
{
v2_s32 Result = {A.x + B.x, A.y + B.y};
return(Result);
}
inline v2_s32 operator-(v2_s32 A, v2_s32 B)
{
v2_s32 Result = {A.x - B.x, A.y - B.y};
return(Result);
}
inline v2_s32 operator+=(v2_s32 &A, v2_s32 B) { return A = A + B; }
inline v2_s32 operator-=(v2_s32 &A, v2_s32 B) { return A = A - B; }
inline v2_s32 Min(v2_s32 A, v2_s32 B)
{
v2_s32 Result = V2S32(Min(A.x, B.x), Min(A.y, B.y));
return(Result);
}
inline v2_s32 Max(v2_s32 A, v2_s32 B)
{
v2_s32 Result = V2S32(Max(A.x, B.x), Max(A.y, B.y));
return(Result);
}
//- sixten: V2 S64
inline v2_s64 V2S64(s64 x, s64 y)
{
v2_s64 Result = {x, y};
return(Result);
}
inline v2_s64 operator+(v2_s64 A, v2_s64 B)
{
v2_s64 Result = {A.x + B.x, A.y + B.y};
return(Result);
}
inline v2_s64 operator-(v2_s64 A, v2_s64 B)
{
v2_s64 Result = {A.x - B.x, A.y - B.y};
return(Result);
}
inline v2_s64 operator+=(v2_s64 &A, v2_s64 B) { return A = A + B; }
inline v2_s64 operator-=(v2_s64 &A, v2_s64 B) { return A = A - B; }
inline v2_s64 Min(v2_s64 A, v2_s64 B)
{
v2_s64 Result = V2S64(Min(A.x, B.x), Min(A.y, B.y));
return(Result);
}
inline v2_s64 Max(v2_s64 A, v2_s64 B)
{
v2_s64 Result = V2S64(Max(A.x, B.x), Max(A.y, B.y));
return(Result);
}
//- sixten: V3 R32
inline v3_r32 V3R32(r32 x, r32 y, r32 z)
{
v3_r32 Result = {x, y, z};
return(Result);
}
inline v3_r32 operator+(v3_r32 A, v3_r32 B)
{
v3_r32 Result = {A.x + B.x, A.y + B.y, A.z + B.z};
return(Result);
}
inline v3_r32 operator-(v3_r32 A, v3_r32 B)
{
v3_r32 Result = {A.x - B.x, A.y - B.y, A.z - B.z};
return(Result);
}
inline v3_r32 operator*(v3_r32 A, v3_r32 B)
{
v3_r32 Result = {A.x * B.x, A.y * B.y, A.z * B.z};
return(Result);
}
inline v3_r32 operator/(v3_r32 A, v3_r32 B)
{
v3_r32 Result = {A.x / B.x, A.y / B.y, A.z / B.z};
return(Result);
}
inline v3_r32 operator*(v3_r32 A, r32 B)
{
v3_r32 Result = {A.x * B, A.y * B, A.z * B};
return(Result);
}
inline v3_r32 operator+=(v3_r32 &A, v3_r32 B) { return A = A + B; }
inline v3_r32 operator-=(v3_r32 &A, v3_r32 B) { return A = A - B; }
inline v3_r32 operator*=(v3_r32 &A, v3_r32 B) { return A = A * B; }
inline v3_r32 operator/=(v3_r32 &A, v3_r32 B) { return A = A / B; }
inline v3_r32 operator*=(v3_r32 &A, r32 B) { return A = A * B; }
inline r32 Inner(v3_r32 A, v3_r32 B)
{
r32 Result = A.x*B.x + A.y*B.y + A.z*B.z;
return(Result);
}
inline r32 LengthSquared(v3_r32 V)
{
r32 Result = Inner(V, V);
return(Result);;
}
inline r32 Length(v3_r32 V)
{
r32 Result = SquareRoot(LengthSquared(V));
return(Result);
}
inline v3_r32 NOZ(v3_r32 V)
{
v3_r32 Result = {};
r32 Len = Length(V);
if(Len != 0)
{
Result = V / V3R32(Len, Len, Len);
}
return(Result);
}
inline v3_r32 LinearBlend(v3_r32 A, v3_r32 B, r32 x)
{
v3_r32 Result = A+(B-A)*x;
return(Result);
}
inline v3_r32 Min(v3_r32 A, v3_r32 B)
{
v3_r32 Result = V3R32(Min(A.x, B.x), Min(A.y, B.y), Min(A.z, B.z));
return(Result);
}
inline v3_r32 Max(v3_r32 A, v3_r32 B)
{
v3_r32 Result = V3R32(Max(A.x, B.x), Max(A.y, B.y), Max(A.z, B.z));
return(Result);
}
//- sixten: V3 S32
inline v3_s32 V3S32(s32 x, s32 y, s32 z)
{
v3_s32 Result = {x, y, z};
return(Result);
}
inline v3_s32 operator+(v3_s32 A, v3_s32 B)
{
v3_s32 Result = {A.x + B.x, A.y + B.y, A.z + B.z};
return(Result);
}
inline v3_s32 operator-(v3_s32 A, v3_s32 B)
{
v3_s32 Result = {A.x - B.x, A.y - B.y, A.z - B.z};
return(Result);
}
inline v3_s32 operator+=(v3_s32 &A, v3_s32 B) { return A = A + B; }
inline v3_s32 operator-=(v3_s32 &A, v3_s32 B) { return A = A - B; }
inline v3_s32 Min(v3_s32 A, v3_s32 B)
{
v3_s32 Result = V3S32(Min(A.x, B.x), Min(A.y, B.y), Min(A.z, B.z));
return(Result);
}
inline v3_s32 Max(v3_s32 A, v3_s32 B)
{
v3_s32 Result = V3S32(Max(A.x, B.x), Max(A.y, B.y), Max(A.z, B.z));
return(Result);
}
//- sixten: V3 S64
inline v3_s64 V4S64(s64 x, s64 y, s64 z)
{
v3_s64 Result = {x, y, z};
return(Result);
}
inline v3_s64 operator+(v3_s64 A, v3_s64 B)
{
v3_s64 Result = {A.x + B.x, A.y + B.y, A.z + B.z};
return(Result);
}
inline v3_s64 operator-(v3_s64 A, v3_s64 B)
{
v3_s64 Result = {A.x - B.x, A.y - B.y, A.z - B.z};
return(Result);
}
inline v3_s64 operator+=(v3_s64 &A, v3_s64 B) { return A = A + B; }
inline v3_s64 operator-=(v3_s64 &A, v3_s64 B) { return A = A - B; }
inline v3_s64 Min(v3_s64 A, v3_s64 B)
{
v3_s64 Result = V4S64(Min(A.x, B.x), Min(A.y, B.y), Min(A.z, B.z));
return(Result);
}
inline v3_s64 Max(v3_s64 A, v3_s64 B)
{
v3_s64 Result = V4S64(Max(A.x, B.x), Max(A.y, B.y), Max(A.z, B.z));
return(Result);
}
//- sixten: V4 R32
inline v4_r32 V4R32(r32 x, r32 y, r32 z, r32 w)
{
v4_r32 Result = {x, y, z, w};
return(Result);
}
inline v4_r32 operator+(v4_r32 A, v4_r32 B)
{
v4_r32 Result = {A.x + B.x, A.y + B.y, A.z + B.z, A.w + B.w};
return(Result);
}
inline v4_r32 operator-(v4_r32 A, v4_r32 B)
{
v4_r32 Result = {A.x - B.x, A.y - B.y, A.z - B.z, A.w - B.w};
return(Result);
}
inline v4_r32 operator*(v4_r32 A, v4_r32 B)
{
v4_r32 Result = {A.x * B.x, A.y * B.y, A.z * B.z, A.w * B.w};
return(Result);
}
inline v4_r32 operator/(v4_r32 A, v4_r32 B)
{
v4_r32 Result = {A.x / B.x, A.y / B.y, A.z / B.z, A.w / B.w};
return(Result);
}
inline v4_r32 operator*(v4_r32 A, r32 B)
{
v4_r32 Result = {A.x * B, A.y * B, A.z * B, A.w * B};
return(Result);
}
inline v4_r32 operator+=(v4_r32 &A, v4_r32 B) { return A = A + B; }
inline v4_r32 operator-=(v4_r32 &A, v4_r32 B) { return A = A - B; }
inline v4_r32 operator*=(v4_r32 &A, v4_r32 B) { return A = A * B; }
inline v4_r32 operator/=(v4_r32 &A, v4_r32 B) { return A = A / B; }
inline v4_r32 operator*=(v4_r32 &A, r32 B) { return A = A * B; }
inline r32 Inner(v4_r32 A, v4_r32 B)
{
r32 Result = A.x*B.x + A.y*B.y + A.z*B.z + A.w*B.w;
return(Result);
}
inline r32 LengthSquared(v4_r32 V)
{
r32 Result = Inner(V, V);
return(Result);;
}
inline r32 Length(v4_r32 V)
{
r32 Result = SquareRoot(LengthSquared(V));
return(Result);
}
inline v4_r32 NOZ(v4_r32 V)
{
v4_r32 Result = {};
r32 Len = Length(V);
if(Len != 0)
{
Result = V / V4R32(Len, Len, Len, Len);
}
return(Result);
}
inline v4_r32 LinearBlend(v4_r32 A, v4_r32 B, r32 x)
{
v4_r32 Result = A+(B-A)*x;
return(Result);
}
inline v4_r32 Min(v4_r32 A, v4_r32 B)
{
v4_r32 Result = V4R32(Min(A.x, B.x), Min(A.y, B.y), Min(A.z, B.z), Min(A.w, B.w));
return(Result);
}
inline v4_r32 Max(v4_r32 A, v4_r32 B)
{
v4_r32 Result = V4R32(Max(A.x, B.x), Max(A.y, B.y), Max(A.z, B.z), Max(A.w, B.w));
return(Result);
}
//- sixten: V4 S32
inline v4_s32 V4S32(s32 x, s32 y, s32 z, s32 w)
{
v4_s32 Result = {x, y, z, w};
return(Result);
}
inline v4_s32 operator+(v4_s32 A, v4_s32 B)
{
v4_s32 Result = {A.x + B.x, A.y + B.y, A.z + B.z, A.w + B.w};
return(Result);
}
inline v4_s32 operator-(v4_s32 A, v4_s32 B)
{
v4_s32 Result = {A.x - B.x, A.y - B.y, A.z - B.z, A.w - B.w};
return(Result);
}
inline v4_s32 operator+=(v4_s32 &A, v4_s32 B) { return A = A + B; }
inline v4_s32 operator-=(v4_s32 &A, v4_s32 B) { return A = A - B; }
inline v4_s32 Min(v4_s32 A, v4_s32 B)
{
v4_s32 Result = V4S32(Min(A.x, B.x), Min(A.y, B.y), Min(A.z, B.z), Min(A.w, B.w));
return(Result);
}
inline v4_s32 Max(v4_s32 A, v4_s32 B)
{
v4_s32 Result = V4S32(Max(A.x, B.x), Max(A.y, B.y), Max(A.z, B.z), Max(A.w, B.w));
return(Result);
}
//- sixten: V4 S64
inline v4_s64 V4S64(s64 x, s64 y, s64 z, s64 w)
{
v4_s64 Result = {x, y, z, w};
return(Result);
}
inline v4_s64 operator+(v4_s64 A, v4_s64 B)
{
v4_s64 Result = {A.x + B.x, A.y + B.y, A.z + B.z, A.w + B.w};
return(Result);
}
inline v4_s64 operator-(v4_s64 A, v4_s64 B)
{
v4_s64 Result = {A.x - B.x, A.y - B.y, A.z - B.z, A.w - B.w};
return(Result);
}
inline v4_s64 operator+=(v4_s64 &A, v4_s64 B) { return A = A + B; }
inline v4_s64 operator-=(v4_s64 &A, v4_s64 B) { return A = A - B; }
inline v4_s64 Min(v4_s64 A, v4_s64 B)
{
v4_s64 Result = V4S64(Min(A.x, B.x), Min(A.y, B.y), Min(A.z, B.z), Min(A.w, B.w));
return(Result);
}
inline v4_s64 Max(v4_s64 A, v4_s64 B)
{
v4_s64 Result = V4S64(Max(A.x, B.x), Max(A.y, B.y), Max(A.z, B.z), Max(A.w, B.w));
return(Result);
}
//- sixten: Range functions
2023-07-19 15:09:41 +00:00
inline range1_r32 Range1R32(r32 A, r32 B)
2023-06-21 16:59:36 +00:00
{
2023-07-19 15:09:41 +00:00
range1_r32 Result = {Min(A, B), Max(A, B)};
2023-06-21 16:59:36 +00:00
return(Result);
}
inline b32 InRange(range1_r32 Range, r32 Value)
{
b32 Result = (Value >= Range.Min) && (Value < Range.Max);
return(Result);
}
2023-07-19 15:09:41 +00:00
inline b32 Contains(range1_r32 Range, r32 Value)
{
b32 Result = (Value >= Range.Min) && (Value <= Range.Max);
return(Result);
}
2023-06-21 16:59:36 +00:00
inline r32 DimOfRange(range1_r32 Range)
{
r32 Result = Range.Max - Range.Min;
return(Result);
}
2023-06-27 14:14:28 +00:00
inline range1_r32 Intersection(range1_r32 A, range1_r32 B)
{
range1_r32 Result = {Max(A.Min, B.Min), Min(A.Max, B.Max)};
return(Result);
}
inline range1_r32 Pad(range1_r32 Range, r32 Value)
{
range1_r32 Result = Range1R32(Range.Min-Value, Range.Max+Value);
return(Result);
}
2023-07-19 15:09:41 +00:00
inline range1_s32 Range1S32(s32 A, s32 B)
2023-06-21 16:59:36 +00:00
{
2023-07-19 15:09:41 +00:00
range1_s32 Result = {Min(A, B), Max(A, B)};
2023-06-21 16:59:36 +00:00
return(Result);
}
inline b32 InRange(range1_s32 Range, s32 Value)
{
b32 Result = (Value >= Range.Min) && (Value < Range.Max);
return(Result);
}
2023-07-19 15:09:41 +00:00
inline b32 Contains(range1_s32 Range, s32 Value)
{
b32 Result = (Value >= Range.Min) && (Value <= Range.Max);
return(Result);
}
2023-06-21 16:59:36 +00:00
inline s32 DimOfRange(range1_s32 Range)
{
s32 Result = Range.Max - Range.Min;
return(Result);
}
2023-06-27 14:14:28 +00:00
inline range1_s32 Intersection(range1_s32 A, range1_s32 B)
{
range1_s32 Result = {Max(A.Min, B.Min), Min(A.Max, B.Max)};
return(Result);
}
inline range1_s32 Pad(range1_s32 Range, s32 Value)
{
range1_s32 Result = Range1S32(Range.Min-Value, Range.Max+Value);
return(Result);
}
2023-07-19 15:09:41 +00:00
inline range1_s64 Range1S64(s64 A, s64 B)
2023-06-21 16:59:36 +00:00
{
2023-07-19 15:09:41 +00:00
range1_s64 Result = {Min(A, B), Max(A, B)};
2023-06-21 16:59:36 +00:00
return(Result);
}
inline b32 InRange(range1_s64 Range, s64 Value)
{
b32 Result = (Value >= Range.Min) && (Value < Range.Max);
return(Result);
}
2023-07-19 15:09:41 +00:00
inline b32 Contains(range1_s64 Range, s64 Value)
{
b32 Result = (Value >= Range.Min) && (Value <= Range.Max);
return(Result);
}
2023-06-21 16:59:36 +00:00
inline s64 DimOfRange(range1_s64 Range)
{
s64 Result = Range.Max - Range.Min;
return(Result);
}
2023-06-27 14:14:28 +00:00
inline range1_s64 Intersection(range1_s64 A, range1_s64 B)
{
range1_s64 Result = {Max(A.Min, B.Min), Min(A.Max, B.Max)};
return(Result);
}
inline range1_s64 Pad(range1_s64 Range, s64 Value)
{
range1_s64 Result = Range1S64(Range.Min-Value, Range.Max+Value);
return(Result);
}
2023-07-19 15:09:41 +00:00
inline range2_r32 Range2R32(v2_r32 A, v2_r32 B)
2023-06-21 16:59:36 +00:00
{
2023-07-19 15:09:41 +00:00
range2_r32 Result = {Min(A, B), Max(A, B)};
2023-06-21 16:59:36 +00:00
return(Result);
}
inline b32 InRange(range2_r32 Range, v2_r32 Value)
{
b32 Result = ((Value.x >= Range.Min.x) && (Value.y >= Range.Min.y) &&
(Value.x < Range.Max.x) && (Value.y < Range.Max.y));
return(Result);
}
2023-07-19 15:09:41 +00:00
inline b32 Contains(range2_r32 Range, v2_r32 Value)
{
b32 Result = ((Value.x >= Range.Min.x) && (Value.y >= Range.Min.y) &&
(Value.x <= Range.Max.x) && (Value.y <= Range.Max.y));
return(Result);
}
2023-06-21 16:59:36 +00:00
inline v2_r32 DimOfRange(range2_r32 Range)
{
v2_r32 Result = Range.Max - Range.Min;
return(Result);
}
2023-06-27 14:14:28 +00:00
inline range2_r32 Intersection(range2_r32 A, range2_r32 B)
{
range2_r32 Result = {Max(A.Min, B.Min), Min(A.Max, B.Max)};
return(Result);
}
2023-07-19 15:09:41 +00:00
inline v2_r32 CornerFromRange(range2_r32 Range, corner Corner)
2023-06-21 16:59:36 +00:00
{
v2_r32 Result = V2R32((Corner & (1 << Axis2_X))?Range.Max.x:Range.Min.x,
(Corner & (1 << Axis2_Y))?Range.Max.y:Range.Min.y);
return(Result);
}
inline range2_r32 Pad(range2_r32 Range, v2_r32 Value)
{
range2_r32 Result = Range2R32(Range.Min-Value, Range.Max+Value);
2023-07-19 15:09:41 +00:00
return(Result);
}
inline range2_s32 Range2S32(v2_s32 A, v2_s32 B)
{
range2_s32 Result = {Min(A, B), Max(A, B)};
2023-06-21 16:59:36 +00:00
return(Result);
}
inline b32 InRange(range2_s32 Range, v2_s32 Value)
{
b32 Result = ((Value.x >= Range.Min.x) && (Value.y >= Range.Min.y) &&
(Value.x < Range.Max.x) && (Value.y < Range.Max.y));
return(Result);
}
2023-07-19 15:09:41 +00:00
inline b32 Contains(range2_s32 Range, v2_s32 Value)
{
b32 Result = ((Value.x >= Range.Min.x) && (Value.y >= Range.Min.y) &&
(Value.x <= Range.Max.x) && (Value.y <= Range.Max.y));
return(Result);
}
2023-06-21 16:59:36 +00:00
inline v2_s32 DimOfRange(range2_s32 Range)
{
v2_s32 Result = Range.Max - Range.Min;
return(Result);
}
2023-06-27 14:14:28 +00:00
inline range2_s32 Intersection(range2_s32 A, range2_s32 B)
{
range2_s32 Result = {Max(A.Min, B.Min), Min(A.Max, B.Max)};
return(Result);
}
2023-07-19 15:09:41 +00:00
inline v2_s32 CornerFromRange(range2_s32 Range, corner Corner)
{
v2_s32 Result = V2S32((Corner & (1 << Axis2_X))?Range.Max.x:Range.Min.x,
(Corner & (1 << Axis2_Y))?Range.Max.y:Range.Min.y);
2023-07-19 15:09:41 +00:00
return(Result);
}
inline range2_s64 Range2S64(v2_s64 A, v2_s64 B)
2023-06-21 16:59:36 +00:00
{
2023-07-19 15:09:41 +00:00
range2_s64 Result = {Min(A, B), Max(A, B)};
2023-06-21 16:59:36 +00:00
return(Result);
}
inline b32 InRange(range2_s64 Range, v2_s64 Value)
{
b32 Result = ((Value.x >= Range.Min.x) && (Value.y >= Range.Min.y) &&
(Value.x < Range.Max.x) && (Value.y < Range.Max.y));
return(Result);
}
2023-07-19 15:09:41 +00:00
inline b32 Contains(range2_s64 Range, v2_s64 Value)
{
b32 Result = ((Value.x >= Range.Min.x) && (Value.y >= Range.Min.y) &&
(Value.x <= Range.Max.x) && (Value.y <= Range.Max.y));
return(Result);
}
2023-06-21 16:59:36 +00:00
inline v2_s64 DimOfRange(range2_s64 Range)
{
v2_s64 Result = Range.Max - Range.Min;
return(Result);
2023-06-27 14:14:28 +00:00
}
inline range2_s64 Intersection(range2_s64 A, range2_s64 B)
{
range2_s64 Result = {Max(A.Min, B.Min), Min(A.Max, B.Max)};
return(Result);
2023-07-19 15:09:41 +00:00
}
inline v2_s64 CornerFromRange(range2_s64 Range, corner Corner)
{
v2_s64 Result = V2S64((Corner & (1 << Axis2_X))?Range.Max.x:Range.Min.x,
(Corner & (1 << Axis2_Y))?Range.Max.y:Range.Min.y);
2023-07-19 15:09:41 +00:00
return(Result);
2023-06-21 16:59:36 +00:00
}