TrinityCore
vec3d.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 VEC3D_H
19#define VEC3D_H
20
21#include <iostream>
22#include <cmath>
23
24class Vec3D
25{
26public:
27 float x, y, z;
28
29 Vec3D(float x0 = 0.0f, float y0 = 0.0f, float z0 = 0.0f) : x(x0), y(y0), z(z0) { }
30
31 Vec3D(Vec3D const& v) = default;
32
33 Vec3D& operator=(Vec3D const& v) = default;
34
35 Vec3D operator+(Vec3D const& v) const
36 {
37 Vec3D r(x + v.x, y + v.y, z + v.z);
38 return r;
39 }
40
41 Vec3D operator-(Vec3D const& v) const
42 {
43 Vec3D r(x - v.x, y - v.y, z - v.z);
44 return r;
45 }
46
47 float operator*(Vec3D const& v) const
48 {
49 return x * v.x + y * v.y + z * v.z;
50 }
51
52 Vec3D operator*(float d) const
53 {
54 Vec3D r(x * d, y * d, z * d);
55 return r;
56 }
57
58 friend Vec3D operator*(float d, Vec3D const& v)
59 {
60 return v * d;
61 }
62
63 Vec3D operator%(Vec3D const& v) const
64 {
65 Vec3D r(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
66 return r;
67 }
68
70 {
71 x += v.x;
72 y += v.y;
73 z += v.z;
74 return *this;
75 }
76
78 {
79 x -= v.x;
80 y -= v.y;
81 z -= v.z;
82 return *this;
83 }
84
85 Vec3D& operator*=(float d)
86 {
87 x *= d;
88 y *= d;
89 z *= d;
90 return *this;
91 }
92
93 float lengthSquared() const
94 {
95 return x * x + y * y + z * z;
96 }
97
98 float length() const
99 {
100 return std::sqrt(lengthSquared());
101 }
102
104 {
105 *this *= (1.0f / length());
106 return *this;
107 }
108
110 {
111 Vec3D r(*this);
112 r.normalize();
113 return r;
114 }
115
116 friend std::istream& operator>>(std::istream& in, Vec3D& v)
117 {
118 in >> v.x >> v.y >> v.z;
119 return in;
120 }
121
122 friend std::ostream& operator<<(std::ostream& out, Vec3D const& v)
123 {
124 out << v.x << " " << v.y << " " << v.z;
125 return out;
126 }
127
128 operator float*()
129 {
130 return (float*)this;
131 }
132};
133
135{
136public:
139
140 AaBox3D& operator+=(Vec3D const& offset)
141 {
142 min += offset;
143 max += offset;
144 return *this;
145 }
146};
147
148class Vec2D
149{
150public:
151 float x, y;
152
153 Vec2D(float x0 = 0.0f, float y0 = 0.0f) : x(x0), y(y0) { }
154
155 Vec2D(Vec2D const& v) = default;
156
157 Vec2D& operator=(Vec2D const& v) = default;
158
159 Vec2D operator+(Vec2D const& v) const
160 {
161 Vec2D r(x + v.x, y + v.y);
162 return r;
163 }
164
165 Vec2D operator-(Vec2D const& v) const
166 {
167 Vec2D r(x - v.x, y - v.y);
168 return r;
169 }
170
171 float operator*(Vec2D const& v) const
172 {
173 return x * v.x + y * v.y;
174 }
175
176 Vec2D operator*(float d) const
177 {
178 Vec2D r(x * d, y * d);
179 return r;
180 }
181
182 friend Vec2D operator*(float d, Vec2D const& v)
183 {
184 return v * d;
185 }
186
188 {
189 x += v.x;
190 y += v.y;
191 return *this;
192 }
193
195 {
196 x -= v.x;
197 y -= v.y;
198 return *this;
199 }
200
202 {
203 x *= d;
204 y *= d;
205 return *this;
206 }
207
208 float lengthSquared() const
209 {
210 return x * x + y * y;
211 }
212
213 float length() const
214 {
215 return std::sqrt(lengthSquared());
216 }
217
219 {
220 *this *= (1.0f / length());
221 return *this;
222 }
223
225 {
226 Vec2D r(*this);
227 r.normalize();
228 return r;
229 }
230
231 friend std::istream& operator>>(std::istream& in, Vec2D& v)
232 {
233 in >> v.x >> v.y;
234 return in;
235 }
236
237 operator float*()
238 {
239 return (float*)this;
240 }
241};
242
243inline void rotate(float x0, float y0, float* x, float* y, float angle)
244{
245 float xa = *x - x0;
246 float ya = *y - y0;
247 *x = xa*cosf(angle) - ya*sinf(angle) + x0;
248 *y = xa*sinf(angle) + ya*cosf(angle) + y0;
249}
250
252{
253 float X, Y, Z, W;
254};
255
256#endif
Definition: vec3d.h:135
Vec3D max
Definition: vec3d.h:138
Vec3D min
Definition: vec3d.h:137
AaBox3D & operator+=(Vec3D const &offset)
Definition: vec3d.h:140
Definition: vec3d.h:149
Vec2D(float x0=0.0f, float y0=0.0f)
Definition: vec3d.h:153
Vec2D & operator=(Vec2D const &v)=default
float length() const
Definition: vec3d.h:213
friend std::istream & operator>>(std::istream &in, Vec2D &v)
Definition: vec3d.h:231
float operator*(Vec2D const &v) const
Definition: vec3d.h:171
Vec2D(Vec2D const &v)=default
Vec2D operator~() const
Definition: vec3d.h:224
Vec2D & normalize()
Definition: vec3d.h:218
float x
Definition: vec3d.h:151
Vec2D & operator*=(float d)
Definition: vec3d.h:201
Vec2D operator+(Vec2D const &v) const
Definition: vec3d.h:159
Vec2D & operator-=(Vec2D const &v)
Definition: vec3d.h:194
float lengthSquared() const
Definition: vec3d.h:208
friend Vec2D operator*(float d, Vec2D const &v)
Definition: vec3d.h:182
Vec2D operator-(Vec2D const &v) const
Definition: vec3d.h:165
float y
Definition: vec3d.h:151
Vec2D & operator+=(Vec2D const &v)
Definition: vec3d.h:187
Vec2D operator*(float d) const
Definition: vec3d.h:176
Definition: vec3d.h:25
Vec3D(Vec3D const &v)=default
float x
Definition: vec3d.h:27
Vec3D operator+(Vec3D const &v) const
Definition: vec3d.h:35
float operator*(Vec3D const &v) const
Definition: vec3d.h:47
Vec3D operator-(Vec3D const &v) const
Definition: vec3d.h:41
float y
Definition: vec3d.h:27
Vec3D operator*(float d) const
Definition: vec3d.h:52
float z
Definition: vec3d.h:27
Vec3D(float x0=0.0f, float y0=0.0f, float z0=0.0f)
Definition: vec3d.h:29
float lengthSquared() const
Definition: vec3d.h:93
Vec3D & operator=(Vec3D const &v)=default
friend std::istream & operator>>(std::istream &in, Vec3D &v)
Definition: vec3d.h:116
Vec3D & operator-=(Vec3D const &v)
Definition: vec3d.h:77
Vec3D & operator*=(float d)
Definition: vec3d.h:85
float length() const
Definition: vec3d.h:98
friend Vec3D operator*(float d, Vec3D const &v)
Definition: vec3d.h:58
Vec3D & normalize()
Definition: vec3d.h:103
Vec3D operator~() const
Definition: vec3d.h:109
friend std::ostream & operator<<(std::ostream &out, Vec3D const &v)
Definition: vec3d.h:122
Vec3D & operator+=(Vec3D const &v)
Definition: vec3d.h:69
Vec3D operator%(Vec3D const &v) const
Definition: vec3d.h:63
float X
Definition: vec3d.h:253
float Z
Definition: vec3d.h:253
float Y
Definition: vec3d.h:253
float W
Definition: vec3d.h:253
void rotate(float x0, float y0, float *x, float *y, float angle)
Definition: vec3d.h:243