CrystalSpace

Public API Reference

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

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 
00029 #ifndef __CS_CSSYSDEFS_H__
00030 #error "cssysdef.h must be included in EVERY source file!"
00031 #endif
00032 
00033 #include "csextern.h"
00034 
00035 #include "csgeom/math3d_d.h"
00036 
00040 class CS_CSGEOM_EXPORT csVector3
00041 {
00042 public:
00044   float x;
00046   float y;
00048   float z;
00049 
00055   csVector3 () {}
00056 
00062   csVector3 (float m) : x(m), y(m), z(m) {}
00063 
00065   csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
00066 
00068   csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
00069 
00071   csVector3 (const csDVector3&);
00072 
00074   inline friend csVector3 operator+ (const csVector3& v1, const csVector3& v2)
00075   { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
00076 
00078   inline friend csDVector3 operator+ (const csDVector3& v1, const csVector3& v2)
00079   { return csDVector3(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 csVector3 operator- (const csVector3& v1, const csVector3& v2)
00087   { return csVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00088 
00090   inline friend csDVector3 operator- (const csVector3& v1, const csDVector3& v2)
00091   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00092 
00094   inline friend csDVector3 operator- (const csDVector3& v1, const csVector3& v2)
00095   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
00096 
00098   inline friend float operator* (const csVector3& v1, const csVector3& v2)
00099   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
00100 
00102   inline friend csVector3 operator% (const csVector3& v1, const csVector3& v2)
00103   {
00104     return csVector3 (v1.y*v2.z-v1.z*v2.y,
00105                       v1.z*v2.x-v1.x*v2.z,
00106                       v1.x*v2.y-v1.y*v2.x);
00107   }
00108 
00110   void Cross (const csVector3 & px, const csVector3 & py)
00111   {
00112     x = px.y*py.z - px.z*py.y;
00113     y = px.z*py.x - px.x*py.z;
00114     z = px.x*py.y - px.y*py.x;
00115   }
00116 
00118   inline friend csVector3 operator* (const csVector3& v, float f)
00119   { return csVector3(v.x*f, v.y*f, v.z*f); }
00120 
00122   inline friend csVector3 operator* (float f, const csVector3& v)
00123   { return csVector3(v.x*f, v.y*f, v.z*f); }
00124 
00126   inline friend csDVector3 operator* (const csVector3& v, double f)
00127   { return csDVector3(v) * f; }
00128 
00130   inline friend csDVector3 operator* (double f, const csVector3& v)
00131   { return csDVector3(v) * f; }
00132 
00134   inline friend csVector3 operator* (const csVector3& v, int f)
00135   { return v * (float)f; }
00136 
00138   inline friend csVector3 operator* (int f, const csVector3& v)
00139   { return v * (float)f; }
00140 
00142   inline friend csVector3 operator/ (const csVector3& v, float f)
00143   { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
00144 
00146   inline friend csDVector3 operator/ (const csVector3& v, double f)
00147   { return csDVector3(v) / f; }
00148 
00150   inline friend csVector3 operator/ (const csVector3& v, int f)
00151   { return v / (float)f; }
00152 
00154   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
00155   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
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 csVector3 operator>> (const csVector3& v1, const csVector3& v2)
00163   { return v2*(v1*v2)/(v2*v2); }
00164 
00166   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
00167   { return v1*(v1*v2)/(v1*v1); }
00168 
00170   inline friend bool operator< (const csVector3& v, float f)
00171   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00172 
00174   inline friend bool operator> (float f, const csVector3& v)
00175   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
00176 
00178   inline float operator[] (int n) const { return !n?x:n&1?y:z; }
00179 
00181   inline float & operator[] (int n) { return !n?x:n&1?y:z; }
00182 
00184   inline csVector3& operator+= (const csVector3& v)
00185   {
00186     x += v.x;
00187     y += v.y;
00188     z += v.z;
00189 
00190     return *this;
00191   }
00192 
00194   inline csVector3& operator-= (const csVector3& v)
00195   {
00196     x -= v.x;
00197     y -= v.y;
00198     z -= v.z;
00199 
00200     return *this;
00201   }
00202 
00204   inline csVector3& operator*= (float f)
00205   { x *= f; y *= f; z *= f; return *this; }
00206 
00208   inline csVector3& operator/= (float f)
00209   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
00210 
00212   inline csVector3 operator+ () const { return *this; }
00213 
00215   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
00216 
00218   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
00219 
00221   inline void Set (const csVector3& v) { x = v.x; y = v.y; z = v.z; }
00222 
00224   float Norm () const;
00225 
00227   float SquaredNorm () const
00228   { return x * x + y * y + z * z; }
00229 
00235   csVector3 Unit () const { return (*this)/(this->Norm()); }
00236 
00238   inline static float Norm (const csVector3& v) { return v.Norm(); }
00239 
00241   inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
00242 
00244   void Normalize ();
00245 
00247   inline bool IsZero (float precision = SMALL_EPSILON) const
00248   { return (ABS(x) < precision) && (ABS(y) < precision)
00249             && (ABS(z) < precision);
00250   }
00251 };
00252 
00255 #endif // __CS_VECTOR3_H__

Generated for Crystal Space by doxygen 1.2.18