plugify 1.2.6
Loading...
Searching...
No Matches
numerics.hpp
1#pragma once
2
3#include "vector.hpp"
4#include "string.hpp"
5
6namespace plg {
7 PLUGIFY_WARN_PUSH()
8
9#if PLUGIFY_COMPILER_CLANG
10 PLUGIFY_WARN_IGNORE("-Wgnu-anonymous-struct")
11 PLUGIFY_WARN_IGNORE("-Wnested-anon-types")
12#elif PLUGIFY_COMPILER_GCC
13 PLUGIFY_WARN_IGNORE("-Wpedantic")
14#elif PLUGIFY_COMPILER_MSVC
15 PLUGIFY_WARN_IGNORE(4201)
16#endif
17
18 extern "C" {
19 struct vec2 {
20 union {
21 struct {
22 float x;
23 float y;
24 };
25 float data[2];
26 };
27 };
28
29 struct vec3 {
30 union {
31 struct {
32 float x;
33 float y;
34 float z;
35 };
36 float data[3];
37 };
38 };
39
40 struct vec4 {
41 union {
42 struct {
43 float x;
44 float y;
45 float z;
46 float w;
47 };
48 float data[4];
49 };
50 };
51
52 struct mat4x4 {
53 union {
54 struct {
55 float m00, m01, m02, m03;
56 float m10, m11, m12, m13;
57 float m20, m21, m22, m23;
58 float m30, m31, m32, m33;
59 };
60 float m[4][4];
61 float data[16];
62 };
63 };
64 }
65
66 PLUGIFY_WARN_POP()
67
68 constexpr float epsilon = 1e-5f;
69
70 // TODO: Replace by std::fabs in C++23
71 constexpr float fabs(float x) noexcept {
72 return x < 0 ? -x : x;
73 }
74
75 constexpr bool operator==(vec2 lhs, vec2 rhs) {
76 return fabs(lhs.x - rhs.x) < epsilon && fabs(lhs.y - rhs.y) < epsilon;
77 }
78
79 constexpr bool operator==(vec3 lhs, vec3 rhs) {
80 return fabs(lhs.x - rhs.x) < epsilon && fabs(lhs.y - rhs.y) < epsilon && fabs(lhs.z - rhs.z) < epsilon;
81 }
82
83 constexpr bool operator==(vec4 lhs, vec4 rhs) {
84 return fabs(lhs.x - rhs.x) < epsilon && fabs(lhs.y - rhs.y) < epsilon && fabs(lhs.z - rhs.z) < epsilon && fabs(lhs.w - rhs.w) < epsilon;
85 }
86
87 constexpr bool operator==(const mat4x4& lhs, const mat4x4& rhs) {
88 for (int i = 0; i < 16; ++i) {
89 if (fabs(lhs.data[i] - rhs.data[i]) > epsilon)
90 return false;
91 }
92 return true;
93 }
94} // namespace plg