Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/BlackMATov/vmath.hpp

C++17 tiny vector math library
https://github.com/BlackMATov/vmath.hpp

Last synced: about 2 months ago
JSON representation

C++17 tiny vector math library

Awesome Lists containing this project

README

        

# vmath.hpp

> C++17 tiny vector math library

[![linux][badge.linux]][linux]
[![darwin][badge.darwin]][darwin]
[![windows][badge.windows]][windows]
[![language][badge.language]][language]
[![license][badge.license]][license]

[badge.darwin]: https://img.shields.io/github/actions/workflow/status/BlackMATov/vmath.hpp/.github/workflows/darwin.yml?label=Xcode&logo=xcode
[badge.linux]: https://img.shields.io/github/actions/workflow/status/BlackMATov/vmath.hpp/.github/workflows/linux.yml?label=GCC%2FClang&logo=linux
[badge.windows]: https://img.shields.io/github/actions/workflow/status/BlackMATov/vmath.hpp/.github/workflows/windows.yml?label=Visual%20Studio&logo=visual-studio
[badge.language]: https://img.shields.io/badge/language-C%2B%2B17-yellow
[badge.license]: https://img.shields.io/badge/license-MIT-blue

[darwin]: https://github.com/BlackMATov/vmath.hpp/actions?query=workflow%3Adarwin
[linux]: https://github.com/BlackMATov/vmath.hpp/actions?query=workflow%3Alinux
[windows]: https://github.com/BlackMATov/vmath.hpp/actions?query=workflow%3Awindows
[language]: https://en.wikipedia.org/wiki/C%2B%2B17
[license]: https://en.wikipedia.org/wiki/MIT_License

[vmath]: https://github.com/BlackMATov/vmath.hpp

## Requirements

- [clang](https://clang.llvm.org/) **>= 7**
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
- [msvc](https://visualstudio.microsoft.com/) **>= 2019**
- [xcode](https://developer.apple.com/xcode/) **>= 11.7**

## Installation

[vmath.hpp][vmath] is a header-only library. All you need to do is copy the headers files from `headers` directory into your project and include them:

```cpp
#include "vmath.hpp/vmath_all.hpp"
```

Also, you can add the root repository directory to your [cmake](https://cmake.org) project:

```cmake
add_subdirectory(external/vmath.hpp)
target_link_libraries(your_project_target PUBLIC vmath.hpp::vmath.hpp)
```

Or just use the single-header version of the library, which you can find [here](develop/singles/headers/vmath.hpp/vmath_all.hpp).

## Disclaimer

The [vmath.hpp][vmath] is a tiny vector math library mainly for games, game engines, and other graphics software. It will never be mathematically strict (e.g. the vector class has operator plus for adding scalars to a vector, which is convenient for developing CG applications but makes no sense in "real" math). For the same reason, the library does not provide flexible vector and matrix sizes. The library functions follow the same principles.

Most functions and types are based on the HLSL ([High-Level Shading Language for DirectX](https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl)) specification. Matrices are row-major, which implies that vector, matrix multiplication is: v * M, not M * v.

## API

- [Vector Types](#Vector-Types)
- [Matrix Types](#Matrix-Types)
- [Quaternion Types](#Quaternion-Types)
- [Vector Operators](#Vector-Operators)
- [Matrix Operators](#Matrix-Operators)
- [Quaternion Operators](#Quaternion-Operators)
- [Common Functions](#Common-Functions)
- [Angle and Trigonometric Functions](#Angle-and-Trigonometric-Functions)
- [Exponential Functions](#Exponential-Functions)
- [Geometric Functions](#Geometric-Functions)
- [Relational Functions](#Relational-Functions)
- [Matrix Functions](#Matrix-Functions)
- [Quaternion Functions](#Quaternion-Functions)
- [Units](#Units)
- [Cast](#Cast)
- [Access](#Access)
- [Matrix Transform 3D](#Matrix-Transform-3D)
- [Matrix Transform 2D](#Matrix-Transform-2D)
- [Matrix Projections](#Matrix-Projections)
- [Vector Transform](#Vector-Transform)
- [Quaternion Transform](#Quaternion-Transform)

### Vector Types

```cpp
template < typename T, size_t Size >
class vec_base;

template < typename T >
class vec_base {
public:
T x, y;

vec_base();

vec_base(no_init_t);
vec_base(zero_init_t);
vec_base(unit_init_t);

explicit vec_base(T v);
vec_base(T x, T y);

template < typename U > vec_base(const vec_base& other);
template < typename U > explicit vec_base(const vec_base& other);
template < typename U > explicit vec_base(const vec_base& other);

template < typename U > explicit vec_base(const U* p);
};

template < typename T >
class vec_base {
public:
T x, y, z;

vec_base();

vec_base(no_init_t);
vec_base(zero_init_t);
vec_base(unit_init_t);

explicit vec_base(T v);
vec_base(T x, T y, T z);

vec_base(const vec_base& xy, T z);
vec_base(T x, const vec_base& yz);

template < typename U > vec_base(const vec_base& other);
template < typename U > explicit vec_base(const vec_base& other);

template < typename U > explicit vec_base(const U* p);
};

template < typename T >
class vec_base {
public:
T x, y, z, w;

vec_base();

vec_base(no_init_t);
vec_base(zero_init_t);
vec_base(unit_init_t);

explicit vec_base(T v);
vec_base(T x, T y, T z, T w);

vec_base(const vec_base& xy, T z, T w);
vec_base(T x, const vec_base& yz, T w);
vec_base(T x, T y, const vec_base& zw);
vec_base(const vec_base& xy, const vec_base& zw);

vec_base(const vec_base& xyz, T w);
vec_base(T x, const vec_base& yzw);

template < typename U > vec_base(const vec_base& other);

template < typename U > explicit vec_base(const U* p);
};

template < typename T, size_t Size >
class vec final : public vec_base {
public:
using self_type = vec;
using base_type = vec_base;
using component_type = T;

using pointer = component_type*;
using const_pointer = const component_type*;

using reference = component_type&;
using const_reference = const component_type&;

using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator;
using const_reverse_iterator = std::reverse_iterator;

static inline size_t size = Size;

void swap(vec& other);

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;

const_iterator cbegin() const;
const_iterator cend() const;
const_reverse_iterator crbegin() const;
const_reverse_iterator crend() const;

pointer data();
const_pointer data() const;

reference at(size_t index);
const_reference at(size_t index) const;

reference operator[](size_t index);
const_reference operator[](size_t index) const;
};

using bvec2 = vec;
using bvec3 = vec;
using bvec4 = vec;

using ivec2 = vec;
using ivec3 = vec;
using ivec4 = vec;

using uvec2 = vec;
using uvec3 = vec;
using uvec4 = vec;

using fvec2 = vec;
using fvec3 = vec;
using fvec4 = vec;

using dvec2 = vec;
using dvec3 = vec;
using dvec4 = vec;
```

### Matrix Types

```cpp
template < typename T, size_t Size >
class mat_base;

template < typename T >
class mat_base {
public:
using row_type = vec;
row_type rows[2];

mat_base();

mat_base(no_init_t);
mat_base(zero_init_t);
mat_base(unit_init_t);
mat_base(identity_init_t);

explicit mat_base(T d);
explicit mat_base(const row_type& d);

mat_base(
T m11, T m12,
T m21, T m22);

mat_base(
const row_type& row0,
const row_type& row1);

template < typename U > mat_base(const mat_base& other);
template < typename U > explicit mat_base(const mat_base& other);
template < typename U > explicit mat_base(const mat_base& other);

template < typename U > explicit mat_base(const U* p);
};

template < typename T >
class mat_base {
public:
using row_type = vec;
row_type rows[3];

mat_base();

mat_base(no_init_t);
mat_base(zero_init_t);
mat_base(unit_init_t);
mat_base(identity_init_t);

explicit mat_base(T d);
explicit mat_base(const row_type& d);

mat_base(
T m11, T m12, T m13,
T m21, T m22, T m23,
T m31, T m32, T m33);

mat_base(
const row_type& row0,
const row_type& row1,
const row_type& row2);

mat_base(
const mat_base& m,
const vec_base& v);

template < typename U > mat_base(const mat_base& other);
template < typename U > explicit mat_base(const mat_base& other);
template < typename U > explicit mat_base(const mat_base& other);

template < typename U > explicit mat_base(const U* p);
};

template < typename T >
class mat_base {
public:
using row_type = vec;
row_type rows[4];

mat_base();

mat_base(no_init_t);
mat_base(zero_init_t);
mat_base(unit_init_t);
mat_base(identity_init_t);

explicit mat_base(T d);
explicit mat_base(const row_type& d);

mat_base(
T m11, T m12, T m13, T m14,
T m21, T m22, T m23, T m24,
T m31, T m32, T m33, T m34,
T m41, T m42, T m43, T m44);

mat_base(
const row_type& row0,
const row_type& row1,
const row_type& row2,
const row_type& row3);

mat_base(
const mat_base& m,
const vec_base& v);

template < typename U > mat_base(const mat_base& other);
template < typename U > explicit mat_base(const mat_base& other);
template < typename U > explicit mat_base(const mat_base& other);

template < typename U > explicit mat_base(const U* p);
};

template < typename T, size_t Size >
class mat final : public mat_base {
public:
using self_type = mat;
using base_type = mat_base;
using component_type = T;

using row_type = vec;

using pointer = row_type*;
using const_pointer = const row_type*;

using reference = row_type&;
using const_reference = const row_type&;

using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator;
using const_reverse_iterator = std::reverse_iterator;

static inline size_t size = Size;

void swap(mat& other);

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;

const_iterator cbegin() const;
const_iterator cend() const;
const_reverse_iterator crbegin() const;
const_reverse_iterator crend() const;

pointer data();
const_pointer data() const;

reference at(size_t index);
const_reference at(size_t index) const;

reference operator[](size_t index);
const_reference operator[](size_t index) const;
};

using bmat2 = mat;
using bmat3 = mat;
using bmat4 = mat;

using imat2 = mat;
using imat3 = mat;
using imat4 = mat;

using umat2 = mat;
using umat3 = mat;
using umat4 = mat;

using fmat2 = mat;
using fmat3 = mat;
using fmat4 = mat;

using dmat2 = mat;
using dmat3 = mat;
using dmat4 = mat;
```

### Quaternion Types

```cpp
template < typename T >
class qua_base {
public:
vec v;
T s;

qua_base();

qua_base(no_init_t);
qua_base(zero_init_t);
qua_base(identity_init_t);

qua_base(T vx, T vy, T vz, T s);
qua_base(const vec& v, T s);
explicit qua_base(const vec& vs);

template < typename U > qua_base(const qua_base& other);
template < typename U > explicit operator vec() const;

template < typename U > explicit qua_base(const U* p);
};

template < typename T >
class qua final {
public:
using self_type = qua;
using base_type = qua_base;
using component_type = T;

using imag_type = vec;
using real_type = T;

using pointer = component_type*;
using const_pointer = const component_type*;

using reference = component_type&;
using const_reference = const component_type&;

using iterator = pointer;
using const_iterator = const_pointer;
using reverse_iterator = std::reverse_iterator;
using const_reverse_iterator = std::reverse_iterator;

static inline size_t size = 4;

void swap(qua& other);

iterator begin();
const_iterator begin() const;
iterator end();
const_iterator end() const;

reverse_iterator rbegin();
const_reverse_iterator rbegin() const;
reverse_iterator rend();
const_reverse_iterator rend() const;

const_iterator cbegin() const;
const_iterator cend() const;
const_reverse_iterator crbegin() const;
const_reverse_iterator crend() const;

pointer data();
const_pointer data() const;

reference at(size_t index);
const_reference at(size_t index) const;

reference operator[](size_t index);
const_reference operator[](size_t index) const;
};

using fqua = qua;
using dqua = qua;
```

### Vector Operators

```cpp
// +operator

template < typename T, size_t Size >
auto operator+(const vec& xs);

// -operator

template < typename T, size_t Size >
auto operator-(const vec& xs);

// ~operator

template < typename T, size_t Size >
auto operator~(const vec& xs);

// !operator

template < typename T, size_t Size >
auto operator!(const vec& xs);

// ++operator

template < typename T, size_t Size >
vec& operator++(vec& xs);

// --operator

template < typename T, size_t Size >
vec& operator--(vec& xs);

// operator++

template < typename T, size_t Size >
vec operator++(vec& xs, int);

// operator--

template < typename T, size_t Size >
vec operator--(vec& xs, int);

// operator+

template < typename T, typename U, size_t Size >
auto operator+(const vec& xs, U y);

template < typename T, typename U, size_t Size >
auto operator+(T x, const vec& ys);

template < typename T, typename U, size_t Size >
auto operator+(const vec& xs, const vec& ys);

// operator+=

template < typename T, size_t Size >
vec& operator+=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator+=(vec& xs, const vec& ys);

// operator-

template < typename T, typename U, size_t Size >
auto operator-(const vec& xs, U y);

template < typename T, typename U, size_t Size >
auto operator-(T x, const vec& ys);

template < typename T, typename U, size_t Size >
auto operator-(const vec& xs, const vec& ys);

// operator-=

template < typename T, size_t Size >
vec& operator-=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator-=(vec& xs, const vec& ys);

// operator*

template < typename T, typename U, size_t Size >
auto operator*(const vec& xs, U y);

template < typename T, typename U, size_t Size >
auto operator*(T x, const vec& ys);

template < typename T, typename U, size_t Size >
auto operator*(const vec& xs, const vec& ys);

// operator*=

template < typename T, size_t Size >
vec& operator*=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator*=(vec& xs, const vec& ys);

// operator/

template < typename T, typename U, size_t Size >
auto operator/(const vec& xs, U y);

template < typename T, typename U, size_t Size >
auto operator/(T x, const vec& ys);

template < typename T, typename U, size_t Size >
auto operator/(const vec& xs, const vec& ys);

// operator/=

template < typename T, size_t Size >
vec& operator/=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator/=(vec& xs, const vec& ys);

// operator&

template < typename T, size_t Size >
auto operator&(const vec& xs, T y);

template < typename T, size_t Size >
auto operator&(T x, const vec& ys);

template < typename T, size_t Size >
auto operator&(const vec& xs, const vec& ys);

// operator&=

template < typename T, size_t Size >
vec& operator&=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator&=(vec& xs, const vec& ys);

// operator|

template < typename T, size_t Size >
auto operator|(const vec& xs, T y);

template < typename T, size_t Size >
auto operator|(T x, const vec& ys);

template < typename T, size_t Size >
auto operator|(const vec& xs, const vec& ys);

// operator|=

template < typename T, size_t Size >
vec& operator|=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator|=(vec& xs, const vec& ys);

// operator^

template < typename T, size_t Size >
auto operator^(const vec& xs, T y);

template < typename T, size_t Size >
auto operator^(T x, const vec& ys);

template < typename T, size_t Size >
auto operator^(const vec& xs, const vec& ys);

// operator^=

template < typename T, size_t Size >
vec& operator^=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator^=(vec& xs, const vec& ys);

// operator<<

template < typename T, size_t Size >
auto operator<<(const vec& xs, T y);

template < typename T, size_t Size >
auto operator<<(T x, const vec& ys);

template < typename T, size_t Size >
auto operator<<(const vec& xs, const vec& ys);

// operator<<=

template < typename T, size_t Size >
vec& operator<<=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator<<=(vec& xs, const vec& ys);

// operator>>

template < typename T, size_t Size >
auto operator>>(const vec& xs, T y);

template < typename T, size_t Size >
auto operator>>(T x, const vec& ys);

template < typename T, size_t Size >
auto operator>>(const vec& xs, const vec& ys);

// operator>>=

template < typename T, size_t Size >
vec& operator>>=(vec& xs, T y);

template < typename T, size_t Size >
vec& operator>>=(vec& xs, const vec& ys);

// operator&&

template < typename T, size_t Size >
auto operator&&(const vec& xs, T y);

template < typename T, size_t Size >
auto operator&&(T x, const vec& ys);

template < typename T, size_t Size >
auto operator&&(const vec& xs, const vec& ys);

// operator||

template < typename T, size_t Size >
auto operator||(const vec& xs, T y);

template < typename T, size_t Size >
auto operator||(T x, const vec& ys);

template < typename T, size_t Size >
auto operator||(const vec& xs, const vec& ys);

// operator==

template < typename T, size_t Size >
bool operator==(const vec& xs, const vec& ys);

// operator!=

template < typename T, size_t Size >
bool operator!=(const vec& xs, const vec& ys);

// operator<

template < typename T, size_t Size >
bool operator<(const vec& xs, const vec& ys);
```

### Matrix Operators

```cpp
// +operator

template < typename T, size_t Size >
auto operator+(const mat& xs);

// -operator

template < typename T, size_t Size >
auto operator-(const mat& xs);

// ~operator

template < typename T, size_t Size >
auto operator~(const mat& xs);

// !operator

template < typename T, size_t Size >
auto operator!(const mat& xs);

// ++operator

template < typename T, size_t Size >
mat& operator++(mat& xs);

// --operator

template < typename T, size_t Size >
mat& operator--(mat& xs);

// operator++

template < typename T, size_t Size >
mat operator++(mat& xs, int);

// operator--

template < typename T, size_t Size >
mat operator--(mat& xs, int);

// operator+

template < typename T, typename U, size_t Size >
auto operator+(const mat& xs, U y);

template < typename T, typename U, size_t Size >
auto operator+(T x, const mat& ys);

template < typename T, typename U, size_t Size >
auto operator+(const mat& xs, const mat& ys);

// operator+=

template < typename T, size_t Size >
mat& operator+=(mat& xs, T y);

template < typename T, size_t Size >
mat& operator+=(mat& xs, const mat& ys);

// operator-

template < typename T, typename U, size_t Size >
auto operator-(const mat& xs, U y);

template < typename T, typename U, size_t Size >
auto operator-(T x, const mat& ys);

template < typename T, typename U, size_t Size >
auto operator-(const mat& xs, const mat& ys);

// operator-=

template < typename T, size_t Size >
mat& operator-=(mat& xs, T y);

template < typename T, size_t Size >
mat& operator-=(mat& xs, const mat& ys);

// operator*

template < typename T, typename U, size_t Size >
auto operator*(const mat& xs, U y);

template < typename T, typename U, size_t Size >
auto operator*(T x, const mat& ys);

template < typename T, typename U, size_t Size >
auto operator*(const vec& xs, const mat& ys);

template < typename T, typename U, size_t Size >
auto operator*(const mat& xs, const mat& ys);

// operator*=

template < typename T, size_t Size >
mat& operator*=(mat& xs, T y);

template < typename T, size_t Size >
vec& operator*=(vec& xs, const mat& ys);

template < typename T, size_t Size >
mat& operator*=(mat& xs, const mat& ys);

// operator/

template < typename T, typename U, size_t Size >
auto operator/(const mat& xs, U y);

template < typename T, typename U, size_t Size >
auto operator/(T x, const mat& ys);

// operator/=

template < typename T, size_t Size >
mat& operator/=(mat& xs, T y);

// operator&

template < typename T, size_t Size >
auto operator&(const mat& xs, T y);

template < typename T, size_t Size >
auto operator&(T x, const mat& ys);

template < typename T, size_t Size >
auto operator&(const mat& xs, const mat& ys);

// operator&=

template < typename T, size_t Size >
mat& operator&=(mat& xs, T y);

template < typename T, size_t Size >
mat& operator&=(mat& xs, const mat& ys);

// operator|

template < typename T, size_t Size >
auto operator|(const mat& xs, T y);

template < typename T, size_t Size >
auto operator|(T x, const mat& ys);

template < typename T, size_t Size >
auto operator|(const mat& xs, const mat& ys);

// operator|=

template < typename T, size_t Size >
mat