CrystalSpace

Public API Reference

Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

vector3.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 
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 
00029 #ifndef __CS_CSSYSDEFS_H__
00030 #error "cssysdef.h must be included in EVERY source file!"
00031 #endif
00032 
00033 #include "csgeom/math3d_d.h"
00034 
00038 class csVector3
00039 {
00040 public:
00042   float x;
00044   float y;
00046   float z;
00047 
00053   csVector3 () {}
00054 
00060   csVector3 (float m) : x(m), y(m), z(m) {}
00061 
00063   csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
00064 
00066   csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
00067 
00069   csVector3 (const csDVector3&);
00070 
00072   inline friend csVector3 operator+ (const csVector3& v1, const csVector3& v2)
00073   { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00074 
00076   inline friend csDVector3 operator+ (const csDVector3& v1, const csVector3& v2)
00077   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00078 
00080   inline friend csDVector3 operator+ (const csVector3& v1, const csDVector3& v2)
00081   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00082 
00084   inline friend csVector3 operator- (const csVector3& v1, const csVector3& v2)
00085   { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00086 
00088   inline friend csDVector3 operator- (const csVector3& v1, const csDVector3& v2)
00089   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00090 
00092   inline friend csDVector3 operator- (const csDVector3& v1, const csVector3& v2)
00093   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00094 
00096   inline friend float operator* (const csVector3& v1, const csVector3& v2)
00097   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00098 
00100   inline friend csVector3 operator% (const csVector3& v1, const csVector3& v2)
00101   {
00102     return csVector3 (v1.y*v2.z-v1.z*v2.y,
00103                       v1.z*v2.x-v1.x*v2.z,
00104                       v1.x*v2.y-v1.y*v2.x);
00105   }
00106 
00108   void Cross (const csVector3 & px, const csVector3 & py)
00109   {
00110     x = px.y*py.z - px.z*py.y;
00111     y = px.z*py.x - px.x*py.z;
00112     z = px.x*py.y - px.y*py.x;
00113   }
00114 
00116   inline friend csVector3 operator* (const csVector3& v, float f)
00117   { return csVector3(v.x*f, v.y*f, v.z*f); }
00118 
00120   inline friend csVector3 operator* (float f, const csVector3& v)
00121   { return csVector3(v.x*f, v.y*f, v.z*f); }
00122 
00124   inline friend csDVector3 operator* (const csVector3& v, double f)
00125   { return csDVector3(v) * f; }
00126 
00128   inline friend csDVector3 operator* (double f, const csVector3& v)
00129   { return csDVector3(v) * f; }
00130 
00132   inline friend csVector3 operator* (const csVector3& v, int f)
00133   { return v * (float)f; }
00134 
00136   inline friend csVector3 operator* (int f, const csVector3& v)
00137   { return v * (float)f; }
00138 
00140   inline friend csVector3 operator/ (const csVector3& v, float f)
00141   { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
00142 
00144   inline friend csDVector3 operator/ (const csVector3& v, double f)
00145   { return csDVector3(v) / f; }
00146 
00148   inline friend csVector3 operator/ (const csVector3& v, int f)
00149   { return v / (float)f; }
00150 
00152   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
00153   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
00154 
00156   inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
00157   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
00158 
00160   inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
00161   { return v2*(v1*v2)/(v2*v2); }
00162 
00164   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
00165   { return v1*(v1*v2)/(v1*v1); }
00166 
00168   inline friend bool operator< (const csVector3& v, float f)
00169   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00170 
00172   inline friend bool operator> (float f, const csVector3& v)
00173   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00174 
00176   inline float operator[] (int n) const { return !n?x:n&1?y:z; }
00177 
00179   inline float & operator[] (int n) { return !n?x:n&1?y:z; }
00180 
00182   inline csVector3& operator+= (const csVector3& v)
00183   {
00184     x += v.x;
00185     y += v.y;
00186     z += v.z;
00187 
00188     return *this;
00189   }
00190 
00192   inline csVector3& operator-= (const csVector3& v)
00193   {
00194     x -= v.x;
00195     y -= v.y;
00196     z -= v.z;
00197 
00198     return *this;
00199   }
00200 
00202   inline csVector3& operator*= (float f)
00203   { x *= f; y *= f; z *= f; return *this; }
00204 
00206   inline csVector3& operator/= (float f)
00207   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
00208 
00210   inline csVector3 operator+ () const { return *this; }
00211 
00213   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
00214 
00216   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
00217 
00219   inline void Set (const csVector3& v) { x = v.x; y = v.y; z = v.z; }
00220 
00222   float Norm () const;
00223 
00225   float SquaredNorm () const
00226   { return x * x + y * y + z * z; }
00227 
00233   csVector3 Unit () const { return (*this)/(this->Norm()); }
00234 
00236   inline static float Norm (const csVector3& v) { return v.Norm(); }
00237 
00239   inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
00240 
00242   void Normalize ();
00243 
00245   inline bool IsZero (float precision = SMALL_EPSILON) const
00246   { return (ABS(x) < precision) && (ABS(y) < precision)
00247             && (ABS(z) < precision);
00248   }
00249 };
00250 
00253 #endif // __CS_VECTOR3_H__

Generated for Crystal Space by doxygen 1.2.14