-
Notifications
You must be signed in to change notification settings - Fork 2
/
Math.hpp
57 lines (48 loc) · 985 Bytes
/
Math.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#pragma once
#include <algorithm>
#include <cmath>
namespace Pvl {
const float PI = M_PI;
template <typename T>
T sqr(const T& value) {
return value * value;
}
template <typename T>
int sign(const T& value) {
if (value > 0) {
return 1;
} else if (value < 0) {
return -1;
} else {
return 0;
}
}
template <typename T, int N>
struct Pow;
template <typename T>
struct Pow<T, 2> {
T operator()(const T& value) {
return value * value;
}
};
template <typename T>
struct Pow<T, 3> {
T operator()(const T& value) {
return value * value * value;
}
};
template <typename T>
struct Pow<T, 4> {
T operator()(const T& value) {
return sqr(value * value);
}
};
template <int N, typename T>
T pow(const T& value) {
return Pow<T, N>()(value);
}
template <typename T>
T clamp(const T& value, const T& lower, const T& upper) {
return std::min(std::max(value, lower), upper);
}
} // namespace Pvl