CrystalSpace

Public API Reference

csgeom/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 
00030 #include "csextern.h"
00031 #include "csgeom/math3d_d.h"
00032 #include "csqsqrt.h"
00033 
00034 class csString;
00035 
00040 enum
00041 {
00043   CS_AXIS_NONE = -1,
00045   CS_AXIS_X = 0,
00047   CS_AXIS_Y = 1,
00049   CS_AXIS_Z = 2,
00051   CS_AXIS_W = 3
00052 };
00053 
00057 class CS_CRYSTALSPACE_EXPORT csVector3
00058 {
00059 public:
00060 
00061 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
00062   union
00063   {
00064     struct 
00065     {
00066 #endif
00067 
00068       float x;
00070       float y;
00072       float z;
00073 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
00074     };
00076     float m[3];
00077   };
00078 #endif
00079 
00085   csVector3 () 
00086   {}
00087 
00093   csVector3 (float m) 
00094     : x(m), y(m), z(m) 
00095   {}
00096 
00098   csVector3 (float ix, float iy, float iz = 0) 
00099     : x(ix), y(iy), z(iz) 
00100   {}
00101 
00103   csVector3 (const csVector3& v) 
00104     : x(v.x), y(v.y), z(v.z) 
00105   {}
00106 
00108   csVector3 (const csDVector3&);
00109 
00111   csString Description() const;
00112 
00114   inline friend csVector3 operator+(const csVector3& v1, const csVector3& v2)
00115   { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00116 
00118   inline friend csDVector3 operator+(const csDVector3& v1, const csVector3& v2)
00119   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00120 
00122   inline friend csDVector3 operator+(const csVector3& v1, const csDVector3& v2)
00123   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00124 
00126   inline friend csVector3 operator-(const csVector3& v1, const csVector3& v2)
00127   { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00128 
00130   inline friend csDVector3 operator-(const csVector3& v1, const csDVector3& v2)
00131   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00132 
00134   inline friend csDVector3 operator-(const csDVector3& v1, const csVector3& v2)
00135   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00136 
00138   inline friend float operator*(const csVector3& v1, const csVector3& v2)
00139   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00140 
00142   inline friend csVector3 operator%(const csVector3& v1, const csVector3& v2)
00143   {
00144     return csVector3 (v1.y*v2.z-v1.z*v2.y,
00145                       v1.z*v2.x-v1.x*v2.z,
00146                       v1.x*v2.y-v1.y*v2.x);
00147   }
00148 
00150   void Cross (const csVector3 & px, const csVector3 & py)
00151   {
00152     x = px.y*py.z - px.z*py.y;
00153     y = px.z*py.x - px.x*py.z;
00154     z = px.x*py.y - px.y*py.x;
00155   }
00156 
00158   inline friend csVector3 operator* (const csVector3& v, float f)
00159   { return csVector3 (v.x*f, v.y*f, v.z*f); }
00160 
00162   inline friend csVector3 operator* (float f, const csVector3& v)
00163   { return csVector3 (v.x*f, v.y*f, v.z*f); }
00164 
00166   inline friend csDVector3 operator* (const csVector3& v, double f)
00167   { return csDVector3 (v) * f; }
00168 
00170   inline friend csDVector3 operator* (double f, const csVector3& v)
00171   { return csDVector3 (v) * f; }
00172 
00174   inline friend csVector3 operator* (const csVector3& v, int f)
00175   { return v * (float)f; }
00176 
00178   inline friend csVector3 operator* (int f, const csVector3& v)
00179   { return v * (float)f; }
00180 
00182   inline friend csVector3 operator/ (const csVector3& v, float f)
00183   { f = 1.0f/f; return csVector3 (v.x*f, v.y*f, v.z*f); }
00184 
00186   inline friend csDVector3 operator/ (const csVector3& v, double f)
00187   { return csDVector3 (v) / f; }
00188 
00190   inline friend csVector3 operator/ (const csVector3& v, int f)
00191   { return v / (float)f; }
00192 
00194   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
00195   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
00196 
00198   inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
00199   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
00200 
00202   inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
00203   { return v2*(v1*v2)/(v2*v2); }
00204 
00206   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
00207   { return v1*(v1*v2)/(v1*v1); }
00208 
00210   inline friend bool operator< (const csVector3& v, float f)
00211   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00212 
00214   inline friend bool operator> (float f, const csVector3& v)
00215   { return f>ABS(v.x) && f>ABS(v.y) && f>ABS(v.z); }
00216 
00218 #if defined( __STRICT_ANSI__) || defined(SWIG)
00219   inline float operator[] (size_t n) const { return !n?x:n&1?y:z; }
00220 #else
00221   inline float operator[] (size_t n) const { return m[n]; }
00222 #endif
00223 
00225 #if defined( __STRICT_ANSI__) || defined(SWIG)
00226   inline float & operator[] (size_t n) { return !n?x:n&1?y:z; }
00227 #else
00228   inline float & operator[] (size_t n) { return m[n]; }
00229 #endif
00230 
00232   inline csVector3& operator+= (const csVector3& v)
00233   {
00234     x += v.x;
00235     y += v.y;
00236     z += v.z;
00237 
00238     return *this;
00239   }
00240 
00242   inline csVector3& operator-= (const csVector3& v)
00243   {
00244     x -= v.x;
00245     y -= v.y;
00246     z -= v.z;
00247 
00248     return *this;
00249   }
00250 
00252   inline csVector3& operator*= (float f)
00253   { x *= f; y *= f; z *= f; return *this; }
00254 
00256   inline csVector3& operator/= (float f)
00257   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
00258 
00260   inline csVector3 operator+ () const { return *this; }
00261 
00263   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
00264 
00266   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
00267 
00269   inline void Set (csVector3 const& v) { x = v.x; y = v.y; z = v.z; }
00270 
00272   inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; }
00273 
00275   inline void Set (float v) { x = y = z = v; }
00276 
00278   inline void Get (float* v) const { v[0] = x; v[1] = y; v[2] = z; }
00279 
00281   inline float Norm () const
00282   { return csQsqrt(SquaredNorm()); }
00283 
00285   inline float InverseNorm () const
00286   { return csQisqrt(SquaredNorm()); }
00287 
00289   inline float SquaredNorm () const
00290   { return x * x + y * y + z * z; }
00291 
00297   inline csVector3 Unit () const 
00298   { return (*this)*(this->InverseNorm()); }
00299 
00301   inline static float Norm (const csVector3& v) 
00302   { return v.Norm(); }
00303 
00305   inline static csVector3 Unit (const csVector3& v) 
00306   { return v.Unit(); }
00307 
00309   inline void Normalize ()
00310   {
00311     float sqlen = SquaredNorm();
00312     if (sqlen < SMALL_EPSILON) return ;
00313 
00314     float invlen = csQisqrt (sqlen);
00315     *this *= invlen;
00316   }
00317 
00319   inline bool IsZero (float precision = SMALL_EPSILON) const
00320   { 
00321     return (fabsf(x) < precision) && (fabsf(y) < precision)
00322             && (fabsf(z) < precision);
00323   }
00324 
00326   inline csVector3 UnitAxisClamped () const
00327   {
00328     if (IsZero ())
00329       return csVector3 (0, 0, 0);
00330 
00331     if (fabsf (x) > fabsf (y) && fabsf (x) > fabsf (z))
00332       return csVector3 (x / fabsf (x), 0, 0); //X biggest
00333     else if (fabsf (y) > fabsf (z))
00334       return csVector3 (0, y / fabsf (y), 0); //Y biggest
00335     else
00336       return csVector3 (0, 0, z / fabsf (z)); //Z biggest
00337   }
00338 
00340   inline int DominantAxis () const
00341   {
00342     if (fabsf (x) > fabsf (y) && fabsf (x) > fabsf (z))
00343       return CS_AXIS_X;
00344     else if (fabsf (y) > fabsf (z))
00345       return CS_AXIS_Y;
00346     else
00347       return CS_AXIS_Z;
00348   }
00349 };
00350 
00353 #endif // __CS_VECTOR3_H__

Generated for Crystal Space 2.0 by doxygen 1.7.6.1