1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
5 | * |
---|
6 | * This program is free software; you can redistribute it and/or modify |
---|
7 | * it under the terms of the GNU General Public License as published by |
---|
8 | * the Free Software Foundation; either version 2 of the License, or |
---|
9 | * (at your option) any later version. |
---|
10 | * |
---|
11 | * This program is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * GNU General Public License for more details. |
---|
15 | * |
---|
16 | * You should have received a copy of the GNU General Public License |
---|
17 | * along with this program; if not, write to the Free Software |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
19 | */ |
---|
20 | |
---|
21 | #ifndef _SHORTVECTOR_H |
---|
22 | #define _SHORTVECTOR_H |
---|
23 | |
---|
24 | #include <G3D/Vector3.h> |
---|
25 | |
---|
26 | namespace VMAP |
---|
27 | { |
---|
28 | /** |
---|
29 | Vector with 16 bit fix point values 12.4 bit. |
---|
30 | */ |
---|
31 | |
---|
32 | class ShortVector |
---|
33 | { |
---|
34 | private: |
---|
35 | short iX; |
---|
36 | short iY; |
---|
37 | short iZ; |
---|
38 | |
---|
39 | const static short maxvalue = 0x7fff; |
---|
40 | const static short minvalue = -0x7fff; |
---|
41 | const static int fixpointdiv = 16; |
---|
42 | const static short fixpoint_maxvalue = maxvalue / fixpointdiv; |
---|
43 | const static short fixpoint_minvalue = minvalue / fixpointdiv; |
---|
44 | |
---|
45 | inline short float2Short(float fv) const |
---|
46 | { |
---|
47 | short sv; |
---|
48 | debugAssert((fv <= fixpoint_maxvalue || fv >= 1000000) && (fv >= fixpoint_minvalue || fv <= -1000000)); |
---|
49 | if(fv >= fixpoint_maxvalue) |
---|
50 | sv=maxvalue; |
---|
51 | else if(fv <= fixpoint_minvalue) |
---|
52 | sv=minvalue; |
---|
53 | else |
---|
54 | sv = (short) (fv * fixpointdiv + 0.5); |
---|
55 | return(sv); |
---|
56 | } |
---|
57 | inline float short2Float(short sv) const |
---|
58 | { |
---|
59 | float fv; |
---|
60 | if(sv >= maxvalue) |
---|
61 | fv=G3D::inf(); |
---|
62 | else if(sv <= minvalue) |
---|
63 | fv=-G3D::inf(); |
---|
64 | else |
---|
65 | fv = ((float)sv) / fixpointdiv; |
---|
66 | return fv; |
---|
67 | } |
---|
68 | |
---|
69 | inline float getFX() const { return(short2Float(iX)); } |
---|
70 | inline float getFY() const { return(short2Float(iY)); } |
---|
71 | inline float getFZ() const { return(short2Float(iZ)); } |
---|
72 | public: |
---|
73 | inline ShortVector() {} |
---|
74 | inline ShortVector(const G3D::Vector3& pVector) |
---|
75 | { |
---|
76 | iX = float2Short(pVector.x); |
---|
77 | iY = float2Short(pVector.y); |
---|
78 | iZ = float2Short(pVector.z); |
---|
79 | } |
---|
80 | |
---|
81 | inline ShortVector(float pX, float pY, float pZ) |
---|
82 | { |
---|
83 | iX = float2Short(pX); |
---|
84 | iY = float2Short(pY); |
---|
85 | iZ = float2Short(pZ); |
---|
86 | } |
---|
87 | inline ShortVector(short pX, short pY, short pZ) |
---|
88 | { |
---|
89 | iX = pX; |
---|
90 | iY = pY; |
---|
91 | iZ = pZ; |
---|
92 | } |
---|
93 | inline ShortVector(const ShortVector& pShortVector) |
---|
94 | { |
---|
95 | iX = pShortVector.iX; |
---|
96 | iY = pShortVector.iY; |
---|
97 | iZ = pShortVector.iZ; |
---|
98 | } |
---|
99 | |
---|
100 | inline float getX() const { return(iX); } |
---|
101 | inline float getY() const { return(iY); } |
---|
102 | inline float getZ() const { return(iZ); } |
---|
103 | |
---|
104 | inline G3D::Vector3 getVector3() const { return(G3D::Vector3(getFX(), getFY(), getFZ())); } |
---|
105 | |
---|
106 | inline ShortVector min(const ShortVector pShortVector) |
---|
107 | { |
---|
108 | ShortVector result = pShortVector; |
---|
109 | if(pShortVector.iX > iX) { result.iX = iX; } |
---|
110 | if(pShortVector.iY > iY) { result.iY = iY; } |
---|
111 | if(pShortVector.iZ > iZ) { result.iZ = iZ; } |
---|
112 | return(result); |
---|
113 | } |
---|
114 | |
---|
115 | inline ShortVector max(const ShortVector pShortVector) |
---|
116 | { |
---|
117 | ShortVector result = pShortVector; |
---|
118 | if(pShortVector.iX < iX) { result.iX = iX; } |
---|
119 | if(pShortVector.iY < iY) { result.iY = iY; } |
---|
120 | if(pShortVector.iZ < iZ) { result.iZ = iZ; } |
---|
121 | return(result); |
---|
122 | } |
---|
123 | |
---|
124 | inline bool operator==(const ShortVector& v) const |
---|
125 | { |
---|
126 | return (iX == v.iX && iY == v.iY && iZ == v.iZ); |
---|
127 | } |
---|
128 | |
---|
129 | inline bool operator!=(const ShortVector& v) const |
---|
130 | { |
---|
131 | return !(iX == v.iX && iY == v.iY && iZ == v.iZ); |
---|
132 | } |
---|
133 | |
---|
134 | }; |
---|
135 | } |
---|
136 | #endif |
---|