1
0
mirror of https://github.com/slendidev/smath.git synced 2026-03-17 02:26:50 +02:00

Compare commits

...

24 Commits

Author SHA1 Message Date
3b9adb0ddc Update README with logo
Add logo
2026-03-11 20:15:23 +02:00
889a42f9b5 Update README
Fix typo, add badges and add quick start section

Signed-off-by: Slendi <slendi@socopon.com>
2026-03-11 20:06:29 +02:00
8dc5ef8f24 Add helper Quaternion methods
Signed-off-by: Slendi <slendi@socopon.com>
2026-01-11 09:35:27 +02:00
0b685eb30b Fix more math
Signed-off-by: Slendi <slendi@socopon.com>
2026-01-10 16:15:49 +02:00
aa8e3b1637 Fixes
Signed-off-by: Slendi <slendi@socopon.com>
2026-01-10 15:33:04 +02:00
830c64b25a Add modules support
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-28 00:24:52 +02:00
a6fe9c2f2c Add const accessors to Quaternion
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-27 21:43:35 +02:00
75d5a197a9 Quick fix for deg/rad/turns functions
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-27 21:36:38 +02:00
d3511a9b52 Forgot to change something in the CI oops
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-12 01:07:10 +02:00
eed719674f Add CI
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-12 01:05:57 +02:00
5f0badfe64 Fix packaging for Nix
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-12 01:05:14 +02:00
1a42238a41 Formatting + new packing related functions
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-11 12:55:04 +02:00
a5d669235e Fix some math
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-07 00:11:21 +02:00
2d86e02038 Translate
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-06 23:01:49 +02:00
b5e0aabe37 Create unpacking function for vectors
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-06 21:37:46 +02:00
e32204db0c Add default case to shut compilers up
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-04 17:42:13 +02:00
37f085ee36 Quick matrix aliases
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-04 17:17:00 +02:00
13288eda01 Matrix stuff
Signed-off-by: Slendi <slendi@socopon.com>
2025-12-04 16:30:30 +02:00
bf1c2ee0c8 Add Matrices
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-28 19:02:52 +02:00
f6e2bc01b1 Add quaternions
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-28 16:14:49 +02:00
271f04581c Quick style fixes
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-14 16:29:56 +02:00
26a0a8d046 Remove some methods if Vec doesn't use floats
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-14 16:17:57 +02:00
446ab9c679 Add angle conversion functions
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-14 16:03:17 +02:00
df49368e9a direnv
Signed-off-by: Slendi <slendi@socopon.com>
2025-11-14 15:21:17 +02:00
11 changed files with 1312 additions and 348 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
use flake

29
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,29 @@
name: Build project
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
name: Build
runs-on: ubuntu-22.04
permissions:
id-token: write
contents: read
steps:
- name: git checkout
uses: actions/checkout@v3
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@main
- name: Building default package
run: nix build .#default
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: result
path: result

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
[Bb]uild* [Bb]uild*
.cache .cache
result result
.direnv

View File

@@ -4,8 +4,14 @@ project(SmathExamples LANGUAGES CXX VERSION 0.1.0)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
option(BUILD_EXAMPLES "Build example programs" ON) option(SMATH_BUILD_EXAMPLES "Build example programs" ON)
option(BUILD_TESTS "Build unit tests" ON) option(SMATH_BUILD_TESTS "Build unit tests" ON)
option(SMATH_BUILD_MODULES "Enable C++20 modules support" OFF)
if(SMATH_BUILD_MODULES)
message(STATUS "Building smath C++ module")
add_subdirectory(src/modules)
endif()
add_library(smath INTERFACE) add_library(smath INTERFACE)
target_include_directories(smath target_include_directories(smath
@@ -55,7 +61,7 @@ install(FILES
DESTINATION lib/cmake/smath DESTINATION lib/cmake/smath
) )
if(BUILD_EXAMPLES) if(SMATH_BUILD_EXAMPLES)
file(GLOB EXAMPLE_SOURCES "${CMAKE_SOURCE_DIR}/examples/*.cpp") file(GLOB EXAMPLE_SOURCES "${CMAKE_SOURCE_DIR}/examples/*.cpp")
foreach(EXAMPLE_FILE ${EXAMPLE_SOURCES}) foreach(EXAMPLE_FILE ${EXAMPLE_SOURCES})
get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE) get_filename_component(EXAMPLE_NAME ${EXAMPLE_FILE} NAME_WE)
@@ -64,9 +70,12 @@ if(BUILD_EXAMPLES)
endforeach() endforeach()
endif() endif()
if(BUILD_TESTS) if(SMATH_BUILD_TESTS)
enable_testing() enable_testing()
find_package(GTest QUIET)
if(NOT GTest_FOUND)
include(FetchContent) include(FetchContent)
FetchContent_Declare( FetchContent_Declare(
googletest googletest
@@ -74,6 +83,7 @@ if(BUILD_TESTS)
) )
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest) FetchContent_MakeAvailable(googletest)
endif()
file(GLOB TEST_SOURCES "${CMAKE_SOURCE_DIR}/tests/*.cpp") file(GLOB TEST_SOURCES "${CMAKE_SOURCE_DIR}/tests/*.cpp")
@@ -86,3 +96,4 @@ if(BUILD_TESTS)
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(smath_tests) gtest_discover_tests(smath_tests)
endif() endif()

View File

@@ -1,4 +1,56 @@
# smath <p align="center">
<img alt="smath logo" src="https://github.com/user-attachments/assets/d6f2ef4b-eca0-4004-9099-37423528bcba" />
<br><br>
<a href="https://github.com/slendidev/smath/actions/workflows/build.yml">
<img src="https://github.com/slendidev/smath/actions/workflows/build.yml/badge.svg" alt="Build">
</a>
<a href="LICENSE.txt">
<img src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" alt="License: Apache-2.0">
</a>
<a href="https://en.cppreference.com/w/cpp/23">
<img src="https://img.shields.io/badge/C%2B%2B-23-00599C.svg" alt="C++23">
</a>
</p>
Single-file linear algebra math library for C++23. ## Quick Start
Create `main.cpp`:
```cpp
#include <print>
#include <smath.hpp>
int main() {
using namespace smath;
Vec3 a{1.0f, 2.0f, 3.0f};
Vec3 b{3.0f, 2.0f, 1.0f};
std::println("a + b = {}", a + b);
std::println("dot(a, b) = {}", a.dot(b));
std::println("normalized(a) = {}", a.normalized());
}
```
Build and run:
```bash
g++ -std=c++23 -Iinclude main.cpp -o quickstart
./quickstart
```
## 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.
## License
This library is licensed under the Apache License 2.0. See the [LICENSE.txt](LICENSE.txt) file for more details.

View File

@@ -39,6 +39,9 @@ int main() {
std::println("std::get<1>(v): {}", std::get<1>(v)); std::println("std::get<1>(v): {}", std::get<1>(v));
auto [x, y, z] = v; auto [x, y, z] = v;
std::println("Bindings: [{}, {}, {}]", x, y, z); std::println("Bindings: [{}, {}, {}]", x, y, z);
float x1{}, y1{}, z1{};
v.unpack(x1, y1, z1);
std::println("Unpacked: {}, {}, {}", x1, y1, z1);
// Let's mix and match! // Let's mix and match!
Vec<6> v3(v, 7, swizzle<"zy">(v2)); Vec<6> v3(v, 7, swizzle<"zy">(v2));

View File

@@ -1,8 +1,5 @@
let
desc = "Single-file linear algebra math library for C++23.";
in
{ {
description = desc; description = "Single-file linear algebra math library for C++23.";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
@@ -39,7 +36,11 @@ in
src = ./.; src = ./.;
nativeBuildInputs = [ pkgs.copyPkgconfigItems ]; nativeBuildInputs = with pkgs; [
cmake
gtest
copyPkgconfigItems
];
pkgconfigItems = [ pkgconfigItems = [
(pkgs.makePkgconfigItem rec { (pkgs.makePkgconfigItem rec {
@@ -50,21 +51,22 @@ in
prefix = "${placeholder "out"}"; prefix = "${placeholder "out"}";
includedir = "${prefix}/include"; includedir = "${prefix}/include";
}; };
description = desc; description = "Single-file linear algebra math library for C++23.";
}) })
]; ];
dontBuild = true;
installPhase = '' installPhase = ''
runHook preInstall runHook preInstall
mkdir -p $out/include mkdir -p $out/include
cp include/*.hpp $out/include/ cp ../include/smath.hpp $out/include/
runHook postInstall runHook postInstall
''; '';
dontBuild = false;
doCheck = true;
meta = with pkgs.lib; { meta = with pkgs.lib; {
description = desc; description = "Single-file linear algebra math library for C++23.";
homepage = "https://github.com/slendidev/smath"; homepage = "https://github.com/slendidev/smath";
license = licenses.asl20; license = licenses.asl20;
platforms = platforms.all; platforms = platforms.all;

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,8 @@
add_library(smath_modules)
target_sources(smath_modules PUBLIC FILE_SET CXX_MODULES FILES smath.cppm)
target_link_libraries(smath_modules PUBLIC smath::smath)
target_compile_features(smath_modules PUBLIC cxx_std_20)

54
src/modules/smath.cppm Normal file
View File

@@ -0,0 +1,54 @@
module;
#include "smath.hpp"
export module smath;
export namespace smath {
export using ::smath::Vec;
export using ::smath::VecOrScalar;
export using ::smath::Quaternion;
export using ::smath::Mat;
export using ::smath::Vec2;
export using ::smath::Vec3;
export using ::smath::Vec4;
export using ::smath::Vec2d;
export using ::smath::Vec3d;
export using ::smath::Vec4d;
export using ::smath::Mat2;
export using ::smath::Mat3;
export using ::smath::Mat4;
export using ::smath::Mat2d;
export using ::smath::Mat3d;
export using ::smath::Mat4d;
export using ::smath::swizzle;
export using ::smath::deg;
export using ::smath::rad;
export using ::smath::turns;
export using ::smath::pack_unorm4x8;
export using ::smath::pack_snorm4x8;
export using ::smath::unpack_unorm4x8;
export using ::smath::unpack_snorm4x8;
export using ::smath::operator*;
export using ::smath::translate;
export using ::smath::rotate;
export using ::smath::scale;
export using ::smath::shear_x;
export using ::smath::shear_y;
export using ::smath::shear_z;
export using ::smath::matrix_ortho3d;
export using ::smath::matrix_perspective;
export using ::smath::matrix_infinite_perspective;
export using ::smath::matrix_look_at;
}

15
tests/angles.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include <gtest/gtest.h>
#include <smath.hpp>
TEST(AngleReturnRadians, DegInput) {
EXPECT_NEAR(smath::deg(180.0), std::numbers::pi, 1e-12);
}
TEST(AngleReturnRadians, RadInput) {
EXPECT_DOUBLE_EQ(smath::rad(std::numbers::pi), std::numbers::pi);
}
TEST(AngleReturnRadians, TurnsInput) {
EXPECT_NEAR(smath::turns(0.5), std::numbers::pi, 1e-12);
}