vsg 1.1.13
VulkanSceneGraph library
Loading...
Searching...
No Matches
vec2.h
1#pragma once
2
3/* <editor-fold desc="MIT License">
4
5Copyright(c) 2018 Robert Osfield
6
7Permission 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:
8
9The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
11THE 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.
12
13</editor-fold> */
14
15// we can't implement the anonymous union/structs combination without causing warnings, so disable them for just this header
16#if defined(__GNUC__)
17# pragma GCC diagnostic push
18# pragma GCC diagnostic ignored "-Wpedantic"
19#endif
20#if defined(__clang__)
21# pragma clang diagnostic push
22# pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
23# pragma clang diagnostic ignored "-Wnested-anon-types"
24#endif
25
26#include <vsg/core/type_name.h>
27#include <vsg/maths/numbers.h>
28
29#include <cmath>
30#include <cstdint>
31#include <type_traits>
32
33namespace vsg
34{
35
37 template<typename T>
38 struct t_vec2
39 {
40 using value_type = T;
41
42 union
43 {
44 value_type value[2];
45 struct
46 {
47 value_type x, y;
48 };
49 struct
50 {
51 value_type r, g;
52 };
53 struct
54 {
55 value_type s, t;
56 };
57 };
58
59 constexpr t_vec2() :
60 value{} {}
61 constexpr t_vec2(const t_vec2& v) :
62 value{v.x, v.y} {}
63 constexpr t_vec2& operator=(const t_vec2&) = default;
64 constexpr t_vec2(value_type in_x, value_type in_y) :
65 value{in_x, in_y} {}
66
67 template<typename R>
68 constexpr explicit t_vec2(const t_vec2<R>& v) :
69 value{static_cast<T>(v.x), static_cast<T>(v.y)} {}
70
71 constexpr std::size_t size() const { return 2; }
72
73 value_type& operator[](std::size_t i) { return value[i]; }
74 value_type operator[](std::size_t i) const { return value[i]; }
75
76 template<typename R>
77 t_vec2& operator=(const t_vec2<R>& rhs)
78 {
79 value[0] = static_cast<value_type>(rhs[0]);
80 value[1] = static_cast<value_type>(rhs[1]);
81 return *this;
82 }
83
84 T* data() { return value; }
85 const T* data() const { return value; }
86
87 void set(value_type in_x, value_type in_y)
88 {
89 x = in_x;
90 y = in_y;
91 }
92
93 inline t_vec2& operator+=(const t_vec2& rhs)
94 {
95 value[0] += rhs.value[0];
96 value[1] += rhs.value[1];
97 return *this;
98 }
99
100 inline t_vec2& operator-=(const t_vec2& rhs)
101 {
102 value[0] -= rhs.value[0];
103 value[1] -= rhs.value[1];
104 return *this;
105 }
106
107 inline t_vec2& operator*=(value_type rhs)
108 {
109 value[0] *= rhs;
110 value[1] *= rhs;
111 return *this;
112 }
113
114 inline t_vec2& operator*=(const t_vec2& rhs)
115 {
116 value[0] *= rhs.value[0];
117 value[1] *= rhs.value[1];
118 return *this;
119 }
120
121 friend constexpr t_vec2<T> operator*(const t_vec2<T>& lhs, T rhs)
122 {
123 return t_vec2<T>(lhs[0] * rhs, lhs[1] * rhs);
124 }
125
126 friend constexpr t_vec2<T> operator*(T lhs, const t_vec2<T>& rhs)
127 {
128 return t_vec2<T>(lhs * rhs[0], lhs * rhs[1]);
129 }
130
131 inline t_vec2& operator/=(value_type rhs)
132 {
133 if constexpr (std::is_floating_point_v<value_type>)
134 {
135 value_type inv = numbers<value_type>::one() / rhs;
136 value[0] *= inv;
137 value[1] *= inv;
138 }
139 else
140 {
141 value[0] /= rhs;
142 value[1] /= rhs;
143 }
144 return *this;
145 }
146
147 explicit operator bool() const noexcept { return value[0] != numbers<value_type>::zero() || value[1] != numbers<value_type>::zero(); }
148 };
149
150 using vec2 = t_vec2<float>; // float 2D vector
151 using dvec2 = t_vec2<double>; // double 2D vector
152 using ldvec2 = t_vec2<long double>; // long double 2D vector
153 using bvec2 = t_vec2<int8_t>; // signed 8 bit integer 2D vector
154 using svec2 = t_vec2<int16_t>; // signed 16 bit integer 2D vector
155 using ivec2 = t_vec2<int32_t>; // signed 32 bit integer 2D vector
156 using ubvec2 = t_vec2<uint8_t>; // unsigned 8 bit integer 2D vector
157 using usvec2 = t_vec2<uint16_t>; // unsigned 16 bit integer 2D vector
158 using uivec2 = t_vec2<uint32_t>; // unsigned 32 bit integer 2D vector
159
160 VSG_type_name(vsg::vec2);
161 VSG_type_name(vsg::dvec2);
162 VSG_type_name(vsg::bvec2);
163 VSG_type_name(vsg::svec2);
164 VSG_type_name(vsg::ivec2);
165 VSG_type_name(vsg::ubvec2);
166 VSG_type_name(vsg::usvec2);
167 VSG_type_name(vsg::uivec2);
168
169 template<typename T>
170 constexpr bool operator==(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
171 {
172 return lhs[0] == rhs[0] && lhs[1] == rhs[1];
173 }
174
175 template<typename T>
176 constexpr bool operator!=(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
177 {
178 return lhs[0] != rhs[0] || lhs[1] != rhs[1];
179 }
180
181 template<typename T>
182 constexpr bool operator<(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
183 {
184 if (lhs[0] < rhs[0]) return true;
185 if (lhs[0] > rhs[0]) return false;
186 return lhs[1] < rhs[1];
187 }
188
189 template<typename T>
190 constexpr t_vec2<T> operator-(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
191 {
192 return t_vec2<T>(lhs[0] - rhs[0], lhs[1] - rhs[1]);
193 }
194
195 template<typename T>
196 constexpr t_vec2<T> operator-(const t_vec2<T>& v)
197 {
198 return t_vec2<T>(-v[0], -v[1]);
199 }
200
201 template<typename T>
202 constexpr t_vec2<T> operator+(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
203 {
204 return t_vec2<T>(lhs[0] + rhs[0], lhs[1] + rhs[1]);
205 }
206
207 template<typename T>
208 constexpr t_vec2<T> operator*(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
209 {
210 return t_vec2<T>(lhs[0] * rhs[0], lhs[1] * rhs[1]);
211 }
212
213 template<typename T>
214 constexpr t_vec2<T> operator/(const t_vec2<T>& lhs, T rhs)
215 {
216 if constexpr (std::is_floating_point_v<T>)
217 {
218 T inv = numbers<T>::one() / rhs;
219 return t_vec2<T>(lhs[0] * inv, lhs[1] * inv);
220 }
221 else
222 {
223 return t_vec2<T>(lhs[0] / rhs, lhs[1] / rhs);
224 }
225 }
226
227 template<typename T>
228 constexpr T length(const t_vec2<T>& v)
229 {
230 return std::sqrt(v[0] * v[0] + v[1] * v[1]);
231 }
232
233 template<typename T>
234 constexpr T length2(const t_vec2<T>& v)
235 {
236 return v[0] * v[0] + v[1] * v[1];
237 }
238
239 template<typename T>
240 constexpr t_vec2<T> normalize(const t_vec2<T>& v)
241 {
242 return v / length(v);
243 }
244
245 template<typename T>
246 constexpr T dot(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
247 {
248 return lhs[0] * rhs[0] + lhs[1] * rhs[1];
249 }
250
253 template<typename T>
254 constexpr T cross(const t_vec2<T>& lhs, const t_vec2<T>& rhs)
255 {
256 return (lhs[0] * rhs[1] - rhs[0] * lhs[1]);
257 }
258
259 template<typename T>
260 constexpr t_vec2<T> mix(const t_vec2<T>& start, const t_vec2<T>& end, T r)
261 {
262 T one_minus_r = numbers<T>::one() - r;
263 return t_vec2<T>(start[0] * one_minus_r + end[0] * r,
264 start[1] * one_minus_r + end[1] * r);
265 }
266
267} // namespace vsg
268
269#if defined(__clang__)
270# pragma clang diagnostic pop
271#endif
272#if defined(__GNUC__)
273# pragma GCC diagnostic pop
274#endif
t_vec2 template class that represents a 2D vector
Definition vec2.h:39