Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Vector2.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Vector2_INCLUDE_ONCE
33 #define Vector2_INCLUDE_ONCE
34 
35 #include <vlCore/OpenGLDefs.hpp>
36 #include <vlCore/std_types.hpp>
37 #include <cmath>
38 
39 #ifdef min
40 #undef min
41 #endif
42 
43 #ifdef max
44 #undef max
45 #endif
46 
47 #ifdef dot
48 #undef dot
49 #endif
50 
51 #ifdef cross
52 #undef cross
53 #endif
54 
55 namespace vl
56 {
57  // fast square root
58 
59  #if VL_FAST_SQUARE_ROOTS == 1
60  #define VL_FLOAT_SQRT(x) fast_sqrt(x)
61  #define VL_FLOAT_INVSQRT(x) fast2_inversesqrt(x)
62  #else
63  #define VL_FLOAT_SQRT(x) ((float)::sqrt(x))
64  #define VL_FLOAT_INVSQRT(x) (1.0f/(float)::sqrt(x))
65  #endif
66 
67  // fast square root functions, see Dave Eberly's paper and http://www.beyond3d.com/content/articles/8/
68 
69  inline float fast1_inversesqrt(float x)
70  {
71  float xhalf = 0.5f*x;
72  union { float f; unsigned int i; } num;
73  num.f = x;
74  num.i = 0x5f3759df - (num.i>>1);
75  x = num.f;
76  x = x*(1.5f - xhalf*x*x); // single iteration, very quick, but very poor precision
77  return x;
78  }
79  inline float fast2_inversesqrt(float x)
80  {
81  float xhalf = 0.5f*x;
82  union { float f; unsigned int i; } num;
83  num.f = x;
84  num.i = 0x5f3759df - (num.i>>1);
85  x = num.f;
86  x = x*(1.5f - xhalf*x*x);
87  x = x*(1.5f - xhalf*x*x); // two iterations, sligthtly better precision
88  return x;
89  }
90  inline float fast_sqrt(float x) { if (x == 0.0f) return 0.0f; else return x * fast2_inversesqrt(x); }
91 
96  template<typename T_Scalar>
97  class Vector2
98  {
99  public:
100  typedef T_Scalar scalar_type;
101  typedef T_Scalar* scalar_ptr_type;
102  static const int scalar_count = 2;
103  Vector2(const Vector2& other) { *this = other; }
104  Vector2() { x() = y() = 0; }
105 
106  template<class T>
107  explicit Vector2(const T& other)
108  {
109  x() = (T_Scalar)other.x();
110  y() = (T_Scalar)other.y();
111  }
112 
113  explicit Vector2(const scalar_ptr_type& pval)
114  {
115  mScalar[0] = pval[0];
116  mScalar[1] = pval[1];
117  }
118 
119  explicit Vector2(T_Scalar val)
120  {
121  mScalar[0] = mScalar[1] = val;
122  }
123 
124  explicit Vector2(T_Scalar x, T_Scalar y)
125  {
126  mScalar[0] = x;
127  mScalar[1] = y;
128  }
129 
130  T_Scalar* ptr() { return mScalar; }
131  const T_Scalar* ptr() const { return mScalar; }
132 
133  const T_Scalar& x() const { return mScalar[0]; }
134  const T_Scalar& y() const { return mScalar[1]; }
135 
136  T_Scalar& x() { return mScalar[0]; }
137  T_Scalar& y() { return mScalar[1]; }
138 
139  const T_Scalar& r() const { return mScalar[0]; }
140  const T_Scalar& g() const { return mScalar[1]; }
141 
142  T_Scalar& r() { return mScalar[0]; }
143  T_Scalar& g() { return mScalar[1]; }
144 
145  const T_Scalar& s() const { return mScalar[0]; }
146  const T_Scalar& t() const { return mScalar[1]; }
147 
148  T_Scalar& s() { return mScalar[0]; }
149  T_Scalar& t() { return mScalar[1]; }
150 
151  Vector2 operator+(const Vector2& other) const
152  {
153  return Vector2(x()+other.x(), y()+other.y());
154  }
155  Vector2 operator-(const Vector2& other) const
156  {
157  return Vector2(x()-other.x(), y()-other.y());
158  }
159  Vector2 operator*(const Vector2& other) const
160  {
161  return Vector2(x()*other.x(), y()*other.y());
162  }
163  Vector2 operator/(const Vector2& other) const
164  {
165  return Vector2(x()/other.x(), y()/other.y());
166  }
167  Vector2 operator+(T_Scalar val) const
168  {
169  return Vector2(x()+val, y()+val);
170  }
171  Vector2 operator-(T_Scalar val) const
172  {
173  return Vector2(x()-val, y()-val);
174  }
175  Vector2 operator*(T_Scalar val) const
176  {
177  return Vector2(x()*val, y()*val);
178  }
179  Vector2 operator/(T_Scalar val) const
180  {
181  return Vector2(x()/val, y()/val);
182  }
184  {
185  return Vector2(-x(), -y());
186  }
187  Vector2& operator+=(const Vector2& other)
188  {
189  *this = *this + other;
190  return *this;
191  }
192  Vector2& operator-=(const Vector2& other)
193  {
194  *this = *this - other;
195  return *this;
196  }
197  Vector2& operator*=(const Vector2& other)
198  {
199  *this = *this * other;
200  return *this;
201  }
202  Vector2& operator/=(const Vector2& other)
203  {
204  *this = *this / other;
205  return *this;
206  }
207  Vector2& operator+=(T_Scalar val)
208  {
209  *this = *this + val;
210  return *this;
211  }
212  Vector2& operator-=(T_Scalar val)
213  {
214  *this = *this - val;
215  return *this;
216  }
217  Vector2& operator*=(T_Scalar val)
218  {
219  *this = *this * val;
220  return *this;
221  }
222  Vector2& operator/=(T_Scalar val)
223  {
224  *this = *this / val;
225  return *this;
226  }
227  Vector2& operator=(const Vector2& other)
228  {
229  x() = other.x();
230  y() = other.y();
231  return *this;
232  }
233  Vector2& operator=(T_Scalar val)
234  {
235  x() = y() = val;
236  return *this;
237  }
238  bool operator==(const Vector2& other) const
239  {
240  return x() == other.x() && y() == other.y();
241  }
242  bool operator!=(const Vector2& other) const
243  {
244  return !operator==(other);
245  }
246  bool operator<(const Vector2& other) const
247  {
248  if (x() != other.x())
249  return x() < other.x();
250  else
251  return y() < other.y();
252  }
253  T_Scalar& operator[](unsigned i) { return mScalar[i]; }
254  const T_Scalar& operator[](unsigned i) const { return mScalar[i]; }
255  T_Scalar length() const { return ::sqrt(x()*x()+y()*y()); }
256  T_Scalar lengthSquared() const { return x()*x()+y()*y(); }
257  bool isNull() const { return !x() && !y(); }
258  const Vector2& normalize(T_Scalar *len=NULL)
259  {
260  T_Scalar l = length();
261  if (len)
262  *len = l;
263  if (l)
264  *this *= (T_Scalar)(1.0/l);
265  return *this;
266  }
267 
268  protected:
270  };
271 
272  template<typename T>
273  inline const Vector2<T> operator*(T val, const Vector2<T>& v)
274  {
275  return v * val;
276  }
277 
294 
295  #if VL_PIPELINE_PRECISION == 2
296  typedef dvec2 vec2;
298  #else
299  typedef fvec2 vec2;
301  #endif
302 
303  inline float dot(const fvec2& v1, const fvec2& v2) { return v1.x()*v2.x() + v1.y()*v2.y(); }
304  inline double dot(const dvec2& v1, const dvec2& v2) { return v1.x()*v2.x() + v1.y()*v2.y(); }
305  inline float dot(const ivec2& v1, const ivec2& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y()); }
306  inline float dot(const uvec2& v1, const uvec2& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y()); }
307 
308  inline float min(float a, float b) { return a < b ? a : b; }
309  inline double min(double a, double b) { return a < b ? a : b; }
310  inline int min(int a, int b) { return a < b ? a : b; }
311  inline unsigned int min(unsigned int a, unsigned int b) { return a < b ? a : b; }
312  inline float max(float a, float b) { return a > b ? a : b; }
313  inline double max(double a, double b) { return a > b ? a : b; }
314  inline int max(int a, int b) { return a > b ? a : b; }
315  inline unsigned int max(unsigned int a, unsigned int b) { return a > b ? a : b; }
316  inline float clamp(float x, float minval, float maxval) { return min(max(x,minval),maxval); }
317  inline double clamp(double x, double minval, double maxval) { return min(max(x,minval),maxval); }
318  inline int clamp(int x, int minval, int maxval) { return min(max(x,minval),maxval); }
319  inline unsigned int clamp(unsigned int x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); }
320 
321  inline fvec2 min(const fvec2& a, const fvec2& b)
322  {
323  return fvec2( a.x() < b.x() ? a.x() : b.x(),
324  a.y() < b.y() ? a.y() : b.y());
325  }
326  inline fvec2 min(const fvec2& a, float b)
327  {
328  return fvec2( a.x() < b ? a.x() : b,
329  a.y() < b ? a.y() : b);
330  }
331  inline dvec2 min(const dvec2& a, const dvec2& b)
332  {
333  return dvec2( a.x() < b.x() ? a.x() : b.x(),
334  a.y() < b.y() ? a.y() : b.y());
335  }
336  inline dvec2 min(const dvec2& a, double b)
337  {
338  return dvec2( a.x() < b ? a.x() : b,
339  a.y() < b ? a.y() : b);
340  }
341  inline ivec2 min(const ivec2& a, const ivec2& b)
342  {
343  return ivec2( a.x() < b.x() ? a.x() : b.x(),
344  a.y() < b.y() ? a.y() : b.y());
345  }
346  inline ivec2 min(const ivec2& a, int b)
347  {
348  return ivec2( a.x() < b ? a.x() : b,
349  a.y() < b ? a.y() : b);
350  }
351  inline uvec2 min(const uvec2& a, const uvec2& b)
352  {
353  return uvec2( a.x() < b.x() ? a.x() : b.x(),
354  a.y() < b.y() ? a.y() : b.y());
355  }
356  inline uvec2 min(const uvec2& a, unsigned int b)
357  {
358  return uvec2( a.x() < b ? a.x() : b,
359  a.y() < b ? a.y() : b);
360  }
361  inline fvec2 max(const fvec2& a, const fvec2& b)
362  {
363  return fvec2( a.x() > b.x() ? a.x() : b.x(),
364  a.y() > b.y() ? a.y() : b.y());
365  }
366  inline fvec2 max(const fvec2& a, float b)
367  {
368  return fvec2( a.x() > b ? a.x() : b,
369  a.y() > b ? a.y() : b);
370  }
371  inline dvec2 max(const dvec2& a, const dvec2& b)
372  {
373  return dvec2( a.x() > b.x() ? a.x() : b.x(),
374  a.y() > b.y() ? a.y() : b.y());
375  }
376  inline dvec2 max(const dvec2& a, double b)
377  {
378  return dvec2( a.x() > b ? a.x() : b,
379  a.y() > b ? a.y() : b);
380  }
381  inline ivec2 max(const ivec2& a, const ivec2& b)
382  {
383  return ivec2( a.x() > b.x() ? a.x() : b.x(),
384  a.y() > b.y() ? a.y() : b.y());
385  }
386  inline ivec2 max(const ivec2& a, int b)
387  {
388  return ivec2( a.x() > b ? a.x() : b,
389  a.y() > b ? a.y() : b);
390  }
391  inline uvec2 max(const uvec2& a, const uvec2& b)
392  {
393  return uvec2( a.x() > b.x() ? a.x() : b.x(),
394  a.y() > b.y() ? a.y() : b.y());
395  }
396  inline uvec2 max(const uvec2& a, unsigned int b)
397  {
398  return uvec2( a.x() > b ? a.x() : b,
399  a.y() > b ? a.y() : b);
400  }
401  inline fvec2 clamp(const fvec2& x, float minval, float maxval) { return min(max(x,minval),maxval); }
402  inline fvec2 clamp(const fvec2& x, const fvec2& minval, const fvec2& maxval) { return min(max(x,minval),maxval); }
403  inline dvec2 clamp(const dvec2& x, double minval, double maxval) { return min(max(x,minval),maxval); }
404  inline dvec2 clamp(const dvec2& x, const dvec2& minval, const dvec2& maxval) { return min(max(x,minval),maxval); }
405  inline ivec2 clamp(const ivec2& x, int minval, int maxval) { return min(max(x,minval),maxval); }
406  inline ivec2 clamp(const ivec2& x, const ivec2& minval, const ivec2& maxval) { return min(max(x,minval),maxval); }
407  inline uvec2 clamp(const uvec2& x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); }
408  inline uvec2 clamp(const uvec2& x, const uvec2& minval, const uvec2& maxval) { return min(max(x,minval),maxval); }
409 }
410 
411 #endif
Vector2 operator-(T_Scalar val) const
Definition: Vector2.hpp:171
float clamp(float x, float minval, float maxval)
Definition: Vector2.hpp:316
Vector2< int > ivec2
A 2 components vector with int precision.
Definition: Vector2.hpp:279
const T_Scalar & t() const
Definition: Vector2.hpp:146
Vector2 & operator*=(const Vector2 &other)
Definition: Vector2.hpp:197
Vector2(T_Scalar x, T_Scalar y)
Definition: Vector2.hpp:124
Vector2< double > dvec2
A 2 components vector with double precision.
Definition: Vector2.hpp:285
T sqrt(T a)
Definition: glsl_math.hpp:592
const T_Scalar & g() const
Definition: Vector2.hpp:140
fvec2 vec2
Defined as: &#39;typedef fvec2 vec2&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector2.hpp:300
static const int scalar_count
Definition: Vector2.hpp:102
T_Scalar & operator[](unsigned i)
Definition: Vector2.hpp:253
Vector2< unsigned int > uvec2
A 2 components vector with unsigned int precision.
Definition: Vector2.hpp:281
T_Scalar mScalar[scalar_count]
Definition: Vector2.hpp:269
const T_Scalar & s() const
Definition: Vector2.hpp:145
Vector2 operator-() const
Definition: Vector2.hpp:183
const Vector2 & normalize(T_Scalar *len=NULL)
Definition: Vector2.hpp:258
T_Scalar lengthSquared() const
Definition: Vector2.hpp:256
T_Scalar & g()
Definition: Vector2.hpp:143
Vector2 operator-(const Vector2 &other) const
Definition: Vector2.hpp:155
T_Scalar & x()
Definition: Vector2.hpp:136
Vector2(T_Scalar val)
Definition: Vector2.hpp:119
Vector2 & operator-=(T_Scalar val)
Definition: Vector2.hpp:212
Vector2 operator+(T_Scalar val) const
Definition: Vector2.hpp:167
Vector2 operator*(T_Scalar val) const
Definition: Vector2.hpp:175
Vector2(const Vector2 &other)
Definition: Vector2.hpp:103
Vector2(const T &other)
Definition: Vector2.hpp:107
Vector2 operator*(const Vector2 &other) const
Definition: Vector2.hpp:159
Vector2 & operator-=(const Vector2 &other)
Definition: Vector2.hpp:192
Visualization Library main namespace.
float dot(float a, float b)
Definition: glsl_math.hpp:1111
T_Scalar * ptr()
Definition: Vector2.hpp:130
Vector2< float > fvec2
A 2 components vector with float precision.
Definition: Vector2.hpp:283
T_Scalar * scalar_ptr_type
Definition: Vector2.hpp:101
Vector2 & operator/=(const Vector2 &other)
Definition: Vector2.hpp:202
T_Scalar length() const
Definition: Vector2.hpp:255
Vector2< unsigned char > ubvec2
A 2 components vector with unsigned char precision.
Definition: Vector2.hpp:289
bool operator==(const Vector2 &other) const
Definition: Vector2.hpp:238
const T_Scalar & r() const
Definition: Vector2.hpp:139
const T_Scalar & operator[](unsigned i) const
Definition: Vector2.hpp:254
bool operator<(const Vector2 &other) const
Definition: Vector2.hpp:246
float max(float a, float b)
Definition: Vector2.hpp:312
float min(float a, float b)
Definition: Vector2.hpp:308
T_Scalar & t()
Definition: Vector2.hpp:149
Vector2 & operator=(T_Scalar val)
Definition: Vector2.hpp:233
float fast1_inversesqrt(float x)
Definition: Vector2.hpp:69
T_Scalar & y()
Definition: Vector2.hpp:137
#define NULL
Definition: OpenGLDefs.hpp:81
Vector2 & operator+=(T_Scalar val)
Definition: Vector2.hpp:207
Vector2(const scalar_ptr_type &pval)
Definition: Vector2.hpp:113
Vector2 & operator=(const Vector2 &other)
Definition: Vector2.hpp:227
Vector2< char > bvec2
A 2 components vector with char precision.
Definition: Vector2.hpp:287
float fast2_inversesqrt(float x)
Definition: Vector2.hpp:79
bool isNull() const
Definition: Vector2.hpp:257
The Vector2 class is a template class that implements a generic 2 components vector, see also vl::fvec2, vl::dvec2, vl::uvec2, vl::ivec2, vl::svec2, vl::usvec2, vl::bvec2, vl::ubvec2.
Definition: Vector2.hpp:97
Vector2< short > svec2
A 2 components vector with short precision.
Definition: Vector2.hpp:291
T_Scalar & s()
Definition: Vector2.hpp:148
T_Scalar & r()
Definition: Vector2.hpp:142
Vector2 operator+(const Vector2 &other) const
Definition: Vector2.hpp:151
Vector2 & operator+=(const Vector2 &other)
Definition: Vector2.hpp:187
Vector2 operator/(const Vector2 &other) const
Definition: Vector2.hpp:163
Vector2< unsigned short > usvec2
A 2 components vector with unsigned short precision.
Definition: Vector2.hpp:293
Vector2 operator/(T_Scalar val) const
Definition: Vector2.hpp:179
const T_Scalar & x() const
Definition: Vector2.hpp:133
bool operator!=(const Vector2 &other) const
Definition: Vector2.hpp:242
Vector2 & operator/=(T_Scalar val)
Definition: Vector2.hpp:222
const T_Scalar & y() const
Definition: Vector2.hpp:134
T_Scalar scalar_type
Definition: Vector2.hpp:100
Vector2 & operator*=(T_Scalar val)
Definition: Vector2.hpp:217
const T_Scalar * ptr() const
Definition: Vector2.hpp:131
float fast_sqrt(float x)
Definition: Vector2.hpp:90