TrinityCore
Position.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 Trinity_game_Position_h__
19#define Trinity_game_Position_h__
20
21#include "Define.h"
22#include <string>
23#include <cmath>
24
25class ByteBuffer;
26
28{
29 Position(float x = 0, float y = 0, float z = 0, float o = 0)
30 : m_positionX(x), m_positionY(y), m_positionZ(z), m_orientation(NormalizeOrientation(o)) { }
31
32 // streamer tags
33 struct XY;
34 struct XYZ;
35 struct XYZO;
36 struct PackedXYZ;
37
38 template <class Tag>
40 {
41 explicit ConstStreamer(Position const& pos) : Pos(&pos) { }
42 Position const* Pos;
43 };
44
45 template <class Tag>
46 struct Streamer
47 {
48 explicit Streamer(Position& pos) : Pos(&pos) { }
49 operator ConstStreamer<Tag>() const { return ConstStreamer<Tag>(*Pos); }
51 };
52
56 // Better to limit access to _orientation field, to guarantee the value is normalized
57private:
59
60public:
61 bool operator==(Position const& a) const;
62
63 void Relocate(float x, float y) { m_positionX = x; m_positionY = y; }
64 void Relocate(float x, float y, float z) { Relocate(x, y); m_positionZ = z; }
65 void Relocate(float x, float y, float z, float o) { Relocate(x, y, z); SetOrientation(o); }
66 void Relocate(Position const& pos) { *this = pos; }
67 void Relocate(Position const* pos) { *this = *pos; }
68
69 void RelocateOffset(Position const& offset);
70
71 void SetOrientation(float orientation)
72 {
73 m_orientation = NormalizeOrientation(orientation);
74 }
75
76 float GetPositionX() const { return m_positionX; }
77 float GetPositionY() const { return m_positionY; }
78 float GetPositionZ() const { return m_positionZ; }
79 float GetOrientation() const { return m_orientation; }
80
81 void GetPosition(float &x, float &y) const { x = m_positionX; y = m_positionY; }
82 void GetPosition(float &x, float &y, float &z) const { GetPosition(x, y); z = m_positionZ; }
83 void GetPosition(float &x, float &y, float &z, float &o) const { GetPosition(x, y, z); o = m_orientation; }
84 Position GetPosition() const { return *this; }
85
94
95 bool IsPositionValid() const;
96
97 float GetExactDist2dSq(const float x, const float y) const
98 {
99 float dx = x - m_positionX;
100 float dy = y - m_positionY;
101 return dx*dx + dy*dy;
102 }
103 float GetExactDist2dSq(Position const& pos) const { return GetExactDist2dSq(pos.m_positionX, pos.m_positionY); }
104 float GetExactDist2dSq(Position const* pos) const { return GetExactDist2dSq(*pos); }
105
106 float GetExactDist2d(const float x, const float y) const { return std::sqrt(GetExactDist2dSq(x, y)); }
107 float GetExactDist2d(Position const& pos) const { return GetExactDist2d(pos.m_positionX, pos.m_positionY); }
108 float GetExactDist2d(Position const* pos) const { return GetExactDist2d(*pos); }
109
110 float GetExactDistSq(float x, float y, float z) const
111 {
112 float dz = z - m_positionZ;
113 return GetExactDist2dSq(x, y) + dz*dz;
114 }
115 float GetExactDistSq(Position const& pos) const { return GetExactDistSq(pos.m_positionX, pos.m_positionY, pos.m_positionZ); }
116 float GetExactDistSq(Position const* pos) const { return GetExactDistSq(*pos); }
117
118 float GetExactDist(float x, float y, float z) const { return std::sqrt(GetExactDistSq(x, y, z)); }
119 float GetExactDist(Position const& pos) const { return GetExactDist(pos.m_positionX, pos.m_positionY, pos.m_positionZ); }
120 float GetExactDist(Position const* pos) const { return GetExactDist(*pos); }
121
122 void GetPositionOffsetTo(Position const & endPos, Position & retOffset) const;
123 Position GetPositionWithOffset(Position const& offset) const;
124
125 float GetAbsoluteAngle(float x, float y) const
126 {
127 float dx = x - m_positionX;
128 float dy = y - m_positionY;
129 return NormalizeOrientation(std::atan2(dy, dx));
130 }
131 float GetAbsoluteAngle(Position const& pos) const { return GetAbsoluteAngle(pos.m_positionX, pos.m_positionY); }
132 float GetAbsoluteAngle(Position const* pos) const { return GetAbsoluteAngle(*pos); }
133 float ToAbsoluteAngle(float relAngle) const { return NormalizeOrientation(relAngle + m_orientation); }
134
135 float ToRelativeAngle(float absAngle) const { return NormalizeOrientation(absAngle - m_orientation); }
136 float GetRelativeAngle(float x, float y) const { return ToRelativeAngle(GetAbsoluteAngle(x, y)); }
137 float GetRelativeAngle(Position const& pos) const { return ToRelativeAngle(GetAbsoluteAngle(pos)); }
138 float GetRelativeAngle(Position const* pos) const { return ToRelativeAngle(GetAbsoluteAngle(pos)); }
139
140 bool IsInDist2d(float x, float y, float dist) const { return GetExactDist2dSq(x, y) < dist * dist; }
141 bool IsInDist2d(Position const* pos, float dist) const { return GetExactDist2dSq(pos) < dist * dist; }
142
143 bool IsInDist(float x, float y, float z, float dist) const { return GetExactDistSq(x, y, z) < dist * dist; }
144 bool IsInDist(Position const& pos, float dist) const { return GetExactDistSq(pos) < dist * dist; }
145 bool IsInDist(Position const* pos, float dist) const { return GetExactDistSq(pos) < dist * dist; }
146
147 bool IsWithinBox(Position const& center, float xradius, float yradius, float zradius) const;
148
149 // dist2d < radius && abs(dz) < height
150 bool IsWithinDoubleVerticalCylinder(Position const* center, float radius, float height) const;
151
152 bool HasInArc(float arcangle, Position const* pos, float border = 2.0f) const;
153 bool HasInLine(Position const* pos, float objSize, float width) const;
154 std::string ToString() const;
155
156 // constrain arbitrary radian orientation to interval [0,2*PI)
157 static float NormalizeOrientation(float o);
158};
159
160#define MAPID_INVALID 0xFFFFFFFF
161
163{
164public:
165 explicit WorldLocation(uint32 mapId = MAPID_INVALID, float x = 0.f, float y = 0.f, float z = 0.f, float o = 0.f)
166 : Position(x, y, z, o), m_mapId(mapId) { }
167
168 WorldLocation(uint32 mapId, Position const& position)
169 : Position(position), m_mapId(mapId) { }
170
171 void WorldRelocate(WorldLocation const& loc) { m_mapId = loc.GetMapId(); Relocate(loc); }
172 void WorldRelocate(WorldLocation const* loc) { m_mapId = loc->GetMapId(); Relocate(loc); }
173 void WorldRelocate(uint32 mapId, Position const& pos) { m_mapId = mapId; Relocate(pos); }
174 void WorldRelocate(uint32 mapId = MAPID_INVALID, float x = 0.f, float y = 0.f, float z = 0.f, float o = 0.f)
175 {
176 m_mapId = mapId;
177 Relocate(x, y, z, o);
178 }
179
181 {
182 return *this;
183 }
184
185 uint32 GetMapId() const { return m_mapId; }
186
188
189 std::string GetDebugInfo() const;
190};
191
199
200template<class Tag>
202{
203 TaggedPosition(float x = 0.0f, float y = 0.0f, float z = 0.0f, float o = 0.0f) : Pos(x, y, z, o) { }
204 TaggedPosition(Position const& pos) : Pos(pos) { }
205
207 {
208 Pos.Relocate(pos);
209 return *this;
210 }
211
212 operator Position() const { return Pos; }
213
214 friend bool operator==(TaggedPosition const& left, TaggedPosition const& right) { return left.Pos == right.Pos; }
215 friend bool operator!=(TaggedPosition const& left, TaggedPosition const& right) { return left.Pos != right.Pos; }
216
217 friend ByteBuffer& operator<<(ByteBuffer& buf, TaggedPosition const& tagged) { return buf << Position::ConstStreamer<Tag>(tagged.Pos); }
218 friend ByteBuffer& operator>>(ByteBuffer& buf, TaggedPosition& tagged) { return buf >> Position::Streamer<Tag>(tagged.Pos); }
219
221};
222
223#endif // Trinity_game_Position_h__
#define TC_GAME_API
Definition: Define.h:124
uint32_t uint32
Definition: Define.h:143
std::string GetDebugInfo()
Definition: Errors.cpp:157
TC_GAME_API ByteBuffer & operator<<(ByteBuffer &buf, Position::ConstStreamer< Position::XY > const &streamer)
Definition: Position.cpp:149
#define MAPID_INVALID
Definition: Position.h:160
TC_GAME_API ByteBuffer & operator>>(ByteBuffer &buf, Position::Streamer< Position::XY > const &streamer)
Definition: Position.cpp:156
void WorldRelocate(WorldLocation const &loc)
Definition: Position.h:171
WorldLocation(uint32 mapId, Position const &position)
Definition: Position.h:168
uint32 GetMapId() const
Definition: Position.h:185
WorldLocation GetWorldLocation() const
Definition: Position.h:180
void WorldRelocate(uint32 mapId, Position const &pos)
Definition: Position.h:173
uint32 m_mapId
Definition: Position.h:187
void WorldRelocate(WorldLocation const *loc)
Definition: Position.h:172
void WorldRelocate(uint32 mapId=MAPID_INVALID, float x=0.f, float y=0.f, float z=0.f, float o=0.f)
Definition: Position.h:174
WorldLocation(uint32 mapId=MAPID_INVALID, float x=0.f, float y=0.f, float z=0.f, float o=0.f)
Definition: Position.h:165
std::string ToString(Type &&val, Params &&... params)
ConstStreamer(Position const &pos)
Definition: Position.h:41
Position const * Pos
Definition: Position.h:42
Position * Pos
Definition: Position.h:50
Streamer(Position &pos)
Definition: Position.h:48
Streamer< XYZO > PositionXYZOStream()
Definition: Position.h:90
float GetRelativeAngle(Position const &pos) const
Definition: Position.h:137
float m_positionZ
Definition: Position.h:55
void Relocate(Position const *pos)
Definition: Position.h:67
bool IsInDist(Position const &pos, float dist) const
Definition: Position.h:144
float GetExactDist2dSq(const float x, const float y) const
Definition: Position.h:97
ConstStreamer< XY > PositionXYStream() const
Definition: Position.h:87
Streamer< XY > PositionXYStream()
Definition: Position.h:86
float GetExactDist2d(const float x, const float y) const
Definition: Position.h:106
float GetRelativeAngle(float x, float y) const
Definition: Position.h:136
float GetExactDist(float x, float y, float z) const
Definition: Position.h:118
float GetExactDist(Position const &pos) const
Definition: Position.h:119
void Relocate(float x, float y, float z)
Definition: Position.h:64
float ToAbsoluteAngle(float relAngle) const
Definition: Position.h:133
float GetExactDist2dSq(Position const &pos) const
Definition: Position.h:103
float m_positionX
Definition: Position.h:53
float GetPositionZ() const
Definition: Position.h:78
float m_positionY
Definition: Position.h:54
float GetAbsoluteAngle(Position const &pos) const
Definition: Position.h:131
Streamer< PackedXYZ > PositionPackedXYZStream()
Definition: Position.h:92
bool IsInDist(Position const *pos, float dist) const
Definition: Position.h:145
bool IsInDist2d(float x, float y, float dist) const
Definition: Position.h:140
float GetOrientation() const
Definition: Position.h:79
float GetExactDist(Position const *pos) const
Definition: Position.h:120
float GetExactDist2d(Position const &pos) const
Definition: Position.h:107
bool IsInDist(float x, float y, float z, float dist) const
Definition: Position.h:143
void GetPosition(float &x, float &y, float &z) const
Definition: Position.h:82
float GetAbsoluteAngle(float x, float y) const
Definition: Position.h:125
Streamer< XYZ > PositionXYZStream()
Definition: Position.h:88
float GetExactDist2dSq(Position const *pos) const
Definition: Position.h:104
ConstStreamer< XYZ > PositionXYZStream() const
Definition: Position.h:89
void GetPosition(float &x, float &y, float &z, float &o) const
Definition: Position.h:83
void SetOrientation(float orientation)
Definition: Position.h:71
ConstStreamer< PackedXYZ > PositionPackedXYZStream() const
Definition: Position.h:93
float GetRelativeAngle(Position const *pos) const
Definition: Position.h:138
float GetExactDistSq(Position const *pos) const
Definition: Position.h:116
float GetExactDist2d(Position const *pos) const
Definition: Position.h:108
ConstStreamer< XYZO > PositionXYZOStream() const
Definition: Position.h:91
Position GetPosition() const
Definition: Position.h:84
void Relocate(Position const &pos)
Definition: Position.h:66
float m_orientation
Definition: Position.h:58
bool IsInDist2d(Position const *pos, float dist) const
Definition: Position.h:141
float GetAbsoluteAngle(Position const *pos) const
Definition: Position.h:132
Position(float x=0, float y=0, float z=0, float o=0)
Definition: Position.h:29
float GetPositionX() const
Definition: Position.h:76
void GetPosition(float &x, float &y) const
Definition: Position.h:81
float GetPositionY() const
Definition: Position.h:77
float GetExactDistSq(Position const &pos) const
Definition: Position.h:115
void Relocate(float x, float y, float z, float o)
Definition: Position.h:65
float GetExactDistSq(float x, float y, float z) const
Definition: Position.h:110
float ToRelativeAngle(float absAngle) const
Definition: Position.h:135
void Relocate(float x, float y)
Definition: Position.h:63
friend bool operator==(TaggedPosition const &left, TaggedPosition const &right)
Definition: Position.h:214
friend ByteBuffer & operator>>(ByteBuffer &buf, TaggedPosition &tagged)
Definition: Position.h:218
friend ByteBuffer & operator<<(ByteBuffer &buf, TaggedPosition const &tagged)
Definition: Position.h:217
TaggedPosition & operator=(Position const &pos)
Definition: Position.h:206
Position Pos
Definition: Position.h:220
TaggedPosition(Position const &pos)
Definition: Position.h:204
friend bool operator!=(TaggedPosition const &left, TaggedPosition const &right)
Definition: Position.h:215
TaggedPosition(float x=0.0f, float y=0.0f, float z=0.0f, float o=0.0f)
Definition: Position.h:203