[7017] Updated copyright notice for new year
[getmangos.git] / src / shared / vmap / ShortVector.h
blobcbb143c22869cc77c7406b10b8adee4def47f410
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef _SHORTVECTOR_H
20 #define _SHORTVECTOR_H
22 #include <G3D/Vector3.h>
24 namespace VMAP
26 /**
27 Vector with 16 bit fix point values 12.4 bit.
30 class ShortVector
32 private:
33 short iX;
34 short iY;
35 short iZ;
37 const static short maxvalue = 0x7fff;
38 const static short minvalue = -0x7fff;
39 const static int fixpointdiv = 16;
40 const static short fixpoint_maxvalue = maxvalue / fixpointdiv;
41 const static short fixpoint_minvalue = minvalue / fixpointdiv;
43 inline short float2Short(float fv) const
45 short sv;
46 debugAssert((fv <= fixpoint_maxvalue || fv >= 1000000) && (fv >= fixpoint_minvalue || fv <= -1000000));
47 if(fv >= fixpoint_maxvalue)
48 sv=maxvalue;
49 else if(fv <= fixpoint_minvalue)
50 sv=minvalue;
51 else
52 sv = (short) (fv * fixpointdiv + 0.5);
53 return(sv);
55 inline float short2Float(short sv) const
57 float fv;
58 if(sv >= maxvalue)
59 fv=G3D::inf();
60 else if(sv <= minvalue)
61 fv=-G3D::inf();
62 else
63 fv = ((float)sv) / fixpointdiv;
64 return fv;
67 inline float getFX() const { return(short2Float(iX)); }
68 inline float getFY() const { return(short2Float(iY)); }
69 inline float getFZ() const { return(short2Float(iZ)); }
70 public:
71 inline ShortVector() {}
72 inline ShortVector(const G3D::Vector3& pVector)
74 iX = float2Short(pVector.x);
75 iY = float2Short(pVector.y);
76 iZ = float2Short(pVector.z);
79 inline ShortVector(float pX, float pY, float pZ)
81 iX = float2Short(pX);
82 iY = float2Short(pY);
83 iZ = float2Short(pZ);
85 inline ShortVector(short pX, short pY, short pZ)
87 iX = pX;
88 iY = pY;
89 iZ = pZ;
91 inline ShortVector(const ShortVector& pShortVector)
93 iX = pShortVector.iX;
94 iY = pShortVector.iY;
95 iZ = pShortVector.iZ;
98 inline float getX() const { return(iX); }
99 inline float getY() const { return(iY); }
100 inline float getZ() const { return(iZ); }
102 inline G3D::Vector3 getVector3() const { return(G3D::Vector3(getFX(), getFY(), getFZ())); }
104 inline ShortVector min(const ShortVector pShortVector)
106 ShortVector result = pShortVector;
107 if(pShortVector.iX > iX) { result.iX = iX; }
108 if(pShortVector.iY > iY) { result.iY = iY; }
109 if(pShortVector.iZ > iZ) { result.iZ = iZ; }
110 return(result);
113 inline ShortVector max(const ShortVector pShortVector)
115 ShortVector result = pShortVector;
116 if(pShortVector.iX < iX) { result.iX = iX; }
117 if(pShortVector.iY < iY) { result.iY = iY; }
118 if(pShortVector.iZ < iZ) { result.iZ = iZ; }
119 return(result);
122 inline bool operator==(const ShortVector& v) const
124 return (iX == v.iX && iY == v.iY && iZ == v.iZ);
127 inline bool operator!=(const ShortVector& v) const
129 return !(iX == v.iX && iY == v.iY && iZ == v.iZ);
134 #endif