fennec
Loading...
Searching...
No Matches
geometric.h
Go to the documentation of this file.
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
30
31
32#ifndef FENNEC_MATH_GEOMETRIC_H
33#define FENNEC_MATH_GEOMETRIC_H
34
35
109
110#include <fennec/math/vector.h>
111#include <fennec/math/common.h>
113
114namespace fennec
115{
116
117
118// dot -----------------------------------------------------------------------------------------------------------------
119
132template<typename genType, size_t...i>
133constexpr genType dot(const vector<genType, i...>& x, const vector<genType, i...>& y) {
134 return ((x[i] * y[i]) + ...);
135}
136
137
138// length2 -------------------------------------------------------------------------------------------------------------
139
150template<typename genType, size_t...i>
151constexpr genType length2(const vector<genType, i...>& x) {
152 return fennec::dot(x, x);
153}
154
155
156// length --------------------------------------------------------------------------------------------------------------
157
168template<typename genType, size_t...i>
169constexpr genType length(const vector<genType, i...>& x) {
170 return fennec::sqrt(fennec::length2(x));
171}
172
173
174// distance ------------------------------------------------------------------------------------------------------------
175
188template<typename genType, size_t...i>
189constexpr genType distance(const vector<genType, i...>& p0, const vector<genType, i...>& p1) {
190 return fennec::length(p1 - p0);
191}
192
193
194// cross ---------------------------------------------------------------------------------------------------------------
195
210template<typename genType, size_t...i> requires(sizeof...(i) == 3)
211constexpr vector<genType, i...> cross(const vector<genType, i...>& x, const vector<genType, i...>& y) {
212 return vector<genType, i...>(x[1]*y[2]-y[1]*x[2], x[2]*y[0]-y[2]*x[0], x[0]*y[1]-y[0]*x[1]);
213}
214
215
216// normalize -----------------------------------------------------------------------------------------------------------
217
228template<typename genType, size_t...i>
229constexpr vector<genType, i...> normalize(const vector<genType, i...>& x) {
230 return x * fennec::inversesqrt(fennec::dot(x, x));
231}
232
233
234// faceforward ---------------------------------------------------------------------------------------------------------
235
244template<typename genType, size_t...i>
245constexpr vector<genType, i...> faceforward(const vector<genType, i...>& N, const vector<genType, i...>& I, const vector<genType, i...>& Nref) {
246 return fennec::sign(fennec::dot(Nref, I)) * N;
247}
248
249
250// reflect -------------------------------------------------------------------------------------------------------------
251
261template<typename genType, size_t...i>
262constexpr vector<genType, i...> reflect(const vector<genType, i...>& I, const vector<genType, i...>& N) {
263 return I - genType(2.0) * fennec::dot(N, I) * N;
264}
265
266// refract -------------------------------------------------------------------------------------------------------------
267
280template<typename genType, size_t...i>
281constexpr vector<genType, i...> refract(const vector<genType, i...>& I, const vector<genType, i...>& N, genType eta) {
282 genType ndi = fennec::dot(N, I);
283 genType k = genType(1.0) - eta * eta * (genType(1.0) - ndi * ndi);
284 if (k < 0) return vector<genType, i...>(0);
285 return eta * I - N * (eta * ndi + fennec::sqrt(k));
286}
287
288}
289
290#endif // FENNEC_MATH_GEOMETRIC_H
Exponential
constexpr genType inversesqrt(genType x)
Returns .
Definition exponential.h:192
constexpr genType sqrt(genType x)
Returns .
Definition exponential.h:178
constexpr vector< genType, i... > faceforward(const vector< genType, i... > &N, const vector< genType, i... > &I, const vector< genType, i... > &Nref)
If return , otherwise return .
Definition geometric.h:245
constexpr genType distance(const vector< genType, i... > &p0, const vector< genType, i... > &p1)
Returns the length of vector , i.e., .
Definition geometric.h:189
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
constexpr genType length2(const vector< genType, i... > &x)
Returns the squared length of vector , i.e., .
Definition geometric.h:151
constexpr vector< genType, i... > cross(const vector< genType, i... > &x, const vector< genType, i... > &y)
Returns the cross product of and , i.e., .
Definition geometric.h:211
constexpr vector< genType, i... > reflect(const vector< genType, i... > &I, const vector< genType, i... > &N)
For the incident vector and surface orientation , returns the reflection direction.
Definition geometric.h:262
constexpr vector< genType, i... > refract(const vector< genType, i... > &I, const vector< genType, i... > &N, genType eta)
For the incident vector and surface normal , and the ratio of indices of refraction ,...
Definition geometric.h:281
constexpr genType length(const vector< genType, i... > &x)
Returns the length of vector , i.e., .
Definition geometric.h:169
constexpr vector< genType, i... > normalize(const vector< genType, i... > &x)
Returns a vector in the same direction as , but with a length of , i.e.
Definition geometric.h:229
Common
constexpr genType sign(genType x)
Returns if , if , or if .
Definition common.h:306
constexpr genType y()
Definition constants.h:672
Math Vector Type.
Definition vector.h:183
the Vectors