vector2d.h 14 KB

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