mirror of
https://github.com/slendidev/smath.git
synced 2026-03-17 02:26:50 +02:00
Add optional interop headers
Those headers are used for interoping with various popular C++ libraries. For now, those libraries are Eigen, GLM, HandmadeMath, Raylib, and SFML. Some other ones may be added in the future. Those are disabled by default, as most projects I would imagine would not use them. But in case you need any of them for special reasons like I did, they are there. Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
131
include/smath/interop/eigen.hpp
Normal file
131
include/smath/interop/eigen.hpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* smath - Single-file linear algebra math library for C++23.
|
||||
*
|
||||
* Copyright 2025 Slendi <slendi@socopon.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Eigen/Core>
|
||||
#include <Eigen/Geometry>
|
||||
|
||||
#include <smath/smath.hpp>
|
||||
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<class Scalar, int N, int Options, int MaxRows, int MaxCols>
|
||||
requires(std::is_arithmetic_v<Scalar> && (N > 0))
|
||||
struct interop_adapter<Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols>>
|
||||
{
|
||||
using smath_type = Vec<static_cast<std::size_t>(N), Scalar>;
|
||||
|
||||
static constexpr auto to_smath(
|
||||
Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols> const &v)
|
||||
-> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i)
|
||||
out[i] = v(static_cast<int>(i));
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v)
|
||||
-> Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols>
|
||||
{
|
||||
Eigen::Matrix<Scalar, N, 1, Options, MaxRows, MaxCols> out;
|
||||
for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i)
|
||||
out(static_cast<int>(i)) = v[i];
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Scalar, int N, int Options, int MaxRows, int MaxCols>
|
||||
requires(std::is_arithmetic_v<Scalar> && (N > 0))
|
||||
struct interop_adapter<Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols>>
|
||||
{
|
||||
using smath_type = Vec<static_cast<std::size_t>(N), Scalar>;
|
||||
|
||||
static constexpr auto to_smath(
|
||||
Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols> const &v)
|
||||
-> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i)
|
||||
out[i] = v(static_cast<int>(i));
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v)
|
||||
-> Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols>
|
||||
{
|
||||
Eigen::Matrix<Scalar, 1, N, Options, MaxRows, MaxCols> out;
|
||||
for (std::size_t i = 0; i < static_cast<std::size_t>(N); ++i)
|
||||
out(static_cast<int>(i)) = v[i];
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Scalar, int R, int C, int Options, int MaxRows, int MaxCols>
|
||||
requires(std::is_arithmetic_v<Scalar> && (R > 1) && (C > 1))
|
||||
struct interop_adapter<Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols>>
|
||||
{
|
||||
using smath_type
|
||||
= Mat<static_cast<std::size_t>(R), static_cast<std::size_t>(C), Scalar>;
|
||||
|
||||
static constexpr auto to_smath(
|
||||
Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols> const &m)
|
||||
-> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
for (std::size_t c = 0; c < static_cast<std::size_t>(C); ++c) {
|
||||
for (std::size_t r = 0; r < static_cast<std::size_t>(R); ++r)
|
||||
out[r, c] = m(static_cast<int>(r), static_cast<int>(c));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &m)
|
||||
-> Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols>
|
||||
{
|
||||
Eigen::Matrix<Scalar, R, C, Options, MaxRows, MaxCols> out;
|
||||
for (std::size_t c = 0; c < static_cast<std::size_t>(C); ++c) {
|
||||
for (std::size_t r = 0; r < static_cast<std::size_t>(R); ++r)
|
||||
out(static_cast<int>(r), static_cast<int>(c)) = m[r, c];
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Scalar, int Options>
|
||||
requires std::is_arithmetic_v<Scalar>
|
||||
struct interop_adapter<Eigen::Quaternion<Scalar, Options>>
|
||||
{
|
||||
using smath_type = Quaternion<Scalar>;
|
||||
|
||||
static constexpr auto to_smath(Eigen::Quaternion<Scalar, Options> const &q)
|
||||
-> smath_type
|
||||
{
|
||||
return { q.x(), q.y(), q.z(), q.w() };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &q)
|
||||
-> Eigen::Quaternion<Scalar, Options>
|
||||
{
|
||||
return { q.w(), q.x(), q.y(), q.z() };
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace smath
|
||||
103
include/smath/interop/glm.hpp
Normal file
103
include/smath/interop/glm.hpp
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* smath - Single-file linear algebra math library for C++23.
|
||||
*
|
||||
* Copyright 2025 Slendi <slendi@socopon.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
|
||||
#include <smath/smath.hpp>
|
||||
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<glm::length_t L, class T, glm::qualifier Q>
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct interop_adapter<glm::vec<L, T, Q>>
|
||||
{
|
||||
using smath_type = Vec<static_cast<std::size_t>(L), T>;
|
||||
|
||||
static constexpr auto to_smath(glm::vec<L, T, Q> const &v) -> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
for (std::size_t i = 0; i < static_cast<std::size_t>(L); ++i)
|
||||
out[i] = v[static_cast<glm::length_t>(i)];
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> glm::vec<L, T, Q>
|
||||
{
|
||||
glm::vec<L, T, Q> out {};
|
||||
for (std::size_t i = 0; i < static_cast<std::size_t>(L); ++i)
|
||||
out[static_cast<glm::length_t>(i)] = v[i];
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<glm::length_t C, glm::length_t R, class T, glm::qualifier Q>
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct interop_adapter<glm::mat<C, R, T, Q>>
|
||||
{
|
||||
using smath_type
|
||||
= Mat<static_cast<std::size_t>(R), static_cast<std::size_t>(C), T>;
|
||||
|
||||
static constexpr auto to_smath(glm::mat<C, R, T, Q> const &m) -> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
for (std::size_t c = 0; c < static_cast<std::size_t>(C); ++c) {
|
||||
for (std::size_t r = 0; r < static_cast<std::size_t>(R); ++r) {
|
||||
out[r, c] = m[static_cast<glm::length_t>(c)]
|
||||
[static_cast<glm::length_t>(r)];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &m)
|
||||
-> glm::mat<C, R, T, Q>
|
||||
{
|
||||
glm::mat<C, R, T, Q> out {};
|
||||
for (std::size_t c = 0; c < static_cast<std::size_t>(C); ++c) {
|
||||
for (std::size_t r = 0; r < static_cast<std::size_t>(R); ++r) {
|
||||
out[static_cast<glm::length_t>(c)]
|
||||
[static_cast<glm::length_t>(r)]
|
||||
= m[r, c];
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<class T, glm::qualifier Q>
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct interop_adapter<glm::qua<T, Q>>
|
||||
{
|
||||
using smath_type = Quaternion<T>;
|
||||
|
||||
static constexpr auto to_smath(glm::qua<T, Q> const &q) -> smath_type
|
||||
{
|
||||
return { q.x, q.y, q.z, q.w };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &q) -> glm::qua<T, Q>
|
||||
{
|
||||
return { q.w(), q.x(), q.y(), q.z() };
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace smath
|
||||
125
include/smath/interop/hmm.hpp
Normal file
125
include/smath/interop/hmm.hpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* smath - Single-file linear algebra math library for C++23.
|
||||
*
|
||||
* Copyright 2025 Slendi <slendi@socopon.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <HandmadeMath.h>
|
||||
|
||||
#include <smath/smath.hpp>
|
||||
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<> struct interop_adapter<HMM_Vec2>
|
||||
{
|
||||
using smath_type = Vec<2, float>;
|
||||
|
||||
static constexpr auto to_smath(HMM_Vec2 const &v) -> smath_type
|
||||
{
|
||||
return { v.X, v.Y };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> HMM_Vec2
|
||||
{
|
||||
return { v.x(), v.y() };
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<HMM_Vec3>
|
||||
{
|
||||
using smath_type = Vec<3, float>;
|
||||
|
||||
static constexpr auto to_smath(HMM_Vec3 const &v) -> smath_type
|
||||
{
|
||||
return { v.X, v.Y, v.Z };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> HMM_Vec3
|
||||
{
|
||||
return { v.x(), v.y(), v.z() };
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<HMM_Vec4>
|
||||
{
|
||||
using smath_type = Vec<4, float>;
|
||||
|
||||
static constexpr auto to_smath(HMM_Vec4 const &v) -> smath_type
|
||||
{
|
||||
return { v.X, v.Y, v.Z, v.W };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> HMM_Vec4
|
||||
{
|
||||
return { v.x(), v.y(), v.z(), v.w() };
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<HMM_Mat4>
|
||||
{
|
||||
using smath_type = Mat<4, 4, float>;
|
||||
|
||||
static constexpr auto to_smath(HMM_Mat4 const &m) -> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
out[0, 0] = m.Columns[0].X;
|
||||
out[1, 0] = m.Columns[0].Y;
|
||||
out[2, 0] = m.Columns[0].Z;
|
||||
out[3, 0] = m.Columns[0].W;
|
||||
out[0, 1] = m.Columns[1].X;
|
||||
out[1, 1] = m.Columns[1].Y;
|
||||
out[2, 1] = m.Columns[1].Z;
|
||||
out[3, 1] = m.Columns[1].W;
|
||||
out[0, 2] = m.Columns[2].X;
|
||||
out[1, 2] = m.Columns[2].Y;
|
||||
out[2, 2] = m.Columns[2].Z;
|
||||
out[3, 2] = m.Columns[2].W;
|
||||
out[0, 3] = m.Columns[3].X;
|
||||
out[1, 3] = m.Columns[3].Y;
|
||||
out[2, 3] = m.Columns[3].Z;
|
||||
out[3, 3] = m.Columns[3].W;
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &m) -> HMM_Mat4
|
||||
{
|
||||
HMM_Mat4 out {};
|
||||
out.Columns[0] = { m[0, 0], m[1, 0], m[2, 0], m[3, 0] };
|
||||
out.Columns[1] = { m[0, 1], m[1, 1], m[2, 1], m[3, 1] };
|
||||
out.Columns[2] = { m[0, 2], m[1, 2], m[2, 2], m[3, 2] };
|
||||
out.Columns[3] = { m[0, 3], m[1, 3], m[2, 3], m[3, 3] };
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<HMM_Quat>
|
||||
{
|
||||
using smath_type = Quaternion<float>;
|
||||
|
||||
static constexpr auto to_smath(HMM_Quat const &q) -> smath_type
|
||||
{
|
||||
return { q.X, q.Y, q.Z, q.W };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &q) -> HMM_Quat
|
||||
{
|
||||
return { q.x(), q.y(), q.z(), q.w() };
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace smath
|
||||
122
include/smath/interop/raylib.hpp
Normal file
122
include/smath/interop/raylib.hpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* smath - Single-file linear algebra math library for C++23.
|
||||
*
|
||||
* Copyright 2025 Slendi <slendi@socopon.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
#include <smath/smath.hpp>
|
||||
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<> struct interop_adapter<Vector2>
|
||||
{
|
||||
using smath_type = Vec<2, float>;
|
||||
|
||||
static constexpr auto to_smath(Vector2 const &v) -> smath_type
|
||||
{
|
||||
return { v.x, v.y };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> Vector2
|
||||
{
|
||||
return { v.x(), v.y() };
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<Vector3>
|
||||
{
|
||||
using smath_type = Vec<3, float>;
|
||||
|
||||
static constexpr auto to_smath(Vector3 const &v) -> smath_type
|
||||
{
|
||||
return { v.x, v.y, v.z };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> Vector3
|
||||
{
|
||||
return { v.x(), v.y(), v.z() };
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<Vector4>
|
||||
{
|
||||
using smath_type = Vec<4, float>;
|
||||
|
||||
static constexpr auto to_smath(Vector4 const &v) -> smath_type
|
||||
{
|
||||
return { v.x, v.y, v.z, v.w };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> Vector4
|
||||
{
|
||||
return { v.x(), v.y(), v.z(), v.w() };
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct interop_adapter<Matrix>
|
||||
{
|
||||
using smath_type = Mat<4, 4, float>;
|
||||
|
||||
static constexpr auto to_smath(Matrix const &m) -> smath_type
|
||||
{
|
||||
smath_type out {};
|
||||
out[0, 0] = m.m0;
|
||||
out[1, 0] = m.m1;
|
||||
out[2, 0] = m.m2;
|
||||
out[3, 0] = m.m3;
|
||||
out[0, 1] = m.m4;
|
||||
out[1, 1] = m.m5;
|
||||
out[2, 1] = m.m6;
|
||||
out[3, 1] = m.m7;
|
||||
out[0, 2] = m.m8;
|
||||
out[1, 2] = m.m9;
|
||||
out[2, 2] = m.m10;
|
||||
out[3, 2] = m.m11;
|
||||
out[0, 3] = m.m12;
|
||||
out[1, 3] = m.m13;
|
||||
out[2, 3] = m.m14;
|
||||
out[3, 3] = m.m15;
|
||||
return out;
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &m) -> Matrix
|
||||
{
|
||||
return {
|
||||
m[0, 0],
|
||||
m[0, 1],
|
||||
m[0, 2],
|
||||
m[0, 3],
|
||||
m[1, 0],
|
||||
m[1, 1],
|
||||
m[1, 2],
|
||||
m[1, 3],
|
||||
m[2, 0],
|
||||
m[2, 1],
|
||||
m[2, 2],
|
||||
m[2, 3],
|
||||
m[3, 0],
|
||||
m[3, 1],
|
||||
m[3, 2],
|
||||
m[3, 3],
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace smath
|
||||
63
include/smath/interop/sfml.hpp
Normal file
63
include/smath/interop/sfml.hpp
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* smath - Single-file linear algebra math library for C++23.
|
||||
*
|
||||
* Copyright 2025 Slendi <slendi@socopon.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <SFML/System/Vector3.hpp>
|
||||
|
||||
#include <smath/smath.hpp>
|
||||
|
||||
namespace smath
|
||||
{
|
||||
|
||||
template<class T>
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct interop_adapter<sf::Vector2<T>>
|
||||
{
|
||||
using smath_type = Vec<2, T>;
|
||||
|
||||
static constexpr auto to_smath(sf::Vector2<T> const &v) -> smath_type
|
||||
{
|
||||
return { v.x, v.y };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> sf::Vector2<T>
|
||||
{
|
||||
return { v.x(), v.y() };
|
||||
}
|
||||
};
|
||||
|
||||
template<class T>
|
||||
requires std::is_arithmetic_v<T>
|
||||
struct interop_adapter<sf::Vector3<T>>
|
||||
{
|
||||
using smath_type = Vec<3, T>;
|
||||
|
||||
static constexpr auto to_smath(sf::Vector3<T> const &v) -> smath_type
|
||||
{
|
||||
return { v.x, v.y, v.z };
|
||||
}
|
||||
|
||||
static constexpr auto from_smath(smath_type const &v) -> sf::Vector3<T>
|
||||
{
|
||||
return { v.x(), v.y(), v.z() };
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace smath
|
||||
Reference in New Issue
Block a user