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