mirror of
https://github.com/orange-cpp/omath.git
synced 2026-04-18 21:03:27 +00:00
Compare commits
31 Commits
7b9a181b0e
...
v5.1.0.rc2
| Author | SHA1 | Date | |
|---|---|---|---|
| 6fca106edc | |||
| 78cb644920 | |||
| 646a920e4c | |||
| 52687a70c7 | |||
| a9eff7d320 | |||
| 211e4c3d9b | |||
| 74dc2234f7 | |||
| 7ebbed6763 | |||
| e271bccaf5 | |||
| 50765f69c5 | |||
| 1169534133 | |||
| 783501aab9 | |||
| 1a79566731 | |||
|
|
9183406e7a | ||
|
|
b592af91a9 | ||
|
|
af9c043e95 | ||
| 2aaa8633b9 | |||
| 0d1cc2088f | |||
| 260b3b97f9 | |||
| e0904690e6 | |||
| 7021d9d365 | |||
| 862d52593a | |||
| 97a3111791 | |||
| a9bec00973 | |||
|
|
66debb46fa | ||
|
|
b8323d3bc0 | ||
|
|
f363fa6f1a | ||
|
|
0546272493 | ||
|
|
58801bfab3 | ||
| 4567cfa318 | |||
| 1db9340dbf |
4
.github/FUNDING.yml
vendored
Normal file
4
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
open_collective: libomathorg
|
||||
github: orange-cpp
|
||||
618
.github/workflows/release.yml
vendored
Normal file
618
.github/workflows/release.yml
vendored
Normal file
@@ -0,0 +1,618 @@
|
||||
name: Release
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
concurrency:
|
||||
group: release-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
jobs:
|
||||
##############################################################################
|
||||
# 1) Linux – Clang / Ninja
|
||||
##############################################################################
|
||||
linux-release:
|
||||
name: ${{ matrix.name }}
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- name: Linux (Clang) (x64-linux)
|
||||
triplet: x64-linux
|
||||
runner: ubuntu-latest
|
||||
preset: linux-release-vcpkg
|
||||
archive: omath-linux-x64
|
||||
install_cmd: |
|
||||
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
|
||||
sudo add-apt-repository -y "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-21 main"
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y git build-essential cmake ninja-build \
|
||||
zip unzip curl pkg-config ca-certificates \
|
||||
clang-21 lld-21 libc++-21-dev libc++abi-21-dev
|
||||
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-21 100
|
||||
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-21 100
|
||||
sudo update-alternatives --install /usr/bin/lld lld /usr/bin/lld-21 100
|
||||
- name: Linux (Clang) (x86-linux)
|
||||
triplet: x86-linux
|
||||
runner: ubuntu-latest
|
||||
preset: linux-release-vcpkg-x86
|
||||
archive: omath-linux-x86
|
||||
install_cmd: |
|
||||
# Add LLVM 21 repository
|
||||
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
|
||||
sudo add-apt-repository -y "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-21 main"
|
||||
# Add GCC Toolchain PPA
|
||||
sudo add-apt-repository -y "deb http://archive.ubuntu.com/ubuntu plucky main universe"
|
||||
# Enable i386 architecture
|
||||
sudo dpkg --add-architecture i386
|
||||
sudo apt-get update
|
||||
# Install Clang 21
|
||||
sudo apt-get install -y git build-essential cmake ninja-build \
|
||||
zip unzip curl pkg-config ca-certificates \
|
||||
clang-21 lld-21 libc++-21-dev libc++abi-21-dev
|
||||
sudo apt-get install -y -t plucky binutils
|
||||
# Install GCC 15 with multilib support
|
||||
sudo apt-get install -y gcc-15-multilib g++-15-multilib
|
||||
# Set up alternatives for Clang
|
||||
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-21 100
|
||||
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-21 100
|
||||
sudo update-alternatives --install /usr/bin/lld lld /usr/bin/lld-21 100
|
||||
# Set up alternatives for GCC
|
||||
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-15 100
|
||||
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-15 100
|
||||
- name: Linux (Clang) (arm64-linux)
|
||||
triplet: arm64-linux
|
||||
runner: ubuntu-24.04-arm
|
||||
preset: linux-release-vcpkg-arm64
|
||||
archive: omath-linux-arm64
|
||||
install_cmd: |
|
||||
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
|
||||
sudo add-apt-repository -y "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-21 main"
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y git build-essential cmake ninja-build \
|
||||
zip unzip curl pkg-config ca-certificates \
|
||||
clang-21 lld-21 libc++-21-dev libc++abi-21-dev
|
||||
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-21 100
|
||||
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/clang++-21 100
|
||||
sudo update-alternatives --install /usr/bin/lld lld /usr/bin/lld-21 100
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
steps:
|
||||
- name: Install basic tool-chain
|
||||
shell: bash
|
||||
run: ${{ matrix.install_cmd }}
|
||||
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Set up vcpkg
|
||||
shell: bash
|
||||
run: |
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
cd "$VCPKG_ROOT"
|
||||
./bootstrap-vcpkg.sh
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
|
||||
- name: Install
|
||||
shell: bash
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: bash
|
||||
run: tar -czf ${{ matrix.archive }}.tar.gz -C staging .
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.tar.gz" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 2) Windows – MSVC / Ninja
|
||||
##############################################################################
|
||||
windows-release:
|
||||
name: ${{ matrix.name }}
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- name: Windows (MSVC) (x64-windows)
|
||||
runner: windows-latest
|
||||
arch: amd64
|
||||
preset: windows-release-vcpkg
|
||||
triplet: x64-windows
|
||||
archive: omath-windows-x64
|
||||
- name: Windows (MSVC) (x86-windows)
|
||||
runner: windows-latest
|
||||
arch: amd64_x86
|
||||
preset: windows-release-vcpkg-x86
|
||||
triplet: x86-windows
|
||||
archive: omath-windows-x86
|
||||
- name: Windows (MSVC) (arm64-windows)
|
||||
runner: windows-11-arm
|
||||
arch: arm64
|
||||
preset: windows-release-vcpkg-arm64
|
||||
triplet: arm64-windows
|
||||
archive: omath-windows-arm64
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
steps:
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Install Ninja
|
||||
uses: seanmiddleditch/gha-setup-ninja@v4
|
||||
|
||||
- name: Set up MSVC developer command-prompt
|
||||
uses: ilammy/msvc-dev-cmd@v1
|
||||
with:
|
||||
arch: ${{ matrix.arch }}
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
|
||||
- name: Install
|
||||
shell: bash
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: pwsh
|
||||
run: Compress-Archive -Path staging\* -DestinationPath "${{ matrix.archive }}.zip"
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.zip" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 3) macOS – AppleClang / Ninja
|
||||
##############################################################################
|
||||
macos-release:
|
||||
name: ${{ matrix.name }}
|
||||
runs-on: ${{ matrix.runner }}
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- name: macOS (AppleClang) (arm64-osx)
|
||||
runner: macos-latest
|
||||
preset: darwin-release-vcpkg
|
||||
triplet: arm64-osx
|
||||
archive: omath-macos-arm64
|
||||
- name: macOS (AppleClang) (x64-osx)
|
||||
runner: macos-15-intel
|
||||
preset: darwin-release-vcpkg-x64
|
||||
triplet: x64-osx
|
||||
archive: omath-macos-x64
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
steps:
|
||||
- name: Install basic tool-chain with Homebrew
|
||||
shell: bash
|
||||
run: |
|
||||
brew install cmake ninja
|
||||
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Set up vcpkg
|
||||
shell: bash
|
||||
run: |
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
|
||||
- name: Install
|
||||
shell: bash
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: bash
|
||||
run: tar -czf ${{ matrix.archive }}.tar.gz -C staging .
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.tar.gz" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 4) iOS – AppleClang / Xcode / arm64-ios
|
||||
##############################################################################
|
||||
ios-release:
|
||||
name: iOS (AppleClang) (${{ matrix.triplet }})
|
||||
runs-on: macOS-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- triplet: arm64-ios
|
||||
preset: ios-release-vcpkg
|
||||
archive: omath-ios-arm64
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
steps:
|
||||
- name: Install CMake tooling
|
||||
shell: bash
|
||||
run: |
|
||||
brew install cmake ninja
|
||||
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Set up vcpkg
|
||||
shell: bash
|
||||
run: |
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
cd "$VCPKG_ROOT"
|
||||
./bootstrap-vcpkg.sh
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --config Release --target omath
|
||||
|
||||
- name: Install
|
||||
shell: bash
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: bash
|
||||
run: tar -czf ${{ matrix.archive }}.tar.gz -C staging .
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.tar.gz" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 5) FreeBSD – Clang / Ninja
|
||||
##############################################################################
|
||||
freebsd-release:
|
||||
name: FreeBSD (Clang) (${{ matrix.triplet }})
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- triplet: x64-freebsd
|
||||
preset: freebsd-release-vcpkg
|
||||
arch: x86-64
|
||||
archive: omath-freebsd-x64
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/tmp/vcpkg
|
||||
steps:
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Build and Package
|
||||
uses: cross-platform-actions/action@v0.31.0
|
||||
with:
|
||||
operating_system: freebsd
|
||||
architecture: ${{ matrix.arch }}
|
||||
version: '15.0'
|
||||
memory: '12G'
|
||||
cpu_count: 4
|
||||
run: |
|
||||
sudo pkg install -y git curl zip unzip gmake llvm gsed bash perl5 openssl 7-zip coreutils cmake ninja pkgconf patchelf
|
||||
git config --global --add safe.directory `pwd`
|
||||
# Build vcpkg in /tmp to avoid sshfs timestamp sync issues
|
||||
export VCPKG_ROOT=/tmp/vcpkg
|
||||
rm -rf "$VCPKG_ROOT"
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
cd "$VCPKG_ROOT"
|
||||
./bootstrap-vcpkg.sh
|
||||
cd -
|
||||
export VCPKG_FORCE_SYSTEM_BINARIES=0
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui;avx2" \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported"
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
tar -czf ${{ matrix.archive }}.tar.gz -C staging .
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.tar.gz" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 6) Android NDK – Clang / Ninja
|
||||
##############################################################################
|
||||
android-release:
|
||||
name: Android NDK (${{ matrix.triplet }})
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- triplet: arm-neon-android
|
||||
preset: android-arm-neon-release-vcpkg
|
||||
archive: omath-android-arm-neon
|
||||
- triplet: arm64-android
|
||||
preset: android-arm64-release-vcpkg
|
||||
archive: omath-android-arm64
|
||||
- triplet: x64-android
|
||||
preset: android-x64-release-vcpkg
|
||||
archive: omath-android-x64
|
||||
- triplet: x86-android
|
||||
preset: android-x86-release-vcpkg
|
||||
archive: omath-android-x86
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
ANDROID_NDK_HOME: ${{ github.workspace }}/android-ndk
|
||||
steps:
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Install Android NDK
|
||||
shell: bash
|
||||
run: |
|
||||
NDK_VERSION="r28b"
|
||||
NDK_ZIP="android-ndk-${NDK_VERSION}-linux.zip"
|
||||
wget -q "https://dl.google.com/android/repository/${NDK_ZIP}"
|
||||
unzip -q "${NDK_ZIP}" -d "${{ github.workspace }}"
|
||||
mv "${{ github.workspace }}/android-ndk-${NDK_VERSION}" "$ANDROID_NDK_HOME"
|
||||
rm "${NDK_ZIP}"
|
||||
echo "ANDROID_NDK_HOME=${ANDROID_NDK_HOME}" >> $GITHUB_ENV
|
||||
|
||||
- name: Install basic tool-chain
|
||||
shell: bash
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ninja-build cmake
|
||||
|
||||
- name: Set up vcpkg
|
||||
shell: bash
|
||||
run: |
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
cd "$VCPKG_ROOT"
|
||||
./bootstrap-vcpkg.sh
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
|
||||
- name: Install
|
||||
shell: bash
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: bash
|
||||
run: tar -czf ${{ matrix.archive }}.tar.gz -C staging .
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.tar.gz" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 7) WebAssembly (Emscripten) – Clang / Ninja / wasm32-emscripten
|
||||
##############################################################################
|
||||
wasm-release:
|
||||
name: WebAssembly (Emscripten) (${{ matrix.triplet }})
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- triplet: wasm32-emscripten
|
||||
preset: wasm-release-vcpkg
|
||||
archive: omath-wasm32
|
||||
fail-fast: false
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
steps:
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Install basic tool-chain
|
||||
shell: bash
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install -y ninja-build
|
||||
|
||||
- name: Setup Emscripten
|
||||
uses: mymindstorm/setup-emsdk@v14
|
||||
with:
|
||||
version: 'latest'
|
||||
|
||||
- name: Verify Emscripten
|
||||
shell: bash
|
||||
run: |
|
||||
echo "EMSDK=$EMSDK"
|
||||
emcc --version
|
||||
ls -la "$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake"
|
||||
|
||||
- name: Set up vcpkg
|
||||
shell: bash
|
||||
run: |
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
cd "$VCPKG_ROOT"
|
||||
./bootstrap-vcpkg.sh
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui"
|
||||
|
||||
- name: Build
|
||||
shell: bash
|
||||
run: |
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
|
||||
- name: Install
|
||||
shell: bash
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: bash
|
||||
run: tar -czf ${{ matrix.archive }}.tar.gz -C staging .
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.tar.gz" --clobber
|
||||
|
||||
##############################################################################
|
||||
# 8) Windows MSYS2 MinGW – GCC / Ninja
|
||||
##############################################################################
|
||||
mingw-release:
|
||||
name: ${{ matrix.name }}
|
||||
runs-on: windows-latest
|
||||
strategy:
|
||||
matrix:
|
||||
include:
|
||||
- name: MINGW64 (MSYS2) (x64-mingw-dynamic)
|
||||
msystem: MINGW64
|
||||
pkg_prefix: mingw-w64-x86_64
|
||||
preset: mingw-release-vcpkg
|
||||
archive: omath-mingw64-x64
|
||||
- name: UCRT64 (MSYS2) (x64-mingw-dynamic)
|
||||
msystem: UCRT64
|
||||
pkg_prefix: mingw-w64-ucrt-x86_64
|
||||
preset: mingw-release-vcpkg
|
||||
archive: omath-ucrt64-x64
|
||||
- name: MINGW32 (MSYS2) (x86-mingw-dynamic)
|
||||
msystem: MINGW32
|
||||
pkg_prefix: mingw-w64-i686
|
||||
preset: mingw32-release-vcpkg
|
||||
archive: omath-mingw32-x86
|
||||
fail-fast: false
|
||||
|
||||
defaults:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
|
||||
env:
|
||||
VCPKG_ROOT: ${{ github.workspace }}/vcpkg
|
||||
|
||||
steps:
|
||||
- name: Setup MSYS2
|
||||
uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
msystem: ${{ matrix.msystem }}
|
||||
update: true
|
||||
install: >-
|
||||
${{ matrix.pkg_prefix }}-toolchain
|
||||
${{ matrix.pkg_prefix }}-cmake
|
||||
${{ matrix.pkg_prefix }}-ninja
|
||||
${{ matrix.pkg_prefix }}-pkg-config
|
||||
git
|
||||
base-devel
|
||||
|
||||
- name: Checkout repository (with sub-modules)
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: recursive
|
||||
|
||||
- name: Set up vcpkg
|
||||
run: |
|
||||
git clone https://github.com/microsoft/vcpkg "$VCPKG_ROOT"
|
||||
cd "$VCPKG_ROOT"
|
||||
./bootstrap-vcpkg.sh
|
||||
|
||||
- name: Configure (cmake --preset)
|
||||
run: |
|
||||
cmake --preset ${{ matrix.preset }} \
|
||||
-DVCPKG_INSTALL_OPTIONS="--allow-unsupported" \
|
||||
-DOMATH_BUILD_TESTS=OFF \
|
||||
-DOMATH_BUILD_BENCHMARK=OFF \
|
||||
-DVCPKG_MANIFEST_FEATURES="imgui"
|
||||
|
||||
- name: Build
|
||||
run: |
|
||||
cmake --build cmake-build/build/${{ matrix.preset }} --target omath
|
||||
|
||||
- name: Install
|
||||
run: cmake --install cmake-build/build/${{ matrix.preset }} --prefix staging
|
||||
|
||||
- name: Package
|
||||
shell: pwsh
|
||||
run: Compress-Archive -Path staging\* -DestinationPath "${{ matrix.archive }}.zip"
|
||||
|
||||
- name: Upload release asset
|
||||
env:
|
||||
GH_TOKEN: ${{ github.token }}
|
||||
shell: bash
|
||||
run: gh release upload "${{ github.event.release.tag_name }}" "${{ matrix.archive }}.zip" --clobber
|
||||
@@ -130,11 +130,10 @@ set_target_properties(
|
||||
CXX_STANDARD 23
|
||||
CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if (OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES
|
||||
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>"
|
||||
)
|
||||
endif ()
|
||||
if(OMATH_STATIC_MSVC_RUNTIME_LIBRARY)
|
||||
set_target_properties(${PROJECT_NAME} PROPERTIES MSVC_RUNTIME_LIBRARY
|
||||
"MultiThreaded$<$<CONFIG:Debug>:Debug>")
|
||||
endif()
|
||||
|
||||
if(OMATH_USE_AVX2)
|
||||
if(MSVC)
|
||||
|
||||
37
CODEOWNERS
Normal file
37
CODEOWNERS
Normal file
@@ -0,0 +1,37 @@
|
||||
## List of maintainers for the omath library
|
||||
|
||||
## This file purpose is to give newcomers to the project the responsible
|
||||
## developers when submitting a pull request on GitHub, or opening a bug
|
||||
## report in issues.
|
||||
|
||||
## This file will notably establish who is responsible for a specific
|
||||
## area of omath. Being a maintainer means the following:
|
||||
## - that person has good knownledge in the area
|
||||
## - that person is able to enforce consistency in the area
|
||||
## - that person may be available for giving help in the area
|
||||
## - that person has push access on the repository
|
||||
## Being a maintainer does not mean the following:
|
||||
## - that person is dedicated to the area
|
||||
## - that person is working full-time on the area/on omath
|
||||
## - that person is paid
|
||||
## - that person is always available
|
||||
|
||||
|
||||
# omath core source code
|
||||
/source @orange-cpp
|
||||
/include @orange-cpp
|
||||
|
||||
# Tests and becnchmarks
|
||||
/benchmark @orange-cpp
|
||||
/tests @orange-cpp @luadebug
|
||||
|
||||
# Examples and documentation
|
||||
/examples @luadebug @orange-cpp
|
||||
/docs @orange-cpp
|
||||
|
||||
# Misc like formating
|
||||
/scripts @luadebug
|
||||
/pixi @luadebug
|
||||
|
||||
# CI/CD
|
||||
/.github/workflows @luadbg @orange-cpp
|
||||
42
INSTALL.md
42
INSTALL.md
@@ -1,6 +1,6 @@
|
||||
# 📥Installation Guide
|
||||
|
||||
## <img width="28px" src="https://vcpkg.io/assets/mark/mark.svg" /> Using vcpkg
|
||||
## <img width="28px" src="https://vcpkg.io/assets/mark/mark.svg" /> Using vcpkg (recomended)
|
||||
**Note**: Support vcpkg for package management
|
||||
1. Install [vcpkg](https://github.com/microsoft/vcpkg)
|
||||
2. Run the following command to install the orange-math package:
|
||||
@@ -28,6 +28,46 @@ target("...")
|
||||
add_packages("omath")
|
||||
```
|
||||
|
||||
## <img width="28px" src="https://github.githubassets.com/favicons/favicon.svg" /> Using prebuilt binaries (GitHub Releases)
|
||||
|
||||
**Note**: This is the fastest option if you don’t want to build from source.
|
||||
|
||||
1. **Go to the Releases page**
|
||||
- Open the project’s GitHub **Releases** page and choose the latest version.
|
||||
|
||||
2. **Download the correct asset for your platform**
|
||||
- Pick the archive that matches your OS and architecture (for example: Windows x64 / Linux x64 / macOS arm64).
|
||||
|
||||
3. **Extract the archive**
|
||||
- You should end up with something like:
|
||||
- `include/` (headers)
|
||||
- `lib/` or `bin/` (library files / DLLs)
|
||||
- sometimes `cmake/` (CMake package config)
|
||||
|
||||
4. **Use it in your project**
|
||||
|
||||
### Option A: CMake package (recommended if the release includes CMake config files)
|
||||
If the extracted folder contains something like `lib/cmake/omath` or `cmake/omath`, you can point CMake to it:
|
||||
|
||||
```cmake
|
||||
# Example: set this to the extracted prebuilt folder
|
||||
list(APPEND CMAKE_PREFIX_PATH "path/to/omath-prebuilt")
|
||||
|
||||
find_package(omath CONFIG REQUIRED)
|
||||
target_link_libraries(main PRIVATE omath::omath)
|
||||
```
|
||||
### Option B: Manual include + link (works with any layout)
|
||||
If there’s no CMake package config, link it manually:
|
||||
```cmake
|
||||
target_include_directories(main PRIVATE "path/to/omath-prebuilt/include")
|
||||
|
||||
# Choose ONE depending on what you downloaded:
|
||||
# - Static library: .lib / .a
|
||||
# - Shared library: .dll + .lib import (Windows), .so (Linux), .dylib (macOS)
|
||||
|
||||
target_link_directories(main PRIVATE "path/to/omath-prebuilt/lib")
|
||||
target_link_libraries(main PRIVATE omath) # or the actual library filename
|
||||
```
|
||||
## <img width="28px" src="https://upload.wikimedia.org/wikipedia/commons/e/ef/CMake_logo.svg?" /> Build from source using CMake
|
||||
1. **Preparation**
|
||||
|
||||
|
||||
@@ -80,9 +80,10 @@ if (auto screen = camera.world_to_screen(world_position)) {
|
||||
- **Collision Detection**: Production ready code to handle collision detection by using simple interfaces.
|
||||
- **No Additional Dependencies**: No additional dependencies need to use OMath except unit test execution
|
||||
- **Ready for meta-programming**: Omath use templates for common types like Vectors, Matrixes etc, to handle all types!
|
||||
- **Engine support**: Supports coordinate systems of **Source, Unity, Unreal, Frostbite, IWEngine and canonical OpenGL**.
|
||||
- **Engine support**: Supports coordinate systems of **Source, Unity, Unreal, Frostbite, IWEngine, CryEngine and canonical OpenGL**.
|
||||
- **Cross platform**: Supports Windows, MacOS and Linux.
|
||||
- **Algorithms**: Has ability to scan for byte pattern with wildcards in PE files/modules, binary slices, works even with Wine apps.
|
||||
- **Algorithms**: Has ability to scan for byte pattern with wildcards in ELF/Mach-O/PE files/modules, binary slices, works even with Wine apps.
|
||||
- **Battle tested**: It's already used by some big players on the market like wraith.su and bluedream.ltd
|
||||
<div align = center>
|
||||
|
||||
# Gallery
|
||||
|
||||
@@ -1,35 +1,7 @@
|
||||
project(examples)
|
||||
|
||||
add_executable(example_projection_matrix_builder example_proj_mat_builder.cpp)
|
||||
set_target_properties(
|
||||
example_projection_matrix_builder
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
target_link_libraries(example_projection_matrix_builder PRIVATE omath::omath)
|
||||
|
||||
add_executable(example_signature_scan example_signature_scan.cpp)
|
||||
set_target_properties(
|
||||
example_signature_scan
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
target_link_libraries(example_signature_scan PRIVATE omath::omath)
|
||||
|
||||
add_executable(example_glfw3 example_glfw3.cpp)
|
||||
set_target_properties(
|
||||
example_glfw3
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
find_package(OpenGL)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(glfw3 CONFIG REQUIRED)
|
||||
target_link_libraries(example_glfw3 PRIVATE omath::omath GLEW::GLEW glfw OpenGL::GL)
|
||||
add_subdirectory(example_barycentric)
|
||||
add_subdirectory(example_glfw3)
|
||||
add_subdirectory(example_proj_mat_builder)
|
||||
add_subdirectory(example_signature_scan)
|
||||
|
||||
if(OMATH_ENABLE_VALGRIND)
|
||||
omath_setup_valgrind(example_projection_matrix_builder)
|
||||
|
||||
14
examples/example_barycentric/CMakeLists.txt
Normal file
14
examples/example_barycentric/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
project(example_barycentric)
|
||||
|
||||
add_executable(${PROJECT_NAME} example_barycentric.cpp)
|
||||
set_target_properties(
|
||||
${PROJECT_NAME}
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
find_package(OpenGL)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(glfw3 CONFIG REQUIRED)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE omath::omath GLEW::GLEW glfw OpenGL::GL)
|
||||
427
examples/example_barycentric/example_barycentric.cpp
Normal file
427
examples/example_barycentric/example_barycentric.cpp
Normal file
@@ -0,0 +1,427 @@
|
||||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <omath/omath.hpp>
|
||||
#include <vector>
|
||||
|
||||
using omath::Color;
|
||||
using omath::Triangle;
|
||||
using omath::Vector3;
|
||||
|
||||
static const char* vertexShaderSource = R"(
|
||||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aColor;
|
||||
layout (location = 2) in float aPointSize;
|
||||
layout (location = 3) in float aIsLine;
|
||||
|
||||
out vec3 vColor;
|
||||
out float vIsLine;
|
||||
|
||||
void main() {
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
vColor = aColor;
|
||||
gl_PointSize = aPointSize;
|
||||
vIsLine = aIsLine;
|
||||
}
|
||||
)";
|
||||
|
||||
static const char* fragmentShaderSource = R"(
|
||||
#version 330 core
|
||||
in vec3 vColor;
|
||||
in float vIsLine;
|
||||
out vec4 FragColor;
|
||||
|
||||
void main() {
|
||||
if (vIsLine < 0.5) {
|
||||
// Calculate distance from center of the point
|
||||
vec2 coord = gl_PointCoord - vec2(0.5);
|
||||
if(length(coord) > 0.5)
|
||||
discard;
|
||||
}
|
||||
|
||||
FragColor = vec4(vColor, 1.0);
|
||||
}
|
||||
)";
|
||||
|
||||
GLuint compileShader(GLenum type, const char* src)
|
||||
{
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, nullptr);
|
||||
glCompileShader(shader);
|
||||
GLint ok;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
|
||||
if (!ok)
|
||||
{
|
||||
char log[1024];
|
||||
glGetShaderInfoLog(shader, sizeof(log), nullptr, log);
|
||||
std::cerr << "Shader error: " << log << std::endl;
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
|
||||
void drawChar(char c, float x, float y, float scale, const Color& color, std::vector<float>& lines)
|
||||
{
|
||||
float w = 0.5f * scale;
|
||||
float h = 1.0f * scale;
|
||||
|
||||
auto add = [&](float x1, float y1, float x2, float y2)
|
||||
{
|
||||
lines.push_back(x + x1 * w);
|
||||
lines.push_back(y + y1 * h);
|
||||
lines.push_back(0.0f);
|
||||
lines.push_back(color.x);
|
||||
lines.push_back(color.y);
|
||||
lines.push_back(color.z);
|
||||
lines.push_back(1.0f); // size
|
||||
lines.push_back(1.0f); // isLine
|
||||
|
||||
lines.push_back(x + x2 * w);
|
||||
lines.push_back(y + y2 * h);
|
||||
lines.push_back(0.0f);
|
||||
lines.push_back(color.x);
|
||||
lines.push_back(color.y);
|
||||
lines.push_back(color.z);
|
||||
lines.push_back(1.0f); // size
|
||||
lines.push_back(1.0f); // isLine
|
||||
};
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '0':
|
||||
add(0, 0, 1, 0);
|
||||
add(1, 0, 1, 1);
|
||||
add(1, 1, 0, 1);
|
||||
add(0, 1, 0, 0);
|
||||
break;
|
||||
case '1':
|
||||
add(0.5f, 0, 0.5f, 1);
|
||||
add(0.25f, 0.75f, 0.5f, 1);
|
||||
add(0.25f, 0, 0.75f, 0);
|
||||
break;
|
||||
case '2':
|
||||
add(0, 1, 1, 1);
|
||||
add(1, 1, 1, 0.5f);
|
||||
add(1, 0.5f, 0, 0.5f);
|
||||
add(0, 0.5f, 0, 0);
|
||||
add(0, 0, 1, 0);
|
||||
break;
|
||||
case '3':
|
||||
add(0, 1, 1, 1);
|
||||
add(1, 1, 1, 0);
|
||||
add(1, 0, 0, 0);
|
||||
add(0, 0.5f, 1, 0.5f);
|
||||
break;
|
||||
case '4':
|
||||
add(0, 1, 0, 0.5f);
|
||||
add(0, 0.5f, 1, 0.5f);
|
||||
add(1, 1, 1, 0);
|
||||
break;
|
||||
case '5':
|
||||
add(1, 1, 0, 1);
|
||||
add(0, 1, 0, 0.5f);
|
||||
add(0, 0.5f, 1, 0.5f);
|
||||
add(1, 0.5f, 1, 0);
|
||||
add(1, 0, 0, 0);
|
||||
break;
|
||||
case '6':
|
||||
add(1, 1, 0, 1);
|
||||
add(0, 1, 0, 0);
|
||||
add(0, 0, 1, 0);
|
||||
add(1, 0, 1, 0.5f);
|
||||
add(1, 0.5f, 0, 0.5f);
|
||||
break;
|
||||
case '7':
|
||||
add(0, 1, 1, 1);
|
||||
add(1, 1, 0.5f, 0);
|
||||
break;
|
||||
case '8':
|
||||
add(0, 0, 1, 0);
|
||||
add(1, 0, 1, 1);
|
||||
add(1, 1, 0, 1);
|
||||
add(0, 1, 0, 0);
|
||||
add(0, 0.5f, 1, 0.5f);
|
||||
break;
|
||||
case '9':
|
||||
add(1, 0.5f, 0, 0.5f);
|
||||
add(0, 0.5f, 0, 1);
|
||||
add(0, 1, 1, 1);
|
||||
add(1, 1, 1, 0);
|
||||
add(1, 0, 0, 0);
|
||||
break;
|
||||
case '.':
|
||||
add(0.4f, 0, 0.6f, 0);
|
||||
add(0.6f, 0, 0.6f, 0.2f);
|
||||
add(0.6f, 0.2f, 0.4f, 0.2f);
|
||||
add(0.4f, 0.2f, 0.4f, 0);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void drawText(const std::string& text, float x, float y, float scale, const Color& color, std::vector<float>& lines)
|
||||
{
|
||||
float cursor = x;
|
||||
for (char c : text)
|
||||
{
|
||||
drawChar(c, cursor, y, scale, color, lines);
|
||||
cursor += (c == '.' ? 0.3f : 0.7f) * scale;
|
||||
}
|
||||
}
|
||||
|
||||
GLFWwindow* initWindow(int width, int height, const char* title)
|
||||
{
|
||||
if (!glfwInit())
|
||||
{
|
||||
std::cerr << "Failed to initialize GLFW\n";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
|
||||
if (!window)
|
||||
{
|
||||
std::cerr << "Failed to create GLFW window\n";
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
// Check if context is valid using standard GL
|
||||
const GLubyte* renderer = glGetString(GL_RENDERER);
|
||||
const GLubyte* version = glGetString(GL_VERSION);
|
||||
if (renderer && version)
|
||||
{
|
||||
std::cout << "Renderer: " << renderer << "\n";
|
||||
std::cout << "OpenGL version supported: " << version << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Failed to get GL_RENDERER or GL_VERSION. Context might be invalid.\n";
|
||||
}
|
||||
|
||||
glewExperimental = GL_TRUE;
|
||||
GLenum glewErr = glewInit();
|
||||
if (glewErr != GLEW_OK)
|
||||
{
|
||||
// Ignore GLEW_ERROR_NO_GLX_DISPLAY if we have a valid context (e.g. Wayland)
|
||||
if (glewErr == GLEW_ERROR_NO_GLX_DISPLAY && renderer)
|
||||
{
|
||||
std::cerr << "GLEW warning: " << glewGetErrorString(glewErr) << " (Ignored because context seems valid)\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Failed to initialize GLEW: " << glewGetErrorString(glewErr) << "\n";
|
||||
glfwTerminate();
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
return window;
|
||||
}
|
||||
|
||||
GLuint createShaderProgram()
|
||||
{
|
||||
GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
||||
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
||||
GLuint shaderProgram = glCreateProgram();
|
||||
glAttachShader(shaderProgram, vs);
|
||||
glAttachShader(shaderProgram, fs);
|
||||
glLinkProgram(shaderProgram);
|
||||
return shaderProgram;
|
||||
}
|
||||
|
||||
void generatePointCloud(std::vector<float>& pointCloud, const Triangle<Vector3<float>>& triangle)
|
||||
{
|
||||
const auto& A = triangle.m_vertex1;
|
||||
const auto& B = triangle.m_vertex2;
|
||||
const auto& C = triangle.m_vertex3;
|
||||
// Iterating over barycentric coordinates (u, v, w) from 0.0 to 1.0
|
||||
for (float u = 0.0f; u <= 1.0f; u += 0.015f)
|
||||
{
|
||||
for (float v = 0.0f; v <= 1.0f - u; v += 0.015f)
|
||||
{
|
||||
float w = 1.0f - u - v;
|
||||
if (w >= 0.0f && w <= 1.0f)
|
||||
{
|
||||
Vector3<float> P = A * u + B * v + C * w;
|
||||
pointCloud.push_back(P.x);
|
||||
pointCloud.push_back(P.y);
|
||||
pointCloud.push_back(P.z);
|
||||
pointCloud.push_back(u);
|
||||
pointCloud.push_back(v);
|
||||
pointCloud.push_back(w);
|
||||
pointCloud.push_back(2.0f); // size
|
||||
pointCloud.push_back(0.0f); // isLine
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setupBuffers(GLuint& VAO_cloud, GLuint& VBO_cloud, const std::vector<float>& pointCloud, GLuint& VAO_dyn,
|
||||
GLuint& VBO_dyn)
|
||||
{
|
||||
glGenVertexArrays(1, &VAO_cloud);
|
||||
glGenBuffers(1, &VBO_cloud);
|
||||
glBindVertexArray(VAO_cloud);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_cloud);
|
||||
glBufferData(GL_ARRAY_BUFFER, pointCloud.size() * sizeof(float), pointCloud.data(), GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(7 * sizeof(float)));
|
||||
glEnableVertexAttribArray(3);
|
||||
|
||||
glGenVertexArrays(1, &VAO_dyn);
|
||||
glGenBuffers(1, &VBO_dyn);
|
||||
glBindVertexArray(VAO_dyn);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_dyn);
|
||||
glBufferData(GL_ARRAY_BUFFER, 1000 * 8 * sizeof(float), NULL, GL_DYNAMIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(7 * sizeof(float)));
|
||||
glEnableVertexAttribArray(3);
|
||||
}
|
||||
|
||||
void updateDynamicData(std::vector<float>& dynData, float u, float v, float w, const Vector3<float>& P,
|
||||
const Triangle<Vector3<float>>& triangle)
|
||||
{
|
||||
const auto& A = triangle.m_vertex1;
|
||||
const auto& B = triangle.m_vertex2;
|
||||
const auto& C = triangle.m_vertex3;
|
||||
|
||||
float sizeA = 10.0f + u * 30.0f;
|
||||
float sizeB = 10.0f + v * 30.0f;
|
||||
float sizeC = 10.0f + w * 30.0f;
|
||||
float sizeP = 12.0f;
|
||||
|
||||
dynData = {// Lines from P to A, B, C
|
||||
P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, A.x, A.y, A.z, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
|
||||
|
||||
P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, B.x, B.y, B.z, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
|
||||
|
||||
P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, C.x, C.y, C.z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
|
||||
|
||||
// The animated dot itself (White)
|
||||
P.x, P.y, P.z, 1.0f, 1.0f, 1.0f, sizeP, 0.0f,
|
||||
|
||||
// The 3 corner dots
|
||||
A.x, A.y, A.z, 1.0f, 0.0f, 0.0f, sizeA, 0.0f, B.x, B.y, B.z, 0.0f, 1.0f, 0.0f, sizeB, 0.0f, C.x, C.y,
|
||||
C.z, 0.0f, 0.0f, 1.0f, sizeC, 0.0f};
|
||||
|
||||
char bufA[16], bufB[16], bufC[16];
|
||||
snprintf(bufA, sizeof(bufA), "%.2f", u);
|
||||
snprintf(bufB, sizeof(bufB), "%.2f", v);
|
||||
snprintf(bufC, sizeof(bufC), "%.2f", w);
|
||||
|
||||
// Keep text at a fixed distance from the dots
|
||||
float distA = 0.13f;
|
||||
float distB = 0.13f;
|
||||
float distC = 0.13f;
|
||||
|
||||
drawText(bufA, A.x - 0.05f, A.y + distA, 0.1f, Color(1, 0, 0, 1), dynData);
|
||||
drawText(bufB, B.x - 0.15f - distB, B.y - 0.05f - distB, 0.1f, Color(0, 1, 0, 1), dynData);
|
||||
drawText(bufC, C.x + 0.05f + distC, C.y - 0.05f - distC, 0.1f, Color(0, 0, 1, 1), dynData);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
GLFWwindow* window = initWindow(800, 800, "Barycentric Coordinates");
|
||||
if (!window)
|
||||
return -1;
|
||||
|
||||
GLuint shaderProgram = createShaderProgram();
|
||||
|
||||
// Triangle vertices as shown in the picture (Red, Green, Blue)
|
||||
// Scaled down slightly to leave room for text
|
||||
Triangle<Vector3<float>> triangle(Vector3<float>(0.0f, 0.6f, 0.0f), // Red dot (top)
|
||||
Vector3<float>(-0.6f, -0.6f, 0.0f), // Green dot (bottom left)
|
||||
Vector3<float>(0.6f, -0.6f, 0.0f) // Blue dot (bottom right)
|
||||
);
|
||||
|
||||
std::vector<float> pointCloud;
|
||||
generatePointCloud(pointCloud, triangle);
|
||||
|
||||
GLuint VAO_cloud, VBO_cloud, VAO_dyn, VBO_dyn;
|
||||
setupBuffers(VAO_cloud, VBO_cloud, pointCloud, VAO_dyn, VBO_dyn);
|
||||
|
||||
glEnable(GL_PROGRAM_POINT_SIZE);
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
glClearColor(0.02f, 0.02f, 0.02f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
glUseProgram(shaderProgram);
|
||||
|
||||
// Draw the point cloud (the iterated points)
|
||||
glBindVertexArray(VAO_cloud);
|
||||
glDrawArrays(GL_POINTS, 0, pointCloud.size() / 8);
|
||||
|
||||
// Animate the white dot to simulate dragging
|
||||
float t = glfwGetTime();
|
||||
float u = (std::sin(t * 1.5f) * 0.5f + 0.5f);
|
||||
float v = (std::cos(t * 1.1f) * 0.5f + 0.5f);
|
||||
|
||||
if (u + v > 1.0f)
|
||||
{
|
||||
u = 1.0f - u;
|
||||
v = 1.0f - v;
|
||||
}
|
||||
float w = 1.0f - u - v;
|
||||
|
||||
if (w > 1.0f)
|
||||
{
|
||||
float diff = w - 1.0f;
|
||||
w = 1.0f;
|
||||
u += diff / 2.0f;
|
||||
v += diff / 2.0f;
|
||||
}
|
||||
else if (w < 0.0f)
|
||||
{
|
||||
float diff = -w;
|
||||
w = 0.0f;
|
||||
u -= diff / 2.0f;
|
||||
v -= diff / 2.0f;
|
||||
}
|
||||
|
||||
Vector3<float> P = triangle.m_vertex1 * u + triangle.m_vertex2 * v + triangle.m_vertex3 * w;
|
||||
|
||||
std::vector<float> dynData;
|
||||
updateDynamicData(dynData, u, v, w, P, triangle);
|
||||
|
||||
glBindVertexArray(VAO_dyn);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO_dyn);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, dynData.size() * sizeof(float), dynData.data());
|
||||
|
||||
// Draw lines
|
||||
glDrawArrays(GL_LINES, 0, 6);
|
||||
|
||||
// Draw text lines
|
||||
int numTextVertices = (dynData.size() / 8) - 10;
|
||||
if (numTextVertices > 0)
|
||||
{
|
||||
glDrawArrays(GL_LINES, 10, numTextVertices);
|
||||
}
|
||||
|
||||
// Draw dots
|
||||
glDrawArrays(GL_POINTS, 6, 4);
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
return 0;
|
||||
}
|
||||
14
examples/example_glfw3/CMakeLists.txt
Normal file
14
examples/example_glfw3/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
project(example_glfw3)
|
||||
|
||||
add_executable(${PROJECT_NAME} example_glfw3.cpp)
|
||||
set_target_properties(
|
||||
${PROJECT_NAME}
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
find_package(OpenGL)
|
||||
find_package(GLEW REQUIRED)
|
||||
find_package(glfw3 CONFIG REQUIRED)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE omath::omath GLEW::GLEW glfw OpenGL::GL)
|
||||
@@ -342,7 +342,7 @@ int main()
|
||||
old_mouse_time = glfwGetTime();
|
||||
mouse_capture = !mouse_capture;
|
||||
|
||||
glfwSetInputMode(window, GLFW_CURSOR, mouse_capture ? GLFW_CURSOR_CAPTURED : GLFW_CURSOR_NORMAL);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, mouse_capture ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
|
||||
}
|
||||
if (mouse_capture)
|
||||
{
|
||||
10
examples/example_proj_mat_builder/CMakeLists.txt
Normal file
10
examples/example_proj_mat_builder/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
project(example_projection_matrix_builder)
|
||||
|
||||
add_executable(${PROJECT_NAME} example_proj_mat_builder.cpp)
|
||||
set_target_properties(
|
||||
${PROJECT_NAME}
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE omath::omath)
|
||||
10
examples/example_signature_scan/CMakeLists.txt
Normal file
10
examples/example_signature_scan/CMakeLists.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
project(example_signature_scan)
|
||||
|
||||
add_executable(${PROJECT_NAME} example_signature_scan.cpp)
|
||||
set_target_properties(
|
||||
${PROJECT_NAME}
|
||||
PROPERTIES CXX_STANDARD 23
|
||||
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
|
||||
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE omath::omath)
|
||||
@@ -14,11 +14,15 @@ namespace omath::collision
|
||||
Simplex<VertexType> simplex; // valid only if hit == true and size==4
|
||||
};
|
||||
|
||||
struct GjkSettings final
|
||||
{
|
||||
float epsilon = 1e-6f;
|
||||
std::size_t max_iterations = 64;
|
||||
};
|
||||
template<class ColliderInterfaceType>
|
||||
class GjkAlgorithm final
|
||||
{
|
||||
using VectorType = ColliderInterfaceType::VectorType;
|
||||
|
||||
public:
|
||||
[[nodiscard]]
|
||||
static VectorType find_support_vertex(const ColliderInterfaceType& collider_a,
|
||||
@@ -36,7 +40,8 @@ namespace omath::collision
|
||||
|
||||
[[nodiscard]]
|
||||
static GjkHitInfo<VectorType> is_collide_with_simplex_info(const ColliderInterfaceType& collider_a,
|
||||
const ColliderInterfaceType& collider_b)
|
||||
const ColliderInterfaceType& collider_b,
|
||||
const GjkSettings& settings = {})
|
||||
{
|
||||
auto support = find_support_vertex(collider_a, collider_b, VectorType{1, 0, 0});
|
||||
|
||||
@@ -45,11 +50,11 @@ namespace omath::collision
|
||||
|
||||
auto direction = -support;
|
||||
|
||||
while (true)
|
||||
for (std::size_t iteration = 0; iteration < settings.max_iterations; ++iteration)
|
||||
{
|
||||
support = find_support_vertex(collider_a, collider_b, direction);
|
||||
|
||||
if (support.dot(direction) <= 0.f)
|
||||
if (support.dot(direction) <= settings.epsilon)
|
||||
return {false, simplex};
|
||||
|
||||
simplex.push_front(support);
|
||||
@@ -57,6 +62,7 @@ namespace omath::collision
|
||||
if (simplex.handle(direction))
|
||||
return {true, simplex};
|
||||
}
|
||||
return {false, simplex};
|
||||
}
|
||||
};
|
||||
} // namespace omath::collision
|
||||
@@ -46,9 +46,26 @@ namespace omath::collision
|
||||
[[nodiscard]]
|
||||
const VertexType& find_furthest_vertex(const VectorType& direction) const
|
||||
{
|
||||
return *std::ranges::max_element(
|
||||
m_mesh.m_vertex_buffer, [&direction](const auto& first, const auto& second)
|
||||
{ return first.position.dot(direction) < second.position.dot(direction); });
|
||||
// The support query arrives in world space, but vertex positions are stored
|
||||
// in local space. We need argmax_v { world(v) · d }.
|
||||
//
|
||||
// world(v) = M·v (ignoring translation, which is constant across vertices)
|
||||
// world(v) · d = v · Mᵀ·d
|
||||
//
|
||||
// So we transform the direction to local space once — O(1) — then compare
|
||||
// raw local positions, which is far cheaper than calling
|
||||
// vertex_position_to_world_space (full 4×4 multiply) for every vertex.
|
||||
//
|
||||
// d_local = upper-left 3×3 of M, transposed, times d_world:
|
||||
// d_local[j] = sum_i M.at(i,j) * d[i] (i.e. column j of M dotted with d)
|
||||
const auto& m = m_mesh.get_to_world_matrix();
|
||||
const VectorType d_local = {
|
||||
m[0, 0] * direction.x + m[1, 0] * direction.y + m[2, 0] * direction.z,
|
||||
m[0, 1] * direction.x + m[1, 1] * direction.y + m[2, 1] * direction.z,
|
||||
m[0, 2] * direction.x + m[1, 2] * direction.y + m[2, 2] * direction.z,
|
||||
};
|
||||
return *std::ranges::max_element(m_mesh.m_vertex_buffer, [&d_local](const auto& first, const auto& second)
|
||||
{ return first.position.dot(d_local) < second.position.dot(d_local); });
|
||||
}
|
||||
MeshType m_mesh;
|
||||
};
|
||||
|
||||
@@ -62,20 +62,13 @@ namespace omath::detail
|
||||
return splitmix64(base_seed() + 0xD1B54A32D192ED03ull * (Stream + 1));
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
consteval std::uint64_t bounded_u64(const std::uint64_t x, const std::uint64_t bound)
|
||||
{
|
||||
return (x * bound) >> 64;
|
||||
}
|
||||
|
||||
template<std::int64_t Lo, std::int64_t Hi, std::uint64_t Stream>
|
||||
[[nodiscard]]
|
||||
consteval std::int64_t rand_uint8_t()
|
||||
{
|
||||
static_assert(Lo <= Hi);
|
||||
const std::uint64_t span = static_cast<std::uint64_t>(Hi - Lo) + 1ull;
|
||||
const std::uint64_t r = rand_u64<Stream>();
|
||||
return static_cast<std::int64_t>(bounded_u64(r, span)) + Lo;
|
||||
return static_cast<std::int64_t>(r) + Lo;
|
||||
}
|
||||
[[nodiscard]]
|
||||
consteval std::uint64_t rand_u64(const std::uint64_t seed, const std::uint64_t i)
|
||||
|
||||
219
include/omath/linear_algebra/quaternion.hpp
Normal file
219
include/omath/linear_algebra/quaternion.hpp
Normal file
@@ -0,0 +1,219 @@
|
||||
//
|
||||
// Created by vlad on 3/1/2026.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#include "omath/linear_algebra/mat.hpp"
|
||||
#include "omath/linear_algebra/vector3.hpp"
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <format>
|
||||
|
||||
namespace omath
|
||||
{
|
||||
template<class Type>
|
||||
requires std::is_arithmetic_v<Type>
|
||||
class Quaternion
|
||||
{
|
||||
public:
|
||||
using ContainedType = Type;
|
||||
|
||||
Type x = static_cast<Type>(0);
|
||||
Type y = static_cast<Type>(0);
|
||||
Type z = static_cast<Type>(0);
|
||||
Type w = static_cast<Type>(1); // identity quaternion
|
||||
|
||||
constexpr Quaternion() noexcept = default;
|
||||
|
||||
constexpr Quaternion(const Type& x, const Type& y, const Type& z, const Type& w) noexcept
|
||||
: x(x), y(y), z(z), w(w)
|
||||
{
|
||||
}
|
||||
|
||||
// Factory: build from a normalized axis and an angle in radians
|
||||
[[nodiscard]]
|
||||
static Quaternion from_axis_angle(const Vector3<Type>& axis, const Type& angle_rad) noexcept
|
||||
{
|
||||
const Type half = angle_rad / static_cast<Type>(2);
|
||||
const Type s = std::sin(half);
|
||||
return {axis.x * s, axis.y * s, axis.z * s, std::cos(half)};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator==(const Quaternion& other) const noexcept
|
||||
{
|
||||
return x == other.x && y == other.y && z == other.z && w == other.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr bool operator!=(const Quaternion& other) const noexcept
|
||||
{
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
// Hamilton product: this * other
|
||||
[[nodiscard]] constexpr Quaternion operator*(const Quaternion& other) const noexcept
|
||||
{
|
||||
return {
|
||||
w * other.x + x * other.w + y * other.z - z * other.y,
|
||||
w * other.y - x * other.z + y * other.w + z * other.x,
|
||||
w * other.z + x * other.y - y * other.x + z * other.w,
|
||||
w * other.w - x * other.x - y * other.y - z * other.z,
|
||||
};
|
||||
}
|
||||
|
||||
constexpr Quaternion& operator*=(const Quaternion& other) noexcept
|
||||
{
|
||||
return *this = *this * other;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion operator*(const Type& scalar) const noexcept
|
||||
{
|
||||
return {x * scalar, y * scalar, z * scalar, w * scalar};
|
||||
}
|
||||
|
||||
constexpr Quaternion& operator*=(const Type& scalar) noexcept
|
||||
{
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
w *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion operator+(const Quaternion& other) const noexcept
|
||||
{
|
||||
return {x + other.x, y + other.y, z + other.z, w + other.w};
|
||||
}
|
||||
|
||||
constexpr Quaternion& operator+=(const Quaternion& other) noexcept
|
||||
{
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
z += other.z;
|
||||
w += other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion operator-() const noexcept
|
||||
{
|
||||
return {-x, -y, -z, -w};
|
||||
}
|
||||
|
||||
// Conjugate: negates the vector part (x, y, z)
|
||||
[[nodiscard]] constexpr Quaternion conjugate() const noexcept
|
||||
{
|
||||
return {-x, -y, -z, w};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type dot(const Quaternion& other) const noexcept
|
||||
{
|
||||
return x * other.x + y * other.y + z * other.z + w * other.w;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Type length_sqr() const noexcept
|
||||
{
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
#ifndef _MSC_VER
|
||||
[[nodiscard]] constexpr Type length() const noexcept
|
||||
{
|
||||
return std::sqrt(length_sqr());
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Quaternion normalized() const noexcept
|
||||
{
|
||||
const Type len = length();
|
||||
return len != static_cast<Type>(0) ? *this * (static_cast<Type>(1) / len) : *this;
|
||||
}
|
||||
#else
|
||||
[[nodiscard]] Type length() const noexcept
|
||||
{
|
||||
return std::sqrt(length_sqr());
|
||||
}
|
||||
|
||||
[[nodiscard]] Quaternion normalized() const noexcept
|
||||
{
|
||||
const Type len = length();
|
||||
return len != static_cast<Type>(0) ? *this * (static_cast<Type>(1) / len) : *this;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Inverse: q* / |q|^2 (for unit quaternions inverse == conjugate)
|
||||
[[nodiscard]] constexpr Quaternion inverse() const noexcept
|
||||
{
|
||||
return conjugate() * (static_cast<Type>(1) / length_sqr());
|
||||
}
|
||||
|
||||
// Rotate a 3D vector: v' = q * pure(v) * q^-1
|
||||
// Computed via Rodrigues' formula to avoid full quaternion product overhead
|
||||
[[nodiscard]] constexpr Vector3<Type> rotate(const Vector3<Type>& v) const noexcept
|
||||
{
|
||||
const Vector3<Type> q_vec{x, y, z};
|
||||
const Vector3<Type> cross = q_vec.cross(v);
|
||||
return v + cross * (static_cast<Type>(2) * w) + q_vec.cross(cross) * static_cast<Type>(2);
|
||||
}
|
||||
|
||||
// 3x3 rotation matrix from this (unit) quaternion
|
||||
[[nodiscard]] constexpr Mat<3, 3, Type> to_rotation_matrix3() const noexcept
|
||||
{
|
||||
const Type xx = x * x, yy = y * y, zz = z * z;
|
||||
const Type xy = x * y, xz = x * z, yz = y * z;
|
||||
const Type wx = w * x, wy = w * y, wz = w * z;
|
||||
const Type one = static_cast<Type>(1);
|
||||
const Type two = static_cast<Type>(2);
|
||||
|
||||
return {
|
||||
{one - two * (yy + zz), two * (xy - wz), two * (xz + wy) },
|
||||
{two * (xy + wz), one - two * (xx + zz), two * (yz - wx) },
|
||||
{two * (xz - wy), two * (yz + wx), one - two * (xx + yy)},
|
||||
};
|
||||
}
|
||||
|
||||
// 4x4 rotation matrix (with homogeneous row/column)
|
||||
[[nodiscard]] constexpr Mat<4, 4, Type> to_rotation_matrix4() const noexcept
|
||||
{
|
||||
const Type xx = x * x, yy = y * y, zz = z * z;
|
||||
const Type xy = x * y, xz = x * z, yz = y * z;
|
||||
const Type wx = w * x, wy = w * y, wz = w * z;
|
||||
const Type one = static_cast<Type>(1);
|
||||
const Type two = static_cast<Type>(2);
|
||||
const Type zero = static_cast<Type>(0);
|
||||
|
||||
return {
|
||||
{one - two * (yy + zz), two * (xy - wz), two * (xz + wy), zero},
|
||||
{two * (xy + wz), one - two * (xx + zz), two * (yz - wx), zero},
|
||||
{two * (xz - wy), two * (yz + wx), one - two * (xx + yy), zero},
|
||||
{zero, zero, zero, one },
|
||||
};
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr std::array<Type, 4> as_array() const noexcept
|
||||
{
|
||||
return {x, y, z, w};
|
||||
}
|
||||
};
|
||||
} // namespace omath
|
||||
|
||||
template<class Type>
|
||||
struct std::formatter<omath::Quaternion<Type>> // NOLINT(*-dcl58-cpp)
|
||||
{
|
||||
[[nodiscard]]
|
||||
static constexpr auto parse(std::format_parse_context& ctx)
|
||||
{
|
||||
return ctx.begin();
|
||||
}
|
||||
|
||||
template<class FormatContext>
|
||||
[[nodiscard]]
|
||||
static auto format(const omath::Quaternion<Type>& q, FormatContext& ctx)
|
||||
{
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char>)
|
||||
return std::format_to(ctx.out(), "[{}, {}, {}, {}]", q.x, q.y, q.z, q.w);
|
||||
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, wchar_t>)
|
||||
return std::format_to(ctx.out(), L"[{}, {}, {}, {}]", q.x, q.y, q.z, q.w);
|
||||
|
||||
if constexpr (std::is_same_v<typename FormatContext::char_type, char8_t>)
|
||||
return std::format_to(ctx.out(), u8"[{}, {}, {}, {}]", q.x, q.y, q.z, q.w);
|
||||
}
|
||||
};
|
||||
@@ -17,6 +17,9 @@
|
||||
// Matrix classes
|
||||
#include "omath/linear_algebra/mat.hpp"
|
||||
|
||||
// Quaternion
|
||||
#include "omath/linear_algebra/quaternion.hpp"
|
||||
|
||||
// Color functionality
|
||||
#include "omath/utility/color.hpp"
|
||||
|
||||
|
||||
@@ -15,7 +15,10 @@ endif()
|
||||
set(EXAMPLES_BIN_DIR "${PROJECT_ROOT}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
if(NOT EXISTS "${EXAMPLES_BIN_DIR}")
|
||||
message(FATAL_ERROR "Examples binary directory not found: ${EXAMPLES_BIN_DIR}. Please build the project first.")
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Examples binary directory not found: ${EXAMPLES_BIN_DIR}. Please build the project first."
|
||||
)
|
||||
endif()
|
||||
|
||||
message(STATUS "Looking for benchmark executables in: ${EXAMPLES_BIN_DIR}")
|
||||
@@ -43,16 +46,13 @@ foreach(EXAMPLE_PATH ${EXAMPLE_FILES})
|
||||
endif()
|
||||
|
||||
# On Linux/macOS, check permissions or just try to run it.
|
||||
|
||||
|
||||
message(STATUS "-------------------------------------------------")
|
||||
message(STATUS "Running benchmark: ${FILENAME}")
|
||||
message(STATUS "-------------------------------------------------")
|
||||
|
||||
execute_process(
|
||||
COMMAND "${EXAMPLE_PATH}"
|
||||
WORKING_DIRECTORY "${PROJECT_ROOT}"
|
||||
RESULT_VARIABLE EXIT_CODE
|
||||
)
|
||||
execute_process(COMMAND "${EXAMPLE_PATH}" WORKING_DIRECTORY "${PROJECT_ROOT}"
|
||||
RESULT_VARIABLE EXIT_CODE)
|
||||
|
||||
if(NOT EXIT_CODE EQUAL 0)
|
||||
message(WARNING "Benchmark ${FILENAME} exited with error code: ${EXIT_CODE}")
|
||||
|
||||
@@ -15,7 +15,10 @@ endif()
|
||||
set(EXAMPLES_BIN_DIR "${PROJECT_ROOT}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
if(NOT EXISTS "${EXAMPLES_BIN_DIR}")
|
||||
message(FATAL_ERROR "Examples binary directory not found: ${EXAMPLES_BIN_DIR}. Please build the project first.")
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Examples binary directory not found: ${EXAMPLES_BIN_DIR}. Please build the project first."
|
||||
)
|
||||
endif()
|
||||
|
||||
message(STATUS "Looking for example executables in: ${EXAMPLES_BIN_DIR}")
|
||||
@@ -43,16 +46,13 @@ foreach(EXAMPLE_PATH ${EXAMPLE_FILES})
|
||||
endif()
|
||||
|
||||
# On Linux/macOS, check permissions or just try to run it.
|
||||
|
||||
|
||||
message(STATUS "-------------------------------------------------")
|
||||
message(STATUS "Running example: ${FILENAME}")
|
||||
message(STATUS "-------------------------------------------------")
|
||||
|
||||
execute_process(
|
||||
COMMAND "${EXAMPLE_PATH}"
|
||||
WORKING_DIRECTORY "${PROJECT_ROOT}"
|
||||
RESULT_VARIABLE EXIT_CODE
|
||||
)
|
||||
execute_process(COMMAND "${EXAMPLE_PATH}" WORKING_DIRECTORY "${PROJECT_ROOT}"
|
||||
RESULT_VARIABLE EXIT_CODE)
|
||||
|
||||
if(NOT EXIT_CODE EQUAL 0)
|
||||
message(WARNING "Example ${FILENAME} exited with error code: ${EXIT_CODE}")
|
||||
|
||||
@@ -15,7 +15,10 @@ endif()
|
||||
set(EXAMPLES_BIN_DIR "${PROJECT_ROOT}/out/${CMAKE_BUILD_TYPE}")
|
||||
|
||||
if(NOT EXISTS "${EXAMPLES_BIN_DIR}")
|
||||
message(FATAL_ERROR "Examples binary directory not found: ${EXAMPLES_BIN_DIR}. Please build the project first.")
|
||||
message(
|
||||
FATAL_ERROR
|
||||
"Examples binary directory not found: ${EXAMPLES_BIN_DIR}. Please build the project first."
|
||||
)
|
||||
endif()
|
||||
|
||||
message(STATUS "Looking for unit test executables in: ${EXAMPLES_BIN_DIR}")
|
||||
@@ -43,16 +46,13 @@ foreach(EXAMPLE_PATH ${EXAMPLE_FILES})
|
||||
endif()
|
||||
|
||||
# On Linux/macOS, check permissions or just try to run it.
|
||||
|
||||
|
||||
message(STATUS "-------------------------------------------------")
|
||||
message(STATUS "Running unit_tests: ${FILENAME}")
|
||||
message(STATUS "-------------------------------------------------")
|
||||
|
||||
execute_process(
|
||||
COMMAND "${EXAMPLE_PATH}"
|
||||
WORKING_DIRECTORY "${PROJECT_ROOT}"
|
||||
RESULT_VARIABLE EXIT_CODE
|
||||
)
|
||||
execute_process(COMMAND "${EXAMPLE_PATH}" WORKING_DIRECTORY "${PROJECT_ROOT}"
|
||||
RESULT_VARIABLE EXIT_CODE)
|
||||
|
||||
if(NOT EXIT_CODE EQUAL 0)
|
||||
message(WARNING "Example ${FILENAME} exited with error code: ${EXIT_CODE}")
|
||||
|
||||
15
scripts/cmake-format.sh
Normal file
15
scripts/cmake-format.sh
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Format all CMakeLists.txt and *.cmake files in the repo (excluding common build dirs)
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
|
||||
cd "$REPO_ROOT"
|
||||
|
||||
find . \
|
||||
-path "./build" -prune -o \
|
||||
-path "./cmake-build*" -prune -o \
|
||||
-path "./out" -prune -o \
|
||||
-path "./.git" -prune -o \
|
||||
\( -name "CMakeLists.txt" -o -name "*.cmake" \) -print0 \
|
||||
| xargs -0 cmake-format -i
|
||||
402
tests/general/unit_test_quaternion.cpp
Normal file
402
tests/general/unit_test_quaternion.cpp
Normal file
@@ -0,0 +1,402 @@
|
||||
//
|
||||
// Created by vlad on 3/1/2026.
|
||||
//
|
||||
#include <omath/linear_algebra/quaternion.hpp>
|
||||
#include <cmath>
|
||||
#include <gtest/gtest.h>
|
||||
#include <numbers>
|
||||
|
||||
using namespace omath;
|
||||
|
||||
static constexpr float kEps = 1e-5f;
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
static void expect_quat_near(const Quaternion<float>& a, const Quaternion<float>& b, float eps = kEps)
|
||||
{
|
||||
EXPECT_NEAR(a.x, b.x, eps);
|
||||
EXPECT_NEAR(a.y, b.y, eps);
|
||||
EXPECT_NEAR(a.z, b.z, eps);
|
||||
EXPECT_NEAR(a.w, b.w, eps);
|
||||
}
|
||||
|
||||
static void expect_vec3_near(const Vector3<float>& a, const Vector3<float>& b, float eps = kEps)
|
||||
{
|
||||
EXPECT_NEAR(a.x, b.x, eps);
|
||||
EXPECT_NEAR(a.y, b.y, eps);
|
||||
EXPECT_NEAR(a.z, b.z, eps);
|
||||
}
|
||||
|
||||
// ── Constructors ─────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, DefaultConstructorIsIdentity)
|
||||
{
|
||||
constexpr Quaternion<float> q;
|
||||
EXPECT_FLOAT_EQ(q.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(q.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(q.z, 0.f);
|
||||
EXPECT_FLOAT_EQ(q.w, 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, ValueConstructor)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
EXPECT_FLOAT_EQ(q.x, 1.f);
|
||||
EXPECT_FLOAT_EQ(q.y, 2.f);
|
||||
EXPECT_FLOAT_EQ(q.z, 3.f);
|
||||
EXPECT_FLOAT_EQ(q.w, 4.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, DoubleInstantiation)
|
||||
{
|
||||
constexpr Quaternion<double> q{0.0, 0.0, 0.0, 1.0};
|
||||
EXPECT_DOUBLE_EQ(q.w, 1.0);
|
||||
}
|
||||
|
||||
// ── Equality ─────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, EqualityOperators)
|
||||
{
|
||||
constexpr Quaternion<float> a{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr Quaternion<float> b{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr Quaternion<float> c{1.f, 2.f, 3.f, 5.f};
|
||||
|
||||
EXPECT_TRUE(a == b);
|
||||
EXPECT_FALSE(a == c);
|
||||
EXPECT_FALSE(a != b);
|
||||
EXPECT_TRUE(a != c);
|
||||
}
|
||||
|
||||
// ── Arithmetic ───────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, ScalarMultiply)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr auto r = q * 2.f;
|
||||
EXPECT_FLOAT_EQ(r.x, 2.f);
|
||||
EXPECT_FLOAT_EQ(r.y, 4.f);
|
||||
EXPECT_FLOAT_EQ(r.z, 6.f);
|
||||
EXPECT_FLOAT_EQ(r.w, 8.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, ScalarMultiplyAssign)
|
||||
{
|
||||
Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
q *= 3.f;
|
||||
EXPECT_FLOAT_EQ(q.x, 3.f);
|
||||
EXPECT_FLOAT_EQ(q.y, 6.f);
|
||||
EXPECT_FLOAT_EQ(q.z, 9.f);
|
||||
EXPECT_FLOAT_EQ(q.w, 12.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, Addition)
|
||||
{
|
||||
constexpr Quaternion<float> a{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr Quaternion<float> b{4.f, 3.f, 2.f, 1.f};
|
||||
constexpr auto r = a + b;
|
||||
EXPECT_FLOAT_EQ(r.x, 5.f);
|
||||
EXPECT_FLOAT_EQ(r.y, 5.f);
|
||||
EXPECT_FLOAT_EQ(r.z, 5.f);
|
||||
EXPECT_FLOAT_EQ(r.w, 5.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, AdditionAssign)
|
||||
{
|
||||
Quaternion<float> a{1.f, 0.f, 0.f, 0.f};
|
||||
const Quaternion<float> b{0.f, 1.f, 0.f, 0.f};
|
||||
a += b;
|
||||
EXPECT_FLOAT_EQ(a.x, 1.f);
|
||||
EXPECT_FLOAT_EQ(a.y, 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, UnaryNegation)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, -2.f, 3.f, -4.f};
|
||||
constexpr auto r = -q;
|
||||
EXPECT_FLOAT_EQ(r.x, -1.f);
|
||||
EXPECT_FLOAT_EQ(r.y, 2.f);
|
||||
EXPECT_FLOAT_EQ(r.z, -3.f);
|
||||
EXPECT_FLOAT_EQ(r.w, 4.f);
|
||||
}
|
||||
|
||||
// ── Hamilton product ──────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, MultiplyByIdentityIsNoop)
|
||||
{
|
||||
constexpr Quaternion<float> identity;
|
||||
constexpr Quaternion<float> q{0.5f, 0.5f, 0.5f, 0.5f};
|
||||
expect_quat_near(q * identity, q);
|
||||
expect_quat_near(identity * q, q);
|
||||
}
|
||||
|
||||
TEST(Quaternion, MultiplyAssign)
|
||||
{
|
||||
constexpr Quaternion<float> identity;
|
||||
Quaternion<float> q{0.5f, 0.5f, 0.5f, 0.5f};
|
||||
q *= identity;
|
||||
expect_quat_near(q, {0.5f, 0.5f, 0.5f, 0.5f});
|
||||
}
|
||||
|
||||
TEST(Quaternion, MultiplyKnownResult)
|
||||
{
|
||||
// i * j = k → (1,0,0,0) * (0,1,0,0) = (0,0,1,0)
|
||||
constexpr Quaternion<float> i{1.f, 0.f, 0.f, 0.f};
|
||||
constexpr Quaternion<float> j{0.f, 1.f, 0.f, 0.f};
|
||||
constexpr auto k = i * j;
|
||||
EXPECT_FLOAT_EQ(k.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(k.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(k.z, 1.f);
|
||||
EXPECT_FLOAT_EQ(k.w, 0.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, MultiplyByInverseGivesIdentity)
|
||||
{
|
||||
const Quaternion<float> q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f},
|
||||
std::numbers::pi_v<float> / 3.f);
|
||||
const auto result = q * q.inverse();
|
||||
expect_quat_near(result, Quaternion<float>{});
|
||||
}
|
||||
|
||||
// ── Conjugate ────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, Conjugate)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr auto c = q.conjugate();
|
||||
EXPECT_FLOAT_EQ(c.x, -1.f);
|
||||
EXPECT_FLOAT_EQ(c.y, -2.f);
|
||||
EXPECT_FLOAT_EQ(c.z, -3.f);
|
||||
EXPECT_FLOAT_EQ(c.w, 4.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, ConjugateOfIdentityIsIdentity)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr auto c = id.conjugate();
|
||||
EXPECT_FLOAT_EQ(c.x, 0.f);
|
||||
EXPECT_FLOAT_EQ(c.y, 0.f);
|
||||
EXPECT_FLOAT_EQ(c.z, 0.f);
|
||||
EXPECT_FLOAT_EQ(c.w, 1.f);
|
||||
}
|
||||
|
||||
// ── Dot / length ─────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, Dot)
|
||||
{
|
||||
constexpr Quaternion<float> a{1.f, 0.f, 0.f, 0.f};
|
||||
constexpr Quaternion<float> b{0.f, 1.f, 0.f, 0.f};
|
||||
EXPECT_FLOAT_EQ(a.dot(b), 0.f);
|
||||
EXPECT_FLOAT_EQ(a.dot(a), 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, LengthSqrIdentity)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
EXPECT_FLOAT_EQ(id.length_sqr(), 1.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, LengthSqrGeneral)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
EXPECT_FLOAT_EQ(q.length_sqr(), 30.f);
|
||||
}
|
||||
|
||||
TEST(Quaternion, LengthIdentity)
|
||||
{
|
||||
const Quaternion<float> id;
|
||||
EXPECT_NEAR(id.length(), 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, Normalized)
|
||||
{
|
||||
const Quaternion<float> q{1.f, 1.f, 1.f, 1.f};
|
||||
const auto n = q.normalized();
|
||||
EXPECT_NEAR(n.length(), 1.f, kEps);
|
||||
EXPECT_NEAR(n.x, 0.5f, kEps);
|
||||
EXPECT_NEAR(n.y, 0.5f, kEps);
|
||||
EXPECT_NEAR(n.z, 0.5f, kEps);
|
||||
EXPECT_NEAR(n.w, 0.5f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, NormalizedOfZeroLengthReturnsSelf)
|
||||
{
|
||||
// length_sqr = 0 would be UB, but zero-vector part + zero w is degenerate;
|
||||
// we just verify the guard branch (divides by zero) doesn't crash by
|
||||
// keeping length > 0 via the default constructor path.
|
||||
const Quaternion<float> unit;
|
||||
const auto n = unit.normalized();
|
||||
expect_quat_near(n, unit);
|
||||
}
|
||||
|
||||
// ── Inverse ───────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, InverseOfUnitIsConjugate)
|
||||
{
|
||||
const Quaternion<float> q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f},
|
||||
std::numbers::pi_v<float> / 4.f);
|
||||
const auto inv = q.inverse();
|
||||
const auto conj = q.conjugate();
|
||||
expect_quat_near(inv, conj);
|
||||
}
|
||||
|
||||
// ── from_axis_angle ──────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, FromAxisAngleZeroAngleIsIdentity)
|
||||
{
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f}, 0.f);
|
||||
EXPECT_NEAR(q.x, 0.f, kEps);
|
||||
EXPECT_NEAR(q.y, 0.f, kEps);
|
||||
EXPECT_NEAR(q.z, 0.f, kEps);
|
||||
EXPECT_NEAR(q.w, 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, FromAxisAngle90DegZ)
|
||||
{
|
||||
const float half_pi = std::numbers::pi_v<float> / 2.f;
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f}, half_pi);
|
||||
const float s = std::sin(half_pi / 2.f);
|
||||
const float c = std::cos(half_pi / 2.f);
|
||||
EXPECT_NEAR(q.x, 0.f, kEps);
|
||||
EXPECT_NEAR(q.y, 0.f, kEps);
|
||||
EXPECT_NEAR(q.z, s, kEps);
|
||||
EXPECT_NEAR(q.w, c, kEps);
|
||||
}
|
||||
|
||||
// ── rotate ───────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, RotateByIdentityIsNoop)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr Vector3<float> v{1.f, 2.f, 3.f};
|
||||
const auto r = id.rotate(v);
|
||||
expect_vec3_near(r, v);
|
||||
}
|
||||
|
||||
TEST(Quaternion, Rotate90DegAroundZ)
|
||||
{
|
||||
// Rotating (1,0,0) by 90° around Z should give (0,1,0)
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f}, std::numbers::pi_v<float> / 2.f);
|
||||
const auto r = q.rotate({1.f, 0.f, 0.f});
|
||||
expect_vec3_near(r, {0.f, 1.f, 0.f});
|
||||
}
|
||||
|
||||
TEST(Quaternion, Rotate180DegAroundY)
|
||||
{
|
||||
// Rotating (1,0,0) by 180° around Y should give (-1,0,0)
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 1.f, 0.f}, std::numbers::pi_v<float>);
|
||||
const auto r = q.rotate({1.f, 0.f, 0.f});
|
||||
expect_vec3_near(r, {-1.f, 0.f, 0.f});
|
||||
}
|
||||
|
||||
TEST(Quaternion, Rotate90DegAroundX)
|
||||
{
|
||||
// Rotating (0,1,0) by 90° around X should give (0,0,1)
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f}, std::numbers::pi_v<float> / 2.f);
|
||||
const auto r = q.rotate({0.f, 1.f, 0.f});
|
||||
expect_vec3_near(r, {0.f, 0.f, 1.f});
|
||||
}
|
||||
|
||||
// ── to_rotation_matrix3 ───────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, RotationMatrix3FromIdentityIsIdentityMatrix)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr auto m = id.to_rotation_matrix3();
|
||||
for (size_t i = 0; i < 3; ++i)
|
||||
for (size_t j = 0; j < 3; ++j)
|
||||
EXPECT_NEAR(m.at(i, j), i == j ? 1.f : 0.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix3From90DegZ)
|
||||
{
|
||||
// Expected: | 0 -1 0 |
|
||||
// | 1 0 0 |
|
||||
// | 0 0 1 |
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 0.f, 1.f}, std::numbers::pi_v<float> / 2.f);
|
||||
const auto m = q.to_rotation_matrix3();
|
||||
EXPECT_NEAR(m.at(0, 0), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(0, 1), -1.f, kEps);
|
||||
EXPECT_NEAR(m.at(0, 2), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(1, 0), 1.f, kEps);
|
||||
EXPECT_NEAR(m.at(1, 1), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(1, 2), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(2, 0), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(2, 1), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(2, 2), 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix3ConsistentWithRotate)
|
||||
{
|
||||
// Matrix-vector multiply must agree with the rotate() method
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 1.f, 0.f}, std::numbers::pi_v<float> / 3.f);
|
||||
const Vector3<float> v{2.f, -1.f, 0.5f};
|
||||
|
||||
const auto rotated = q.rotate(v);
|
||||
const auto m = q.to_rotation_matrix3();
|
||||
|
||||
// manual mat-vec multiply (row-major)
|
||||
const float rx = m.at(0, 0) * v.x + m.at(0, 1) * v.y + m.at(0, 2) * v.z;
|
||||
const float ry = m.at(1, 0) * v.x + m.at(1, 1) * v.y + m.at(1, 2) * v.z;
|
||||
const float rz = m.at(2, 0) * v.x + m.at(2, 1) * v.y + m.at(2, 2) * v.z;
|
||||
|
||||
EXPECT_NEAR(rotated.x, rx, kEps);
|
||||
EXPECT_NEAR(rotated.y, ry, kEps);
|
||||
EXPECT_NEAR(rotated.z, rz, kEps);
|
||||
}
|
||||
|
||||
// ── to_rotation_matrix4 ───────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, RotationMatrix4FromIdentityIsIdentityMatrix)
|
||||
{
|
||||
constexpr Quaternion<float> id;
|
||||
constexpr auto m = id.to_rotation_matrix4();
|
||||
for (size_t i = 0; i < 4; ++i)
|
||||
for (size_t j = 0; j < 4; ++j)
|
||||
EXPECT_NEAR(m.at(i, j), i == j ? 1.f : 0.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix4HomogeneousRowAndColumn)
|
||||
{
|
||||
const auto q = Quaternion<float>::from_axis_angle({1.f, 0.f, 0.f}, std::numbers::pi_v<float> / 5.f);
|
||||
const auto m = q.to_rotation_matrix4();
|
||||
|
||||
// Last row and last column must be (0,0,0,1)
|
||||
for (size_t i = 0; i < 3; ++i)
|
||||
{
|
||||
EXPECT_NEAR(m.at(3, i), 0.f, kEps);
|
||||
EXPECT_NEAR(m.at(i, 3), 0.f, kEps);
|
||||
}
|
||||
EXPECT_NEAR(m.at(3, 3), 1.f, kEps);
|
||||
}
|
||||
|
||||
TEST(Quaternion, RotationMatrix4Upper3x3MatchesMatrix3)
|
||||
{
|
||||
const auto q = Quaternion<float>::from_axis_angle({0.f, 1.f, 0.f}, std::numbers::pi_v<float> / 7.f);
|
||||
const auto m3 = q.to_rotation_matrix3();
|
||||
const auto m4 = q.to_rotation_matrix4();
|
||||
|
||||
for (size_t i = 0; i < 3; ++i)
|
||||
for (size_t j = 0; j < 3; ++j)
|
||||
EXPECT_NEAR(m4.at(i, j), m3.at(i, j), kEps);
|
||||
}
|
||||
|
||||
// ── as_array ──────────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, AsArray)
|
||||
{
|
||||
constexpr Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
constexpr auto arr = q.as_array();
|
||||
EXPECT_FLOAT_EQ(arr[0], 1.f);
|
||||
EXPECT_FLOAT_EQ(arr[1], 2.f);
|
||||
EXPECT_FLOAT_EQ(arr[2], 3.f);
|
||||
EXPECT_FLOAT_EQ(arr[3], 4.f);
|
||||
}
|
||||
|
||||
// ── std::formatter ────────────────────────────────────────────────────────────
|
||||
|
||||
TEST(Quaternion, Formatter)
|
||||
{
|
||||
const Quaternion<float> q{1.f, 2.f, 3.f, 4.f};
|
||||
const auto s = std::format("{}", q);
|
||||
EXPECT_EQ(s, "[1, 2, 3, 4]");
|
||||
}
|
||||
Reference in New Issue
Block a user