Visualization Library 2.0.0-b3

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Vector3.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2017, 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 Vector3_INCLUDE_ONCE
33 #define Vector3_INCLUDE_ONCE
34 
35 #include <vlCore/Vector2.hpp>
36 
37 namespace vl
38 {
43  template<typename T_Scalar>
44  class Vector3
45  {
46  public:
47  typedef T_Scalar scalar_type;
48  static const int scalar_count = 3;
49  Vector3(const Vector3& other) { *this = other; }
50  Vector3() { x() = y() = z() = 0; }
51 
52  template<class T>
53  explicit Vector3(const T& other)
54  {
55  x() = (T_Scalar)other.x();
56  y() = (T_Scalar)other.y();
57  z() = (T_Scalar)other.z();
58  }
59 
60  explicit Vector3(T_Scalar val)
61  {
62  mScalar[0] = mScalar[1] = mScalar[2] = val;
63  }
64 
65  //explicit Vector3(const T_Scalar* pval)
66  //{
67  // mScalar[0] = pval[0];
68  // mScalar[1] = pval[1];
69  // mScalar[2] = pval[2];
70  //}
71 
72  explicit Vector3(T_Scalar x, T_Scalar y, T_Scalar z)
73  {
74  mScalar[0] = x;
75  mScalar[1] = y;
76  mScalar[2] = z;
77  }
78 
79  explicit Vector3(const Vector2<T_Scalar>& v, T_Scalar z)
80  {
81  mScalar[0] = v.x();
82  mScalar[1] = v.y();
83  mScalar[2] = z;
84  }
85 
86  T_Scalar* ptr() { return mScalar; }
87  const T_Scalar* ptr() const { return mScalar; }
88 
89  const T_Scalar& x() const { return mScalar[0]; }
90  const T_Scalar& y() const { return mScalar[1]; }
91  const T_Scalar& z() const { return mScalar[2]; }
92 
93  T_Scalar& x() { return mScalar[0]; }
94  T_Scalar& y() { return mScalar[1]; }
95  T_Scalar& z() { return mScalar[2]; }
96 
97  const T_Scalar& r() const { return mScalar[0]; }
98  const T_Scalar& g() const { return mScalar[1]; }
99  const T_Scalar& b() const { return mScalar[2]; }
100 
101  T_Scalar& r() { return mScalar[0]; }
102  T_Scalar& g() { return mScalar[1]; }
103  T_Scalar& b() { return mScalar[2]; }
104 
105  const T_Scalar& s() const { return mScalar[0]; }
106  const T_Scalar& t() const { return mScalar[1]; }
107  const T_Scalar& p() const { return mScalar[2]; }
108 
109  T_Scalar& s() { return mScalar[0]; }
110  T_Scalar& t() { return mScalar[1]; }
111  T_Scalar& p() { return mScalar[2]; }
112 
113  Vector2<T_Scalar> xy() const { return Vector2<T_Scalar>(x(),y()); }
114  Vector2<T_Scalar> st() const { return Vector2<T_Scalar>(x(),y()); }
115 
116  Vector3 operator+(const Vector3& other) const
117  {
118  return Vector3(x()+other.x(), y()+other.y(), z()+other.z());
119  }
120  Vector3 operator-(const Vector3& other) const
121  {
122  return Vector3(x()-other.x(), y()-other.y(), z()-other.z());
123  }
124  Vector3 operator*(const Vector3& other) const
125  {
126  return Vector3(x()*other.x(), y()*other.y(), z()*other.z());
127  }
128  Vector3 operator/(const Vector3& other) const
129  {
130  return Vector3(x()/other.x(), y()/other.y(), z()/other.z());
131  }
132  Vector3 operator+(T_Scalar val) const
133  {
134  return Vector3(x()+val, y()+val, z()+val);
135  }
136  Vector3 operator-(T_Scalar val) const
137  {
138  return Vector3(x()-val, y()-val, z()-val);
139  }
140  Vector3 operator*(T_Scalar val) const
141  {
142  return Vector3(x()*val, y()*val, z()*val);
143  }
144  Vector3 operator/(T_Scalar val) const
145  {
146  return Vector3(x()/val, y()/val, z()/val);
147  }
149  {
150  return Vector3(-x(), -y(), -z());
151  }
152  Vector3& operator+=(const Vector3& other)
153  {
154  *this = *this + other;
155  return *this;
156  }
157  Vector3& operator-=(const Vector3& other)
158  {
159  *this = *this - other;
160  return *this;
161  }
162  Vector3& operator*=(const Vector3& other)
163  {
164  *this = *this * other;
165  return *this;
166  }
167  Vector3& operator/=(const Vector3& other)
168  {
169  *this = *this / other;
170  return *this;
171  }
172  Vector3& operator+=(T_Scalar val)
173  {
174  *this = *this + val;
175  return *this;
176  }
177  Vector3& operator-=(T_Scalar val)
178  {
179  *this = *this - val;
180  return *this;
181  }
182  Vector3& operator*=(T_Scalar val)
183  {
184  *this = *this * val;
185  return *this;
186  }
187  Vector3& operator/=(T_Scalar val)
188  {
189  *this = *this / val;
190  return *this;
191  }
192  Vector3& operator=(const Vector3& other)
193  {
194  x() = other.x();
195  y() = other.y();
196  z() = other.z();
197  return *this;
198  }
199  Vector3& operator=(T_Scalar val)
200  {
201  x() = y() = z() = val;
202  return *this;
203  }
204  bool operator==(const Vector3& other) const
205  {
206  return x() == other.x() && y() == other.y() && z() == other.z();
207  }
208  bool operator!=(const Vector3& other) const
209  {
210  return !operator==(other);
211  }
212  bool operator<(const Vector3& other) const
213  {
214  if (x() != other.x())
215  return x() < other.x();
216  else
217  if (y() != other.y())
218  return y() < other.y();
219  else
220  return z() < other.z();
221  }
222  T_Scalar& operator[](unsigned i) { return mScalar[i]; }
223  const T_Scalar& operator[](unsigned i) const { return mScalar[i]; }
224  T_Scalar length() const { return ::sqrt(x()*x()+y()*y()+z()*z()); }
225  T_Scalar lengthSquared() const { return x()*x()+y()*y()+z()*z(); }
226  bool isNull() const { return !x() && !y() && !z(); }
227  const Vector3& normalize(T_Scalar *len=NULL)
228  {
229  T_Scalar l = length();
230  if (len)
231  *len = l;
232  if (l)
233  *this *= (T_Scalar)(1.0/l);
234  return *this;
235  }
236 
237  protected:
239  };
240 
241  template<typename T>
242  inline const Vector3<T> operator*(T val, const Vector3<T>& v)
243  {
244  return v * val;
245  }
246 
263 
264  #if VL_PIPELINE_PRECISION == 2
265  typedef dvec3 vec3;
267  #else
268  typedef fvec3 vec3;
270  #endif
271 
272  inline float dot(const fvec3& v1, const fvec3& v2) { return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z(); }
273  inline double dot(const dvec3& v1, const dvec3& v2) { return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z(); }
274  inline float dot(const ivec3& v1, const ivec3& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z()); }
275  inline float dot(const uvec3& v1, const uvec3& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z()); }
276 
277  inline fvec3 cross(const fvec3& v1, const fvec3& v2)
278  {
279  fvec3 t;
280  t.x() = v1.y()*v2.z() - v1.z()*v2.y();
281  t.y() = v1.z()*v2.x() - v1.x()*v2.z();
282  t.z() = v1.x()*v2.y() - v1.y()*v2.x();
283  return t;
284  }
285 
286  inline dvec3 cross(const dvec3& v1, const dvec3& v2)
287  {
288  dvec3 t;
289  t.x() = v1.y()*v2.z() - v1.z()*v2.y();
290  t.y() = v1.z()*v2.x() - v1.x()*v2.z();
291  t.z() = v1.x()*v2.y() - v1.y()*v2.x();
292  return t;
293  }
294 
295  inline fvec3 min(const fvec3& a, const fvec3& b)
296  {
297  return fvec3( a.x() < b.x() ? a.x() : b.x(),
298  a.y() < b.y() ? a.y() : b.y(),
299  a.z() < b.z() ? a.z() : b.z() );
300  }
301  inline fvec3 min(const fvec3& a, float b)
302  {
303  return fvec3( a.x() < b ? a.x() : b,
304  a.y() < b ? a.y() : b,
305  a.z() < b ? a.z() : b );
306  }
307  inline dvec3 min(const dvec3& a, const dvec3& b)
308  {
309  return dvec3( a.x() < b.x() ? a.x() : b.x(),
310  a.y() < b.y() ? a.y() : b.y(),
311  a.z() < b.z() ? a.z() : b.z() );
312  }
313  inline dvec3 min(const dvec3& a, double b)
314  {
315  return dvec3( a.x() < b ? a.x() : b,
316  a.y() < b ? a.y() : b,
317  a.z() < b ? a.z() : b );
318  }
319  inline ivec3 min(const ivec3& a, const ivec3& b)
320  {
321  return ivec3( a.x() < b.x() ? a.x() : b.x(),
322  a.y() < b.y() ? a.y() : b.y(),
323  a.z() < b.z() ? a.z() : b.z() );
324  }
325  inline ivec3 min(const ivec3& a, int b)
326  {
327  return ivec3( a.x() < b ? a.x() : b,
328  a.y() < b ? a.y() : b,
329  a.z() < b ? a.z() : b );
330  }
331  inline uvec3 min(const uvec3& a, const uvec3& b)
332  {
333  return uvec3( a.x() < b.x() ? a.x() : b.x(),
334  a.y() < b.y() ? a.y() : b.y(),
335  a.z() < b.z() ? a.z() : b.z() );
336  }
337  inline uvec3 min(const uvec3& a, unsigned int b)
338  {
339  return uvec3( a.x() < b ? a.x() : b,
340  a.y() < b ? a.y() : b,
341  a.z() < b ? a.z() : b );
342  }
343  inline fvec3 max(const fvec3& a, const fvec3& b)
344  {
345  return fvec3( a.x() > b.x() ? a.x() : b.x(),
346  a.y() > b.y() ? a.y() : b.y(),
347  a.z() > b.z() ? a.z() : b.z() );
348  }
349  inline fvec3 max(const fvec3& a, float b)
350  {
351  return fvec3( a.x() > b ? a.x() : b,
352  a.y() > b ? a.y() : b,
353  a.z() > b ? a.z() : b );
354  }
355  inline dvec3 max(const dvec3& a, const dvec3& b)
356  {
357  return dvec3( a.x() > b.x() ? a.x() : b.x(),
358  a.y() > b.y() ? a.y() : b.y(),
359  a.z() > b.z() ? a.z() : b.z() );
360  }
361  inline dvec3 max(const dvec3& a, double b)
362  {
363  return dvec3( a.x() > b ? a.x() : b,
364  a.y() > b ? a.y() : b,
365  a.z() > b ? a.z() : b );
366  }
367  inline ivec3 max(const ivec3& a, const ivec3& b)
368  {
369  return ivec3( a.x() > b.x() ? a.x() : b.x(),
370  a.y() > b.y() ? a.y() : b.y(),
371  a.z() > b.z() ? a.z() : b.z() );
372  }
373  inline ivec3 max(const ivec3& a, int b)
374  {
375  return ivec3( a.x() > b ? a.x() : b,
376  a.y() > b ? a.y() : b,
377  a.z() > b ? a.z() : b );
378  }
379  inline uvec3 max(const uvec3& a, const uvec3& b)
380  {
381  return uvec3( a.x() > b.x() ? a.x() : b.x(),
382  a.y() > b.y() ? a.y() : b.y(),
383  a.z() > b.z() ? a.z() : b.z() );
384  }
385  inline uvec3 max(const uvec3& a, unsigned int b)
386  {
387  return uvec3( a.x() > b ? a.x() : b,
388  a.y() > b ? a.y() : b,
389  a.z() > b ? a.z() : b );
390  }
391  inline fvec3 clamp(const fvec3& x, float minval, float maxval) { return min(max(x,minval),maxval); }
392  inline fvec3 clamp(const fvec3& x, const fvec3& minval, const fvec3& maxval) { return min(max(x,minval),maxval); }
393  inline ivec3 clamp(const ivec3& x, const ivec3& minval, const ivec3& maxval) { return min(max(x,minval),maxval); }
394  inline dvec3 clamp(const dvec3& x, double minval, double maxval) { return min(max(x,minval),maxval); }
395  inline dvec3 clamp(const dvec3& x, const dvec3& minval, const dvec3& maxval) { return min(max(x,minval),maxval); }
396  inline ivec3 clamp(const ivec3& x, int minval, int maxval) { return min(max(x,minval),maxval); }
397  inline uvec3 clamp(const uvec3& x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); }
398  inline uvec3 clamp(const uvec3& x, const uvec3& minval, const uvec3& maxval) { return min(max(x,minval),maxval); }
399 }
400 
401 #endif
Vector3 & operator=(T_Scalar val)
Definition: Vector3.hpp:199
T_Scalar & x()
Definition: Vector3.hpp:93
T_Scalar & s()
Definition: Vector3.hpp:109
float clamp(float x, float minval, float maxval)
Definition: Vector2.hpp:315
T_Scalar & g()
Definition: Vector3.hpp:102
bool operator==(const Vector3 &other) const
Definition: Vector3.hpp:204
const Vector3 & normalize(T_Scalar *len=NULL)
Definition: Vector3.hpp:227
Vector3(T_Scalar x, T_Scalar y, T_Scalar z)
Definition: Vector3.hpp:72
Vector3< float > fvec3
A 3 components vector with float precision.
Definition: Vector3.hpp:252
T sqrt(T a)
Definition: glsl_math.hpp:592
const T_Scalar & b() const
Definition: Vector3.hpp:99
Vector3 & operator+=(T_Scalar val)
Definition: Vector3.hpp:172
Vector3 & operator-=(const Vector3 &other)
Definition: Vector3.hpp:157
const T_Scalar & z() const
Definition: Vector3.hpp:91
Vector3 operator*(const Vector3 &other) const
Definition: Vector3.hpp:124
Vector3 & operator*=(T_Scalar val)
Definition: Vector3.hpp:182
Vector3(T_Scalar val)
Definition: Vector3.hpp:60
T_Scalar & t()
Definition: Vector3.hpp:110
bool operator!=(const Vector3 &other) const
Definition: Vector3.hpp:208
Vector3 operator*(T_Scalar val) const
Definition: Vector3.hpp:140
Vector3 operator-() const
Definition: Vector3.hpp:148
Vector3< unsigned char > ubvec3
A 3 components vector with unsigned char precision.
Definition: Vector3.hpp:258
fvec3 cross(const fvec3 &v1, const fvec3 &v2)
Definition: Vector3.hpp:277
T_Scalar mScalar[scalar_count]
Definition: Vector3.hpp:238
const T_Scalar & r() const
Definition: Vector3.hpp:97
Vector3 & operator/=(const Vector3 &other)
Definition: Vector3.hpp:167
Vector3 operator-(const Vector3 &other) const
Definition: Vector3.hpp:120
Vector2< T_Scalar > xy() const
Definition: Vector3.hpp:113
Visualization Library main namespace.
float dot(float a, float b)
Definition: glsl_math.hpp:1111
Vector3< short > svec3
A 3 components vector with short precision.
Definition: Vector3.hpp:260
Vector3(const T &other)
Definition: Vector3.hpp:53
T_Scalar scalar_type
Definition: Vector3.hpp:47
Vector3 operator-(T_Scalar val) const
Definition: Vector3.hpp:136
T_Scalar lengthSquared() const
Definition: Vector3.hpp:225
The Vector3 class is a template class that implements a generic 3 components vector, see also vl::fvec3, vl::dvec3, vl::uvec3, vl::ivec3, vl::svec3, vl::usvec3, vl::bvec3, vl::ubvec3.
Definition: Vector3.hpp:44
Vector3 operator/(const Vector3 &other) const
Definition: Vector3.hpp:128
float max(float a, float b)
Definition: Vector2.hpp:311
const T_Scalar & operator[](unsigned i) const
Definition: Vector3.hpp:223
float min(float a, float b)
Definition: Vector2.hpp:307
Vector3 operator/(T_Scalar val) const
Definition: Vector3.hpp:144
Vector3< double > dvec3
A 3 components vector with double precision.
Definition: Vector3.hpp:254
T_Scalar length() const
Definition: Vector3.hpp:224
Vector3 operator+(T_Scalar val) const
Definition: Vector3.hpp:132
const T_Scalar & y() const
Definition: Vector3.hpp:90
Vector3< unsigned int > uvec3
A 3 components vector with unsigned int precision.
Definition: Vector3.hpp:250
const T_Scalar & s() const
Definition: Vector3.hpp:105
const T_Scalar & p() const
Definition: Vector3.hpp:107
T_Scalar & b()
Definition: Vector3.hpp:103
T_Scalar & z()
Definition: Vector3.hpp:95
#define NULL
Definition: OpenGLDefs.hpp:81
Vector3< int > ivec3
A 3 components vector with int precision.
Definition: Vector3.hpp:248
T_Scalar * ptr()
Definition: Vector3.hpp:86
T_Scalar & p()
Definition: Vector3.hpp:111
Vector3 & operator+=(const Vector3 &other)
Definition: Vector3.hpp:152
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
fvec3 vec3
Defined as: &#39;typedef fvec3 vec3&#39;. See also VL_PIPELINE_PRECISION.
Definition: Vector3.hpp:269
const T_Scalar & t() const
Definition: Vector3.hpp:106
Vector3 & operator=(const Vector3 &other)
Definition: Vector3.hpp:192
Vector3(const Vector3 &other)
Definition: Vector3.hpp:49
bool operator<(const Vector3 &other) const
Definition: Vector3.hpp:212
const T_Scalar & x() const
Definition: Vector3.hpp:89
Vector2< T_Scalar > st() const
Definition: Vector3.hpp:114
const T_Scalar * ptr() const
Definition: Vector3.hpp:87
T_Scalar & y()
Definition: Vector3.hpp:94
Vector3(const Vector2< T_Scalar > &v, T_Scalar z)
Definition: Vector3.hpp:79
const T_Scalar & x() const
Definition: Vector2.hpp:132
Vector3 operator+(const Vector3 &other) const
Definition: Vector3.hpp:116
T_Scalar & r()
Definition: Vector3.hpp:101
bool isNull() const
Definition: Vector3.hpp:226
Vector3 & operator-=(T_Scalar val)
Definition: Vector3.hpp:177
Vector3< unsigned short > usvec3
A 3 components vector with unsigned short precision.
Definition: Vector3.hpp:262
T_Scalar & operator[](unsigned i)
Definition: Vector3.hpp:222
Vector3 & operator/=(T_Scalar val)
Definition: Vector3.hpp:187
static const int scalar_count
Definition: Vector3.hpp:48
const T_Scalar & y() const
Definition: Vector2.hpp:133
Vector3< char > bvec3
A 3 components vector with char precision.
Definition: Vector3.hpp:256
const T_Scalar & g() const
Definition: Vector3.hpp:98
Vector3 & operator*=(const Vector3 &other)
Definition: Vector3.hpp:162