src/maths.h

Tue, 27 Jul 2021 13:23:34 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Tue, 27 Jul 2021 13:23:34 +0300
changeset 117
121a40d5e34c
parent 55
cb81ecb5fb23
child 120
8c9fff699241
permissions
-rw-r--r--

Add vertex map

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 20
diff changeset
18
20
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include <cmath>
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
21 #include "utility.h"
20
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 namespace math
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 {
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 using std::abs;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 using std::sqrt;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 using std::sin;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 using std::cos;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 using std::tan;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 using std::atan;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 using std::atan2;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 using std::acos;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 using std::asin;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 using std::exp;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 using std::log;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 using std::log10;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 using std::hypot;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 using std::floor;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 using std::ceil;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 using std::trunc;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 using std::round;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 template<typename T, typename... Rest>
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 inline auto hypot(T&& x, Rest&&... rest)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 return math::hypot(x, math::hypot(rest...));
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 template<typename T, typename... Rest>
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 const T& max(const T& x, const T& y)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 if (x > y)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 return x;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 else
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 return y;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 }
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 template<typename T, typename... Rest>
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 const T& max(const T& x, const T& y, Rest&&... rest)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 {
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 return math::max(x, math::max(y, rest...));
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 }
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 template<typename T, typename... Rest>
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 const T& min(const T& x, const T& y)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 if (x < y)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 return x;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 else
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 return y;
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 }
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 template<typename T, typename... Rest>
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 const T& min(const T& x, const T& y, Rest&&... rest)
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 {
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 return math::min(x, math::min(y, rest...));
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 constexpr double infinity = std::numeric_limits<double>::infinity();
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 constexpr long double pi = M_PIl;
26
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
75
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
76 /*
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
77 * Returns the empty sum. (recursion base)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
78 */
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
79 template<typename T>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
80 constexpr T sum()
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
81 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
82 return {};
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
83 }
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
84
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
85 /*
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
86 * Returns the sum of n arguments.
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
87 */
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
88 template<typename T, typename... Rest>
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
89 constexpr auto sum(const T& arg, Rest&&... rest)
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
90 {
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
91 return arg + sum<T>(rest...);
3a9e761e4faa at least VAOs work now
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
92 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
93
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 33
diff changeset
94 std::optional<glm::vec3> linePlaneIntersection();
20
cef43609a374 fixed build
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
96
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
97 inline unsigned int qHash(const glm::vec3& key)
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
98 {
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
99 return qHash(key.x) ^ utility::rotl10(qHash(key.y)) ^ utility::rotl20(qHash(key.z));
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 26
diff changeset
100 }

mercurial