OregonCore  revision fb2a440-git
Your Favourite TBC server
Position.h
Go to the documentation of this file.
1 /*
2 * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #ifndef POSITION_H
19 #define POSITION_H
20 
21 #include "Common.h"
22 
23 class ByteBuffer;
24 
25 struct Position
26 {
27  Position(float x = 0, float y = 0, float z = 0, float o = 0)
29 
30  Position(Position const& loc) { Relocate(loc); }
31 
33  {
34  explicit PositionXYStreamer(Position& pos) : Pos(&pos) { }
36  };
37 
39  {
40  explicit PositionXYZStreamer(Position& pos) : Pos(&pos) { }
42  };
43 
45  {
46  explicit PositionXYZOStreamer(Position& pos) : Pos(&pos) { }
48  };
49 
50  float m_positionX;
51  float m_positionY;
52  float m_positionZ;
53  // Better to limit access to _orientation field, to guarantee the value is normalized
54 private:
56 
57 public:
58  bool operator==(Position const &a);
59 
60  inline bool operator!=(Position const &a)
61  {
62  return !(operator==(a));
63  }
64 
65  void Relocate(float x, float y)
66  {
67  m_positionX = x; m_positionY = y;
68  }
69 
70  void Relocate(float x, float y, float z)
71  {
72  m_positionX = x; m_positionY = y; m_positionZ = z;
73  }
74 
75  void Relocate(float x, float y, float z, float orientation)
76  {
77  m_positionX = x; m_positionY = y; m_positionZ = z; SetOrientation(orientation);
78  }
79 
80  void Relocate(Position const &pos)
81  {
82  m_positionX = pos.m_positionX; m_positionY = pos.m_positionY; m_positionZ = pos.m_positionZ; SetOrientation(pos.m_orientation);
83  }
84 
85  void Relocate(Position const* pos)
86  {
87  m_positionX = pos->m_positionX; m_positionY = pos->m_positionY; m_positionZ = pos->m_positionZ; SetOrientation(pos->m_orientation);
88  }
89 
90  void RelocateOffset(Position const &offset);
91 
92  void SetOrientation(float orientation)
93  {
94  m_orientation = NormalizeOrientation(orientation);
95  }
96 
97  float GetPositionX() const { return m_positionX; }
98  float GetPositionY() const { return m_positionY; }
99  float GetPositionZ() const { return m_positionZ; }
100  float GetOrientation() const { return m_orientation; }
101 
102  void GetPosition(float &x, float &y) const
103  {
104  x = m_positionX; y = m_positionY;
105  }
106 
107  void GetPosition(float &x, float &y, float &z) const
108  {
109  x = m_positionX; y = m_positionY; z = m_positionZ;
110  }
111 
112  void GetPosition(float &x, float &y, float &z, float &o) const
113  {
115  }
116 
117  Position GetPosition() const { return *this; }
118 
122 
123  bool IsPositionValid() const;
124 
125  float GetExactDist2dSq(float x, float y) const
126  {
127  float dx = m_positionX - x; float dy = m_positionY - y; return dx*dx + dy*dy;
128  }
129 
130  float GetExactDist2d(const float x, const float y) const
131  {
132  return std::sqrt(GetExactDist2dSq(x, y));
133  }
134 
135  float GetExactDist2dSq(Position const* pos) const
136  {
137  float dx = m_positionX - pos->m_positionX; float dy = m_positionY - pos->m_positionY; return dx*dx + dy*dy;
138  }
139 
140  float GetExactDist2d(Position const* pos) const
141  {
142  return std::sqrt(GetExactDist2dSq(pos));
143  }
144 
145  float GetExactDistSq(float x, float y, float z) const
146  {
147  float dz = m_positionZ - z; return GetExactDist2dSq(x, y) + dz*dz;
148  }
149 
150  float GetExactDist(float x, float y, float z) const
151  {
152  return std::sqrt(GetExactDistSq(x, y, z));
153  }
154 
155  float GetExactDistSq(Position const* pos) const
156  {
157  float dx = m_positionX - pos->m_positionX; float dy = m_positionY - pos->m_positionY; float dz = m_positionZ - pos->m_positionZ; return dx*dx + dy*dy + dz*dz;
158  }
159 
160  float GetExactDist(Position const* pos) const
161  {
162  return std::sqrt(GetExactDistSq(pos));
163  }
164 
165  void GetPositionOffsetTo(Position const & endPos, Position & retOffset) const;
166  Position GetPositionWithOffset(Position const& offset) const;
167 
168  float GetAngle(Position const* pos) const;
169  float GetAngle(float x, float y) const;
170  float GetRelativeAngle(Position const* pos) const
171  {
172  return GetAngle(pos) - m_orientation;
173  }
174 
175  float GetRelativeAngle(float x, float y) const { return GetAngle(x, y) - m_orientation; }
176  void GetSinCos(float x, float y, float &vsin, float &vcos) const;
177 
178  bool IsInDist2d(float x, float y, float dist) const
179  {
180  return GetExactDist2dSq(x, y) < dist * dist;
181  }
182 
183  bool IsInDist2d(Position const* pos, float dist) const
184  {
185  return GetExactDist2dSq(pos) < dist * dist;
186  }
187 
188  bool IsInDist(float x, float y, float z, float dist) const
189  {
190  return GetExactDistSq(x, y, z) < dist * dist;
191  }
192 
193  bool IsInDist(Position const* pos, float dist) const
194  {
195  return GetExactDistSq(pos) < dist * dist;
196  }
197 
198  bool IsWithinBox(const Position& center, float xradius, float yradius, float zradius) const;
199  bool HasInArc(float arcangle, Position const* pos, float border = 2.0f) const;
200  bool HasInLine(Position const* pos, float width) const;
201  std::string ToString() const;
202 
203  // modulos a radian orientation to the range of 0..2PI
204  static float NormalizeOrientation(float o)
205  {
206  // fmod only supports positive numbers. Thus we have
207  // to emulate negative numbers
208  if (o < 0)
209  {
210  float mod = o *-1;
211  mod = std::fmod(mod, 2.0f * static_cast<float>(M_PI));
212  mod = -mod + 2.0f * static_cast<float>(M_PI);
213  return mod;
214  }
215  return std::fmod(o, 2.0f * static_cast<float>(M_PI));
216  }
217 };
218 
225 
226 #endif // POSITION_H
bool IsWithinBox(const Position &center, float xradius, float yradius, float zradius) const
Definition: Position.cpp:101
Position GetPositionWithOffset(Position const &offset) const
Definition: Position.cpp:56
PositionXYZOStreamer(Position &pos)
Definition: Position.h:46
Position::PositionXYZOStreamer PositionXYZOStream()
Definition: Position.h:121
float GetExactDist(Position const *pos) const
Definition: Position.h:160
Position::PositionXYStreamer PositionXYStream()
Definition: Position.h:119
void RelocateOffset(Position const &offset)
Definition: Position.cpp:32
float GetExactDist2dSq(float x, float y) const
Definition: Position.h:125
float m_positionY
Definition: Position.h:51
bool HasInLine(Position const *pos, float width) const
Definition: Position.cpp:151
void GetPosition(float &x, float &y, float &z) const
Definition: Position.h:107
void SetOrientation(float orientation)
Definition: Position.h:92
float m_positionX
Definition: Position.h:50
static float NormalizeOrientation(float o)
Definition: Position.h:204
void GetSinCos(float x, float y, float &vsin, float &vcos) const
Definition: Position.cpp:82
Position::PositionXYZStreamer PositionXYZStream()
Definition: Position.h:120
void Relocate(float x, float y, float z)
Definition: Position.h:70
bool IsInDist2d(Position const *pos, float dist) const
Definition: Position.h:183
float GetExactDist2d(const float x, const float y) const
Definition: Position.h:130
float GetExactDist2d(Position const *pos) const
Definition: Position.h:140
float GetOrientation() const
Definition: Position.h:100
void Relocate(float x, float y)
Definition: Position.h:65
float GetRelativeAngle(float x, float y) const
Definition: Position.h:175
ByteBuffer & operator>>(ByteBuffer &buf, Position::PositionXYStreamer const &streamer)
Definition: Position.cpp:174
float GetPositionY() const
Definition: Position.h:98
std::string ToString() const
Definition: Position.cpp:160
void GetPosition(float &x, float &y) const
Definition: Position.h:102
Position(float x=0, float y=0, float z=0, float o=0)
Definition: Position.h:27
float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:145
void GetPositionOffsetTo(Position const &endPos, Position &retOffset) const
Definition: Position.cpp:45
PositionXYStreamer(Position &pos)
Definition: Position.h:34
float GetPositionZ() const
Definition: Position.h:99
float GetAngle(Position const *pos) const
float GetRelativeAngle(Position const *pos) const
Definition: Position.h:170
bool HasInArc(float arcangle, Position const *pos, float border=2.0f) const
Definition: Position.cpp:129
float m_positionZ
Definition: Position.h:52
Position GetPosition() const
Definition: Position.h:117
void Relocate(Position const *pos)
Definition: Position.h:85
float GetExactDist2dSq(Position const *pos) const
Definition: Position.h:135
bool IsInDist2d(float x, float y, float dist) const
Definition: Position.h:178
ByteBuffer & operator<<(ByteBuffer &buf, Position::PositionXYStreamer const &streamer)
Definition: Position.cpp:167
bool IsInDist(Position const *pos, float dist) const
Definition: Position.h:193
Position(Position const &loc)
Definition: Position.h:30
float GetExactDist(float x, float y, float z) const
Definition: Position.h:150
bool IsPositionValid() const
Definition: Position.cpp:40
void Relocate(Position const &pos)
Definition: Position.h:80
void GetPosition(float &x, float &y, float &z, float &o) const
Definition: Position.h:112
float GetPositionX() const
Definition: Position.h:97
void Relocate(float x, float y, float z, float orientation)
Definition: Position.h:75
PositionXYZStreamer(Position &pos)
Definition: Position.h:40
float m_orientation
Definition: Position.h:55
bool operator==(Position const &a)
Definition: Position.cpp:24
bool IsInDist(float x, float y, float z, float dist) const
Definition: Position.h:188
float GetExactDistSq(Position const *pos) const
Definition: Position.h:155
bool operator!=(Position const &a)
Definition: Position.h:60