CrystalSpace

Public API Reference

Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

vector4.h

Go to the documentation of this file.
00001 /*
00002     Copyright (C) 1998,1999,2000 by Jorrit Tyberghein
00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
00004     Extended (and some methods removed) to 4 component by Marten
00005     Svanfeldt
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public
00018     License along with this library; if not, write to the Free
00019     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #ifndef __CS_VECTOR4_H__
00023 #define __CS_VECTOR4_H__
00024 
00031 #include "csextern.h"
00032 #include "csgeom/vector3.h"
00033 
00034 class csVector4;
00035 
00036 
00040 class CS_CRYSTALSPACE_EXPORT csDVector4
00041 {
00042 public:
00044   double x;
00046   double y;
00048   double z;
00050   double w;
00051 
00056   csDVector4 () {}
00057 
00063   csDVector4 (double m) : x(m), y(m), z(m), w(m) {}
00064 
00066   csDVector4 (double ix, double iy, double iz = 0, double iw = 1)
00067   { x = ix; y = iy; z = iz; w = iw;}
00068 
00070   csDVector4 (const csDVector4& v) { x = v.x; y = v.y; z = v.z; w = v.w; }
00071 
00073   csDVector4 (const csVector4&);
00074 
00076   csDVector4 (const csDVector3& v) { x = v.x; y = v.y; z = v.z; w = 1.0; }
00077 
00079   inline friend
00080   csDVector4 operator+ (const csDVector4& v1, const csDVector4& v2)
00081   { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); }
00082 
00084   inline friend
00085   csDVector4 operator- (const csDVector4& v1, const csDVector4& v2)
00086   { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); }
00087 
00089   inline friend double operator* (const csDVector4& v1, const csDVector4& v2)
00090   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; }
00091 
00093   inline friend csDVector4 operator% (const csDVector4& v1,
00094         const csDVector4& v2)
00095   {
00096     
00097     return csDVector4 (
00098       (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y),
00099       (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z),
00100       (v1.x*v2.z-v1.z-v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z),
00101       (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) );
00102   }
00103 
00105   void Cross (const csDVector4 & v1, const csDVector4 & v2)
00106   {
00107     x = (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y);
00108     y = (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z);
00109     z = (v1.x*v2.z-v1.z-v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z);
00110     w = (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w);
00111   }
00112 
00114   inline friend csDVector4 operator* (const csDVector4& v, double f)
00115   { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); }
00116 
00118   inline friend csDVector4 operator* (double f, const csDVector4& v)
00119   { return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); }
00120 
00122   inline friend csDVector4 operator/ (const csDVector4& v, double f)
00123   { f = 1.0f/f; return csDVector4(v.x*f, v.y*f, v.z*f, v.w*f); }
00124 
00126   inline friend bool operator== (const csDVector4& v1, const csDVector4& v2)
00127   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w == v2.w; }
00128 
00130   inline friend bool operator!= (const csDVector4& v1, const csDVector4& v2)
00131   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; }
00132 
00134   inline friend
00135   csDVector4 operator>> (const csDVector4& v1, const csDVector4& v2)
00136   { return v2*(v1*v2)/(v2*v2); }
00137 
00139   inline friend
00140   csDVector4 operator<< (const csDVector4& v1, const csDVector4& v2)
00141   { return v1*(v1*v2)/(v1*v1); }
00142 
00144   inline friend bool operator< (const csDVector4& v, double f)
00145   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; }
00146 
00148   inline friend bool operator> (double f, const csDVector4& v)
00149   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; }
00150 
00152   inline double operator[](int n) const
00153   { return !n?x:n&1?y:n&2?z:w; }
00154 
00156   inline double & operator[](int n)
00157   { return !n?x:n&1?y:n&2?z:w; }
00158 
00160   inline csDVector4& operator+= (const csDVector4& v)
00161   {
00162     x += v.x;
00163     y += v.y;
00164     z += v.z;
00165     w += v.w;
00166 
00167     return *this;
00168   }
00169 
00171   inline csDVector4& operator-= (const csDVector4& v)
00172   {
00173     x -= v.x;
00174     y -= v.y;
00175     z -= v.z;
00176     w -= v.w;
00177 
00178     return *this;
00179   }
00180 
00182   inline csDVector4& operator*= (double f)
00183   { x *= f; y *= f; z *= f; w *= f; return *this; }
00184 
00186   inline csDVector4& operator/= (double f)
00187   { x /= f; y /= f; z /= f; w /= f; return *this; }
00188 
00190   inline csDVector4 operator+ () const { return *this; }
00191 
00193   inline csDVector4 operator- () const { return csDVector4(-x,-y,-z,-w); }
00194 
00196   inline void Set (double sx, double sy, double sz, double sw)
00197   { x = sx; y = sy; z = sz; w = sw; }
00198 
00200   double Norm () const;
00201 
00203   double SquaredNorm () const;
00204 
00210   csDVector4 Unit () const { return (*this)/(this->Norm()); }
00211 
00213   inline static double Norm (const csDVector4& v) { return v.Norm(); }
00214 
00216   inline static csDVector4 Unit (const csDVector4& v) { return v.Unit(); }
00217 
00219   void Normalize();
00220 };
00221 
00222 
00226 class CS_CRYSTALSPACE_EXPORT csVector4
00227 {
00228 public:
00230   float x;
00232   float y;
00234   float z;
00236   float w;
00237 
00242   csVector4 () {}
00243 
00249   csVector4 (float m) : x(m), y(m), z(m), w(m) {}
00250 
00252   csVector4 (float ix, float iy, float iz = 0, float iw = 1)
00253         : x(ix), y(iy), z(iz), w(iw) {}
00254 
00256   csVector4 (const csVector4& v) : x(v.x), y(v.y), z(v.z), w(v.w) {}
00257 
00259   csVector4 (const csDVector4 &v);
00260 
00262   csVector4 (const csVector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0f) {}
00263 
00265   csString Description() const;
00266     
00268   inline friend csVector4 operator+ (const csVector4& v1, const csVector4& v2)
00269   { return csVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); }
00270 
00272   inline friend csDVector4 operator+ (const csDVector4& v1, const csVector4& v2)
00273   { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); }
00274 
00276   inline friend csDVector4 operator+ (const csVector4& v1, const csDVector4& v2)
00277   { return csDVector4(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z, v1.w+v2.w); }
00278 
00280   inline friend csVector4 operator- (const csVector4& v1, const csVector4& v2)
00281   { return csVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); }
00282 
00284   inline friend csDVector4 operator- (const csVector4& v1, const csDVector4& v2)
00285   { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); }
00286 
00288   inline friend csDVector4 operator- (const csDVector4& v1, const csVector4& v2)
00289   { return csDVector4(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z, v1.w-v2.w); }
00290 
00291 
00293   inline friend float operator* (const csVector4& v1, const csVector4& v2)
00294   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w; }
00295 
00297   inline friend csVector4 operator% (const csVector4& v1, const csVector4& v2)
00298   {
00299     return csVector4 (
00300       (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y),
00301       (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z),
00302       (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z),
00303       (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w) );
00304   }
00305 
00307   void Cross (const csVector4 & v1, const csVector4 & v2)
00308   {
00309     x = (v1.x*v2.y-v1.y*v2.x) + (v1.x*v2.z-v1.z*v2.x) + (v1.y*v2.z-v1.z*v2.y);
00310     y = (v1.z*v2.y-v1.y*v2.z) + (v1.y*v2.w-v1.w*v2.y) + (v1.z*v2.w-v1.w*v2.z);
00311     z = (v1.x*v2.z-v1.z*v2.x) + (v1.w*v2.x-v1.x*v2.w) + (v1.z*v2.w-v1.w*v2.z);
00312     w = (v1.y*v2.x-v1.x*v2.y) + (v1.w*v2.x-v1.x*v2.w) + (v1.w*v2.y-v1.y*v2.w);
00313   }
00314 
00316   inline friend csVector4 operator* (const csVector4& v, float f)
00317   { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); }
00318 
00320   inline friend csVector4 operator* (float f, const csVector4& v)
00321   { return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); }
00322 
00324   inline friend csDVector4 operator* (const csVector4& v, double f)
00325   { return csDVector4(v) * f; }
00326 
00328   inline friend csDVector4 operator* (double f, const csVector4& v)
00329   { return csDVector4(v) * f; }
00330 
00332   inline friend csVector4 operator* (const csVector4& v, int f)
00333   { return v * (float)f; }
00334 
00336   inline friend csVector4 operator* (int f, const csVector4& v)
00337   { return v * (float)f; }
00338 
00340   inline friend csVector4 operator/ (const csVector4& v, float f)
00341   { f = 1.0f/f; return csVector4(v.x*f, v.y*f, v.z*f, v.w*f); }
00342 
00344   inline friend csDVector4 operator/ (const csVector4& v, double f)
00345   { return csDVector4(v) / f; }
00346 
00348   inline friend csVector4 operator/ (const csVector4& v, int f)
00349   { return v / (float)f; }
00350 
00352   inline friend bool operator== (const csVector4& v1, const csVector4& v2)
00353   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w; }
00354 
00356   inline friend bool operator!= (const csVector4& v1, const csVector4& v2)
00357   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z || v1.w!=v2.w; }
00358 
00360   inline friend csVector4 operator>> (const csVector4& v1, const csVector4& v2)
00361   { return v2*(v1*v2)/(v2*v2); }
00362 
00364   inline friend csVector4 operator<< (const csVector4& v1, const csVector4& v2)
00365   { return v1*(v1*v2)/(v1*v1); }
00366 
00368   inline friend bool operator< (const csVector4& v, float f)
00369   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; }
00370 
00372   inline friend bool operator> (float f, const csVector4& v)
00373   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f && ABS(v.w)<f; }
00374 
00376   inline float operator[] (int n) const
00377   { return !n?x:n&1?y:n&2?z:w; }
00378 
00380   inline float & operator[] (int n)
00381   { return !n?x:n&1?y:n&2?z:w; }
00382 
00384   inline csVector4& operator+= (const csVector4& v)
00385   {
00386     x += v.x;
00387     y += v.y;
00388     z += v.z;
00389     w += v.w;
00390 
00391     return *this;
00392   }
00393 
00395   inline csVector4& operator-= (const csVector4& v)
00396   {
00397     x -= v.x;
00398     y -= v.y;
00399     z -= v.z;
00400     w -= v.w;
00401 
00402     return *this;
00403   }
00404 
00406   inline csVector4& operator*= (float f)
00407   { x *= f; y *= f; z *= f; w *= f; return *this; }
00408 
00410   inline csVector4& operator/= (float f)
00411   { f = 1.0f / f; x *= f; y *= f; z *= f; w *= f; return *this; }
00412 
00414   inline csVector4 operator+ () const { return *this; }
00415 
00417   inline csVector4 operator- () const { return csVector4(-x,-y,-z, -w); }
00418 
00420   inline void Set (float sx, float sy, float sz, float sw)
00421   { x = sx; y = sy; z = sz; w = sw; }
00422 
00424   inline void Set (csVector4 const& v) { x = v.x; y = v.y; z = v.z; w = v.w; }
00425 
00427   inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; w = v[3]; }
00428 
00430   inline void Set (float v) { x = y = z = w = v; }
00431 
00433   inline void Get (float* v) { v[0] = x; v[1] = y; v[2] = z; v[3] = w; }
00434 
00436   float Norm () const;
00437 
00439   float SquaredNorm () const
00440   { return x * x + y * y + z * z + w * w; }
00441 
00447   csVector4 Unit () const { return (*this)/(this->Norm()); }
00448 
00450   inline static float Norm (const csVector4& v) { return v.Norm(); }
00451 
00453   inline static csVector4 Unit (const csVector4& v) { return v.Unit(); }
00454 
00456   void Normalize ();
00457 
00459   inline bool IsZero (float precision = SMALL_EPSILON) const
00460   { return (ABS(x) < precision) && (ABS(y) < precision)
00461             && (ABS(z) < precision) &&  (ABS(w) < precision);
00462   }
00463 };
00464 
00467 #endif // __CS_VECTOR3_H__

Generated for Crystal Space by doxygen 1.4.4