fennec
Loading...
Searching...
No Matches
common.h
1// =====================================================================================================================
2// fennec, a free and open source game engine
3// Copyright © 2025 Medusa Slockbower
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17// =====================================================================================================================
18
19#ifndef FENNEC_MATH_EXT_COMMON_H
20#define FENNEC_MATH_EXT_COMMON_H
21
22#include <fennec/lang/limits.h>
23
24#include <fennec/math/common.h>
26
27#include <fennec/math/ext/quaternion.h>
28
29namespace fennec
30{
31
32// Sign Functions ======================================================================================================
33
34template<typename genType>
35constexpr qua<genType> sign(const qua<genType>& x) {
36 return qua<genType>(
37 fennec::sign(x.w),
38 fennec::sign(x.x),
39 fennec::sign(x.y),
40 fennec::sign(x.z)
41 );
42}
43
44template<typename genType>
45constexpr qua<genType> abs(const qua<genType>& x) {
46 return qua<genType>(
47 x.w * fennec::sign(x.w),
48 x.x * fennec::sign(x.x),
49 x.y * fennec::sign(x.y),
50 x.z * fennec::sign(x.z)
51 );
52}
53
54
55// Rounding Functions ==================================================================================================
56
57template<typename genType>
58constexpr qua<genType> floor(const qua<genType>& x) {
59 return qua<genType>(
60 fennec::floor(x.w),
61 fennec::floor(x.x),
62 fennec::floor(x.y),
63 fennec::floor(x.z)
64 );
65}
66
67template<typename genType>
68constexpr qua<genType> ceil(const qua<genType>& x) {
69 return qua<genType>(
70 fennec::ceil(x.w),
71 fennec::ceil(x.x),
72 fennec::ceil(x.y),
73 fennec::ceil(x.z)
74 );
75}
76
77template<typename genType>
78constexpr qua<genType> trunc(const qua<genType>& x) {
79 return qua<genType>(
80 fennec::trunc(x.w),
81 fennec::trunc(x.x),
82 fennec::trunc(x.y),
83 fennec::trunc(x.z)
84 );
85}
86
87template<typename genType>
88constexpr qua<genType> round(const qua<genType>& x) {
89 return qua<genType>(
90 fennec::round(x.w),
91 fennec::round(x.x),
92 fennec::round(x.y),
93 fennec::round(x.z)
94 );
95}
96
97template<typename genType>
98constexpr qua<genType> roundEven(const qua<genType>& x) {
99 return qua<genType>(
100 fennec::roundEven(x.w),
101 fennec::roundEven(x.x),
102 fennec::roundEven(x.y),
103 fennec::roundEven(x.z)
104 );
105}
106
107// Fractional Functions ================================================================================================
108
109template<typename genType>
110constexpr qua<genType> fract(const qua<genType>& x) {
111 return qua<genType>(
112 fennec::fract(x.w),
113 fennec::fract(x.x),
114 fennec::fract(x.y),
115 fennec::fract(x.z)
116 );
117}
118
119template<typename genType>
120constexpr qua<genType> mod(const qua<genType>& x) {
121 return qua<genType>(
122 fennec::mod(x.w),
123 fennec::mod(x.x),
124 fennec::mod(x.y),
125 fennec::mod(x.z)
126 );
127}
128
129template<typename genType>
130constexpr qua<genType> modf(const qua<genType>& x) {
131 return qua<genType>(
132 fennec::modf(x.w),
133 fennec::modf(x.x),
134 fennec::modf(x.y),
135 fennec::modf(x.z)
136 );
137}
138
139template<typename genType>
140constexpr qua<genType> isnan(const qua<genType>& x) {
141 return qua<genType>(
142 fennec::isnan(x.w),
143 fennec::isnan(x.x),
144 fennec::isnan(x.y),
145 fennec::isnan(x.z)
146 );
147}
148
149template<typename genType>
150constexpr qua<genType> isinf(const qua<genType>& x) {
151 return qua<genType>(
152 fennec::isinf(x.w),
153 fennec::isinf(x.x),
154 fennec::isinf(x.y),
155 fennec::isinf(x.z)
156 );
157}
158
159template<typename genType>
160constexpr qua<genType> fma(const qua<genType>& a, const qua<genType>& b, const qua<genType>& c) {
161 return qua<genType>(
162 fennec::fma(a.w, b.w, c.w),
163 fennec::fma(a.x, b.x, c.x),
164 fennec::fma(a.y, b.y, c.y),
165 fennec::fma(a.z, b.z, c.z)
166 );
167}
168
169
170// Comparison Functions ================================================================================================
171
172template<typename genType>
173constexpr qua<genType> min(const qua<genType>& x, const qua<genType>& y) {
174 return qua<genType>(
175 fennec::min(x.w, y.w),
176 fennec::min(x.x, y.x),
177 fennec::min(x.y, y.y),
178 fennec::min(x.z, y.z)
179 );
180}
181
182template<typename genType>
183constexpr qua<genType> max(const qua<genType>& x, const qua<genType>& y) {
184 return qua<genType>(
185 fennec::max(x.w, y.w),
186 fennec::max(x.x, y.x),
187 fennec::max(x.y, y.y),
188 fennec::max(x.z, y.z)
189 );
190}
191
192template<typename genType>
193constexpr qua<genType> clamp(const qua<genType>& x, genType minVal, genType maxVal) {
194 return qua<genType>(
195 fennec::clamp(x.w, minVal, maxVal),
196 fennec::clamp(x.x, minVal, maxVal),
197 fennec::clamp(x.y, minVal, maxVal),
198 fennec::clamp(x.z, minVal, maxVal)
199 );
200}
201
202template<typename genType>
203constexpr qua<genType> clamp(const qua<genType>& x, const qua<genType>& minVal, const qua<genType>& maxVal) {
204 return qua<genType>(
205 fennec::clamp(x.w, minVal.w, maxVal.w),
206 fennec::clamp(x.x, minVal.x, maxVal.x),
207 fennec::clamp(x.y, minVal.y, maxVal.y),
208 fennec::clamp(x.z, minVal.z, maxVal.z)
209 );
210}
211
212
213// Curves ==============================================================================================================
214
215template<typename genType>
216constexpr qua<genType> mix(const qua<genType>& x, const qua<genType>& y, genType a) {
217 genType cT = fennec::dot(x, y);
218 qua<genType> z = cT < genType(0) ? -y : y;
219 cT = cT < genType(0) ? -cT : cT;
220
221 if (cT > genType(1) - numeric_limits<genType>::epsilon()) {
222 return x * (genType(1) - a) + y * a;
223 }
224
225 genType t = fennec::acos(cT);
226 return (fennec::sin(x * (genType(1) - a) * t) + z * (fennec::sin(a * t))) / fennec::sin(t);
227}
228
229}
230
231
232#endif // FENNEC_MATH_EXT_COMMON_H
constexpr genType dot(const vector< genType, i... > &x, const vector< genType, i... > &y)
Returns the dot product of and , i.e., .
Definition geometric.h:133
Limits
Common
constexpr genType min(genType x, genType y)
Returns if otherwise it returns .
Definition common.h:688
constexpr genBType isnan(genType x)
Returns true if holds a NaN. Returns false otherwise.
Definition common.h:517
constexpr genType mod(genType x, genType y)
Modulus. Returns .
Definition common.h:479
constexpr genType max(genType x, genType y)
Returns if , otherwise it returns .
Definition common.h:705
constexpr genType trunc(genType x)
Returns a value equal to the nearest integer that is less than or equal to .
Definition common.h:399
constexpr genType roundEven(genType x)
Returns a value equal to the nearest integer. In C++, a fractional part of will always round to the ...
Definition common.h:418
constexpr genType ceil(genType x)
Returns a value equal to the nearest integer that is greater than or equal to .
Definition common.h:365
constexpr genType sign(genType x)
Returns if , if , or if .
Definition common.h:306
constexpr genType floor(genType x)
Returns a value equal to the nearest integer that is less than or equal to .
Definition common.h:348
constexpr genType mix(genType x, genType y, genType a)
Returns the linear blend of and , i.e., .
Definition common.h:799
constexpr genType fma(genType a, genType b, genType c)
Computes and returns .
Definition common.h:613
constexpr genType abs(genType x)
Returns if , otherwise it returns .
Definition common.h:322
constexpr genType fract(genType x)
Returns .
Definition common.h:462
constexpr genType clamp(genType x, genType minVal, genType maxVal)
Returns .
Definition common.h:723
constexpr genType round(genType x)
Returns a value equal to the nearest integer. In C++, a fractional part of will always round up.
Definition common.h:382
constexpr genBType isinf(genType x)
Returns true if holds a positive or negative infinity. Returns false otherwise.
Definition common.h:533
constexpr genType modf(genType x, genType &i)
Returns the fractional part of and stores the integral part in .
Definition common.h:496
constexpr genType y()
Definition constants.h:672
static constexpr TypeT epsilon()
Returns the difference between 1.0 and the next representable value.
Definition limits.h:255
the Trigonometry
constexpr genType acos(genType x)
Arc Cosine. Returns an angle whose cosine is /a x.
Definition trigonometric.h:268
constexpr genType sin(genType x)
The standard trigonometric sine.
Definition trigonometric.h:204