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

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 #include "csextern.h"
00030 #include "math3d_d.h"
00031 
00032 class csString;
00033 
00038 enum
00039 {
00041   CS_AXIS_NONE = -1,
00043   CS_AXIS_X = 0,
00045   CS_AXIS_Y = 1,
00047   CS_AXIS_Z = 2,
00049   CS_AXIS_W = 3
00050 };
00051 
00055 class CS_CRYSTALSPACE_EXPORT csVector3
00056 {
00057 public:
00058 
00059 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
00060   union
00061   {
00062     struct 
00063     {
00064 #endif
00065 
00066       float x;
00068       float y;
00070       float z;
00071 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
00072     };
00074     float m[3];
00075   };
00076 #endif
00077 
00083   csVector3 () {}
00084 
00090   csVector3 (float m) : x(m), y(m), z(m) {}
00091 
00093   csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
00094 
00096   csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
00097 
00099   csVector3 (const csDVector3&);
00100 
00102   csString Description() const;
00103 
00105   inline friend csVector3 operator+(const csVector3& v1, const csVector3& v2)
00106   { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00107 
00109   inline friend csDVector3 operator+(const csDVector3& v1, const csVector3& v2)
00110   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00111 
00113   inline friend csDVector3 operator+(const csVector3& v1, const csDVector3& v2)
00114   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00115 
00117   inline friend csVector3 operator-(const csVector3& v1, const csVector3& v2)
00118   { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00119 
00121   inline friend csDVector3 operator-(const csVector3& v1, const csDVector3& v2)
00122   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00123 
00125   inline friend csDVector3 operator-(const csDVector3& v1, const csVector3& v2)
00126   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00127 
00129   inline friend float operator*(const csVector3& v1, const csVector3& v2)
00130   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00131 
00133   inline friend csVector3 operator%(const csVector3& v1, const csVector3& v2)
00134   {
00135     return csVector3 (v1.y*v2.z-v1.z*v2.y,
00136                       v1.z*v2.x-v1.x*v2.z,
00137                       v1.x*v2.y-v1.y*v2.x);
00138   }
00139 
00141   void Cross (const csVector3 & px, const csVector3 & py)
00142   {
00143     x = px.y*py.z - px.z*py.y;
00144     y = px.z*py.x - px.x*py.z;
00145     z = px.x*py.y - px.y*py.x;
00146   }
00147 
00149   inline friend csVector3 operator* (const csVector3& v, float f)
00150   { return csVector3(v.x*f, v.y*f, v.z*f); }
00151 
00153   inline friend csVector3 operator* (float f, const csVector3& v)
00154   { return csVector3(v.x*f, v.y*f, v.z*f); }
00155 
00157   inline friend csDVector3 operator* (const csVector3& v, double f)
00158   { return csDVector3(v) * f; }
00159 
00161   inline friend csDVector3 operator* (double f, const csVector3& v)
00162   { return csDVector3(v) * f; }
00163 
00165   inline friend csVector3 operator* (const csVector3& v, int f)
00166   { return v * (float)f; }
00167 
00169   inline friend csVector3 operator* (int f, const csVector3& v)
00170   { return v * (float)f; }
00171 
00173   inline friend csVector3 operator/ (const csVector3& v, float f)
00174   { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
00175 
00177   inline friend csDVector3 operator/ (const csVector3& v, double f)
00178   { return csDVector3(v) / f; }
00179 
00181   inline friend csVector3 operator/ (const csVector3& v, int f)
00182   { return v / (float)f; }
00183 
00185   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
00186   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
00187 
00189   inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
00190   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
00191 
00193   inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
00194   { return v2*(v1*v2)/(v2*v2); }
00195 
00197   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
00198   { return v1*(v1*v2)/(v1*v1); }
00199 
00201   inline friend bool operator< (const csVector3& v, float f)
00202   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00203 
00205   inline friend bool operator> (float f, const csVector3& v)
00206   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00207 
00209 #ifdef __STRICT_ANSI__
00210   inline float operator[] (int n) const { return !n?x:n&1?y:z; }
00211 #else
00212   inline float operator[] (int n) const { return m[n]; }
00213 #endif
00214 
00216 #ifdef __STRICT_ANSI__
00217   inline float & operator[] (int n) { return !n?x:n&1?y:z; }
00218 #else
00219   inline float & operator[] (int n) { return m[n]; }
00220 #endif
00221 
00223   inline csVector3& operator+= (const csVector3& v)
00224   {
00225     x += v.x;
00226     y += v.y;
00227     z += v.z;
00228 
00229     return *this;
00230   }
00231 
00233   inline csVector3& operator-= (const csVector3& v)
00234   {
00235     x -= v.x;
00236     y -= v.y;
00237     z -= v.z;
00238 
00239     return *this;
00240   }
00241 
00243   inline csVector3& operator*= (float f)
00244   { x *= f; y *= f; z *= f; return *this; }
00245 
00247   inline csVector3& operator/= (float f)
00248   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
00249 
00251   inline csVector3 operator+ () const { return *this; }
00252 
00254   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
00255 
00257   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
00258 
00260   inline void Set (csVector3 const& v) { x = v.x; y = v.y; z = v.z; }
00261 
00263   inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; }
00264 
00266   inline void Set (float v) { x = y = z = v; }
00267 
00269   inline void Get (float* v) { v[0] = x; v[1] = y; v[2] = z; }
00270 
00272   float Norm () const;
00273 
00275   float SquaredNorm () const
00276   { return x * x + y * y + z * z; }
00277 
00283   csVector3 Unit () const { return (*this)/(this->Norm()); }
00284 
00286   inline static float Norm (const csVector3& v) { return v.Norm(); }
00287 
00289   inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
00290 
00292   void Normalize ();
00293 
00295   inline bool IsZero (float precision = SMALL_EPSILON) const
00296   { return (ABS(x) < precision) && (ABS(y) < precision)
00297             && (ABS(z) < precision);
00298   }
00299 };
00300 
00303 #endif // __CS_VECTOR3_H__

Generated for Crystal Space by doxygen 1.4.4