Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://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 Vector4_INCLUDE_ONCE 00033 #define Vector4_INCLUDE_ONCE 00034 00035 #include <vlCore/Vector3.hpp> 00036 00037 namespace vl 00038 { 00043 template<typename T_Scalar> 00044 class Vector4 00045 { 00046 public: 00047 typedef T_Scalar scalar_type; 00048 static const int scalar_count = 4; 00049 Vector4(const Vector4& other) { *this = other; } 00050 Vector4() { x() = y() = z() = w() = 0; } 00051 00052 template<class T> 00053 explicit Vector4(const T& other) 00054 { 00055 x() = (T_Scalar)other.x(); 00056 y() = (T_Scalar)other.y(); 00057 z() = (T_Scalar)other.z(); 00058 w() = (T_Scalar)other.w(); 00059 } 00060 00061 explicit Vector4(T_Scalar x, T_Scalar y, T_Scalar z, T_Scalar w) 00062 { 00063 mScalar[0] = x; 00064 mScalar[1] = y; 00065 mScalar[2] = z; 00066 mScalar[3] = w; 00067 } 00068 00069 explicit Vector4(const Vector3<T_Scalar>& v, T_Scalar w) 00070 { 00071 mScalar[0] = v.x(); 00072 mScalar[1] = v.y(); 00073 mScalar[2] = v.z(); 00074 mScalar[3] = w; 00075 } 00076 00077 explicit Vector4(const Vector2<T_Scalar>& u, const Vector2<T_Scalar>& v) 00078 { 00079 mScalar[0] = u.x(); 00080 mScalar[1] = u.y(); 00081 mScalar[2] = v.x(); 00082 mScalar[3] = v.y(); 00083 } 00084 00085 T_Scalar* ptr() { return mScalar; } 00086 const T_Scalar* ptr() const { return mScalar; } 00087 00088 const T_Scalar& x() const { return mScalar[0]; } 00089 const T_Scalar& y() const { return mScalar[1]; } 00090 const T_Scalar& z() const { return mScalar[2]; } 00091 const T_Scalar& w() const { return mScalar[3]; } 00092 00093 T_Scalar& x() { return mScalar[0]; } 00094 T_Scalar& y() { return mScalar[1]; } 00095 T_Scalar& z() { return mScalar[2]; } 00096 T_Scalar& w() { return mScalar[3]; } 00097 00098 const T_Scalar& r() const { return mScalar[0]; } 00099 const T_Scalar& g() const { return mScalar[1]; } 00100 const T_Scalar& b() const { return mScalar[2]; } 00101 const T_Scalar& a() const { return mScalar[3]; } 00102 00103 T_Scalar& r() { return mScalar[0]; } 00104 T_Scalar& g() { return mScalar[1]; } 00105 T_Scalar& b() { return mScalar[2]; } 00106 T_Scalar& a() { return mScalar[3]; } 00107 00108 const T_Scalar& s() const { return mScalar[0]; } 00109 const T_Scalar& t() const { return mScalar[1]; } 00110 const T_Scalar& p() const { return mScalar[2]; } 00111 const T_Scalar& q() const { return mScalar[3]; } 00112 00113 T_Scalar& s() { return mScalar[0]; } 00114 T_Scalar& t() { return mScalar[1]; } 00115 T_Scalar& p() { return mScalar[2]; } 00116 T_Scalar& q() { return mScalar[3]; } 00117 00118 Vector3<T_Scalar> xyz() const { return Vector3<T_Scalar>(x(),y(),z()); } 00119 Vector3<T_Scalar> rgb() const { return Vector3<T_Scalar>(x(),y(),z()); } 00120 Vector3<T_Scalar> stp() const { return Vector3<T_Scalar>(x(),y(),z()); } 00121 00122 Vector2<T_Scalar> xy() const { return Vector2<T_Scalar>(x(),y()); } 00123 Vector2<T_Scalar> rg() const { return Vector2<T_Scalar>(x(),y()); } 00124 Vector2<T_Scalar> st() const { return Vector2<T_Scalar>(x(),y()); } 00125 00126 Vector4 operator+(const Vector4& other) const 00127 { 00128 return Vector4(x()+other.x(), y()+other.y(), z()+other.z(), w()+other.w()); 00129 } 00130 Vector4 operator-(const Vector4& other) const 00131 { 00132 return Vector4(x()-other.x(), y()-other.y(), z()-other.z(), w()-other.w()); 00133 } 00134 Vector4 operator*(const Vector4& other) const 00135 { 00136 return Vector4(x()*other.x(), y()*other.y(), z()*other.z(), w()*other.w()); 00137 } 00138 Vector4 operator/(const Vector4& other) const 00139 { 00140 return Vector4(x()/other.x(), y()/other.y(), z()/other.z(), w()/other.w()); 00141 } 00142 Vector4 operator+(T_Scalar val) const 00143 { 00144 return Vector4(x()+val, y()+val, z()+val, w()+val); 00145 } 00146 Vector4 operator-(T_Scalar val) const 00147 { 00148 return Vector4(x()-val, y()-val, z()-val, w()-val); 00149 } 00150 Vector4 operator*(T_Scalar val) const 00151 { 00152 return Vector4(x()*val, y()*val, z()*val, w()*val); 00153 } 00154 Vector4 operator/(T_Scalar val) const 00155 { 00156 return Vector4(x()/val, y()/val, z()/val, w()/val); 00157 } 00158 Vector4 operator-() const 00159 { 00160 return Vector4(-x(), -y(), -z(), -w()); 00161 } 00162 Vector4& operator+=(const Vector4& other) 00163 { 00164 *this = *this + other; 00165 return *this; 00166 } 00167 Vector4& operator-=(const Vector4& other) 00168 { 00169 *this = *this - other; 00170 return *this; 00171 } 00172 Vector4& operator*=(const Vector4& other) 00173 { 00174 *this = *this * other; 00175 return *this; 00176 } 00177 Vector4& operator/=(const Vector4& other) 00178 { 00179 *this = *this / other; 00180 return *this; 00181 } 00182 Vector4& operator+=(T_Scalar val) 00183 { 00184 *this = *this + val; 00185 return *this; 00186 } 00187 Vector4& operator-=(T_Scalar val) 00188 { 00189 *this = *this - val; 00190 return *this; 00191 } 00192 Vector4& operator*=(T_Scalar val) 00193 { 00194 *this = *this * val; 00195 return *this; 00196 } 00197 Vector4& operator/=(T_Scalar val) 00198 { 00199 *this = *this / val; 00200 return *this; 00201 } 00202 Vector4& operator=(const Vector4& other) 00203 { 00204 x() = other.x(); 00205 y() = other.y(); 00206 z() = other.z(); 00207 w() = other.w(); 00208 return *this; 00209 } 00210 Vector4& operator=(T_Scalar val) 00211 { 00212 x() = y() = z() = w() = val; 00213 return *this; 00214 } 00215 bool operator==(const Vector4& other) const 00216 { 00217 return x() == other.x() && y() == other.y() && z() == other.z() && w() == other.w(); 00218 } 00219 bool operator!=(const Vector4& other) const 00220 { 00221 return !operator==(other); 00222 } 00223 bool operator<(const Vector4& other) const 00224 { 00225 if (x() != other.x()) 00226 return x() < other.x(); 00227 else 00228 if (y() != other.y()) 00229 return y() < other.y(); 00230 else 00231 if (z() != other.z()) 00232 return z() < other.z(); 00233 else 00234 return w() < other.w(); 00235 } 00236 T_Scalar& operator[](unsigned i) { return mScalar[i]; } 00237 const T_Scalar& operator[](unsigned i) const { return mScalar[i]; } 00238 T_Scalar length() const { return (T_Scalar)::sqrt(x()*x()+y()*y()+z()*z()+w()*w()); } 00239 T_Scalar lengthSquared() const { return x()*x()+y()*y()+z()*z()+w()*w(); } 00240 bool isNull() const { return !x() && !y() && !z() && !w(); } 00241 const Vector4& normalize(T_Scalar *len=NULL) 00242 { 00243 T_Scalar l = length(); 00244 if (len) 00245 *len = l; 00246 if (l) 00247 *this *= (T_Scalar)(1.0/l); 00248 return *this; 00249 } 00250 00251 protected: 00252 T_Scalar mScalar[scalar_count]; 00253 }; 00254 00255 template<typename T> 00256 inline const Vector4<T> operator*(T val, const Vector4<T>& v) 00257 { 00258 return v * val; 00259 } 00260 00262 typedef Vector4<int> ivec4; 00264 typedef Vector4<unsigned int> uvec4; 00266 typedef Vector4<float> fvec4; 00268 typedef Vector4<double> dvec4; 00270 typedef Vector4<char> bvec4; 00272 typedef Vector4<unsigned char> ubvec4; 00274 typedef Vector4<short> svec4; 00276 typedef Vector4<unsigned short> usvec4; 00277 00278 #if VL_PIPELINE_PRECISION == 2 00279 00280 typedef dvec4 vec4; 00281 #else 00282 00283 typedef fvec4 vec4; 00284 #endif 00285 00286 inline float dot(const fvec4& v1, const fvec4& v2) { return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() + v1.w()*v2.w(); } 00287 inline double dot(const dvec4& v1, const dvec4& v2) { return v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() + v1.w()*v2.w(); } 00288 inline float dot(const ivec4& v1, const ivec4& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() + v1.w()*v2.w()); } 00289 inline float dot(const uvec4& v1, const uvec4& v2) { return (float)(v1.x()*v2.x() + v1.y()*v2.y() + v1.z()*v2.z() + v1.w()*v2.w()); } 00290 00291 inline fvec4 min(const fvec4& a, const fvec4& b) 00292 { 00293 return fvec4( a.x() < b.x() ? a.x() : b.x(), 00294 a.y() < b.y() ? a.y() : b.y(), 00295 a.z() < b.z() ? a.z() : b.z(), 00296 a.w() < b.w() ? a.w() : b.w() ); 00297 } 00298 inline fvec4 min(const fvec4& a, float b) 00299 { 00300 return fvec4( a.x() < b ? a.x() : b, 00301 a.y() < b ? a.y() : b, 00302 a.z() < b ? a.z() : b, 00303 a.w() < b ? a.w() : b ); 00304 } 00305 inline dvec4 min(const dvec4& a, const dvec4& b) 00306 { 00307 return dvec4( a.x() < b.x() ? a.x() : b.x(), 00308 a.y() < b.y() ? a.y() : b.y(), 00309 a.z() < b.z() ? a.z() : b.z(), 00310 a.w() < b.w() ? a.w() : b.w() ); 00311 } 00312 inline dvec4 min(const dvec4& a, double b) 00313 { 00314 return dvec4( a.x() < b ? a.x() : b, 00315 a.y() < b ? a.y() : b, 00316 a.z() < b ? a.z() : b, 00317 a.w() < b ? a.w() : b ); 00318 } 00319 inline ivec4 min(const ivec4& a, const ivec4& b) 00320 { 00321 return ivec4( 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 a.w() < b.w() ? a.w() : b.w() ); 00325 } 00326 inline ivec4 min(const ivec4& a, int b) 00327 { 00328 return ivec4( a.x() < b ? a.x() : b, 00329 a.y() < b ? a.y() : b, 00330 a.z() < b ? a.z() : b, 00331 a.w() < b ? a.w() : b ); 00332 } 00333 inline uvec4 min(const uvec4& a, const uvec4& b) 00334 { 00335 return uvec4( a.x() < b.x() ? a.x() : b.x(), 00336 a.y() < b.y() ? a.y() : b.y(), 00337 a.z() < b.z() ? a.z() : b.z(), 00338 a.w() < b.w() ? a.w() : b.w() ); 00339 } 00340 inline uvec4 min(const uvec4& a, unsigned int b) 00341 { 00342 return uvec4( a.x() < b ? a.x() : b, 00343 a.y() < b ? a.y() : b, 00344 a.z() < b ? a.z() : b, 00345 a.w() < b ? a.w() : b ); 00346 } 00347 inline fvec4 max(const fvec4& a, const fvec4& b) 00348 { 00349 return fvec4( a.x() > b.x() ? a.x() : b.x(), 00350 a.y() > b.y() ? a.y() : b.y(), 00351 a.z() > b.z() ? a.z() : b.z(), 00352 a.w() > b.w() ? a.w() : b.w() ); 00353 } 00354 inline fvec4 max(const fvec4& a, float b) 00355 { 00356 return fvec4( a.x() > b ? a.x() : b, 00357 a.y() > b ? a.y() : b, 00358 a.z() > b ? a.z() : b, 00359 a.w() > b ? a.w() : b ); 00360 } 00361 inline dvec4 max(const dvec4& a, const dvec4& b) 00362 { 00363 return dvec4( a.x() > b.x() ? a.x() : b.x(), 00364 a.y() > b.y() ? a.y() : b.y(), 00365 a.z() > b.z() ? a.z() : b.z(), 00366 a.w() > b.w() ? a.w() : b.w() ); 00367 } 00368 inline dvec4 max(const dvec4& a, double b) 00369 { 00370 return dvec4( a.x() > b ? a.x() : b, 00371 a.y() > b ? a.y() : b, 00372 a.z() > b ? a.z() : b, 00373 a.w() > b ? a.w() : b ); 00374 } 00375 inline ivec4 max(const ivec4& a, const ivec4& b) 00376 { 00377 return ivec4( a.x() > b.x() ? a.x() : b.x(), 00378 a.y() > b.y() ? a.y() : b.y(), 00379 a.z() > b.z() ? a.z() : b.z(), 00380 a.w() > b.w() ? a.w() : b.w() ); 00381 } 00382 inline ivec4 max(const ivec4& a, int b) 00383 { 00384 return ivec4( a.x() > b ? a.x() : b, 00385 a.y() > b ? a.y() : b, 00386 a.z() > b ? a.z() : b, 00387 a.w() > b ? a.w() : b ); 00388 } 00389 inline uvec4 max(const uvec4& a, const uvec4& b) 00390 { 00391 return uvec4( a.x() > b.x() ? a.x() : b.x(), 00392 a.y() > b.y() ? a.y() : b.y(), 00393 a.z() > b.z() ? a.z() : b.z(), 00394 a.w() > b.w() ? a.w() : b.w() ); 00395 } 00396 inline uvec4 max(const uvec4& a, unsigned int b) 00397 { 00398 return uvec4( a.x() > b ? a.x() : b, 00399 a.y() > b ? a.y() : b, 00400 a.z() > b ? a.z() : b, 00401 a.w() > b ? a.w() : b ); 00402 } 00403 inline fvec4 clamp(const fvec4& x, float minval, float maxval) { return min(max(x,minval),maxval); } 00404 inline fvec4 clamp(const fvec4& x, const fvec4& minval, const fvec4& maxval) { return min(max(x,minval),maxval); } 00405 inline dvec4 clamp(const dvec4& x, double minval, double maxval) { return min(max(x,minval),maxval); } 00406 inline dvec4 clamp(const dvec4& x, const dvec4& minval, const dvec4& maxval) { return min(max(x,minval),maxval); } 00407 inline ivec4 clamp(const ivec4& x, int minval, int maxval) { return min(max(x,minval),maxval); } 00408 inline ivec4 clamp(const ivec4& x, const ivec4& minval, const ivec4& maxval) { return min(max(x,minval),maxval); } 00409 inline uvec4 clamp(const uvec4& x, unsigned int minval, unsigned int maxval) { return min(max(x,minval),maxval); } 00410 inline uvec4 clamp(const uvec4& x, const uvec4& minval, const uvec4& maxval) { return min(max(x,minval),maxval); } 00411 } 00412 00413 #endif