Google

Main Page   Class Hierarchy   Compound List   File List   Compound Members  

vector3.h

00001 /*
00002     Copyright (C) 1998,1999,2000 by Jorrit Tyberghein
00003     Largely rewritten by Ivan Avramovic <ivan@avramovic.com>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public
00016     License along with this library; if not, write to the Free
00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 #ifndef __CS_VECTOR3_H__
00021 #define __CS_VECTOR3_H__
00022 
00023 #ifndef __CS_CSSYSDEFS_H__
00024 #error "cssysdef.h must be included in EVERY source file!"
00025 #endif
00026 
00027 #include "csgeom/math3d_d.h"
00028 
00032 class csVector3
00033 {
00034 public:
00036   float x;
00038   float y;
00040   float z;
00041 
00047   csVector3 () {}
00048 
00054   csVector3 (float m) : x(m), y(m), z(m) {}
00055 
00057   csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
00058 
00060   csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
00061 
00063   csVector3 (const csDVector3&);
00064 
00066   inline friend csVector3 operator+ (const csVector3& v1, const csVector3& v2)
00067   { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00068 
00070   inline friend csDVector3 operator+ (const csDVector3& v1, const csVector3& v2)
00071   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00072 
00074   inline friend csDVector3 operator+ (const csVector3& v1, const csDVector3& v2)
00075   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00076 
00078   inline friend csVector3 operator- (const csVector3& v1, const csVector3& v2)
00079   { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00080 
00082   inline friend csDVector3 operator- (const csVector3& v1, const csDVector3& v2)
00083   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00084 
00086   inline friend csDVector3 operator- (const csDVector3& v1, const csVector3& v2)
00087   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00088 
00090   inline friend float operator* (const csVector3& v1, const csVector3& v2)
00091   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00092 
00094   inline friend csVector3 operator% (const csVector3& v1, const csVector3& v2)
00095   {
00096     return csVector3 (v1.y*v2.z-v1.z*v2.y,
00097                       v1.z*v2.x-v1.x*v2.z,
00098                       v1.x*v2.y-v1.y*v2.x);
00099   }
00100 
00102   void Cross (const csVector3 & px, const csVector3 & py)
00103   {
00104     x = px.y*py.z - px.z*py.y;
00105     y = px.z*py.x - px.x*py.z;
00106     z = px.x*py.y - px.y*py.x;
00107   }
00108 
00110   inline friend csVector3 operator* (const csVector3& v, float f)
00111   { return csVector3(v.x*f, v.y*f, v.z*f); }
00112 
00114   inline friend csVector3 operator* (float f, const csVector3& v)
00115   { return csVector3(v.x*f, v.y*f, v.z*f); }
00116 
00118   inline friend csDVector3 operator* (const csVector3& v, double f)
00119   { return csDVector3(v) * f; }
00120 
00122   inline friend csDVector3 operator* (double f, const csVector3& v)
00123   { return csDVector3(v) * f; }
00124 
00126   inline friend csVector3 operator* (const csVector3& v, int f)
00127   { return v * (float)f; }
00128 
00130   inline friend csVector3 operator* (int f, const csVector3& v)
00131   { return v * (float)f; }
00132 
00134   inline friend csVector3 operator/ (const csVector3& v, float f)
00135   { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
00136 
00138   inline friend csDVector3 operator/ (const csVector3& v, double f)
00139   { return csDVector3(v) / f; }
00140 
00142   inline friend csVector3 operator/ (const csVector3& v, int f)
00143   { return v / (float)f; }
00144 
00146   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
00147   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
00148 
00150   inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
00151   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
00152 
00154   inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
00155   { return v2*(v1*v2)/(v2*v2); }
00156 
00158   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
00159   { return v1*(v1*v2)/(v1*v1); }
00160 
00162   inline friend bool operator< (const csVector3& v, float f)
00163   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00164 
00166   inline friend bool operator> (float f, const csVector3& v)
00167   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00168 
00170   inline float operator[] (int n) const { return !n?x:n&1?y:z; }
00171 
00173   inline float & operator[] (int n) { return !n?x:n&1?y:z; }
00174 
00176   inline csVector3& operator+= (const csVector3& v)
00177   {
00178     x += v.x;
00179     y += v.y;
00180     z += v.z;
00181 
00182     return *this;
00183   }
00184 
00186   inline csVector3& operator-= (const csVector3& v)
00187   {
00188     x -= v.x;
00189     y -= v.y;
00190     z -= v.z;
00191 
00192     return *this;
00193   }
00194 
00196   inline csVector3& operator*= (float f)
00197   { x *= f; y *= f; z *= f; return *this; }
00198 
00200   inline csVector3& operator/= (float f)
00201   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
00202 
00204   inline csVector3 operator+ () const { return *this; }
00205 
00207   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
00208 
00210   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
00211 
00213   inline void Set (const csVector3& v) { x = v.x; y = v.y; z = v.z; }
00214 
00216   float Norm () const;
00217 
00219   float SquaredNorm () const
00220   { return x * x + y * y + z * z; }
00221 
00227   csVector3 Unit () const { return (*this)/(this->Norm()); }
00228 
00230   inline static float Norm (const csVector3& v) { return v.Norm(); }
00231 
00233   inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
00234 
00236   void Normalize ();
00237 
00239   inline bool IsZero () const
00240   { return (x == 0) && (y == 0) && (z == 0); }
00241 };
00242 
00243 #endif // __CS_VECTOR3_H__

Generated for Crystal Space by doxygen 1.2.5 written by Dimitri van Heesch, ©1997-2000