Jackalope / jmath (public) (License: GPLv3 or later) (since 2018-10-11) (hash sha1)
C++ conxstexpr template Math library with:
- multidimensional vectors and matrices
- geometry primitives with projection, distance and intersection functions
- coordinate systems conversions
- some routines

Depends on GCE-math C++ library as git submodule.

Used with clang (version 9 or 10) and gcc (version 9).
Written with C++17.
List of commits:
Subject Hash Author Date (UTC)
removed unused matrix files 1bc1e845a6a391cce90be9023fae41937dbd100d jp.larry 2020-01-07 13:55:53
frustum data type 48480173a43561e7502458579378b78591716d1c jp.larry 2020-01-07 13:53:57
different projection matrix built functions for different fov specifications (horizontal, vertical, both) 0aa5dbb5e22c616a559bb6f6d75b0313db7b3e84 jp.larry 2020-01-07 13:53:30
normal from 3 points and plane from normal and offset functions 432070f339b8c48b18c4e2dab4389dc19ffa241c jp.larry 2020-01-06 07:27:29
check if floating point when converting from degrees 75397be78960535bca1ec487221b6b46382e0724 jp.larry 2020-01-06 07:26:59
distance between 2 vectors and intersection line with plane 1620677672179226ea92affb5e3dc600cd3fa133 jp.larry 2020-01-05 03:22:56
2D vector rotate function 049eae7f349cf44bde666b66684a47c8bb4f1300 jp.larry 2020-01-01 05:09:07
removed nodiscard from matrix *= operator and removed redundant function rotate(axis) 0dea084c205202125acfe390a248be61c962d393 jp.larry 2020-01-01 03:09:48
rafactoring update, matrix multiplication now works in different way 2f2e01d80896cd96428a9c9394382e6f040f92fb jp.larry 2019-12-31 11:00:01
round_down function 672a293659a39e29be0d84f28801cf2718070f10 Jackalope 2019-12-28 03:02:03
migw warning solved 80dec7906c5fe2dc026d6774fd526df2ded049cc Jackalope 2019-12-21 10:22:55
refactoring 8b9c8aef3bcbbe3a3a72160eacd15b2d8397524c jp.larry 2019-12-19 08:10:49
lookat and projection functions refactoring f37008f4ad0e4501e89a6a26b4270bc5d6080578 jp.larry 2019-12-16 10:58:53
gcem correct include 03e0350e04d3e1b2c9e7d4f58ef473ce9b5cf319 Your Name 2019-11-27 04:30:39
changed directories layout abcc3ff61e8246f0519498b755bfe23877626144 Your Name 2019-11-26 22:52:02
CMakeLists.txt added 41ddc92280ce3969f8b3f16efce7e63c8c70425a Your Name 2019-11-12 10:32:19
removed excess definition 67b8131470fbbf0d87f2f51cea6326fcdab8d180 Your Name 2019-07-07 12:24:10
gcem updated ff3d073843c2f3a153de930a3651ad8e02c1b7d1 jp.larry 2019-06-19 10:59:53
new functions 46561c6e97aa1ba2d0c41a19ba1977b0427b5f4b Your Name 2019-05-07 05:42:45
new functions 61e3c490daf394f07a4b9de2640d9e1b42fc2dd1 Your Name 2019-05-05 11:28:57
Commit 1bc1e845a6a391cce90be9023fae41937dbd100d - removed unused matrix files
Author: jp.larry
Author date (UTC): 2020-01-07 13:55
Committer name: jp.larry
Committer date (UTC): 2020-01-07 13:55
Parent(s): 48480173a43561e7502458579378b78591716d1c
Signing key:
Tree: 09a6ccda35d9a4315272b7f1f0b5c3e683b35401
File Lines added Lines deleted
include/math/matrix.h 1 1
include/math/matrix/matrix.h 0 10
include/math/matrix/matrix_functions.h 107 0
include/math/matrix/matrix_functions.hpp 0 107
File include/math/matrix.h changed (mode: 100644) (index e8d4e6a..0b2f0fb)
1 1 #pragma once #pragma once
2 2
3 3 #include "matrix/base.hpp" #include "matrix/base.hpp"
4 #include "matrix/matrix_functions.hpp"
4 #include "matrix/matrix_functions.h"
5 5 #include "matrix/type_defs.h" #include "matrix/type_defs.h"
File include/math/matrix/matrix.h deleted (index 349882e..0000000)
1 #pragma once
2
3 #include "base.h"
4 #include "matrix_functions.h"
5
6 #include "base.hpp"
7 #include "../vector/vector_functions.h"
8 #include "matrix_functions.hpp"
9
10 #include "type_defs.h"
File include/math/matrix/matrix_functions.h changed (mode: 100644) (index e69de29..51f69c3)
1 #pragma once
2
3 #include "base.h"
4 #include "../constants.h"
5 #include "../vector/vector_functions.h"
6
7 #include <math.h>
8
9 namespace math
10 {
11 #define T_IMPL template<typename T> [[nodiscard]] constexpr inline
12 #define MATRIX(n) matrix<n, n, T>
13 #define VECTOR(n) vector<n, T>
14 #define VEC_ARG(n) const vector<n, T>&
15 #define MATRIXN_IMP \
16 template<unsigned int size, typename T> [[nodiscard]] constexpr inline \
17 matrix<size, size, T>
18
19 MATRIXN_IMP scale(T scale)
20 {
21 matrix<size, size, T> m{};
22 for (unsigned int i = 0; i < size; ++i)
23 m._[i][i] = scale;
24 return m;
25 }
26 MATRIXN_IMP scale(T n_scale[size])
27 {
28 matrix<size, size, T> m{};
29 for (unsigned int i = 0; i < size; ++i)
30 m._[i][i] = n_scale[i];
31 }
32 T_IMPL MATRIX(4)
33 matrix_3x4(VEC_ARG(3) x, VEC_ARG(3) y, VEC_ARG(3) z, VEC_ARG(3) w = {}) {
34 return {{{ x.x, y.x, z.x, 0 },
35 { x.y, y.y, z.y, 0 },
36 { x.z, y.z, z.z, 0 },
37 { w.x, w.y, w.z, 1 }}};
38 }
39 T_IMPL MATRIX(4) // x, y, z are normalized axis-directions
40 look_at(VEC_ARG(3) x, VEC_ARG(3) y, VEC_ARG(3) z, VEC_ARG(3) pos = {}) {
41 const VECTOR(3) w = { - dot(x, pos), - dot(y, pos), - dot(z, pos) };
42 return matrix_3x4(x, y, z, w);
43 }
44 T_IMPL MATRIX(4)
45 translation(VEC_ARG(3) pos) {
46 auto mat = scale<4, T>(1);
47 mat[3].template part<0,3>() = pos;
48 return mat;
49 }
50 T_IMPL MATRIX(3)
51 rotation(const vector<3, vector<3, T>> &axis) {
52 return rotation(axis.x, axis.y, axis.z);
53 }
54 T_IMPL MATRIX(4)
55 projection(const vector<2, T> scale, T z_near, T z_far) {
56 const T z_scale = z_far / (z_far - z_near);
57 const T z_shift = - z_scale * z_near;
58 return {{{ scale.x, 0, 0, 0 },
59 { 0, scale.y, 0, 0 },
60 { 0, 0, z_scale, 1 },
61 { 0, 0, z_shift, 0 }}};
62 }
63 T_IMPL MATRIX(4)
64 projection_fov_y(T fov_y, T aspect_x_to_y, T z_near, T z_far) {
65 const T y_scale = 1 / std::tan(fov_y / 2);
66 const T x_scale = y_scale / aspect_x_to_y;
67 return projection({x_scale, y_scale}, z_near, z_far);
68 }
69 T_IMPL MATRIX(4)
70 projection_fov_x(T fov_x, T aspect_x_to_y, T z_near, T z_far) {
71 const T x_scale = 1 / std::tan(fov_x / 2);
72 const T y_scale = x_scale * aspect_x_to_y;
73 return projection({x_scale, y_scale}, z_near, z_far);
74 }
75 T_IMPL MATRIX(4)
76 projection_fov_xy(T fov_x, T fov_y, T z_near, T z_far) {
77 const T x_scale = 1 / std::tan(fov_x / 2);
78 const T y_scale = 1 / std::tan(fov_y / 2);
79 return projection({x_scale, y_scale}, z_near, z_far);
80 }
81
82 T_IMPL MATRIX(3)
83 rotation(const VECTOR(3)& unit, T radians)
84 {
85 auto &x = unit._[0], &y = unit._[1], &z = unit._[2];
86 auto c = gcem::cos(radians), s = gcem::sin(radians), t = math::one<T> - c;
87 return {{{ t*x*x + c, t*x*y - s*z, t*x*z + s*y },
88 { t*x*y + s*z, t*y*y + c, t*y*z - s*x },
89 { t*x*z - s*y, t*y*z + s*x, t*z*z + c }}};
90 }
91 T_IMPL MATRIX(3)
92 rotation(T x_angle, T y_angle, T z_angle)
93 {
94 T cosX = gcem::cos<T>(x_angle), sinX = gcem::sin<T>(x_angle);
95 T cosY = gcem::cos<T>(y_angle), sinY = gcem::sin<T>(y_angle);
96 T cosZ = gcem::cos<T>(z_angle), sinZ = gcem::sin<T>(z_angle);
97
98 return {{{ cosY*cosZ, -cosX*sinZ + sinX*sinY*cosZ, sinX*sinZ + cosX*sinY*cosZ },
99 { cosY*sinZ, cosX*cosZ + sinX*sinY*sinZ, -sinX*cosZ + cosX*sinY*sinZ },
100 { -sinY, sinX*cosY, cosX*cosY }}};
101 }
102
103 #undef MATRIXN_IMP
104 #undef T_IMPL
105 #undef VECTOR
106 #undef MATRIX
107 }
File include/math/matrix/matrix_functions.hpp deleted (index 51f69c3..0000000)
1 #pragma once
2
3 #include "base.h"
4 #include "../constants.h"
5 #include "../vector/vector_functions.h"
6
7 #include <math.h>
8
9 namespace math
10 {
11 #define T_IMPL template<typename T> [[nodiscard]] constexpr inline
12 #define MATRIX(n) matrix<n, n, T>
13 #define VECTOR(n) vector<n, T>
14 #define VEC_ARG(n) const vector<n, T>&
15 #define MATRIXN_IMP \
16 template<unsigned int size, typename T> [[nodiscard]] constexpr inline \
17 matrix<size, size, T>
18
19 MATRIXN_IMP scale(T scale)
20 {
21 matrix<size, size, T> m{};
22 for (unsigned int i = 0; i < size; ++i)
23 m._[i][i] = scale;
24 return m;
25 }
26 MATRIXN_IMP scale(T n_scale[size])
27 {
28 matrix<size, size, T> m{};
29 for (unsigned int i = 0; i < size; ++i)
30 m._[i][i] = n_scale[i];
31 }
32 T_IMPL MATRIX(4)
33 matrix_3x4(VEC_ARG(3) x, VEC_ARG(3) y, VEC_ARG(3) z, VEC_ARG(3) w = {}) {
34 return {{{ x.x, y.x, z.x, 0 },
35 { x.y, y.y, z.y, 0 },
36 { x.z, y.z, z.z, 0 },
37 { w.x, w.y, w.z, 1 }}};
38 }
39 T_IMPL MATRIX(4) // x, y, z are normalized axis-directions
40 look_at(VEC_ARG(3) x, VEC_ARG(3) y, VEC_ARG(3) z, VEC_ARG(3) pos = {}) {
41 const VECTOR(3) w = { - dot(x, pos), - dot(y, pos), - dot(z, pos) };
42 return matrix_3x4(x, y, z, w);
43 }
44 T_IMPL MATRIX(4)
45 translation(VEC_ARG(3) pos) {
46 auto mat = scale<4, T>(1);
47 mat[3].template part<0,3>() = pos;
48 return mat;
49 }
50 T_IMPL MATRIX(3)
51 rotation(const vector<3, vector<3, T>> &axis) {
52 return rotation(axis.x, axis.y, axis.z);
53 }
54 T_IMPL MATRIX(4)
55 projection(const vector<2, T> scale, T z_near, T z_far) {
56 const T z_scale = z_far / (z_far - z_near);
57 const T z_shift = - z_scale * z_near;
58 return {{{ scale.x, 0, 0, 0 },
59 { 0, scale.y, 0, 0 },
60 { 0, 0, z_scale, 1 },
61 { 0, 0, z_shift, 0 }}};
62 }
63 T_IMPL MATRIX(4)
64 projection_fov_y(T fov_y, T aspect_x_to_y, T z_near, T z_far) {
65 const T y_scale = 1 / std::tan(fov_y / 2);
66 const T x_scale = y_scale / aspect_x_to_y;
67 return projection({x_scale, y_scale}, z_near, z_far);
68 }
69 T_IMPL MATRIX(4)
70 projection_fov_x(T fov_x, T aspect_x_to_y, T z_near, T z_far) {
71 const T x_scale = 1 / std::tan(fov_x / 2);
72 const T y_scale = x_scale * aspect_x_to_y;
73 return projection({x_scale, y_scale}, z_near, z_far);
74 }
75 T_IMPL MATRIX(4)
76 projection_fov_xy(T fov_x, T fov_y, T z_near, T z_far) {
77 const T x_scale = 1 / std::tan(fov_x / 2);
78 const T y_scale = 1 / std::tan(fov_y / 2);
79 return projection({x_scale, y_scale}, z_near, z_far);
80 }
81
82 T_IMPL MATRIX(3)
83 rotation(const VECTOR(3)& unit, T radians)
84 {
85 auto &x = unit._[0], &y = unit._[1], &z = unit._[2];
86 auto c = gcem::cos(radians), s = gcem::sin(radians), t = math::one<T> - c;
87 return {{{ t*x*x + c, t*x*y - s*z, t*x*z + s*y },
88 { t*x*y + s*z, t*y*y + c, t*y*z - s*x },
89 { t*x*z - s*y, t*y*z + s*x, t*z*z + c }}};
90 }
91 T_IMPL MATRIX(3)
92 rotation(T x_angle, T y_angle, T z_angle)
93 {
94 T cosX = gcem::cos<T>(x_angle), sinX = gcem::sin<T>(x_angle);
95 T cosY = gcem::cos<T>(y_angle), sinY = gcem::sin<T>(y_angle);
96 T cosZ = gcem::cos<T>(z_angle), sinZ = gcem::sin<T>(z_angle);
97
98 return {{{ cosY*cosZ, -cosX*sinZ + sinX*sinY*cosZ, sinX*sinZ + cosX*sinY*cosZ },
99 { cosY*sinZ, cosX*cosZ + sinX*sinY*sinZ, -sinX*cosZ + cosX*sinY*sinZ },
100 { -sinY, sinX*cosY, cosX*cosY }}};
101 }
102
103 #undef MATRIXN_IMP
104 #undef T_IMPL
105 #undef VECTOR
106 #undef MATRIX
107 }
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/Jackalope/jmath

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/Jackalope/jmath

Clone this repository using git:
git clone git://git.rocketgit.com/user/Jackalope/jmath

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main