forked from vsg-dev/VulkanSceneGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec2.h
More file actions
174 lines (141 loc) · 5.16 KB
/
vec2.h
File metadata and controls
174 lines (141 loc) · 5.16 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#pragma once
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
// we can't implement the anonymous union/structs combination without causing warnings, so disabled them for just this header
#if defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wpedantic"
#endif
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
# pragma clang diagnostic ignored "-Wnested-anon-types"
#endif
#include <vsg/core/type_name.h>
#include <cmath>
namespace vsg
{
template<typename T>
struct t_vec2
{
using value_type = T;
union
{
value_type value[2];
struct
{
value_type x, y;
};
struct
{
value_type r, g;
};
struct
{
value_type s, t;
};
};
constexpr t_vec2() :
value{} {}
constexpr t_vec2(const t_vec2& v) :
value{v.x, v.y} {}
constexpr t_vec2(value_type in_x, value_type in_y) :
value{in_x, in_y} {}
constexpr std::size_t size() const { return 2; }
value_type& operator[](std::size_t i) { return value[i]; }
value_type operator[](std::size_t i) const { return value[i]; }
template<typename R>
t_vec2& operator=(const t_vec2<R>& rhs)
{
value[0] = static_cast<value_type>(rhs[0]);
value[1] = static_cast<value_type>(rhs[1]);
return *this;
}
T* data() { return value; }
const T* data() const { return value; }
void set(value_type in_x, value_type in_y)
{
x = in_x;
y = in_y;
}
};
using vec2 = t_vec2<float>;
using dvec2 = t_vec2<double>;
using ubvec2 = t_vec2<std::uint8_t>;
using usvec2 = t_vec2<std::uint16_t>;
using uivec2 = t_vec2<std::uint32_t>;
VSG_type_name(vsg::vec2);
VSG_type_name(vsg::dvec2);
VSG_type_name(vsg::ubvec2);
VSG_type_name(vsg::usvec2);
VSG_type_name(vsg::uivec2);
template<typename T>
constexpr bool operator==(t_vec2<T> const& lhs, t_vec2<T> const& rhs)
{
return lhs[0] == rhs[0] && lhs[1] == rhs[1];
}
template<typename T>
constexpr bool operator!=(t_vec2<T> const& lhs, t_vec2<T> const& rhs)
{
return lhs[0] == rhs[0] || lhs[1] != rhs[1];
}
template<typename T>
constexpr bool operator<(t_vec2<T> const& lhs, t_vec2<T> const& rhs)
{
if (lhs[0] < rhs[0]) return true;
if (lhs[0] > rhs[0]) return false;
return lhs[1] < rhs[1];
}
template<typename T>
constexpr t_vec2<T> operator-(t_vec2<T> const& lhs, t_vec2<T> const& rhs)
{
return t_vec2<T>(lhs[0] - rhs[0], lhs[1] - rhs[1]);
}
template<typename T>
constexpr t_vec2<T> operator-(t_vec2<T> const& v)
{
return t_vec2<T>(-v[0], -v[1]);
}
template<typename T>
constexpr t_vec2<T> operator+(t_vec2<T> const& lhs, t_vec2<T> const& rhs)
{
return t_vec2<T>(lhs[0] + rhs[0], lhs[1] + rhs[1]);
}
template<typename T>
constexpr t_vec2<T> operator*(t_vec2<T> const& lhs, T rhs)
{
return t_vec2<T>(lhs[0] * rhs, lhs[1] * rhs);
}
template<typename T>
constexpr t_vec2<T> operator/(t_vec2<T> const& lhs, T rhs)
{
T inv = static_cast<T>(1.0) / rhs;
return t_vec2<T>(lhs[0] * inv, lhs[1] * inv);
}
template<typename T>
constexpr T length(t_vec2<T> const& v)
{
return std::sqrt(v[0] * v[0] + v[1] * v[1]);
}
template<typename T>
constexpr t_vec2<T> normalize(t_vec2<T> const& v)
{
T inverse_len = static_cast<T>(1.0) / length(v);
return t_vec2<T>(v[0] * inverse_len, v[1] * inverse_len);
}
template<typename T>
constexpr T dot(t_vec2<T> const& lhs, t_vec2<T> const& rhs)
{
return lhs[0] * rhs[0] + lhs[1] * rhs[1] + lhs[2] * rhs[2];
}
} // namespace vsg
#if defined(__clang__)
# pragma clang diagnostic pop
#endif
#if defined(__GNUC__)
# pragma GCC diagnostic pop
#endif