NVidia Gameworks
  • Main Page
  • Classes
  • Files
  • File List
  • File Members

NvVector.h

Go to the documentation of this file.
00001 // TAGRELEASE: CUSTOM
00002 
00003 /*
00004     Copyright (c) 2000 Cass Everitt
00005         Copyright (c) 2000 NVIDIA Corporation
00006     All rights reserved.
00007 
00008     Redistribution and use in source and binary forms, with or
00009         without modification, are permitted provided that the following
00010         conditions are met:
00011 
00012      * Redistributions of source code must retain the above
00013            copyright notice, this list of conditions and the following
00014            disclaimer.
00015 
00016      * Redistributions in binary form must reproduce the above
00017            copyright notice, this list of conditions and the following
00018            disclaimer in the documentation and/or other materials
00019            provided with the distribution.
00020 
00021      * The names of contributors to this software may not be used
00022            to endorse or promote products derived from this software
00023            without specific prior written permission.
00024 
00025        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00026            ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00027            LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00028            FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00029            REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00030            INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00031            BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00032            LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033            CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00034            LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00035            ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036            POSSIBILITY OF SUCH DAMAGE.
00037 
00038 
00039     Cass Everitt - cass@r3.nu
00040 */
00041 #ifndef NV_VECTOR_H
00042 #define NV_VECTOR_H
00043 
00044 #include <NvFoundation.h>
00045 
00048 
00049 #include <algorithm>
00050 
00051 namespace nv {
00052 
00053 template <class T> class vec2;
00054 template <class T> class vec3;
00055 template <class T> class vec4;
00056 
00058 template <class T>
00059 class vec2 {
00060 public:
00061 
00062     typedef T value_type;
00063     int32_t size() const { return 2;}
00064 
00066     vec2(const T & t = T()) {
00067         for(int32_t i = 0; i < size(); i++) _array[i] = t;
00068     }
00069 
00071     vec2(const T * tp) {
00072         for(int32_t i = 0; i < size(); i++) _array[i] = tp[i];
00073     }
00074 
00076     vec2( const T v0, const T v1) {
00077         x = v0;
00078         y = v1;
00079     }
00080 
00082     explicit vec2( const vec3<T> &u) {
00083         for(int32_t i = 0; i < size(); i++) _array[i] = u._array[i];
00084     }
00085 
00087     explicit vec2( const vec4<T> &u) {
00088         for(int32_t i = 0; i < size(); i++) _array[i] = u._array[i];
00089     }
00090 
00091     const T * get_value() const {
00092         return _array;
00093     }
00094 
00095     vec2<T> & set_value( const T * rhs ) {
00096         for(int32_t i = 0; i < size(); i++) _array[i] = rhs[i];
00097         return *this;
00098     }
00099 
00100     // indexing operators
00101     T & operator [] ( int32_t i ) {
00102         return _array[i];
00103     }
00104 
00105     const T & operator [] ( int32_t i ) const {
00106         return _array[i];
00107     }
00108 
00109     // type-cast operators
00110     operator T * () {
00111         return _array;
00112     }
00113 
00114     operator const T * () const {
00115         return _array;
00116     }
00117 
00119     //
00120     //  Math operators
00121     //
00123 
00124     // scalar multiply assign
00125     friend vec2<T> & operator *= ( vec2<T> &lhs, T d ) {
00126         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] *= d;
00127         return lhs;
00128     }
00129 
00130     // component-wise vector multiply assign
00131     friend vec2<T> & operator *= ( vec2<T> &lhs, const vec2<T> &rhs ) {
00132         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] *= rhs[i];
00133         return lhs;
00134     }
00135 
00136     // scalar divide assign
00137     friend vec2<T> & operator /= ( vec2<T> &lhs, T d ) {
00138         if(d == 0) return lhs;
00139         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] /= d;
00140         return lhs;
00141     }
00142 
00143     // component-wise vector divide assign
00144 //    friend vec2<T> & operator /= ( vec2<T> &lhs, const vec2<T> & rhs ) {
00145 //        for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] /= rhs._array[i];
00146 //        return *this;
00147 //    }
00148 
00149     // component-wise vector add assign
00150     friend vec2<T> & operator += ( vec2<T> &lhs, const vec2<T> & rhs ) {
00151         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] += rhs._array[i];
00152         return lhs;
00153     }
00154 
00155     // component-wise vector subtract assign
00156     friend vec2<T> & operator -= ( vec2<T> &lhs, const vec2<T> & rhs ) {
00157         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] -= rhs._array[i];
00158         return lhs;
00159     }
00160 
00161     // unary negate
00162     friend vec2<T> operator - ( const vec2<T> &rhs) {
00163         vec2<T> rv;
00164         for(int32_t i = 0; i < rhs.size(); i++) rv._array[i] = -rhs._array[i];
00165         return rv;
00166     }
00167 
00168     // vector add
00169     friend vec2<T> operator + ( const vec2<T> & lhs, const vec2<T> & rhs) {
00170         vec2<T> rt(lhs);
00171         return rt += rhs;
00172     }
00173 
00174     // vector subtract
00175     friend vec2<T> operator - ( const vec2<T> & lhs, const vec2<T> & rhs) {
00176         vec2<T> rt(lhs);
00177         return rt -= rhs;
00178     }
00179 
00180     // scalar multiply
00181     friend vec2<T> operator * ( const vec2<T> & lhs, T rhs) {
00182         vec2<T> rt(lhs);
00183         return rt *= rhs;
00184     }
00185 
00186     // scalar multiply
00187     friend vec2<T> operator * ( T lhs, const vec2<T> & rhs) {
00188         vec2<T> rt(lhs);
00189         return rt *= rhs;
00190     }
00191 
00192     // vector component-wise multiply
00193     friend vec2<T> operator * ( const vec2<T> & lhs, const vec2<T> & rhs){
00194         vec2<T> rt(lhs);
00195         return rt *= rhs;
00196     }
00197 
00198     // scalar multiply
00199     friend vec2<T> operator / ( const vec2<T> & lhs, T rhs) {
00200         vec2<T> rt(lhs);
00201         return rt /= rhs;
00202     }
00203 
00204     // vector component-wise multiply
00205     friend vec2<T> operator / ( const vec2<T> & lhs, const vec2<T> & rhs){
00206         vec2<T> rt(lhs);
00207         return rt /= rhs;
00208     }
00209 
00211     //
00212     //  Comparison operators
00213     //
00215 
00216     // equality
00217     friend bool operator == ( const vec2<T> &lhs, const vec2<T> &rhs ) {
00218         bool r = true;
00219         for (int32_t i = 0; i < lhs.size(); i++)
00220             r &= lhs._array[i] == rhs._array[i];
00221         return r;
00222     }
00223 
00224     // inequality
00225     friend bool operator != ( const vec2<T> &lhs, const vec2<T> &rhs ) {
00226         bool r = true;
00227         for (int32_t i = 0; i < lhs.size(); i++)
00228             r &= lhs._array[i] != rhs._array[i];
00229         return r;
00230     }
00231 
00232     //data intentionally left public to allow vec2.x
00233     union {
00234         struct {
00235             T x,y;          // standard names for components
00236         };
00237         struct {
00238             T s,t;          // standard names for components
00239         };
00240         T _array[2];     // array access
00241     };
00242 };
00243 
00245 //
00246 // vec3 - template class for 3-tuple vector
00247 //
00249 template <class T>
00250 class vec3 {
00251 public:
00252 
00253     typedef T value_type;
00254     int32_t size() const { return 3;}
00255 
00257     //
00258     //  Constructors
00259     //
00261 
00262     // Default/scalar constructor
00263     vec3(const T & t = T()) {
00264         for(int32_t i = 0; i < size(); i++) _array[i] = t;
00265     }
00266 
00267     // Construct from array
00268     vec3(const T * tp) {
00269         for(int32_t i = 0; i < size(); i++) _array[i] = tp[i];
00270     }
00271 
00272     // Construct from explicit values
00273     vec3( const T v0, const T v1, const T v2) {
00274         x = v0;
00275         y = v1;
00276         z = v2;
00277     }
00278 
00279     explicit vec3( const vec4<T> &u) {
00280         for(int32_t i = 0; i < size(); i++) _array[i] = u._array[i];
00281     }
00282 
00283     explicit vec3( const vec2<T> &u, T v0) {
00284         x = u.x;
00285         y = u.y;
00286         z = v0;
00287     }
00288 
00289     const T * get_value() const {
00290         return _array;
00291     }
00292 
00293     vec3<T> & set_value( const T * rhs ) {
00294         for(int32_t i = 0; i < size(); i++) _array[i] = rhs[i];
00295         return *this;
00296     }
00297 
00298     // indexing operators
00299     T & operator [] ( int32_t i ) {
00300         return _array[i];
00301     }
00302 
00303     const T & operator [] ( int32_t i ) const {
00304         return _array[i];
00305     }
00306 
00307     // type-cast operators
00308     operator T * () {
00309         return _array;
00310     }
00311 
00312     operator const T * () const {
00313         return _array;
00314     }
00315 
00317     //
00318     //  Math operators
00319     //
00321 
00322     // scalar multiply assign
00323     friend vec3<T> & operator *= ( vec3<T> &lhs, T d ) {
00324         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] *= d;
00325         return lhs;
00326     }
00327 
00328     // component-wise vector multiply assign
00329     friend vec3<T> & operator *= ( vec3<T> &lhs, const vec3<T> &rhs ) {
00330         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] *= rhs[i];
00331         return lhs;
00332     }
00333 
00334     // scalar divide assign
00335     friend vec3<T> & operator /= ( vec3<T> &lhs, T d ) {
00336         if(d == 0) return lhs;
00337         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] /= d;
00338         return lhs;
00339     }
00340 
00341     // component-wise vector divide assign
00342     friend vec3<T> & operator /= ( vec3<T> &lhs, const vec3<T> & rhs ) {
00343         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] /= rhs._array[i];
00344         return lhs;
00345     }
00346 
00347     // component-wise vector add assign
00348     friend vec3<T> & operator += ( vec3<T> &lhs, const vec3<T> & rhs ) {
00349         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] += rhs._array[i];
00350         return lhs;
00351     }
00352 
00353     // component-wise vector subtract assign
00354     friend vec3<T> & operator -= ( vec3<T> &lhs, const vec3<T> & rhs ) {
00355         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] -= rhs._array[i];
00356         return lhs;
00357     }
00358 
00359     // unary negate
00360     friend vec3<T> operator - ( const vec3<T> &rhs) {
00361         vec3<T> rv;
00362         for(int32_t i = 0; i < rhs.size(); i++) rv._array[i] = -rhs._array[i];
00363         return rv;
00364     }
00365 
00366     // vector add
00367     friend vec3<T> operator + ( const vec3<T> & lhs, const vec3<T> & rhs) {
00368         vec3<T> rt(lhs);
00369         return rt += rhs;
00370     }
00371 
00372     // vector subtract
00373     friend vec3<T> operator - ( const vec3<T> & lhs, const vec3<T> & rhs) {
00374         vec3<T> rt(lhs);
00375         return rt -= rhs;
00376     }
00377 
00378     // scalar multiply
00379     friend vec3<T> operator * ( const vec3<T> & lhs, T rhs) {
00380         vec3<T> rt(lhs);
00381         return rt *= rhs;
00382     }
00383 
00384     // scalar multiply
00385     friend vec3<T> operator * ( T lhs, const vec3<T> & rhs) {
00386         vec3<T> rt(lhs);
00387         return rt *= rhs;
00388     }
00389 
00390     // vector component-wise multiply
00391     friend vec3<T> operator * ( const vec3<T> & lhs, const vec3<T> & rhs){
00392         vec3<T> rt(lhs);
00393         return rt *= rhs;
00394     }
00395 
00396     // scalar multiply
00397     friend vec3<T> operator / ( const vec3<T> & lhs, T rhs) {
00398         vec3<T> rt(lhs);
00399         return rt /= rhs;
00400     }
00401 
00402     // vector component-wise multiply
00403     friend vec3<T> operator / ( const vec3<T> & lhs, const vec3<T> & rhs){
00404         vec3<T> rt(lhs);
00405         return rt /= rhs;
00406     }
00407 
00408     // vector - axis rotation //harish
00409     void rotate(float angle, T vx, T vy, T vz) //harish
00410     {
00411     vec3<T> NewPosition;
00412 
00413     // Calculate the sine and cosine of the angle once
00414     float cosTheta = (float)cos(angle);
00415     float sinTheta = (float)sin(angle);
00416 
00417 
00418     NewPosition. _array[0]  = (cosTheta + (1 - cosTheta) * vx * vx)                     * _array[0];
00419     NewPosition. _array[0] += ((1 - cosTheta) * vx * vy - vz * sinTheta)        * _array[1];
00420     NewPosition. _array[0] += ((1 - cosTheta) * vx * vz + vy * sinTheta)        * _array[2];
00421 
00422 
00423     NewPosition._array[1]  = ((1 - cosTheta) * vx * vy + vz * sinTheta) * _array[0];
00424     NewPosition._array[1] += (cosTheta + (1 - cosTheta) * vy * vy)              * _array[1];
00425     NewPosition._array[1] += ((1 - cosTheta) * vy * vz - vx * sinTheta) * _array[2];
00426 
00427     NewPosition._array[2]  = ((1 - cosTheta) * vx * vz - vy * sinTheta) *  _array[0];
00428     NewPosition._array[2] += ((1 - cosTheta) * vy * vz + vx * sinTheta) *  _array[1];
00429     NewPosition._array[2] += (cosTheta + (1 - cosTheta) * vz * vz)              *  _array[2];
00430 
00431     _array[0]=NewPosition._array[0];
00432     _array[1]=NewPosition._array[1];
00433     _array[2]=NewPosition._array[2];
00434     }
00435 
00436     //Calculate the cross product
00437     vec3<T> cross(vec3<T> vec) //harish
00438     {
00439             vec3<T> vNormal;                                                                    // The vector to hold the cross product
00440 
00441             vNormal._array[0] = ((_array[1] * vec._array[2]) - (_array[2] * vec._array[1]));
00442             vNormal._array[1] = ((_array[2] * vec._array[0]) - (_array[0] * vec._array[2]));
00443             vNormal._array[2] = ((_array[0] * vec._array[1]) - (_array[1] * vec._array[0]));
00444 
00445             return vNormal;
00446     }
00447 
00449     //
00450     //  Comparison operators
00451     //
00453 
00454     // equality
00455     friend bool operator == ( const vec3<T> &lhs, const vec3<T> &rhs ) {
00456         bool r = true;
00457         for (int32_t i = 0; i < lhs.size(); i++)
00458             r &= lhs._array[i] == rhs._array[i];
00459         return r;
00460     }
00461 
00462     // inequality
00463     friend bool operator != ( const vec3<T> &lhs, const vec3<T> &rhs ) {
00464         bool r = true;
00465         for (int32_t i = 0; i < lhs.size(); i++)
00466             r &= lhs._array[i] != rhs._array[i];
00467         return r;
00468     }
00469 
00471     //
00472     // dimension specific operations
00473     //
00475 
00476     // cross product
00477     friend vec3<T> cross( const vec3<T> & lhs, const vec3<T> & rhs) {
00478         vec3<T> r;
00479 
00480         r.x = lhs.y * rhs.z - lhs.z * rhs.y;
00481         r.y = lhs.z * rhs.x - lhs.x * rhs.z;
00482         r.z = lhs.x * rhs.y - lhs.y * rhs.x;
00483 
00484         return r;
00485     }
00486 
00487     //data intentionally left public to allow vec2.x
00488     union {
00489         struct {
00490             T x, y, z;          // standard names for components
00491         };
00492         struct {
00493             T s, t, r;          // standard names for components
00494         };
00495         T _array[3];     // array access
00496     };
00497 };
00498 
00500 //
00501 // vec4 - template class for 4-tuple vector
00502 //
00504 template <class T>
00505 class vec4 {
00506 public:
00507 
00508     typedef T value_type;
00509     int32_t size() const { return 4;}
00510 
00512     //
00513     //  Constructors
00514     //
00516 
00517     // Default/scalar constructor
00518     vec4(const T & t = T()) {
00519         for(int32_t i = 0; i < size(); i++) _array[i] = t;
00520     }
00521 
00522     // Construct from array
00523     vec4(const T * tp) {
00524         for(int32_t i = 0; i < size(); i++) _array[i] = tp[i];
00525     }
00526 
00527     // Construct from explicit values
00528     vec4( const T v0, const T v1, const T v2, const T v3) {
00529         x = v0;
00530         y = v1;
00531         z = v2;
00532         w = v3;
00533     }
00534 
00535     explicit vec4( const vec3<T> &u, T v0) {
00536         x = u.x;
00537         y = u.y;
00538         z = u.z;
00539         w = v0;
00540     }
00541 
00542     explicit vec4( const vec2<T> &u, T v0, T v1) {
00543         x = u.x;
00544         y = u.y;
00545         z = v0;
00546         w = v1;
00547     }
00548 
00549     const T * get_value() const {
00550         return _array;
00551     }
00552 
00553     vec4<T> & set_value( const T * rhs ) {
00554         for(int32_t i = 0; i < size(); i++) _array[i] = rhs[i];
00555         return *this;
00556     }
00557 
00558     // indexing operators
00559     T & operator [] ( int32_t i ) {
00560         return _array[i];
00561     }
00562 
00563     const T & operator [] ( int32_t i ) const {
00564         return _array[i];
00565     }
00566 
00567     // type-cast operators
00568     operator T * () {
00569         return _array;
00570     }
00571 
00572     operator const T * () const {
00573         return _array;
00574     }
00575 
00577     //
00578     //  Math operators
00579     //
00581 
00582     // scalar multiply assign
00583     friend vec4<T> & operator *= ( vec4<T> &lhs, T d ) {
00584         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] *= d;
00585         return lhs;
00586     }
00587 
00588     // component-wise vector multiply assign
00589     friend vec4<T> & operator *= ( vec4<T> &lhs, const vec4<T> &rhs ) {
00590         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] *= rhs[i];
00591         return lhs;
00592     }
00593 
00594     // scalar divide assign
00595     friend vec4<T> & operator /= ( vec4<T> &lhs, T d ) {
00596         if(d == 0) return lhs;
00597         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] /= d;
00598         return lhs;
00599     }
00600 
00601     // component-wise vector divide assign
00602     friend vec4<T> & operator /= ( vec4<T> &lhs, const vec4<T> & rhs ) {
00603         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] /= rhs._array[i];
00604         return lhs;
00605     }
00606 
00607     // component-wise vector add assign
00608     friend vec4<T> & operator += ( vec4<T> &lhs, const vec4<T> & rhs ) {
00609         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] += rhs._array[i];
00610         return lhs;
00611     }
00612 
00613     // component-wise vector subtract assign
00614     friend vec4<T> & operator -= ( vec4<T> &lhs, const vec4<T> & rhs ) {
00615         for(int32_t i = 0; i < lhs.size(); i++) lhs._array[i] -= rhs._array[i];
00616         return lhs;
00617     }
00618 
00619     // unary negate
00620     friend vec4<T> operator - ( const vec4<T> &rhs) {
00621         vec4<T> rv;
00622         for(int32_t i = 0; i < rhs.size(); i++) rv._array[i] = -rhs._array[i];
00623         return rv;
00624     }
00625 
00626     // vector add
00627     friend vec4<T> operator + ( const vec4<T> & lhs, const vec4<T> & rhs) {
00628         vec4<T> rt(lhs);
00629         return rt += rhs;
00630     }
00631 
00632     // vector subtract
00633     friend vec4<T> operator - ( const vec4<T> & lhs, const vec4<T> & rhs) {
00634         vec4<T> rt(lhs);
00635         return rt -= rhs;
00636     }
00637 
00638     // scalar multiply
00639     friend vec4<T> operator * ( const vec4<T> & lhs, T rhs) {
00640         vec4<T> rt(lhs);
00641         return rt *= rhs;
00642     }
00643 
00644     // scalar multiply
00645     friend vec4<T> operator * ( T lhs, const vec4<T> & rhs) {
00646         vec4<T> rt(lhs);
00647         return rt *= rhs;
00648     }
00649 
00650     // vector component-wise multiply
00651     friend vec4<T> operator * ( const vec4<T> & lhs, const vec4<T> & rhs){
00652         vec4<T> rt(lhs);
00653         return rt *= rhs;
00654     }
00655 
00656     // scalar multiply
00657     friend vec4<T> operator / ( const vec4<T> & lhs, T rhs) {
00658         vec4<T> rt(lhs);
00659         return rt /= rhs;
00660     }
00661 
00662     // vector component-wise multiply
00663     friend vec4<T> operator / ( const vec4<T> & lhs, const vec4<T> & rhs){
00664         vec4<T> rt(lhs);
00665         return rt /= rhs;
00666     }
00667 
00669     //
00670     //  Comparison operators
00671     //
00673 
00674     // equality
00675     friend bool operator == ( const vec4<T> &lhs, const vec4<T> &rhs ) {
00676         bool r = true;
00677         for (int32_t i = 0; i < lhs.size(); i++)
00678             r &= lhs._array[i] == rhs._array[i];
00679         return r;
00680     }
00681 
00682     // inequality
00683     friend bool operator != ( const vec4<T> &lhs, const vec4<T> &rhs ) {
00684         bool r = true;
00685         for (int32_t i = 0; i < lhs.size(); i++)
00686             r &= lhs._array[i] != rhs._array[i];
00687         return r;
00688     }
00689 
00690     //data intentionally left public to allow vec2.x
00691     union {
00692         struct {
00693             T x, y, z, w;          // standard names for components
00694         };
00695         struct {
00696             T s, t, r, q;          // standard names for components
00697         };
00698         T _array[4];     // array access
00699     };
00700 };
00701 
00702 
00703 
00705 //
00706 // Generic vector operations
00707 //
00709 
00710 // compute the dot product of two vectors
00711 //template<class T>
00712 //inline typename T::value_type dot( const T & lhs, const T & rhs ) {
00713 //    T::value_type r = 0;
00714 //    for(int32_t i = 0; i < lhs.size(); i++) r += lhs._array[i] * rhs._array[i];
00715 //    return r;
00716 //}
00717 
00718 template<class T>
00719 inline T dot(const vec2<T> & lhs, const vec2<T> & rhs ) {
00720         T r = 0;
00721         for(int32_t i=0; i < lhs.size(); i++) r += lhs._array[i] * rhs._array[i];
00722         return r;
00723 }
00724 
00725 template<class T>
00726 inline T dot(const vec3<T> & lhs, const vec3<T> & rhs ) {
00727         T r = 0;
00728         for(int32_t i=0; i < lhs.size(); i++) r += lhs._array[i] * rhs._array[i];
00729         return r;
00730 }
00731 
00732 template<class T>
00733 inline T dot(const vec4<T> & lhs, const vec4<T> & rhs ) {
00734         T r = 0;
00735         for(int32_t i=0; i < lhs.size(); i++) r += lhs._array[i] * rhs._array[i];
00736         return r;
00737 }
00738 
00739 // return the length of the provided vector
00740 //template< class T>
00741 //inline typename T::value_type length( const T & vec) {
00742 //    T::value_type r = 0;
00743 //    for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00744 //    return T::value_type(sqrt(r));
00745 //}
00746 
00747 
00748 template<class T>
00749 inline T length( const vec2<T> & vec) {
00750     T r = 0;
00751     for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00752     return T(sqrt(r));
00753 }
00754 
00755 template<class T>
00756 inline T length( const vec3<T> & vec) {
00757     T r = 0;
00758     for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00759     return T(sqrt(r));
00760 }
00761 
00762 template<class T>
00763 inline T length( const vec4<T> & vec) {
00764     T r = 0;
00765     for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00766     return T(sqrt(r));
00767 }
00768 
00769 // return the squared norm
00770 //template< class T>
00771 //inline typename T::value_type square_norm( const T & vec) {
00772 //    T::value_type r = 0;
00773 //    for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00774 //    return r;
00775 //}
00776 
00777 template< class T>
00778 inline T square_norm( const vec2<T> & vec) {
00779     T r = 0;
00780     for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00781     return r;
00782 }
00783 
00784 template< class T>
00785 inline T square_norm( const vec3<T> & vec) {
00786     T r = 0;
00787     for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00788     return r;
00789 }
00790 
00791 template< class T>
00792 inline T square_norm( const vec4<T> & vec) {
00793     T r = 0;
00794     for(int32_t i = 0; i < vec.size(); i++) r += vec._array[i]*vec._array[i];
00795     return r;
00796 }
00797 
00798 // return the normalized version of the vector
00799 //template< class T>
00800 //inline T normalize( const T & vec) {
00801 //    T::value_type sum(0);
00802 //    T r;
00803 //    for(int32_t i = 0; i < vec.size(); i++)
00804 //        sum += vec._array[i] * vec._array[i];
00805 //    sum = T::value_type(sqrt(sum));
00806 //    if (sum > 0)
00807 //        for(int32_t i = 0; i < vec.size(); i++)
00808 //            r._array[i] = vec._array[i] / sum;
00809 //    return r;
00810 //}
00811 
00812 template< class T>
00813 inline vec2<T> normalize( const vec2<T> & vec) {
00814     T sum(0);
00815     vec2<T> r;
00816     for(int32_t i = 0; i < vec.size(); i++)
00817         sum += vec._array[i] * vec._array[i];
00818     sum = T(sqrt(sum));
00819     if (sum > 0)
00820         for(int32_t i = 0; i < vec.size(); i++)
00821             r._array[i] = vec._array[i] / sum;
00822     return r;
00823 }
00824 
00825 template< class T>
00826 inline vec3<T> normalize( const vec3<T> & vec) {
00827     T sum(0);
00828     vec3<T> r;
00829     for(int32_t i = 0; i < vec.size(); i++)
00830         sum += vec._array[i] * vec._array[i];
00831     sum = T(sqrt(sum));
00832     if (sum > 0)
00833         for(int32_t i = 0; i < vec.size(); i++)
00834             r._array[i] = vec._array[i] / sum;
00835     return r;
00836 }
00837 
00838 template< class T>
00839 inline vec4<T> normalize( const vec4<T> & vec) {
00840     T sum(0);
00841     vec4<T> r;
00842     for(int32_t i = 0; i < vec.size(); i++)
00843         sum += vec._array[i] * vec._array[i];
00844     sum = T(sqrt(sum));
00845     if (sum > 0)
00846         for(int32_t i = 0; i < vec.size(); i++)
00847             r._array[i] = vec._array[i] / sum;
00848     return r;
00849 }
00850 
00851 // In VC8 : min and max are already defined by a #define...
00852 #ifdef min
00853 #undef min
00854 #endif
00855 #ifdef max
00856 #undef max
00857 #endif
00858 //componentwise min
00859 template< class T>
00860 inline T min( const T & lhs, const T & rhs ) {
00861     T rt;
00862     for (int32_t i = 0; i < lhs.size(); i++) rt._array[i] = std::min( lhs._array[i], rhs._array[i]);
00863     return rt;
00864 }
00865 
00866 // componentwise max
00867 template< class T>
00868 inline T max( const T & lhs, const T & rhs ) {
00869     T rt;
00870     for (int32_t i = 0; i < lhs.size(); i++) rt._array[i] = std::max( lhs._array[i], rhs._array[i]);
00871     return rt;
00872 }
00873 
00874 
00875 };
00876 
00877 #endif
Generated on Sat Mar 8 14:58:36 2014 for NVIDIA GameWorks OpenGL App Framework and Libraries by Doxygen
©2014 NVIDIA Corporation.