00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
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
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
00073 static bool checkCastSint16(sint64 s)
00074 {
00075 return (s<=32767 && s>=-32768);
00076 }
00077
00078
00079 static bool checkCastSint16(float f)
00080 {
00081 sint64 s = (sint64)f;
00082 return (s<=32767 && s>=-32768);
00083 }
00084
00085
00086
00087 static sint16 pack(float f)
00088 {
00089 return safeCastSint16(f*Vector2sAccuracy);
00090 }
00091
00092
00093 static float unpack(sint16 s)
00094 {
00095 return (float)s/Vector2sAccuracy;
00096 }
00097
00098 public:
00099 sint16 x, y;
00100
00101 public:
00103
00104
00105
00107
00109
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 }
00181
00182
00183 #endif // NL_VECTOR_2S_H
00184
00185