-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloat3.cpp
More file actions
86 lines (65 loc) · 2.07 KB
/
float3.cpp
File metadata and controls
86 lines (65 loc) · 2.07 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#include "float3.h"
#include <algorithm>
template<class T> constexpr T Sign(const T v) { return ((v > T(0)) * T(2) - T(1)); }
template<class T> inline bool epscmp(const T a, const T b, const T eps) {
return ((a == b) || (std::fabs(a - b) <= (eps * (T(1) + std::fabs(a) + std::fabs(b)))));
}
//! gets initialized later when the map is loaded
float float3::maxxpos = -1.0f;
float float3::maxzpos = -1.0f;
bool float3::IsInBounds() const
{
assert(maxxpos > 0.0f); // check if initialized
return ((x >= 0.0f && x <= maxxpos) && (z >= 0.0f && z <= maxzpos));
}
void float3::ClampInBounds()
{
assert(maxxpos > 0.0f); // check if initialized
x = std::clamp(x, 0.0f, maxxpos);
z = std::clamp(z, 0.0f, maxzpos);
}
bool float3::IsInMap() const
{
assert(maxxpos > 0.0f); // check if initialized
return ((x >= 0.0f && x <= maxxpos + 1) && (z >= 0.0f && z <= maxzpos + 1));
}
void float3::ClampInMap()
{
assert(maxxpos > 0.0f); // check if initialized
x = std::clamp(x, 0.0f, maxxpos + 1);
z = std::clamp(z, 0.0f, maxzpos + 1);
}
float3 float3::min(const float3 v1, const float3 v2)
{
return {std::min(v1.x, v2.x), std::min(v1.y, v2.y), std::min(v1.z, v2.z)};
}
float3 float3::max(const float3 v1, const float3 v2)
{
return {std::max(v1.x, v2.x), std::max(v1.y, v2.y), std::max(v1.z, v2.z)};
}
float3 float3::fabs(const float3 v)
{
return {std::fabs(v.x), std::fabs(v.y), std::fabs(v.z)};
}
float3 float3::sign(const float3 v)
{
return {Sign(v.x), Sign(v.y), Sign(v.z)};
}
bool float3::equals(const float3& f, const float3& eps) const
{
return (epscmp(x, f.x, eps.x) && epscmp(y, f.y, eps.y) && epscmp(z, f.z, eps.z));
}
float3 float3::snapToAxis() const {
// https://gamedev.stackexchange.com/questions/83601/from-3d-rotation-snap-to-nearest-90-directions/183342#183342
float nx = std::abs(x);
float ny = std::abs(y);
float nz = std::abs(z);
if (nx > ny && nx > nz) {
return float3(Sign(x), 0, 0);
} else if (ny > nx && ny > nz) {
return float3(0, Sign(y), 0);
} else {
return float3(0, 0, Sign(z));
}
}