mirror of
https://github.com/slendidev/smath.git
synced 2026-03-17 02:26:50 +02:00
Compare commits
18 Commits
bf1c2ee0c8
...
v1.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 3b9adb0ddc | |||
| 889a42f9b5 | |||
| 8dc5ef8f24 | |||
| 0b685eb30b | |||
| aa8e3b1637 | |||
| 830c64b25a | |||
| a6fe9c2f2c | |||
| 75d5a197a9 | |||
| d3511a9b52 | |||
| eed719674f | |||
| 5f0badfe64 | |||
| 1a42238a41 | |||
| a5d669235e | |||
| 2d86e02038 | |||
| b5e0aabe37 | |||
| e32204db0c | |||
| 37f085ee36 | |||
| 13288eda01 |
29
.github/workflows/build.yml
vendored
Normal file
29
.github/workflows/build.yml
vendored
Normal 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
|
||||||
@@ -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()
|
||||||
|
|
||||||
|
|||||||
56
README.md
56
README.md
@@ -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.
|
||||||
|
|||||||
@@ -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));
|
||||||
|
|||||||
15
flake.nix
15
flake.nix
@@ -36,7 +36,11 @@
|
|||||||
|
|
||||||
src = ./.;
|
src = ./.;
|
||||||
|
|
||||||
nativeBuildInputs = [ pkgs.copyPkgconfigItems ];
|
nativeBuildInputs = with pkgs; [
|
||||||
|
cmake
|
||||||
|
gtest
|
||||||
|
copyPkgconfigItems
|
||||||
|
];
|
||||||
|
|
||||||
pkgconfigItems = [
|
pkgconfigItems = [
|
||||||
(pkgs.makePkgconfigItem rec {
|
(pkgs.makePkgconfigItem rec {
|
||||||
@@ -51,17 +55,18 @@
|
|||||||
})
|
})
|
||||||
];
|
];
|
||||||
|
|
||||||
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
8
src/modules/CMakeLists.txt
Normal file
8
src/modules/CMakeLists.txt
Normal 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
54
src/modules/smath.cppm
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user