Crystal Space: csgeom/vector3.h Source File (Crystal Space Public API Reference)

| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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 "math3d_d.h"
    00032
    00033 class csString;
    00034
    00039 enum
    00040 {
    00042   CS_AXIS_NONE = -1,
    00044   CS_AXIS_X = 0,
    00046   CS_AXIS_Y = 1,
    00048   CS_AXIS_Z = 2,
    00050   CS_AXIS_W = 3
    00051 };
    00052
    00056 class CS_CRYSTALSPACE_EXPORT csVector3
    00057 {
    00058 public:
    00059
    00060 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
    00061   union
    00062   {
    00063     struct
    00064     {
    00065 #endif
    00067       float x;
    00069       float y;
    00071       float z;
    00072 #if !defined(__STRICT_ANSI__) && !defined(SWIG)
    00073     };
    00075     float m[3];
    00076   };
    00077 #endif
    00078
    00084   csVector3 () {}
    00085
    00091   csVector3 (float m) : x(m), y(m), z(m) {}
    00092
    00094   csVector3 (float ix, float iy, float iz = 0) : x(ix), y(iy), z(iz) {}
    00095
    00097   csVector3 (const csVector3& v) : x(v.x), y(v.y), z(v.z) {}
    00098
    00100   csVector3 (const csDVector3&);
    00101
    00103   csString Description() const;
    00104
    00106   inline friend csVector3 operator+(const csVector3& v1, const csVector3& v2)
    00107   { return csVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
    00108
    00110   inline friend csDVector3 operator+(const csDVector3& v1, const csVector3& v2)
    00111   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
    00112
    00114   inline friend csDVector3 operator+(const csVector3& v1, const csDVector3& v2)
    00115   { return csDVector3(v1.x+v2.x, v1.y+v2.y, v1.z+v2.z); }
    00116
    00118   inline friend csVector3 operator-(const csVector3& v1, const csVector3& v2)
    00119   { return csVector3(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 csDVector3 operator-(const csDVector3& v1, const csVector3& v2)
    00127   { return csDVector3(v1.x-v2.x, v1.y-v2.y, v1.z-v2.z); }
    00128
    00130   inline friend float operator*(const csVector3& v1, const csVector3& v2)
    00131   { return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z; }
    00132
    00134   inline friend csVector3 operator%(const csVector3& v1, const csVector3& v2)
    00135   {
    00136     return csVector3 (v1.y*v2.z-v1.z*v2.y,
    00137                       v1.z*v2.x-v1.x*v2.z,
    00138                       v1.x*v2.y-v1.y*v2.x);
    00139   }
    00140
    00142   void Cross (const csVector3 & px, const csVector3 & py)
    00143   {
    00144     x = px.y*py.z - px.z*py.y;
    00145     y = px.z*py.x - px.x*py.z;
    00146     z = px.x*py.y - px.y*py.x;
    00147   }
    00148
    00150   inline friend csVector3 operator* (const csVector3& v, float f)
    00151   { return csVector3(v.x*f, v.y*f, v.z*f); }
    00152
    00154   inline friend csVector3 operator* (float f, const csVector3& v)
    00155   { return csVector3(v.x*f, v.y*f, v.z*f); }
    00156
    00158   inline friend csDVector3 operator* (const csVector3& v, double f)
    00159   { return csDVector3(v) * f; }
    00160
    00162   inline friend csDVector3 operator* (double f, const csVector3& v)
    00163   { return csDVector3(v) * f; }
    00164
    00166   inline friend csVector3 operator* (const csVector3& v, int f)
    00167   { return v * (float)f; }
    00168
    00170   inline friend csVector3 operator* (int f, const csVector3& v)
    00171   { return v * (float)f; }
    00172
    00174   inline friend csVector3 operator/ (const csVector3& v, float f)
    00175   { f = 1.0f/f; return csVector3(v.x*f, v.y*f, v.z*f); }
    00176
    00178   inline friend csDVector3 operator/ (const csVector3& v, double f)
    00179   { return csDVector3(v) / f; }
    00180
    00182   inline friend csVector3 operator/ (const csVector3& v, int f)
    00183   { return v / (float)f; }
    00184
    00186   inline friend bool operator== (const csVector3& v1, const csVector3& v2)
    00187   { return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z; }
    00188
    00190   inline friend bool operator!= (const csVector3& v1, const csVector3& v2)
    00191   { return v1.x!=v2.x || v1.y!=v2.y || v1.z!=v2.z; }
    00192
    00194   inline friend csVector3 operator>> (const csVector3& v1, const csVector3& v2)
    00195   { return v2*(v1*v2)/(v2*v2); }
    00196
    00198   inline friend csVector3 operator<< (const csVector3& v1, const csVector3& v2)
    00199   { return v1*(v1*v2)/(v1*v1); }
    00200
    00202   inline friend bool operator< (const csVector3& v, float f)
    00203   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
    00204
    00206   inline friend bool operator> (float f, const csVector3& v)
    00207   { return ABS(v.x)<f && ABS(v.y)<f && ABS(v.z)<f; }
    00208
    00210 #ifdef __STRICT_ANSI__
    00211   inline float operator[] (size_t n) const { return !n?x:n&1?y:z; }
    00212 #else
    00213   inline float operator[] (size_t n) const { return m[n]; }
    00214 #endif
    00215
    00217 #ifdef __STRICT_ANSI__
    00218   inline float & operator[] (size_t n) { return !n?x:n&1?y:z; }
    00219 #else
    00220   inline float & operator[] (size_t n) { return m[n]; }
    00221 #endif
    00222
    00224   inline csVector3& operator+= (const csVector3& v)
    00225   {
    00226     x += v.x;
    00227     y += v.y;
    00228     z += v.z;
    00229
    00230     return *this;
    00231   }
    00232
    00234   inline csVector3& operator-= (const csVector3& v)
    00235   {
    00236     x -= v.x;
    00237     y -= v.y;
    00238     z -= v.z;
    00239
    00240     return *this;
    00241   }
    00242
    00244   inline csVector3& operator*= (float f)
    00245   { x *= f; y *= f; z *= f; return *this; }
    00246
    00248   inline csVector3& operator/= (float f)
    00249   { f = 1.0f / f; x *= f; y *= f; z *= f; return *this; }
    00250
    00252   inline csVector3 operator+ () const { return *this; }
    00253
    00255   inline csVector3 operator- () const { return csVector3(-x,-y,-z); }
    00256
    00258   inline void Set (float sx, float sy, float sz) { x = sx; y = sy; z = sz; }
    00259
    00261   inline void Set (csVector3 const& v) { x = v.x; y = v.y; z = v.z; }
    00262
    00264   inline void Set (float const* v) { x = v[0]; y = v[1]; z = v[2]; }
    00265
    00267   inline void Set (float v) { x = y = z = v; }
    00268
    00270   inline void Get (float* v) { v[0] = x; v[1] = y; v[2] = z; }
    00271
    00273   float Norm () const;
    00274
    00276   float SquaredNorm () const
    00277   { return x * x + y * y + z * z; }
    00278
    00284   csVector3 Unit () const { return (*this)/(this->Norm()); }
    00285
    00287   inline static float Norm (const csVector3& v) { return v.Norm(); }
    00288
    00290   inline static csVector3 Unit (const csVector3& v) { return v.Unit(); }
    00291
    00293   void Normalize ();
    00294
    00296   inline bool IsZero (float precision = SMALL_EPSILON) const
    00297   {
    00298     return (fabsf(x) < precision) && (fabsf(y) < precision)
    00299             && (fabsf(z) < precision);
    00300   }
    00301
    00303   inline csVector3 UnitAxisClamped () const
    00304   {
    00305     if (IsZero ())
    00306       return csVector3 (0, 0, 0);
    00307
    00308     if (fabsf (x) > fabsf (y) && fabsf (x) > fabsf (z))
    00309       return csVector3 (x / fabsf (x), 0, 0); //X biggest
    00310     else if (fabsf (y) > fabsf (z))
    00311       return csVector3 (0, y / fabsf (y), 0); //Y biggest
    00312     else
    00313       return csVector3 (0, 0, z / fabsf (z)); //Z biggest
    00314   }
    00315
    00317   inline size_t DominantAxis () const
    00318   {
    00319     if (fabsf (x) > fabsf (y) && fabsf (x) > fabsf (z))
    00320       return CS_AXIS_X;
    00321     else if (fabsf (y) > fabsf (z))
    00322       return CS_AXIS_Y;
    00323     else
    00324       return CS_AXIS_Z;
    00325   }
    00326 };
    00327
    00330 #endif // __CS_VECTOR3_H__