Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

cl_vector.cpp

Go to the documentation of this file.
00001 /*
00002         $Id: cl_vector.cpp,v 1.4 2001/03/27 19:34:36 grumbel Exp $
00003 
00004         ------------------------------------------------------------------------
00005         ClanLib, the platform independent game SDK.
00006 
00007         This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
00008         version 2. See COPYING for details.
00009 
00010         For a total list of contributers see CREDITS.
00011 
00012         ------------------------------------------------------------------------
00013 
00014         1999/06/19 Daniel Vogel
00015         
00016         totally replaced old CL_Vector with this code
00017 */
00018 
00019 #include "Core/precomp.h"
00020 #include "API/Core/Math/cl_vector.h"
00021 #include "API/Core/System/cl_assert.h"
00022 #include <math.h>
00023 
00024 
00025 CL_Vector::CL_Vector(float x, float y, float z, float w)
00026 {
00027         this->x = x;
00028         this->y = y;
00029         this->z = z;
00030         this->w = w;
00031 }
00032 
00033 CL_Vector::CL_Vector(const CL_Vector &other)
00034 {
00035         x = other.x;
00036         y = other.y;
00037         z = other.z;
00038         w = other.w;
00039 }
00040 
00041 float CL_Vector::norm() const
00042 {
00043 #ifdef BORLAND
00044         return sqrt(x*x+y*y+z*z);
00045 #else
00046         return sqrtf(x*x+y*y+z*z);
00047 #endif
00048 }
00049 
00050 void CL_Vector::normalize()
00051 {
00052         float f = norm();
00053         if (f!=0)
00054         {
00055                 x /= f;
00056                 y /= f;
00057                 z /= f;
00058         }
00059 }
00060 
00061 float CL_Vector::dot(const CL_Vector& v) const
00062 {
00063         return x*v.x + y*v.y + z*v.z;  
00064 }
00065 
00066 float CL_Vector::angle(const CL_Vector& v) const
00067 {
00068 #ifdef BORLAND
00069         return acos(dot(v)/(norm()*v.norm()));
00070 #else
00071         return acosf(dot(v)/(norm()*v.norm()));  
00072 #endif
00073 }
00074 
00075 CL_Vector CL_Vector::cross(const CL_Vector& v) const
00076 {
00077         CL_Vector tmp = CL_Vector(y * v.z - z * v.y,
00078                                   z * v.x - x * v.z,
00079                                   x * v.y - y * v.x);
00080         return tmp;  
00081 }
00082 
00083 // quick hack, same as glRotatef(angle, a);
00084 CL_Vector CL_Vector::rotate(float angle, const CL_Vector& a) const
00085 {
00086         CL_Vector tmp = CL_Vector();
00087 #ifdef BORLAND
00088         float s = sin(angle);
00089         float c = cos(angle);
00090 #else
00091         float s = sinf(angle);
00092         float c = cosf(angle);
00093 #endif
00094         tmp.x = x*(a.x*a.x*(1-c)+c)     + y*(a.x*a.y*(1-c)-a.z*s) + z*(a.x*a.z*(1-c)+a.y*s);
00095         tmp.y = x*(a.y*a.x*(1-c)+a.z*s) + y*(a.y*a.y*(1-c)+c)     + z*(a.y*a.z*(1-c)-a.x*s);
00096         tmp.z = x*(a.x*a.z*(1-c)-a.y*s) + y*(a.y*a.z*(1-c)+a.x*s) + z*(a.z*a.z*(1-c)+c);
00097         return tmp;  
00098 }
00099 
00100 void CL_Vector::round()
00101 {
00102         x = int(x+0.5f);
00103         y = int(y+0.5f);
00104         z = int(z+0.5f);
00105         w = int(w+0.5f);
00106 }
00107 
00108 CL_Vector CL_Vector::operator * (float s)
00109 {
00110         CL_Vector tmp = CL_Vector(s * x,
00111                                   s * y,
00112                                   s * z,
00113                                   s * w);
00114         return tmp;
00115 }
00116 
00117 CL_Vector operator * (float s, const CL_Vector& v)
00118 {
00119         CL_Vector tmp = CL_Vector(s * v.x,
00120                                   s * v.y,
00121                                   s * v.z,
00122                                   s * v.w);
00123         return tmp;
00124 }
00125 
00126 void CL_Vector::operator += (const CL_Vector& v)
00127 {
00128         x += v.x;
00129         y += v.y;
00130         z += v.z;
00131         w += v.z;
00132 }
00133 
00134 void CL_Vector::operator -= (const CL_Vector& v)
00135 {
00136         x -= v.x;
00137         y -= v.y;
00138         z -= v.z;
00139         w -= v.w;
00140 }
00141 
00142 void CL_Vector::operator *= (float s)
00143 {
00144         x *= s;
00145         y *= s;
00146         z *= s;
00147         w *= s;
00148 }
00149 
00150 CL_Vector CL_Vector::operator + (const CL_Vector& v)
00151 {
00152         CL_Vector tmp = CL_Vector(x + v.x,
00153                                   y + v.y,
00154                                   z + v.z,
00155                                   w + v.w);
00156         return tmp;
00157 }
00158 
00159 CL_Vector CL_Vector::operator - (const CL_Vector& v)
00160 {
00161         CL_Vector tmp = CL_Vector(x - v.x,
00162                                   y - v.y,
00163                                   z - v.z,
00164                                   w - v.z);
00165         return tmp;
00166 }
00167 
00168 CL_Vector CL_Vector::operator - ()
00169 {
00170         CL_Vector tmp = CL_Vector(-x,
00171                                   -y,
00172                                   -z,
00173                                   -w);
00174         return tmp;
00175 }
00176 
00177 CL_Vector& CL_Vector::operator = (const CL_Vector& v)
00178 { 
00179         x = v.x;
00180         y = v.y;
00181         z = v.z;
00182         w = v.w;
00183         return *this;
00184 }
00185  
00186 int CL_Vector::operator == (const CL_Vector& v) const
00187 {
00188         return ((x == v.x) && (y == v.y) && (z == v.z) && (w == v.w));
00189 }
00190 
00191 int CL_Vector::operator != (const CL_Vector& v) const
00192 {
00193         return !(operator == (v));
00194 }
00195 
00196 float & CL_Vector::operator [] (int n)
00197 {
00198         switch (n)
00199         {
00200                 case 0: return x;
00201                 case 1: return y;
00202                 case 2: return z;
00203                 case 3: return w;
00204         }
00205         cl_assert(false);
00206         return x;                       // dummy
00207 }
00208 
00209 std::ostream& operator << (std::ostream& os, CL_Vector& v)
00210 {
00211         os << v.x << " " << v.y << " " << v.z;
00212         return os;
00213 }

Generated at Wed Apr 4 19:53:59 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001