liballofw
vector.h
Go to the documentation of this file.
1 #ifndef IV_MATH_VECTOR_H
2 #define IV_MATH_VECTOR_H
3 
4 #include "basic.h"
5 
6 namespace allofw {
7 
8  template <typename T>
9  struct Vector2_ {
10  typedef T number_t;
11  static const size_t size = 2;
12 
13  Vector2_() = default;
14  template <typename number_from_t>
15  inline Vector2_(const Vector2_<number_from_t>& v) : x(v.x), y(v.y) { }
16  inline Vector2_(number_t v) { x = y = v; }
17  inline Vector2_(number_t x_, number_t y_) : x(x_), y(y_) { }
18 
19  template<typename OtherT>
20  inline Vector2_(const OtherT& other)
21  : x(other.x), y(other.y) { }
22 
23  inline Vector2_& operator += (const Vector2_& v) { x += v.x; y += v.y; return *this; }
24  inline Vector2_& operator -= (const Vector2_& v) { x -= v.x; y -= v.y; return *this; }
25  inline Vector2_& operator *= (number_t s) { x *= s; y *= s; return *this; }
26  inline Vector2_& operator /= (number_t s) { x /= s; y /= s; return *this; }
27  inline Vector2_ operator + (const Vector2_& v) const { return Vector2_(x + v.x, y + v.y); }
28  inline Vector2_ operator - (const Vector2_& v) const { return Vector2_(x - v.x, y - v.y); }
29  inline Vector2_ operator - () const { return Vector2_(-x, -y); }
30  inline Vector2_ operator * (number_t s) const { return Vector2_(x * s, y * s); }
31  inline Vector2_ operator / (number_t s) const { return Vector2_(x / s, y / s); }
32  inline number_t dot(const Vector2_& v) const { return x * v.x + y * v.y; }
33  inline number_t len2() const { return x * x + y * y; }
34  inline number_t len() const { return std::sqrt(x * x + y * y); }
35  inline Vector2_ unit() const { return *this / len(); }
36  inline number_t cross(const Vector2_& v) const {
37  return x * v.y - y * v.x;
38  }
39  inline number_t operator * (const Vector2_& v) const { return this->cross(v); }
40 
41  friend inline Vector2_ operator * (number_t s, const Vector2_& v) { return v * s; }
42 
43  inline Vector2_ rotate90deg() const { return Vector2_(-y, x); }
44  inline double angle() const { return std::atan2(y, x); }
45 
46  number_t* data() { return &x; }
47  const number_t* data() const { return &x; }
48  int shape() const { return 2; }
49 
50  static Vector2_ NaN() { return Vector2_(NAN, NAN); }
51 
52  union {
53  number_t x;
54  number_t w;
55  };
56  union {
57  number_t y;
58  number_t h;
59  };
60  };
61 
62  template <typename T>
63  struct Vector3_ {
64  typedef T number_t;
65  static const size_t size = 3;
66 
67  Vector3_() = default;
68  template <typename number_from_t>
69  inline Vector3_(const Vector2_<number_from_t>& v) : x(v.x), y(v.y), z(0) { }
70  template <typename number_from_t>
71  inline Vector3_(const Vector3_<number_from_t>& v) : x(v.x), y(v.y), z(v.z) { }
72  inline Vector3_(number_t v) { x = y = z = v; }
73  inline Vector3_(number_t x_, number_t y_, number_t z_) : x(x_), y(y_), z(z_) { }
74 
75  template<typename OtherT>
76  inline Vector3_(const OtherT& other)
77  : x(other.x), y(other.y), z(other.z) { }
78 
79  inline Vector3_& operator += (const Vector3_& v) { x += v.x; y += v.y; z += v.z; return *this; }
80  inline Vector3_& operator -= (const Vector3_& v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
81  inline Vector3_& operator *= (number_t s) { x *= s; y *= s; z *= s; return *this; }
82  inline Vector3_& operator /= (number_t s) { x /= s; y /= s; z /= s; return *this; }
83  inline Vector3_ operator + (const Vector3_& v) const { return Vector3_(x + v.x, y + v.y, z + v.z); }
84  inline Vector3_ operator - (const Vector3_& v) const { return Vector3_(x - v.x, y - v.y, z - v.z); }
85  inline Vector3_ operator - () const { return Vector3_(-x, -y, -z); }
86  inline Vector3_ operator * (number_t s) const { return Vector3_(x * s, y * s, z * s); }
87  inline Vector3_ operator / (number_t s) const { return Vector3_(x / s, y / s, z / s); }
88  inline number_t dot(const Vector3_& v) const { return x * v.x + y * v.y + z * v.z; }
89  inline number_t len2() const { return x * x + y * y + z * z; }
90  inline number_t len() const { return std::sqrt(x * x + y * y + z * z); }
91  inline Vector3_ unit() const { return *this / len(); }
92  inline Vector3_ cross(const Vector3_& v) const {
93  return Vector3_(
94  y * v.z - z * v.y,
95  z * v.x - x * v.z,
96  x * v.y - y * v.x
97  );
98  }
99 
100  friend inline Vector3_ operator * (number_t s, const Vector3_& v) { return v * s; }
101 
102  inline Vector2_<number_t> xy() const { return Vector2_<number_t>(x, y); }
103 
104  number_t* data() { return &x; }
105  const number_t* data() const { return &x; }
106  int shape() const { return 3; }
107 
108  static Vector3_ NaN() { return Vector3_(NAN, NAN, NAN); }
109 
110  number_t x, y, z;
111  };
112 
113  template <typename T>
114  struct Vector4_ {
115  typedef T number_t;
116  static const size_t size = 4;
117 
118  Vector4_() = default;
119  template <typename number_from_t>
120  inline Vector4_(const Vector2_<number_from_t>& v) : x(v.x), y(v.y), z(0), w(0) { }
121  template <typename number_from_t>
122  inline Vector4_(const Vector3_<number_from_t>& v) : x(v.x), y(v.y), z(v.z), w(0) { }
123  template <typename number_from_t>
124  inline Vector4_(const Vector4_<number_from_t>& v) : x(v.x), y(v.y), z(v.z), w(v.w) { }
125  inline Vector4_(number_t v) { x = y = z = w = v; }
126  inline Vector4_(number_t x_, number_t y_, number_t z_, number_t w_) : x(x_), y(y_), z(z_), w(w_) { }
127 
128  template<typename OtherT>
129  inline Vector4_(const OtherT& other)
130  : x(other.x), y(other.y), z(other.z), w(other.w) { }
131 
132  inline Vector4_& operator += (const Vector4_& v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; }
133  inline Vector4_& operator -= (const Vector4_& v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; }
134  inline Vector4_& operator *= (number_t s) { x *= s; y *= s; z *= s; w *= s; return *this; }
135  inline Vector4_& operator /= (number_t s) { x /= s; y /= s; z /= s; w /= s; return *this; }
136  inline Vector4_ operator + (const Vector4_& v) const { return Vector4_(x + v.x, y + v.y, z + v.z, w + v.w); }
137  inline Vector4_ operator - (const Vector4_& v) const { return Vector4_(x - v.x, y - v.y, z - v.z, w - v.w); }
138  inline Vector4_ operator - () const { return Vector4_(-x, -y, -z, -w); }
139  inline Vector4_ operator * (number_t s) const { return Vector4_(x * s, y * s, z * s, w * s); }
140  inline Vector4_ operator / (number_t s) const { return Vector4_(x / s, y / s, z / s, w / s); }
141  inline number_t dot(const Vector4_& v) const { return x * v.x + y * v.y + z * v.z + w * v.w; }
142  inline number_t len2() const { return x * x + y * y + z * z + w * w; }
143  inline number_t len() const { return std::sqrt(x * x + y * y + z * z + w * w); }
144  inline Vector4_ unit() const { return *this / len(); }
145 
146  friend inline Vector4_ operator * (number_t s, const Vector4_& v) { return v * s; }
147 
148  inline Vector2_<number_t> xy() const { return Vector2_<number_t>(x, y); }
149  inline Vector3_<number_t> xyz() const { return Vector3_<number_t>(x, y, z); }
150 
151  number_t* data() { return &x; }
152  const number_t* data() const { return &x; }
153  int shape() const { return 4; }
154 
155  static Vector4_ NaN() { return Vector4_(NAN, NAN, NAN, NAN); }
156 
157  number_t x, y, z, w;
158  };
159 
162  typedef Vector2d Vector2;
163 
164  static_assert(sizeof(Vector2d) == sizeof(double) * 2, "Vector2d alignment error.");
165  static_assert(sizeof(Vector2f) == sizeof(float) * 2, "Vector2f alignment error.");
166 
167  typedef Vector3_<double> Vector3d;
169  typedef Vector3d Vector3;
170 
171  static_assert(sizeof(Vector3d) == sizeof(double) * 3, "Vector3d alignment error.");
172  static_assert(sizeof(Vector3f) == sizeof(float) * 3, "Vector3f alignment error.");
173 
174  typedef Vector4_<double> Vector4d;
176  typedef Vector4d Vector4;
177 
178  static_assert(sizeof(Vector4d) == sizeof(double) * 4, "Vector4d alignment error.");
179  static_assert(sizeof(Vector4f) == sizeof(float) * 4, "Vector4f alignment error.");
180 
181  template <typename T> bool isnan(const Vector2_<T>& v) { return std::isnan(v.x) || std::isnan(v.y); }
182  template <typename T> bool isnan(const Vector3_<T>& v) { return std::isnan(v.x) || std::isnan(v.y) || std::isnan(v.z); }
183  template <typename T> bool isnan(const Vector4_<T>& v) { return std::isnan(v.x) || std::isnan(v.y) || std::isnan(v.z) || std::isnan(v.w); }
184 
185 }
186 
187 #endif
number_t dot(const Vector2_ &v) const
Definition: vector.h:32
number_t cross(const Vector2_ &v) const
Definition: vector.h:36
Vector2_ operator*(number_t s) const
Definition: vector.h:30
Vector2_< number_t > xy() const
Definition: vector.h:102
number_t dot(const Vector4_ &v) const
Definition: vector.h:141
bool isnan(const Vector4_< T > &v)
Definition: vector.h:183
Vector3_< float > Vector3f
Definition: vector.h:168
number_t x
Definition: vector.h:53
number_t z
Definition: vector.h:157
number_t dot(const Vector3_ &v) const
Definition: vector.h:88
number_t x
Definition: vector.h:110
Vector3_(const OtherT &other)
Definition: vector.h:76
number_t len() const
Definition: vector.h:34
Vector2_(const OtherT &other)
Definition: vector.h:20
number_t y
Definition: vector.h:157
static Vector2_ NaN()
Definition: vector.h:50
Vector2_ & operator*=(number_t s)
Definition: vector.h:25
Vector2_ unit() const
Definition: vector.h:35
Vector2_ rotate90deg() const
Definition: vector.h:43
Vector2_ & operator/=(number_t s)
Definition: vector.h:26
Vector2_ operator/(number_t s) const
Definition: vector.h:31
number_t len() const
Definition: vector.h:143
number_t len2() const
Definition: vector.h:142
Vector4_(const Vector3_< number_from_t > &v)
Definition: vector.h:122
Vector2d Vector2
Definition: vector.h:162
Definition: allofw.h:12
number_t len() const
Definition: vector.h:90
static Vector4_ NaN()
Definition: vector.h:155
Vector2_(const Vector2_< number_from_t > &v)
Definition: vector.h:15
Vector2_()=default
Vector3_ cross(const Vector3_ &v) const
Definition: vector.h:92
number_t y
Definition: vector.h:57
double angle() const
Definition: vector.h:44
number_t w
Definition: vector.h:157
number_t h
Definition: vector.h:58
Vector2_< double > Vector2d
Definition: vector.h:160
number_t len2() const
Definition: vector.h:89
Vector2_ & operator+=(const Vector2_ &v)
Definition: vector.h:23
number_t * data()
Definition: vector.h:46
number_t * data()
Definition: vector.h:104
Vector4d Vector4
Definition: vector.h:176
Vector3_ unit() const
Definition: vector.h:91
Vector4_(const Vector4_< number_from_t > &v)
Definition: vector.h:124
Vector4_< float > Vector4f
Definition: vector.h:175
T number_t
Definition: vector.h:64
Vector3_< double > Vector3d
Definition: vector.h:164
Vector2_< number_t > xy() const
Definition: vector.h:148
Vector4_(number_t v)
Definition: vector.h:125
bool isnan(const Vector2_< T > &v)
Definition: vector.h:181
Vector2_ & operator-=(const Vector2_ &v)
Definition: vector.h:24
Vector3d Vector3
Definition: vector.h:169
const number_t * data() const
Definition: vector.h:47
number_t y
Definition: vector.h:110
Vector4_ unit() const
Definition: vector.h:144
int shape() const
Definition: vector.h:106
Vector4_(const Vector2_< number_from_t > &v)
Definition: vector.h:120
Vector2_ operator+(const Vector2_ &v) const
Definition: vector.h:27
static Vector3_ NaN()
Definition: vector.h:108
T number_t
Definition: vector.h:115
const number_t * data() const
Definition: vector.h:152
Definition: vector.h:63
Definition: vector.h:9
int shape() const
Definition: vector.h:48
Definition: vector.h:114
Vector2_(number_t x_, number_t y_)
Definition: vector.h:17
int shape() const
Definition: vector.h:153
number_t len2() const
Definition: vector.h:33
Vector4_(const OtherT &other)
Definition: vector.h:129
number_t z
Definition: vector.h:110
static const size_t size
Definition: vector.h:11
Vector3_(const Vector3_< number_from_t > &v)
Definition: vector.h:71
Vector4_(number_t x_, number_t y_, number_t z_, number_t w_)
Definition: vector.h:126
number_t * data()
Definition: vector.h:151
Vector3_(number_t x_, number_t y_, number_t z_)
Definition: vector.h:73
Vector4_< double > Vector4d
Definition: vector.h:171
T number_t
Definition: vector.h:10
number_t x
Definition: vector.h:157
number_t w
Definition: vector.h:54
const number_t * data() const
Definition: vector.h:105
Vector2_(number_t v)
Definition: vector.h:16
Vector3_(const Vector2_< number_from_t > &v)
Definition: vector.h:69
Vector2_< float > Vector2f
Definition: vector.h:161
Vector2_ operator-() const
Definition: vector.h:29
Vector3_(number_t v)
Definition: vector.h:72
Vector3_< number_t > xyz() const
Definition: vector.h:149