]> git.lizzy.rs Git - irrlicht.git/blob - include/vector2d.h
Add back LightManager
[irrlicht.git] / include / vector2d.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #ifndef __IRR_POINT_2D_H_INCLUDED__\r
6 #define __IRR_POINT_2D_H_INCLUDED__\r
7 \r
8 #include "irrMath.h"\r
9 #include "dimension2d.h"\r
10 \r
11 #include <functional>\r
12 \r
13 namespace irr\r
14 {\r
15 namespace core\r
16 {\r
17 \r
18 \r
19 //! 2d vector template class with lots of operators and methods.\r
20 /** As of Irrlicht 1.6, this class supersedes position2d, which should\r
21         be considered deprecated. */\r
22 template <class T>\r
23 class vector2d\r
24 {\r
25 public:\r
26         //! Default constructor (null vector)\r
27         vector2d() : X(0), Y(0) {}\r
28         //! Constructor with two different values\r
29         vector2d(T nx, T ny) : X(nx), Y(ny) {}\r
30         //! Constructor with the same value for both members\r
31         explicit vector2d(T n) : X(n), Y(n) {}\r
32 \r
33         vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}\r
34 \r
35         // operators\r
36 \r
37         vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }\r
38 \r
39         vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }\r
40 \r
41         vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }\r
42         vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }\r
43         vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }\r
44         vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }\r
45         vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }\r
46         vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this;  }\r
47 \r
48         vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }\r
49         vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }\r
50         vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }\r
51         vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }\r
52         vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }\r
53         vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this;  }\r
54 \r
55         vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }\r
56         vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }\r
57         vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }\r
58         vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }\r
59 \r
60         vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }\r
61         vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }\r
62         vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }\r
63         vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }\r
64 \r
65         T& operator [](u32 index)\r
66         {\r
67                 _IRR_DEBUG_BREAK_IF(index>1) // access violation\r
68 \r
69                 return *(&X+index);\r
70         }\r
71 \r
72         const T& operator [](u32 index) const\r
73         {\r
74                 _IRR_DEBUG_BREAK_IF(index>1) // access violation\r
75 \r
76                 return *(&X+index);\r
77         }\r
78 \r
79         //! sort in order X, Y. Equality with rounding tolerance.\r
80         bool operator<=(const vector2d<T>&other) const\r
81         {\r
82                 return (X<other.X || core::equals(X, other.X)) ||\r
83                                 (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));\r
84         }\r
85 \r
86         //! sort in order X, Y. Equality with rounding tolerance.\r
87         bool operator>=(const vector2d<T>&other) const\r
88         {\r
89                 return (X>other.X || core::equals(X, other.X)) ||\r
90                                 (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));\r
91         }\r
92 \r
93         //! sort in order X, Y. Difference must be above rounding tolerance.\r
94         bool operator<(const vector2d<T>&other) const\r
95         {\r
96                 return (X<other.X && !core::equals(X, other.X)) ||\r
97                                 (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));\r
98         }\r
99 \r
100         //! sort in order X, Y. Difference must be above rounding tolerance.\r
101         bool operator>(const vector2d<T>&other) const\r
102         {\r
103                 return (X>other.X && !core::equals(X, other.X)) ||\r
104                                 (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));\r
105         }\r
106 \r
107         bool operator==(const vector2d<T>& other) const { return equals(other); }\r
108         bool operator!=(const vector2d<T>& other) const { return !equals(other); }\r
109 \r
110         // functions\r
111 \r
112         //! Checks if this vector equals the other one.\r
113         /** Takes floating point rounding errors into account.\r
114         \param other Vector to compare with.\r
115         \param tolerance Epsilon value for both - comparing X and Y.\r
116         \return True if the two vector are (almost) equal, else false. */\r
117         bool equals(const vector2d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const\r
118         {\r
119                 return core::equals(X, other.X, tolerance) && core::equals(Y, other.Y, tolerance);\r
120         }\r
121 \r
122         vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }\r
123         vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }\r
124 \r
125         //! Gets the length of the vector.\r
126         /** \return The length of the vector. */\r
127         T getLength() const { return core::squareroot( X*X + Y*Y ); }\r
128 \r
129         //! Get the squared length of this vector\r
130         /** This is useful because it is much faster than getLength().\r
131         \return The squared length of the vector. */\r
132         T getLengthSQ() const { return X*X + Y*Y; }\r
133 \r
134         //! Get the dot product of this vector with another.\r
135         /** \param other Other vector to take dot product with.\r
136         \return The dot product of the two vectors. */\r
137         T dotProduct(const vector2d<T>& other) const\r
138         {\r
139                 return X*other.X + Y*other.Y;\r
140         }\r
141 \r
142         //! check if this vector is parallel to another vector\r
143         bool nearlyParallel( const vector2d<T> & other, const T factor = relativeErrorFactor<T>()) const\r
144         {\r
145                 // https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/\r
146                 // if a || b then  a.x/a.y = b.x/b.y (similiar triangles)\r
147                 // if a || b then either both x are 0 or both y are 0.\r
148 \r
149                 return  equalsRelative( X*other.Y, other.X* Y, factor)\r
150                 && // a bit counterintuitive, but makes sure  that\r
151                    // only y or only x are 0, and at same time deals\r
152                    // with the case where one vector is zero vector.\r
153                         (X*other.X + Y*other.Y) != 0;\r
154         }\r
155 \r
156         //! Gets distance from another point.\r
157         /** Here, the vector is interpreted as a point in 2-dimensional space.\r
158         \param other Other vector to measure from.\r
159         \return Distance from other point. */\r
160         T getDistanceFrom(const vector2d<T>& other) const\r
161         {\r
162                 return vector2d<T>(X - other.X, Y - other.Y).getLength();\r
163         }\r
164 \r
165         //! Returns squared distance from another point.\r
166         /** Here, the vector is interpreted as a point in 2-dimensional space.\r
167         \param other Other vector to measure from.\r
168         \return Squared distance from other point. */\r
169         T getDistanceFromSQ(const vector2d<T>& other) const\r
170         {\r
171                 return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();\r
172         }\r
173 \r
174         //! rotates the point anticlockwise around a center by an amount of degrees.\r
175         /** \param degrees Amount of degrees to rotate by, anticlockwise.\r
176         \param center Rotation center.\r
177         \return This vector after transformation. */\r
178         vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())\r
179         {\r
180                 degrees *= DEGTORAD64;\r
181                 const f64 cs = cos(degrees);\r
182                 const f64 sn = sin(degrees);\r
183 \r
184                 X -= center.X;\r
185                 Y -= center.Y;\r
186 \r
187                 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));\r
188 \r
189                 X += center.X;\r
190                 Y += center.Y;\r
191                 return *this;\r
192         }\r
193 \r
194         //! Normalize the vector.\r
195         /** The null vector is left untouched.\r
196         \return Reference to this vector, after normalization. */\r
197         vector2d<T>& normalize()\r
198         {\r
199                 f32 length = (f32)(X*X + Y*Y);\r
200                 if ( length == 0 )\r
201                         return *this;\r
202                 length = core::reciprocal_squareroot ( length );\r
203                 X = (T)(X * length);\r
204                 Y = (T)(Y * length);\r
205                 return *this;\r
206         }\r
207 \r
208         //! Calculates the angle of this vector in degrees in the trigonometric sense.\r
209         /** 0 is to the right (3 o'clock), values increase counter-clockwise.\r
210         This method has been suggested by Pr3t3nd3r.\r
211         \return Returns a value between 0 and 360. */\r
212         f64 getAngleTrig() const\r
213         {\r
214                 if (Y == 0)\r
215                         return X < 0 ? 180 : 0;\r
216                 else\r
217                 if (X == 0)\r
218                         return Y < 0 ? 270 : 90;\r
219 \r
220                 if ( Y > 0)\r
221                         if (X > 0)\r
222                                 return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;\r
223                         else\r
224                                 return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;\r
225                 else\r
226                         if (X > 0)\r
227                                 return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;\r
228                         else\r
229                                 return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;\r
230         }\r
231 \r
232         //! Calculates the angle of this vector in degrees in the counter trigonometric sense.\r
233         /** 0 is to the right (3 o'clock), values increase clockwise.\r
234         \return Returns a value between 0 and 360. */\r
235         inline f64 getAngle() const\r
236         {\r
237                 if (Y == 0) // corrected thanks to a suggestion by Jox\r
238                         return X < 0 ? 180 : 0;\r
239                 else if (X == 0)\r
240                         return Y < 0 ? 90 : 270;\r
241 \r
242                 // don't use getLength here to avoid precision loss with s32 vectors\r
243                 // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp\r
244                 const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);\r
245                 const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;\r
246 \r
247                 if (X>0 && Y>0)\r
248                         return angle + 270;\r
249                 else\r
250                 if (X>0 && Y<0)\r
251                         return angle + 90;\r
252                 else\r
253                 if (X<0 && Y<0)\r
254                         return 90 - angle;\r
255                 else\r
256                 if (X<0 && Y>0)\r
257                         return 270 - angle;\r
258 \r
259                 return angle;\r
260         }\r
261 \r
262         //! Calculates the angle between this vector and another one in degree.\r
263         /** \param b Other vector to test with.\r
264         \return Returns a value between 0 and 90. */\r
265         inline f64 getAngleWith(const vector2d<T>& b) const\r
266         {\r
267                 f64 tmp = (f64)(X*b.X + Y*b.Y);\r
268 \r
269                 if (tmp == 0.0)\r
270                         return 90.0;\r
271 \r
272                 tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));\r
273                 if (tmp < 0.0)\r
274                         tmp = -tmp;\r
275                 if ( tmp > 1.0 ) //   avoid floating-point trouble\r
276                         tmp = 1.0;\r
277 \r
278                 return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;\r
279         }\r
280 \r
281         //! Returns if this vector interpreted as a point is on a line between two other points.\r
282         /** It is assumed that the point is on the line.\r
283         \param begin Beginning vector to compare between.\r
284         \param end Ending vector to compare between.\r
285         \return True if this vector is between begin and end, false if not. */\r
286         bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const\r
287         {\r
288                 //             .  end\r
289                 //            /\r
290                 //           /\r
291                 //          /\r
292                 //         . begin\r
293                 //        -\r
294                 //       -\r
295                 //      . this point (am I inside or outside)?\r
296                 //\r
297                 if (begin.X != end.X)\r
298                 {\r
299                         return ((begin.X <= X && X <= end.X) ||\r
300                                         (begin.X >= X && X >= end.X));\r
301                 }\r
302                 else\r
303                 {\r
304                         return ((begin.Y <= Y && Y <= end.Y) ||\r
305                                         (begin.Y >= Y && Y >= end.Y));\r
306                 }\r
307         }\r
308 \r
309         //! Creates an interpolated vector between this vector and another vector.\r
310         /** \param other The other vector to interpolate with.\r
311         \param d Interpolation value between 0.0f (all the other vector) and 1.0f (all this vector).\r
312         Note that this is the opposite direction of interpolation to getInterpolated_quadratic()\r
313         \return An interpolated vector.  This vector is not modified. */\r
314         vector2d<T> getInterpolated(const vector2d<T>& other, f64 d) const\r
315         {\r
316                 const f64 inv = 1.0f - d;\r
317                 return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));\r
318         }\r
319 \r
320         //! Creates a quadratically interpolated vector between this and two other vectors.\r
321         /** \param v2 Second vector to interpolate with.\r
322         \param v3 Third vector to interpolate with (maximum at 1.0f)\r
323         \param d Interpolation value between 0.0f (all this vector) and 1.0f (all the 3rd vector).\r
324         Note that this is the opposite direction of interpolation to getInterpolated() and interpolate()\r
325         \return An interpolated vector. This vector is not modified. */\r
326         vector2d<T> getInterpolated_quadratic(const vector2d<T>& v2, const vector2d<T>& v3, f64 d) const\r
327         {\r
328                 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;\r
329                 const f64 inv = 1.0f - d;\r
330                 const f64 mul0 = inv * inv;\r
331                 const f64 mul1 = 2.0f * d * inv;\r
332                 const f64 mul2 = d * d;\r
333 \r
334                 return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),\r
335                                         (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));\r
336         }\r
337 \r
338         /*! Test if this point and another 2 points taken as triplet\r
339                 are colinear, clockwise, anticlockwise. This can be used also\r
340                 to check winding order in triangles for 2D meshes.\r
341                 \return 0 if points are colinear, 1 if clockwise, 2 if anticlockwise\r
342         */\r
343         s32 checkOrientation( const vector2d<T> & b, const vector2d<T> & c) const\r
344         {\r
345                 // Example of clockwise points\r
346                 //\r
347                 //   ^ Y\r
348                 //   |       A\r
349                 //   |      . .\r
350                 //   |     .   .\r
351                 //   |    C.....B\r
352                 //   +---------------> X\r
353 \r
354                 T val = (b.Y - Y) * (c.X - b.X) -\r
355                         (b.X - X) * (c.Y - b.Y);\r
356 \r
357                 if (val == 0) return 0;  // colinear\r
358 \r
359                 return (val > 0) ? 1 : 2; // clock or counterclock wise\r
360         }\r
361 \r
362         /*! Returns true if points (a,b,c) are clockwise on the X,Y plane*/\r
363         inline bool areClockwise( const vector2d<T> & b, const vector2d<T> & c) const\r
364         {\r
365                 T val = (b.Y - Y) * (c.X - b.X) -\r
366                         (b.X - X) * (c.Y - b.Y);\r
367 \r
368                 return val > 0;\r
369         }\r
370 \r
371         /*! Returns true if points (a,b,c) are counterclockwise on the X,Y plane*/\r
372         inline bool areCounterClockwise( const vector2d<T> & b, const vector2d<T> & c) const\r
373         {\r
374                 T val = (b.Y - Y) * (c.X - b.X) -\r
375                         (b.X - X) * (c.Y - b.Y);\r
376 \r
377                 return val < 0;\r
378         }\r
379 \r
380         //! Sets this vector to the linearly interpolated vector between a and b.\r
381         /** \param a first vector to interpolate with, maximum at 1.0f\r
382         \param b second vector to interpolate with, maximum at 0.0f\r
383         \param d Interpolation value between 0.0f (all vector b) and 1.0f (all vector a)\r
384         Note that this is the opposite direction of interpolation to getInterpolated_quadratic()\r
385         */\r
386         vector2d<T>& interpolate( const vector2d<T>& a, const vector2d<T>& b, f64 d)\r
387         {\r
388                 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));\r
389                 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));\r
390                 return *this;\r
391         }\r
392 \r
393         //! X coordinate of vector.\r
394         T X;\r
395 \r
396         //! Y coordinate of vector.\r
397         T Y;\r
398 };\r
399 \r
400         //! Typedef for f32 2d vector.\r
401         typedef vector2d<f32> vector2df;\r
402 \r
403         //! Typedef for integer 2d vector.\r
404         typedef vector2d<s32> vector2di;\r
405 \r
406         template<class S, class T>\r
407         vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }\r
408 \r
409         // These methods are declared in dimension2d, but need definitions of vector2d\r
410         template<class T>\r
411         dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }\r
412 \r
413         template<class T>\r
414         bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }\r
415 \r
416 } // end namespace core\r
417 } // end namespace irr\r
418 \r
419 namespace std\r
420 {\r
421 \r
422 template<class T>\r
423 struct hash<irr::core::vector2d<T> >\r
424 {\r
425         size_t operator()(const irr::core::vector2d<T>& vec) const\r
426         {\r
427                 size_t h1 = hash<T>()(vec.X);\r
428                 size_t h2 = hash<T>()(vec.Y);\r
429                 return (h1 << (4 * sizeof(h1)) | h1 >> (4 * sizeof(h1))) ^ h2;\r
430         }\r
431 };\r
432 \r
433 }\r
434 \r
435 #endif\r
436 \r