vector2d.h 14 KB

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