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