Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Vector.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_VECTOR_H
22 #define GF_VECTOR_H
23 
24 #include <cstddef>
25 #include <cstdint>
26 
27 #include <algorithm>
28 #include <initializer_list>
29 #include <type_traits>
30 
31 #include "Portability.h"
32 
33 namespace gf {
34 #ifndef DOXYGEN_SHOULD_SKIP_THIS
35 inline namespace v1 {
36 #endif
37 
59  template<typename T, std::size_t N>
60  struct Vector {
61  #ifndef DOXYGEN_SHOULD_SKIP_THIS
62  static_assert(N > 0, "N must be strictly positive");
63  #endif
64 
71  Vector() = default;
72 
86  explicit Vector(T val)
87  {
88  std::fill_n(data, N, val);
89  }
90 
104  explicit Vector(T *array)
105  {
106  std::copy_n(array, N, data);
107  }
108 
122  Vector(std::initializer_list<T> list)
123  {
124  std::copy_n(list.begin(), std::min(list.size(), N), data);
125  }
126 
135  Vector(const Vector& other) = default;
136 
137 
143  template<typename U>
144  Vector(const Vector<U, N>& other)
145  {
146  static_assert(std::is_convertible<U,T>::value, "");
147  std::transform(data, data + N, other.data, [](U val) { return static_cast<T>(val); });
148  }
149 
156  Vector& operator=(const Vector& other) = default;
157 
169  T operator[](std::size_t i) const {
170  return data[i];
171  }
172 
184  T& operator[](std::size_t i) {
185  return data[i];
186  }
187 
188 
194  T *begin() {
195  return &data[0];
196  }
197 
203  T *end() {
204  return &data[N];
205  }
206 
212  const T *begin() const {
213  return &data[0];
214  }
215 
222  const T *end() const {
223  return &data[N];
224  }
230  const T *cbegin() const {
231  return &data[0];
232  }
233 
240  const T *cend() const {
241  return &data[N];
242  }
243 
251  T data[N];
252  };
253 
297  template <typename T>
298  struct Vector<T, 2> {
305  Vector() = default;
306 
320  explicit Vector(T val) {
321  std::fill_n(data, 2, val);
322  }
323 
337  explicit Vector(T *array)
338  {
339  std::copy_n(array, 2, data);
340  }
341 
348  constexpr Vector(T x, T y)
349  : data{ x, y }
350  {
351 
352  }
353 
362  Vector(const Vector& other) = default;
363 
364 
370  template<typename U>
371  Vector(const Vector<U, 2>& other)
372  : x(other.x)
373  , y(other.y)
374  {
375 
376  }
377 
389  T operator[](std::size_t i) const {
390  return data[i];
391  }
392 
404  T& operator[](std::size_t i) {
405  return data[i];
406  }
407 
408 
414  T *begin() {
415  return &data[0];
416  }
417 
423  T *end() {
424  return &data[2];
425  }
426 
432  const T *begin() const {
433  return &data[0];
434  }
435 
442  const T *end() const {
443  return &data[2];
444  }
450  const T *cbegin() const {
451  return &data[0];
452  }
453 
460  const T *cend() const {
461  return &data[2];
462  }
463 
467  union {
468  T data[2];
469  struct {
470  T x;
471  T y;
472  };
473  struct {
474  T u;
475  T v;
476  };
477  struct {
478  T s;
479  T t;
480  };
481  struct {
484  };
485  struct {
486  T col;
487  T row;
488  };
489  };
490  };
491 
529  template <typename T>
530  struct Vector<T, 3> {
537  Vector() = default;
538 
552  explicit Vector(T val) {
553  std::fill_n(data, 3, val);
554  }
555 
569  explicit Vector(T *array)
570  {
571  std::copy_n(array, 3, data);
572  }
573 
581  constexpr Vector(T x, T y, T z)
582  : data{ x, y, z }
583  {
584 
585  }
586 
593  constexpr Vector(Vector<T, 2> xy, T z)
594  : data{ xy.x, xy.y, z }
595  {
596 
597  }
598 
607  Vector(const Vector& other) = default;
608 
609 
615  template<typename U>
616  Vector(const Vector<U, 3>& other)
617  : x(other.x)
618  , y(other.y)
619  , z(other.z)
620  {
621 
622  }
623 
635  T operator[](std::size_t i) const {
636  return data[i];
637  }
638 
650  T& operator[](std::size_t i) {
651  return data[i];
652  }
653 
654 
660  T *begin() {
661  return &data[0];
662  }
663 
669  T *end() {
670  return &data[3];
671  }
672 
678  const T *begin() const {
679  return &data[0];
680  }
681 
688  const T *end() const {
689  return &data[3];
690  }
691 
697  const T *cbegin() const {
698  return &data[0];
699  }
700 
707  const T *cend() const {
708  return &data[3];
709  }
710 
714  union {
715  T data[3];
716  struct {
717  T x;
718  T y;
719  T z;
720  };
721  struct {
722  T r;
723  T g;
724  T b;
725  };
727  };
728  };
729 
767  template <typename T>
768  struct Vector<T, 4> {
775  Vector() = default;
776 
790  explicit Vector(T val) {
791  std::fill_n(data, 4, val);
792  }
793 
807  explicit Vector(T *array)
808  {
809  std::copy_n(array, 4, data);
810  }
811 
820  constexpr Vector(T x, T y, T z, T w)
821  : data{ x, y, z, w }
822  {
823 
824  }
825 
834  Vector(const Vector& other) = default;
835 
836 
842  template<typename U>
843  Vector(const Vector<U, 4>& other)
844  : x(other.x)
845  , y(other.y)
846  , z(other.z)
847  , w(other.w)
848  {
849 
850  }
851 
863  T operator[](std::size_t i) const {
864  return data[i];
865  }
866 
878  T& operator[](std::size_t i) {
879  return data[i];
880  }
881 
882 
888  T *begin() {
889  return &data[0];
890  }
891 
897  T *end() {
898  return &data[4];
899  }
900 
906  const T *begin() const {
907  return &data[0];
908  }
909 
916  const T *end() const {
917  return &data[4];
918  }
919 
925  const T *cbegin() const {
926  return &data[0];
927  }
928 
935  const T *cend() const {
936  return &data[4];
937  }
938 
942  union {
943  T data[4];
944  struct {
945  T x;
946  T y;
947  T z;
948  T w;
949  };
950  struct {
951  T r;
952  T g;
953  T b;
954  T a;
955  };
959  };
960  };
961 
969 
977 
985 
993 
1001 
1009 
1017 
1025 
1033 
1041 
1049 
1057 
1065 
1073 
1081 
1089 
1097 
1105 
1113 
1121 
1129 
1137 
1138 // MSVC does not like extern template
1139 #ifndef _MSC_VER
1140  extern template struct Vector<float, 2>;
1141  extern template struct Vector<float, 3>;
1142  extern template struct Vector<float, 4>;
1143 
1144  extern template struct Vector<double, 2>;
1145  extern template struct Vector<double, 3>;
1146  extern template struct Vector<double, 4>;
1147 
1148  extern template struct Vector<int, 2>;
1149  extern template struct Vector<int, 3>;
1150  extern template struct Vector<int, 4>;
1151 
1152  extern template struct Vector<unsigned, 2>;
1153  extern template struct Vector<unsigned, 3>;
1154  extern template struct Vector<unsigned, 4>;
1155 
1156  extern template struct Vector<bool, 2>;
1157  extern template struct Vector<bool, 3>;
1158  extern template struct Vector<bool, 4>;
1159 
1160  extern template struct Vector<uint8_t, 3>;
1161  extern template struct Vector<uint8_t, 4>;
1162 #endif
1163 
1181  template<typename T, std::size_t N>
1183 
1190  template<typename T>
1192 
1199  template<typename T>
1201 
1202 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1203 }
1204 #endif
1205 }
1206 
1207 #endif // GAME_VECTOR_H
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:569
Vector< T, 2 > xy
Swizzle to get the first two coordinates as a 2D vector.
Definition: Vector.h:726
T x
First coordinate in the (x,y) representation.
Definition: Vector.h:470
T g
Second coordinate in the (r,g,b) representation.
Definition: Vector.h:723
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:552
const T * cbegin() const
Iterator.on the first element (const version).
Definition: Vector.h:925
const T * cend() const
Iterator on the element after the last one (const version).
Definition: Vector.h:935
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:650
Vector< T, 2 > xy
Swizzle to get the first two coordinates as a 2D vector.
Definition: Vector.h:956
const T * end() const
Iterator on the element after the last one (const version).
Definition: Vector.h:688
Distance< T, 2 > Distance2
A distance function for 2D vectors.
Definition: Vector.h:1191
constexpr Vector2f transform(const Matrix3f &mat, Vector2f point)
Apply an affine transformation to a 2D point.
Definition: Transform.h:331
T z
Third coordinate in the (x,y,z,w) representation.
Definition: Vector.h:947
T * end()
Iterator to the element after the last one.
Definition: Vector.h:203
const T * cend() const
Iterator on the element after the last one (const version).
Definition: Vector.h:707
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:169
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:863
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:348
T * begin()
Iterator.to the first element.
Definition: Vector.h:194
T a
Fourth coordinate in the (r,g,b,a) representation.
Definition: Vector.h:954
T * begin()
Iterator.to the first element.
Definition: Vector.h:660
T v
Second coordinate in the (u,v) representation.
Definition: Vector.h:475
T y
Second coordinate in the (x,y) representation.
Definition: Vector.h:471
T x
First coordinate in the (x,y,z) representation.
Definition: Vector.h:717
const T * begin() const
Iterator.to the first element (const version).
Definition: Vector.h:432
T width
First coordinate in the size representation.
Definition: Vector.h:482
const T * end() const
Iterator on the element after the last one (const version).
Definition: Vector.h:916
T s
First coordinate in the (s,t) representation.
Definition: Vector.h:478
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:878
constexpr Vector(T x, T y, T z)
Constructor that takes 3 components.
Definition: Vector.h:581
T data[N]
The internal representation of the vector.
Definition: Vector.h:251
const T * cend() const
Iterator on the element after the last one (const version).
Definition: Vector.h:460
T height
Second coordinate in the size representation.
Definition: Vector.h:483
T * end()
Iterator to the element after the last one.
Definition: Vector.h:669
constexpr Vector(T x, T y, T z, T w)
Constructor that takes 4 components.
Definition: Vector.h:820
const T * end() const
Iterator on the element after the last one (const version).
Definition: Vector.h:442
Distance< T, 3 > Distance3
A distance function for 3D vectors.
Definition: Vector.h:1200
Vector< T, 3 > xyz
Swizzle to get the first three coordinates as a 3D vector.
Definition: Vector.h:957
Vector< T, 3 > rgb
Swizzle to get the first three coordinates as a RGB color.
Definition: Vector.h:958
The namespace for gf classes.
Definition: Action.h:34
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:635
T g
Second coordinate in the (r,g,b,a) representation.
Definition: Vector.h:952
T x
First coordinate in the (x,y,z,w) representation.
Definition: Vector.h:945
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:184
T r
First coordinate in the (r,g,b) representation.
Definition: Vector.h:722
const T * cend() const
Iterator on the element after the last one (const version).
Definition: Vector.h:240
const T * cbegin() const
Iterator.on the first element (const version).
Definition: Vector.h:450
T b
Third coordinate in the (r,g,b,a) representation.
Definition: Vector.h:953
T & operator[](std::size_t i)
Access to the -th coordinate.
Definition: Vector.h:404
T col
First coordinate in the indices representation.
Definition: Vector.h:486
const T * cbegin() const
Iterator.on the first element (const version).
Definition: Vector.h:697
const T * begin() const
Iterator.to the first element (const version).
Definition: Vector.h:906
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:807
T * end()
Iterator to the element after the last one.
Definition: Vector.h:897
A 2D vector.
Definition: Vector.h:298
constexpr Vector(Vector< T, 2 > xy, T z)
Constructor that takes a 2D vector and a z component.
Definition: Vector.h:593
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:337
T operator[](std::size_t i) const
Access to the -th coordinate.
Definition: Vector.h:389
T y
Second coordinate in the (x,y,z) representation.
Definition: Vector.h:718
Vector(const Vector< U, 3 > &other)
Converting copy constructor.
Definition: Vector.h:616
const T * end() const
Iterator on the element after the last one (const version).
Definition: Vector.h:222
T y
Second coordinate in the (x,y,z,w) representation.
Definition: Vector.h:946
Vector(const Vector< U, 4 > &other)
Converting copy constructor.
Definition: Vector.h:843
T u
First coordinate in the (u,v) representation.
Definition: Vector.h:474
Vector(const Vector< U, N > &other)
Converting copy constructor.
Definition: Vector.h:144
T b
Third coordinate in the (r,g,b) representation.
Definition: Vector.h:724
const T * cbegin() const
Iterator.on the first element (const version).
Definition: Vector.h:230
T * begin()
Iterator.to the first element.
Definition: Vector.h:414
A 3D vector.
Definition: Vector.h:530
T r
First coordinate in the (r,g,b,a) representation.
Definition: Vector.h:951
General purpose math vector.
Definition: Vector.h:60
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:86
T w
Fourth coordinate in the (x,y,z,w) representation.
Definition: Vector.h:948
const T * begin() const
Iterator.to the first element (const version).
Definition: Vector.h:678
T z
Third coordinate in the (x,y,z) representation.
Definition: Vector.h:719
T(*)(Vector< T, N >, Vector< T, N >) Distance
A distance function.
Definition: Vector.h:1182
Vector(std::initializer_list< T > list)
Constructor that takes an initializer list.
Definition: Vector.h:122
const T * begin() const
Iterator.to the first element (const version).
Definition: Vector.h:212
Vector(const Vector< U, 2 > &other)
Converting copy constructor.
Definition: Vector.h:371
T t
Second coordinate in the (s,t) representation.
Definition: Vector.h:479
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:320
T * begin()
Iterator.to the first element.
Definition: Vector.h:888
T row
Second coordinate in the indices representation.
Definition: Vector.h:487
Vector(T *array)
Constructor that takes an array.
Definition: Vector.h:104
T * end()
Iterator to the element after the last one.
Definition: Vector.h:423
Vector(T val)
Constructor that fills the vector with a value.
Definition: Vector.h:790