# 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_2s.h

Go to the documentation of this file.
00001 
00007 /* Copyright, 2001 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 #ifndef NL_VECTOR_2S_H
00027 #define NL_VECTOR_2S_H
00028 
00029 #include "nel/misc/types_nl.h"
00030 #include "nel/misc/file.h"
00031 #include "nel/misc/vector_2f.h"
00032 #include "nel/misc/vector.h"
00033 
00034 
00035 namespace NLPACS {
00036 
00037 
00038 const float             Vector2sAccuracy = 128.0f;
00039 
00046 class CVector2s
00047 {
00048 private:
00049         // safely cast a fixed64 into a fixed16
00050         static sint16   safeCastSint16(sint64 s)
00051         {
00052 #ifdef NL_DEBUG
00053                 if (s>32767 || s<-32768)
00054                         nlerror("in CVector2s::safeCastSint16(sint64): value doesn't fit sint16 (value=%" NL_I64 "d)", s);
00055 #endif
00056                 return (sint16)s;
00057         }
00058 
00059         // safely cast a premuled float into a fixed16
00060         static sint16   safeCastSint16(float f)
00061         {
00062 #ifdef NL_DEBUG
00063                 sint64  s = (sint64)f;
00064                 if (s>32767 || s<-32768)
00065                         nlerror("in CVector2s::safeCastSint16(float): value doesn't fit sint16 (value=%f)", f);
00066                 return (sint16)s;
00067 #else
00068                 return (sint16)f;
00069 #endif
00070         }
00071 
00072         // check the cast into fixed16
00073         static bool             checkCastSint16(sint64 s)
00074         {
00075                 return (s<=32767 && s>=-32768);
00076         }
00077 
00078         // check the cast into fixed16
00079         static bool             checkCastSint16(float f)
00080         {
00081                 sint64  s = (sint64)f;
00082                 return (s<=32767 && s>=-32768);
00083         }
00084 
00085 
00086         // pack a float into a fixed16
00087         static sint16   pack(float f)
00088         {
00089                 return safeCastSint16(f*Vector2sAccuracy);
00090         }
00091 
00092         // unpack a fixed16 into a float
00093         static float    unpack(sint16 s)
00094         {
00095                 return (float)s/Vector2sAccuracy;
00096         }
00097 
00098 public:         // Attributes.
00099         sint16  x, y;
00100 
00101 public:         // Methods.
00103 
00104 
00105         CVector2s() {}
00107         CVector2s(sint16 _x, sint16 _y) : x(_x), y(_y) {}
00109         CVector2s(const CVector2s &v) : x(v.x), y(v.y) {}
00110         CVector2s(const NLMISC::CVector &v) { pack(v); }
00112 
00113 
00115 
00116         CVector2s       &operator+=(const CVector2s &v)         {x=safeCastSint16((sint64)v.x+(sint64)x); y=safeCastSint16((sint64)v.y+(sint64)y); return *this;}
00117         CVector2s       &operator-=(const CVector2s &v)         {x=safeCastSint16((sint64)v.x-(sint64)x); y=safeCastSint16((sint64)v.y-(sint64)y); return *this;}
00118         CVector2s       operator+(const CVector2s &v) const     {return CVector2s(safeCastSint16((sint64)v.x+(sint64)x), safeCastSint16((sint64)v.y+(sint64)y));}
00119         CVector2s       operator-(const CVector2s &v) const     {return CVector2s(safeCastSint16((sint64)v.x-(sint64)x), safeCastSint16((sint64)v.y-(sint64)y));}
00120         CVector2s       operator-() const                                       {return CVector2s(safeCastSint16(-(sint64)x), safeCastSint16(-(sint64)y));}
00121 
00122         CVector2s       &operator*=(float f)                            { x = safeCastSint16(f*x); y = safeCastSint16(f*y); return *this; }
00123         CVector2s       &operator/=(float f)                            { x = safeCastSint16(f/x); y = safeCastSint16(f/y); return *this; }
00124         CVector2s       operator*(float f) const                        {return CVector2s(safeCastSint16(x*f), safeCastSint16(y*f));}
00125         CVector2s       operator/(float f) const                        {return CVector2s(safeCastSint16(x/f), safeCastSint16(y/f));}
00127 
00129 
00130 
00131         float   operator*(const CVector2s &v) const             {return ((float)x*(float)v.x + (float)y*(float)v.y)/(Vector2sAccuracy*Vector2sAccuracy);}
00133         float   norm() const                                                    {return (float)sqrt(sqrnorm());}
00135         float   sqrnorm() const                                                 {return ((float)x*(float)x + (float)y*(float)y)/(Vector2sAccuracy*Vector2sAccuracy);}
00137         void    normalize()
00138         {
00139                 float   f= norm();
00140                 if(f>0)
00141                         *this/=f;
00142         }
00144         CVector2s       normed() const
00145         {
00146                 CVector2s       v= *this;
00147                 v.normalize();
00148                 return v;
00149         }
00151 
00153 
00154         void    set(sint16 _x, sint16 _y)                               {x= _x; y=_y;}
00155         bool    operator==(const CVector2s &v) const    {return x==v.x && y==v.y;}
00156         bool    operator!=(const CVector2s &v) const    {return !(*this==v);}
00157         bool    isNull() const                                                  {return x==0.0f && y==0.0f;}
00159         void    minof(const CVector2s &a, const CVector2s &b)
00160         {
00161                 x= std::min(a.x, b.x);
00162                 y= std::min(a.y, b.y);
00163         }
00165         void    maxof(const CVector2s &a, const CVector2s &b)
00166         {
00167                 x= std::max(a.x, b.x);
00168                 y= std::max(a.y, b.y);
00169         }
00171         void    serial(NLMISC::IStream &f)                              {f.serial(x,y);}
00173 
00174         void                            pack(const NLMISC::CVector &v)          { x = pack(v.x); y = pack(v.y); }
00175         void                            pack(const NLMISC::CVector2f &v)        { x = pack(v.x); y = pack(v.y); }
00176         NLMISC::CVector2f       unpack() const                                          { return NLMISC::CVector2f(unpack(x), unpack(y)); }
00177         NLMISC::CVector         unpack3f(float hintz=0.0f) const        { return NLMISC::CVector(unpack(x), unpack(y), hintz); }
00178 };
00179 
00180 } // NLPACS
00181 
00182 
00183 #endif // NL_VECTOR_2S_H
00184 
00185 /* End of vector_2s.h */