Visualization Library

A lightweight C++ OpenGL middleware for 2D/3D graphics
[Home] [Tutorials] [All Classes] [Grouped Classes]

X:/dropbox/visualizationlibrary/src/vlCore/Vector3.hpp

Go to the documentation of this file.
00001 /**************************************************************************************/
00002 /*                                                                                    */
00003 /*  Visualization Library                                                             */
00004 /*  http://www.visualizationlibrary.org                                               */
00005 /*                                                                                    */
00006 /*  Copyright (c) 2005-2010, Michele Bosi                                             */
00007 /*  All rights reserved.                                                              */
00008 /*                                                                                    */
00009 /*  Redistribution and use in source and binary forms, with or without modification,  */
00010 /*  are permitted provided that the following conditions are met:                     */
00011 /*                                                                                    */
00012 /*  - Redistributions of source code must retain the above copyright notice, this     */
00013 /*  list of conditions and the following disclaimer.                                  */
00014 /*                                                                                    */
00015 /*  - Redistributions in binary form must reproduce the above copyright notice, this  */
00016 /*  list of conditions and the following disclaimer in the documentation and/or       */
00017 /*  other materials provided with the distribution.                                   */
00018 /*                                                                                    */
00019 /*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
00020 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
00021 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
00022 /*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
00023 /*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
00024 /*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
00025 /*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
00026 /*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
00027 /*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
00028 /*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
00029 /*                                                                                    */
00030 /**************************************************************************************/
00031 
00032 #ifndef Vector3_INCLUDE_ONCE
00033 #define Vector3_INCLUDE_ONCE
00034 
00035 #include <vlCore/Vector2.hpp>
00036 
00037 namespace vl
00038 {
00043   template<typename T_Scalar>
00044   class Vector3
00045   {
00046   public:
00047     typedef T_Scalar scalar_type;
00048     static const int scalar_count = 3;
00049     Vector3(const Vector3& other) { *this = other; }
00050     Vector3() { x() = y() = z() = 0; }
00051 
00052     template<class T>
00053     explicit Vector3(const T& other)
00054     {
00055       x() = (T_Scalar)other.x();
00056       y() = (T_Scalar)other.y();
00057       z() = (T_Scalar)other.z();
00058     }
00059 
00060     explicit Vector3(T_Scalar x, T_Scalar y, T_Scalar z)
00061     {
00062       mScalar[0] = x;
00063       mScalar[1] = y;
00064       mScalar[2] = z;
00065     }
00066 
00067     explicit Vector3(const Vector2<T_Scalar>& v, T_Scalar z)
00068     {
00069       mScalar[0] = v.x();
00070       mScalar[1] = v.y();
00071       mScalar[2] = z;
00072     }
00073 
00074     T_Scalar* ptr() { return mScalar; }
00075     const T_Scalar* ptr() const { return mScalar; }
00076 
00077     const T_Scalar& x() const { return mScalar[0]; }
00078     const T_Scalar& y() const { return mScalar[1]; }
00079     const T_Scalar& z() const { return mScalar[2]; }
00080 
00081     T_Scalar& x() { return mScalar[0]; }
00082     T_Scalar& y() { return mScalar[1]; }
00083     T_Scalar& z() { return mScalar[2]; }
00084 
00085     const T_Scalar& r() const { return mScalar[0]; }
00086     const T_Scalar& g() const { return mScalar[1]; }
00087     const T_Scalar& b() const { return mScalar[2]; }
00088 
00089     T_Scalar& r() { return mScalar[0]; }
00090     T_Scalar& g() { return mScalar[1]; }
00091     T_Scalar& b() { return mScalar[2]; }
00092 
00093     const T_Scalar& s() const { return mScalar[0]; }
00094     const T_Scalar& t() const { return mScalar[1]; }
00095     const T_Scalar& p() const { return mScalar[2]; }
00096 
00097     T_Scalar& s() { return mScalar[0]; }
00098     T_Scalar& t() { return mScalar[1]; }
00099     T_Scalar& p() { return mScalar[2]; }
00100 
00101     Vector2<T_Scalar> xy() const { return Vector2<T_Scalar>(x(),y()); }
00102     Vector2<T_Scalar> st() const { return Vector2<T_Scalar>(x(),y()); }
00103 
00104     Vector3 operator+(const Vector3& other) const
00105     {
00106       return Vector3(x()+other.x(), y()+other.y(), z()+other.z());
00107     }
00108     Vector3 operator-(const Vector3& other) const
00109     {
00110       return Vector3(x()-other.x(), y()-other.y(), z()-other.z());
00111     }
00112     Vector3 operator*(const Vector3& other) const
00113     {
00114       return Vector3(x()*other.x(), y()*other.y(), z()*other.z());
00115     }
00116     Vector3 operator/(const Vector3& other) const
00117     {
00118       return Vector3(x()/other.x(), y()/other.y(), z()/other.z());
00119     }
00120     Vector3 operator+(T_Scalar val) const
00121     {
00122       return Vector3(x()+val, y()+val, z()+val);
00123     }
00124     Vector3 operator-(T_Scalar val) const
00125     {
00126       return Vector3(x()-val, y()-val, z()-val);
00127     }
00128     Vector3 operator*(T_Scalar val) const
00129     {
00130       return Vector3(x()*val, y()*val, z()*val);
00131     }
00132     Vector3 operator/(T_Scalar val) const
00133     {
00134       return Vector3(x()/val, y()/val, z()/val);
00135     }
00136     Vector3 operator-() const
00137     {
00138       return Vector3(-x(), -y(), -z());
00139     }
00140     Vector3& operator+=(const Vector3& other)
00141     {
00142       *this = *this + other;
00143       return *this;
00144     }
00145     Vector3& operator-=(const Vector3& other)
00146     {
00147       *this = *this - other;
00148       return *this;
00149     }
00150     Vector3& operator*=(const Vector3& other)
00151     {
00152       *this = *this * other;
00153       return *this;
00154     }
00155     Vector3& operator/=(const Vector3& other)
00156     {
00157       *this = *this / other;
00158       return *this;
00159     }
00160     Vector3& operator+=(T_Scalar val)
00161     {
00162       *this = *this + val;
00163       return *this;
00164     }
00165     Vector3& operator-=(T_Scalar val)
00166     {
00167       *this = *this - val;
00168       return *this;
00169     }
00170     Vector3& operator*=(T_Scalar val)
00171     {
00172       *this = *this * val;
00173       return *this;
00174     }
00175     Vector3& operator/=(T_Scalar val)
00176     {
00177       *this = *this / val;
00178       return *this;
00179     }
00180     Vector3& operator=(const Vector3& other)
00181     {
00182       x() = other.x();
00183       y() = other.y();
00184       z() = other.z();
00185       return *this;
00186     }
00187     Vector3& operator=(T_Scalar val)
00188     {
00189       x() = y() = z() = val;
00190       return *this;
00191     }
00192     bool operator==(const Vector3& other) const
00193     {
00194       return x() == other.x() && y() == other.y() && z() == other.z();
00195     }
00196     bool operator!=(const Vector3& other) const
00197     {
00198       return !operator==(other);
00199     }
00200     bool operator<(const Vector3& other) const
00201     {
00202       if (x() != other.x())
00203         return x() < other.x();
00204       else
00205       if (y() != other.y())
00206         return y() < other.y();
00207       else
00208         return z() < other.z();
00209     }
00210     T_Scalar& operator[](unsigned i) { return mScalar[i]; }
00211     const T_Scalar& operator[](unsigned i) const { return mScalar[i]; }
00212     T_Scalar length() const { return ::sqrt(x()*x()+y()*y()+z()*z()); }
00213     T_Scalar lengthSquared() const { return x()*x()+y()*y()+z()*z(); }
00214     bool isNull() const { return !x() && !y() && !z(); }
00215     const Vector3& normalize(T_Scalar *len=NULL)
00216     {
00217       T_Scalar l = length();
00218       if (len)
00219         *len = l;
00220       if (l)
00221         *this *= (T_Scalar)(1.0/l); 
00222       return *this; 
00223     }
00224 
00225   protected:
00226     T_Scalar mScalar[scalar_count];
00227   };
00228 
00229   template<typename T>
00230   inline const Vector3<T> operator*(T val, const Vector3<T>& v)
00231   {
00232     return v * val;
00233   }
00234 
00236   typedef Vector3<int> ivec3;
00238   typedef Vector3<unsigned int> uvec3;
00240   typedef Vector3<float> fvec3;
00242   typedef Vector3<double> dvec3;
00244   typedef Vector3<char> bvec3;
00246   typedef Vector3<unsigned char> ubvec3;
00248   typedef Vector3<short> svec3;
00250   typedef Vector3<unsigned short> usvec3;
00251 
00252   #if VL_PIPELINE_PRECISION == 2
00253 
00254     typedef dvec3 vec3;
00255   #else
00256 
00257     typedef fvec3 vec3;
00258   #endif
00259 
00260   inline float dot(const fvec3& v1, const fvec3& v2) { return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z(); }
00261   inline double dot(const dvec3& v1, const dvec3& v2) { return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z(); }
00262   inline float dot(const ivec3& v1, const ivec3& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z()); }
00263   inline float dot(const uvec3& v1, const uvec3& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z()); }
00264 
00265   inline fvec3 cross(const fvec3& v1, const fvec3& v2)
00266   {
00267     fvec3 t;
00268     t.x() = v1.y()*v2.z() - v1.z()*v2.y();
00269     t.y() = v1.z()*v2.x() - v1.x()*v2.z();
00270     t.z() = v1.x()*v2.y() - v1.y()*v2.x();
00271     return t;
00272   }
00273 
00274   inline dvec3 cross(const dvec3& v1, const dvec3& v2)
00275   {
00276     dvec3 t;
00277     t.x() = v1.y()*v2.z() - v1.z()*v2.y();
00278     t.y() = v1.z()*v2.x() - v1.x()*v2.z();
00279     t.z() = v1.x()*v2.y() - v1.y()*v2.x();
00280     return t;
00281   }
00282 
00283   inline fvec3 min(const fvec3& a, const fvec3& b)
00284   {
00285     return fvec3( a.x() < b.x() ? a.x() : b.x(),
00286       a.y() < b.y() ? a.y() : b.y(),
00287       a.z() < b.z() ? a.z() : b.z() );
00288   }
00289   inline fvec3 min(const fvec3& a, float b)
00290   {
00291     return fvec3( a.x() < b ? a.x() : b,
00292       a.y() < b ? a.y() : b,
00293       a.z() < b ? a.z() : b );
00294   }
00295   inline dvec3 min(const dvec3& a, const dvec3& b)
00296   {
00297     return dvec3( a.x() < b.x() ? a.x() : b.x(),
00298       a.y() < b.y() ? a.y() : b.y(),
00299       a.z() < b.z() ? a.z() : b.z() );
00300   }
00301   inline dvec3 min(const dvec3& a, double b)
00302   {
00303     return dvec3( a.x() < b ? a.x() : b,
00304       a.y() < b ? a.y() : b,
00305       a.z() < b ? a.z() : b );
00306   }
00307   inline ivec3 min(const ivec3& a, const ivec3& b)
00308   {
00309     return ivec3( a.x() < b.x() ? a.x() : b.x(),
00310       a.y() < b.y() ? a.y() : b.y(),
00311       a.z() < b.z() ? a.z() : b.z() );
00312   }
00313   inline ivec3 min(const ivec3& a, int b)
00314   {
00315     return ivec3( a.x() < b ? a.x() : b,
00316       a.y() < b ? a.y() : b,
00317       a.z() < b ? a.z() : b );
00318   }
00319   inline uvec3 min(const uvec3& a, const uvec3& b)
00320   {
00321     return uvec3( a.x() < b.x() ? a.x() : b.x(),
00322       a.y() < b.y() ? a.y() : b.y(),
00323       a.z() < b.z() ? a.z() : b.z() );
00324   }
00325   inline uvec3 min(const uvec3& a, unsigned int b)
00326   {
00327     return uvec3( a.x() < b ? a.x() : b,
00328       a.y() < b ? a.y() : b,
00329       a.z() < b ? a.z() : b );
00330   }
00331   inline fvec3 max(const fvec3& a, const fvec3& b)
00332   {
00333     return fvec3( a.x() > b.x() ? a.x() : b.x(),
00334       a.y() > b.y() ? a.y() : b.y(),
00335       a.z() > b.z() ? a.z() : b.z() );
00336   }
00337   inline fvec3 max(const fvec3& a, float b)
00338   {
00339     return fvec3( a.x() > b ? a.x() : b,
00340       a.y() > b ? a.y() : b,
00341       a.z() > b ? a.z() : b );
00342   }
00343   inline dvec3 max(const dvec3& a, const dvec3& b)
00344   {
00345     return dvec3( a.x() > b.x() ? a.x() : b.x(),
00346       a.y() > b.y() ? a.y() : b.y(),
00347       a.z() > b.z() ? a.z() : b.z() );
00348   }
00349   inline dvec3 max(const dvec3& a, double b)
00350   {
00351     return dvec3( a.x() > b ? a.x() : b,
00352       a.y() > b ? a.y() : b,
00353       a.z() > b ? a.z() : b );
00354   }
00355   inline ivec3 max(const ivec3& a, const ivec3& b)
00356   {
00357     return ivec3( a.x() > b.x() ? a.x() : b.x(),
00358       a.y() > b.y() ? a.y() : b.y(),
00359       a.z() > b.z() ? a.z() : b.z() );
00360   }
00361   inline ivec3 max(const ivec3& a, int b)
00362   {
00363     return ivec3( a.x() > b ? a.x() : b,
00364       a.y() > b ? a.y() : b,
00365       a.z() > b ? a.z() : b );
00366   }
00367   inline uvec3 max(const uvec3& a, const uvec3& b)
00368   {
00369     return uvec3( a.x() > b.x() ? a.x() : b.x(),
00370       a.y() > b.y() ? a.y() : b.y(),
00371       a.z() > b.z() ? a.z() : b.z() );
00372   }
00373   inline uvec3 max(const uvec3& a, unsigned int b)
00374   {
00375     return uvec3( a.x() > b ? a.x() : b,
00376       a.y() > b ? a.y() : b,
00377       a.z() > b ? a.z() : b );
00378   }
00379   inline fvec3 clamp(const fvec3& x, float minval, float maxval) { return min(max(x,minval),maxval); }
00380   inline fvec3 clamp(const fvec3& x, const fvec3& minval, const fvec3& maxval) { return min(max(x,minval),maxval); }
00381   inline ivec3 clamp(const ivec3& x, const ivec3& minval, const ivec3& maxval) { return min(max(x,minval),maxval); }
00382   inline dvec3 clamp(const dvec3& x, double minval, double maxval) { return min(max(x,minval),maxval); }
00383   inline dvec3 clamp(const dvec3& x, const dvec3& minval, const dvec3& maxval) { return min(max(x,minval),maxval); }
00384   inline ivec3 clamp(const ivec3& x, int minval, int maxval) { return min(max(x,minval),maxval); }
00385   inline uvec3 clamp(const uvec3& x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); }
00386   inline uvec3 clamp(const uvec3& x, const uvec3& minval, const uvec3& maxval) { return min(max(x,minval),maxval); }
00387 }
00388 
00389 #endif

Visualization Library 2011.09.1160 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 2 2013 13:40:32.
Permission is granted to use this page to write and publish articles regarding Visualization Library.