Vec and Lerp

This commit is contained in:
Isuru Samarathunga
2025-09-05 12:38:34 +05:30
parent a804265604
commit 536e9d0e58
4 changed files with 223 additions and 6 deletions

View File

@ -1,4 +1,6 @@
add_library(IAMath STATIC imp/cpp/dummy.cpp) add_library(IAMath STATIC imp/cpp/dummy.cpp)
target_include_directories(IAMath PUBLIC inc/hpp imp/inl) target_include_directories(IAMath PUBLIC inc/ imp/inl)
target_link_libraries(IAMath PUBLIC IACore)

View File

@ -0,0 +1,26 @@
// IAMath; The Math Library for All IA Projects
// Copyright (C) 2025 IAS (ias@iasoft.dev)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAMath/Vec.hpp>
namespace ia::iam
{
template<typename _value_type>
_value_type Lerp(IN CONST _value_type& v1, IN CONST _value_type& v2, IN FLOAT32 t)
{
return v1 + (v2 - v1) * t;
}
}

View File

@ -1,16 +1,199 @@
// IAMath; The Math Library for All IA Projects // IAMath; The Math Library for All IA Projects
// Copyright (C) 2025 IAS (ias@iasoft.dev) // Copyright (C) 2025 IAS (ias@iasoft.dev)
// //
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by // it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or // the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. // (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>. // along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <iacore/string.hpp>
namespace ia::iam
{
template<typename _value_type>
struct Vec2
{
union {
struct {
_value_type Data[2];
};
struct {
_value_type X{}, Y{};
};
struct {
_value_type U, V;
};
struct {
_value_type x, y;
};
struct {
_value_type u, v;
};
};
Vec2 operator+(IN CONST Vec2<_value_type>& other)
{
return { X + other.X, Y + other.Y };
}
Vec2 operator-(IN CONST Vec2<_value_type>& other)
{
return { X - other.X, Y - other.Y };
}
Vec2 operator*(IN CONST Vec2<_value_type>& other)
{
return { X * other.X, Y * other.Y };
}
Vec2 operator/(IN CONST Vec2<_value_type>& other)
{
return { X / other.X, Y / other.Y };
}
Vec2 operator*(IN _value_type other)
{
return { X * other, Y * other };
}
Vec2 operator/(IN _value_type other)
{
return { X / other, Y / other };
}
String ToString() CONST
{
return BuildString("{ X: ", X, ", Y: ", Y, " }");
}
};
template<typename _value_type>
struct Vec3
{
union {
struct {
_value_type Data[3];
};
struct {
_value_type X{}, Y{}, Z{};
};
struct {
_value_type R, G, B;
};
struct {
_value_type x, y, z;
};
struct {
_value_type r, g, b;
};
};
Vec3 operator+(IN CONST Vec3<_value_type>& other)
{
return { X + other.X, Y + other.Y, Z + other.Z };
}
Vec3 operator-(IN CONST Vec3<_value_type>& other)
{
return { X - other.X, Y - other.Y, Z - other.Z };
}
Vec3 operator*(IN CONST Vec3<_value_type>& other)
{
return { X * other.X, Y * other.Y, Z * other.Z };
}
Vec3 operator/(IN CONST Vec3<_value_type>& other)
{
return { X / other.X, Y / other.Y, Z / other.Z };
}
Vec3 operator*(IN _value_type other)
{
return { X * other, Y * other, Z * other };
}
Vec3 operator/(IN _value_type other)
{
return { X / other, Y / other, Z / other };
}
String ToString() CONST
{
return BuildString("{ X: ", X, ", Y: ", Y, ", Z: ", Z, " }");
}
};
template<typename _value_type>
struct Vec4
{
union {
struct {
_value_type Data[4];
};
struct {
_value_type X{}, Y{}, Z{}, W{};
};
struct {
_value_type R, G, B, A;
};
struct {
_value_type x, y, z, w;
};
struct {
_value_type r, g, b, a;
};
};
Vec4 operator+(IN CONST Vec4<_value_type>& other)
{
return { X + other.X, Y + other.Y, Z + other.Z, W + other.W };
}
Vec4 operator-(IN CONST Vec4<_value_type>& other)
{
return { X - other.X, Y - other.Y, Z - other.Z, W - other.W };
}
Vec4 operator*(IN CONST Vec4<_value_type>& other)
{
return { X * other.X, Y * other.Y, Z * other.Z, W * other.W };
}
Vec4 operator/(IN CONST Vec4<_value_type>& other)
{
return { X / other.X, Y / other.Y, Z / other.Z, W / other.W };
}
Vec4 operator*(IN _value_type other)
{
return { X * other, Y * other, Z * other, W * other };
}
Vec4 operator/(IN _value_type other)
{
return { X / other, Y / other, Z / other, W / other };
}
String ToString() CONST
{
return BuildString("{ X: ", X, ", Y: ", Y, ", Z: ", Z, ", W: ", W, " }");
}
};
using Vec2i = Vec2<INT32>;
using Vec3i = Vec3<INT32>;
using Vec4i = Vec4<INT32>;
using Vec2f = Vec2<FLOAT32>;
using Vec3f = Vec3<FLOAT32>;
using Vec4f = Vec4<FLOAT32>;
} // namespace ia::iam

View File

@ -1,7 +1,13 @@
#include <stdio.h> #include <stdio.h>
#include <IAMath/Vec.hpp>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
ia::iam::Vec2f a {6, 10};
ia::iam::Vec2f b {2, 1};
printf("%s, %s, %s\n", a.ToString().c_str(), b.ToString().c_str(), (a + b).ToString().c_str());
return 0; return 0;
} }