2
0

vector2d.h 14 KB

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