# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

vector_inline.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2000 Nevrax Ltd.
00008  *
00009  * This file is part of NEVRAX NEL.
00010  * NEVRAX NEL is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2, or (at your option)
00013  * any later version.
00014 
00015  * NEVRAX NEL is distributed in the hope that it will be useful, but
00016  * WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00018  * General Public License for more details.
00019 
00020  * You should have received a copy of the GNU General Public License
00021  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00023  * MA 02111-1307, USA.
00024  */
00025 
00026 
00027 #ifndef NL_VECTOR_INLINE_H
00028 #define NL_VECTOR_INLINE_H
00029 
00030 
00031 #include "nel/misc/common.h"
00032 
00033 
00034 namespace       NLMISC
00035 {
00036 
00037 
00038 // ============================================================================================
00039 // Base Maths.
00040 inline  CVector &CVector::operator+=(const CVector &v)
00041 {
00042         x+=v.x;
00043         y+=v.y;
00044         z+=v.z;
00045         return *this;
00046 }
00047 inline  CVector &CVector::operator-=(const CVector &v)
00048 {
00049         x-=v.x;
00050         y-=v.y;
00051         z-=v.z;
00052         return *this;
00053 }
00054 inline  CVector &CVector::operator*=(float f)
00055 {
00056         x*=f;
00057         y*=f;
00058         z*=f;
00059         return *this;
00060 }
00061 inline  CVector &CVector::operator/=(float f)
00062 {
00063         return *this*= (1.0f/f);
00064 }
00065 inline  CVector CVector::operator+(const CVector &v) const
00066 {
00067         CVector ret(x+v.x, y+v.y, z+v.z);
00068         return ret;
00069 }
00070 inline  CVector CVector::operator-(const CVector &v) const
00071 {
00072         CVector ret(x-v.x, y-v.y, z-v.z);
00073         return ret;
00074 }
00075 inline  CVector CVector::operator*(float f) const
00076 {
00077         CVector ret(x*f, y*f, z*f);
00078         return ret;
00079 }
00080 inline  CVector CVector::operator/(float f) const
00081 {
00082         return *this*(1.0f/f);
00083 }
00084 inline  CVector CVector::operator-() const
00085 {
00086         return CVector(-x,-y,-z);
00087 }
00088 inline CVector  operator*(float f, const CVector &v)
00089 {
00090         CVector ret(v.x*f, v.y*f, v.z*f);
00091         return ret;
00092 }
00093 
00094 
00095 // ============================================================================================
00096 // Advanced Maths.
00097 inline  float   CVector::operator*(const CVector &v) const
00098 {
00099         return x*v.x + y*v.y + z*v.z;
00100 }
00101 inline  CVector CVector::operator^(const CVector &v) const
00102 {
00103         CVector ret;
00104 
00105         ret.x= y*v.z - z*v.y;
00106         ret.y= z*v.x - x*v.z;
00107         ret.z= x*v.y - y*v.x;
00108 
00109         return ret;
00110 }
00111 inline  float   CVector::sqrnorm() const
00112 {
00113         return (float)(x*x + y*y + z*z);
00114 }
00115 inline  float   CVector::norm() const
00116 {
00117         return (float)sqrt(x*x + y*y + z*z);
00118 }
00119 inline  void    CVector::normalize()
00120 {
00121         float   n=norm();
00122         if(n)
00123                 *this/=n;
00124 }
00125 inline  CVector CVector::normed() const
00126 {
00127         CVector ret;
00128         ret= *this;
00129         ret.normalize();
00130         return ret;
00131 }
00132 
00133 
00134 // ============================================================================================
00135 // Misc.
00136 inline  void    CVector::set(float _x, float _y, float _z)
00137 {
00138         x=_x; y=_y; z=_z;
00139 }
00140 inline  bool    CVector::operator==(const CVector &v) const 
00141 {
00142         return x==v.x && y==v.y && z==v.z;
00143 }
00144 inline  bool    CVector::operator!=(const CVector &v) const 
00145 {
00146         return !(*this==v);
00147 }
00148 inline  bool    CVector::isNull() const
00149 {
00150         return *this==CVector::Null;
00151 }
00152 inline  bool    CVector::operator<(const CVector &v) const
00153 {
00154         if(x!=v.x)
00155                 return x<v.x;
00156         if(y!=v.y)
00157                 return y<v.y;
00158         return z<v.z;
00159 }
00160 
00161 inline  void    CVector::cartesianToSpheric(float &r, float &theta,float &phi) const
00162 {
00163         CVector v;
00164 
00165         r= norm();
00166         v= normed();
00167 
00168         // phi E [-PI/2 et PI/2]
00169         clamp(v.z, -1.0f, 1.0f);
00170         phi= (float)asin(v.z);
00171 
00172         // theta [-PI,PI]
00173         theta= (float)atan2(v.y,v.x);
00174 }
00175 inline  void    CVector::sphericToCartesian(float r, float theta,float phi)
00176 {
00177         double  ct= cos(theta);
00178         double  st= sin(theta);
00179         double  cp= cos(phi);
00180         double  sp= sin(phi);
00181 
00182         x= (float)(r*ct*cp);
00183         y= (float)(r*st*cp);
00184         z= (float)(r*sp);
00185 }
00186 inline  void    CVector::minof(const CVector &a, const CVector &b)
00187 {
00188         x= std::min(a.x, b.x);
00189         y= std::min(a.y, b.y);
00190         z= std::min(a.z, b.z);
00191 }
00192 inline  void    CVector::maxof(const CVector &a, const CVector &b)
00193 {
00194         x= std::max(a.x, b.x);
00195         y= std::max(a.y, b.y);
00196         z= std::max(a.z, b.z);
00197 }
00198 inline  void    CVector::serial(IStream &f)
00199 {
00200         f.serial(x,y,z);
00201 }
00202 
00203 
00204 }
00205 
00206 
00207 #endif
00208