CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | 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 #include "csutil/csstring.h"
00032 
00033 #define CS_AXIS_NONE -1
00034 #define CS_AXIS_X 0
00035 #define CS_AXIS_Y 1
00036 #define CS_AXIS_Z 2
00037 
00041 class CS_CSGEOM_EXPORT csVector3
00042 {
00043 public:
00045   float x;
00047   float y;
00049   float z;
00050   
00056   csVector3 () {}
00057 
00063   csVector3 (float m) : x(m), y(m), z(m) {}
00064 
00066   csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
00067 
00069   csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
00070 
00072   csVector3 (const csDVector3&);
00073 
00075   csString Description() const;
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 csDVector3& v1, const csVector3& v2)
00083   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00084 
00086   inline friend csDVector3 operator+(const csVector3& v1, const csDVector3& v2)
00087   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00088 
00090   inline friend csVector3 operator-(const csVector3& v1, const csVector3& v2)
00091   { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00092 
00094   inline friend csDVector3 operator-(const csVector3& v1, const csDVector3& v2)
00095   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00096 
00098   inline friend csDVector3 operator-(const csDVector3& v1, const csVector3& v2)
00099   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00100 
00102   inline friend float operator*(const csVector3& v1, const csVector3& v2)
00103   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00104 
00106   inline friend csVector3 operator%(const csVector3& v1, const csVector3& v2)
00107   {
00108     return csVector3 (v1.y*v2.z-v1.z*v2.y,
00109                       v1.z*v2.x-v1.x*v2.z,
00110                       v1.x*v2.y-v1.y*v2.x);
00111   }
00112 
00114   void Cross (const csVector3 & px, const csVector3 & py)
00115   {
00116     x = px.y*py.z - px.z*py.y;
00117     y = px.z*py.x - px.x*py.z;
00118     z = px.x*py.y - px.y*py.x;
00119   }
00120 
00122   inline friend csVector3 operator* (const csVector3& v, float f)
00123   { return csVector3(v.x*f, v.y*f, v.z*f); }
00124 
00126   inline friend csVector3 operator* (float f, const csVector3& v)
00127   { return csVector3(v.x*f, v.y*f, v.z*f); }
00128 
00130   inline friend csDVector3 operator* (const csVector3& v, double f)
00131   { return csDVector3(v) * f; }
00132 
00134   inline friend csDVector3 operator* (double f, const csVector3& v)
00135   { return csDVector3(v) * f; }
00136 
00138   inline friend csVector3 operator* (const csVector3& v, int f)
00139   { return v * (float)f; }
00140 
00142   inline friend csVector3 operator* (int f, const csVector3& v)
00143   { return v * (float)f; }
00144 
00146   inline friend csVector3 operator/ (const csVector3& v, float f)
00147   { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
00148 
00150   inline friend csDVector3 operator/ (const csVector3& v, double f)
00151   { return csDVector3(v) / f; }
00152 
00154   inline friend csVector3 operator/ (const csVector3& v, int f)
00155   { return v / (float)f; }
00156 
00158   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
00159   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
00160 
00162   inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
00163   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
00164 
00166   inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
00167   { return v2*(v1*v2)/(v2*v2); }
00168 
00170   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
00171   { return v1*(v1*v2)/(v1*v1); }
00172 
00174   inline friend bool operator< (const csVector3& v, float f)
00175   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00176 
00178   inline friend bool operator> (float f, const csVector3& v)
00179   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00180 
00182   inline float operator[] (int n) const { return !n?x:n&1?y:z; }
00183 
00185   inline float & operator[] (int n) { return !n?x:n&1?y:z; }
00186 
00188   inline csVector3& operator+= (const csVector3& v)
00189   {
00190     x += v.x;
00191     y += v.y;
00192     z += v.z;
00193 
00194     return *this;
00195   }
00196 
00198   inline csVector3& operator-= (const csVector3& v)
00199   {
00200     x -= v.x;
00201     y -= v.y;
00202     z -= v.z;
00203 
00204     return *this;
00205   }
00206 
00208   inline csVector3& operator*= (float f)
00209   { x *= f; y *= f; z *= f; return *this; }
00210 
00212   inline csVector3& operator/= (float f)
00213   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
00214 
00216   inline csVector3 operator+ () const { return *this; }
00217 
00219   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
00220 
00222   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
00223 
00225   inline void Set (csVector3 const& v) { x = v.x; y = v.y; z = v.z; }
00226 
00228   inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; }
00229 
00231   inline void Set (float v) { x = y = z = v; }
00232 
00234   inline void Get (float* v) { v[0] = x; v[1] = y; v[2] = z; }
00235 
00237   float Norm () const;
00238 
00240   float SquaredNorm () const
00241   { return x * x + y * y + z * z; }
00242 
00248   csVector3 Unit () const { return (*this)/(this->Norm()); }
00249 
00251   inline static float Norm (const csVector3& v) { return v.Norm(); }
00252 
00254   inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
00255 
00257   void Normalize ();
00258 
00260   inline bool IsZero (float precision = SMALL_EPSILON) const
00261   { return (ABS(x) < precision) && (ABS(y) < precision)
00262             && (ABS(z) < precision);
00263   }
00264 };
00265 
00268 #endif // __CS_VECTOR3_H__

Generated for Crystal Space by doxygen 1.3.9.1