1
0
mirror of https://github.com/slendidev/smath.git synced 2026-03-16 18:16: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:
2026-03-12 01:32:35 +02:00
parent 2a161e36ab
commit 224b924772
8 changed files with 583 additions and 14 deletions

View File

@@ -8,6 +8,7 @@ option(SMATH_BUILD_EXAMPLES "Build example programs" ON)
option(SMATH_BUILD_TESTS "Build unit tests" ON)
option(SMATH_BUILD_MODULES "Enable C++20 modules support" OFF)
option(SMATH_ENABLE_LEGACY_HEADER "Install legacy include <smath.hpp> shim" OFF)
option(SMATH_INSTALL_INTEROP_HEADERS "Install interop headers" OFF)
if(SMATH_BUILD_MODULES)
message(STATUS "Building smath C++ module")
@@ -27,7 +28,11 @@ include(CMakePackageConfigHelpers)
install(TARGETS smath
EXPORT smathTargets
)
install(DIRECTORY include/ DESTINATION include)
install(FILES include/smath/smath.hpp DESTINATION include/smath)
if(SMATH_INSTALL_INTEROP_HEADERS)
install(DIRECTORY include/smath/interop DESTINATION include/smath)
endif()
if(SMATH_ENABLE_LEGACY_HEADER)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include")

View File

@@ -12,13 +12,26 @@
</a>
</p>
## Features
- Generic `Vec<N, T>` class with useful aliases `Vec2/Vec3/Vec4` and friendly accessors (`x/y/z/w`, `r/g/b/a`). They support approx-equal and tuple/structured bindings.
- `std::format` support.
- Compile-time swizzles via `swizzle<"...">`.
- Generic matrix `Mat` class with useful aliases `Mat2/Mat3/Mat4`.
- `Quaternion<T>` built on `Vec4`.
- Angle helpers `rad/deg/turns` respecting a configurable base unit via the macro `SMATH_ANGLE_UNIT`.
- Optional implicit conversions.
- Packing utilities for normalized RGBA (`pack_unorm4x8`, `unpack_snorm4x8`, etc.).
- C++20 modules support.
- Built-in interop adapter and optional interop headers for various libraries.
## Quick Start
Create `main.cpp`:
```cpp
#include <print>
#include <smath.hpp>
#include <smath/smath.hpp>
int main() {
using namespace smath;
@@ -39,17 +52,20 @@ g++ -std=c++23 -Iinclude main.cpp -o quickstart
./quickstart
```
## Features
## Interop Headers
- Generic `Vec<N, T>` class with useful aliases `Vec2/Vec3/Vec4` and friendly accessors (`x/y/z/w`, `r/g/b/a`). They support approx-equal and tuple/structured bindings.
- `std::format` support.
- Compile-time swizzles via `swizzle<"...">`.
- Generic matrix `Mat` class with useful aliases `Mat2/Mat3/Mat4`.
- `Quaternion<T>` built on `Vec4`.
- Angle helpers `rad/deg/turns` respecting a configurable base unit via the macro `SMATH_ANGLE_UNIT`.
- Optional implicit conversions.
- Packing utilities for normalized RGBA (`pack_unorm4x8`, `unpack_snorm4x8`, etc.).
- C++20 modules support.
smath ships optional interop adapters under `include/smath/interop/`.
When installing with CMake, interop headers are gated behind:
- `-DSMATH_INSTALL_INTEROP_HEADERS=ON`
All of them plug into the same generic API defined in `smath.hpp`:
```cpp
auto v = smath::from_external(external_value);
auto ext = smath::to_external<ExternalType>(smath_value);
```
## License

View File

@@ -55,10 +55,14 @@
})
];
extraCMakeFlags = [
"-DSMATH_INSTALL_INTEROP_HEADERS=ON"
];
installPhase = ''
runHook preInstall
mkdir -p $out/include/smath
cp ../include/smath/smath.hpp $out/include/smath/
mkdir -p $out/include/
cp -r ../include/smath/ $out/include/
runHook postInstall
'';

View 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

View 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

View 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

View 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

View 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