DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

673 lines
15KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef DGL_GEOMETRY_HPP_INCLUDED
  17. #define DGL_GEOMETRY_HPP_INCLUDED
  18. #include "Base.hpp"
  19. START_NAMESPACE_DGL
  20. // -----------------------------------------------------------------------
  21. // Forward class names
  22. template<typename> class Line;
  23. template<typename> class Circle;
  24. template<typename> class Triangle;
  25. template<typename> class Rectangle;
  26. // -----------------------------------------------------------------------
  27. // Point
  28. template<typename T>
  29. class Point
  30. {
  31. public:
  32. /**
  33. Constructor for (0, 0) point.
  34. */
  35. Point() noexcept;
  36. /**
  37. Constructor using custom X and Y values.
  38. */
  39. Point(const T& x, const T& y) noexcept;
  40. /**
  41. Constructor using another Point class values.
  42. */
  43. Point(const Point<T>& pos) noexcept;
  44. /**
  45. Get X value.
  46. */
  47. const T& getX() const noexcept;
  48. /**
  49. Get Y value.
  50. */
  51. const T& getY() const noexcept;
  52. /**
  53. Set X value as @a x.
  54. */
  55. void setX(const T& x) noexcept;
  56. /**
  57. Set Y value as @a y.
  58. */
  59. void setY(const T& y) noexcept;
  60. /**
  61. Set X and Y values as @a x and @a y respectively.
  62. */
  63. void setPos(const T& x, const T& y) noexcept;
  64. /**
  65. Set X and Y values according to @a pos.
  66. */
  67. void setPos(const Point<T>& pos) noexcept;
  68. /**
  69. Move this point by @a x and @a y values.
  70. */
  71. void moveBy(const T& x, const T& y) noexcept;
  72. /**
  73. Move this point by @a pos.
  74. */
  75. void moveBy(const Point<T>& pos) noexcept;
  76. /**
  77. Return true if point is (0, 0).
  78. */
  79. bool isZero() const noexcept;
  80. Point<T> operator+(const Point<T>& pos) noexcept;
  81. Point<T> operator-(const Point<T>& pos) noexcept;
  82. Point<T>& operator=(const Point<T>& pos) noexcept;
  83. Point<T>& operator+=(const Point<T>& pos) noexcept;
  84. Point<T>& operator-=(const Point<T>& pos) noexcept;
  85. bool operator==(const Point<T>& pos) const noexcept;
  86. bool operator!=(const Point<T>& pos) const noexcept;
  87. private:
  88. T fX, fY;
  89. template<typename> friend class Line;
  90. template<typename> friend class Circle;
  91. template<typename> friend class Triangle;
  92. template<typename> friend class Rectangle;
  93. };
  94. // -----------------------------------------------------------------------
  95. // Size
  96. template<typename T>
  97. class Size
  98. {
  99. public:
  100. /**
  101. Constructor for null size (0x0).
  102. */
  103. Size() noexcept;
  104. /**
  105. Constructor using custom width and height values.
  106. */
  107. Size(const T& width, const T& height) noexcept;
  108. /**
  109. Constructor using another Size class values.
  110. */
  111. Size(const Size<T>& size) noexcept;
  112. /**
  113. Get width.
  114. */
  115. const T& getWidth() const noexcept;
  116. /**
  117. Get height.
  118. */
  119. const T& getHeight() const noexcept;
  120. /**
  121. Set width.
  122. */
  123. void setWidth(const T& width) noexcept;
  124. /**
  125. Set height.
  126. */
  127. void setHeight(const T& height) noexcept;
  128. /**
  129. Set size using @a width and @a height.
  130. */
  131. void setSize(const T& width, const T& height) noexcept;
  132. /**
  133. Set size.
  134. */
  135. void setSize(const Size<T>& size) noexcept;
  136. /**
  137. Grow size by @a multiplier.
  138. */
  139. void growBy(const T& multiplier) noexcept;
  140. /**
  141. Shrink size by @a divider.
  142. */
  143. void shrinkBy(const T& divider) noexcept;
  144. /**
  145. Return true if size is null (0x0).
  146. */
  147. bool isNull() const noexcept;
  148. /**
  149. Return true if size is not null (0x0).
  150. */
  151. bool isNotNull() const noexcept;
  152. Size<T> operator+(const Size<T>& size) noexcept;
  153. Size<T> operator-(const Size<T>& size) noexcept;
  154. Size<T>& operator=(const Size<T>& size) noexcept;
  155. Size<T>& operator+=(const Size<T>& size) noexcept;
  156. Size<T>& operator-=(const Size<T>& size) noexcept;
  157. Size<T>& operator*=(const T& m) noexcept;
  158. Size<T>& operator/=(const T& d) noexcept;
  159. bool operator==(const Size<T>& size) const noexcept;
  160. bool operator!=(const Size<T>& size) const noexcept;
  161. private:
  162. T fWidth, fHeight;
  163. template<typename> friend class Rectangle;
  164. };
  165. // -----------------------------------------------------------------------
  166. // Line
  167. template<typename T>
  168. class Line
  169. {
  170. public:
  171. /**
  172. Constructor for a null line ([0, 0] to [0, 0]).
  173. */
  174. Line() noexcept;
  175. /**
  176. Constructor using custom start X, start Y, end X and end Y values.
  177. */
  178. Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept;
  179. /**
  180. Constructor using custom start X, start Y, end pos values.
  181. */
  182. Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept;
  183. /**
  184. Constructor using custom start pos, end X and end Y values.
  185. */
  186. Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept;
  187. /**
  188. Constructor using custom start and end pos values.
  189. */
  190. Line(const Point<T>& startPos, const Point<T>& endPos) noexcept;
  191. /**
  192. Constructor using another Line class values.
  193. */
  194. Line(const Line<T>& line) noexcept;
  195. /**
  196. Get start X value.
  197. */
  198. const T& getStartX() const noexcept;
  199. /**
  200. Get start Y value.
  201. */
  202. const T& getStartY() const noexcept;
  203. /**
  204. Get end X value.
  205. */
  206. const T& getEndX() const noexcept;
  207. /**
  208. Get end Y value.
  209. */
  210. const T& getEndY() const noexcept;
  211. /**
  212. Get start position.
  213. */
  214. const Point<T>& getStartPos() const noexcept;
  215. /**
  216. Get end position.
  217. */
  218. const Point<T>& getEndPos() const noexcept;
  219. /**
  220. Set start X value as @a x.
  221. */
  222. void setStartX(const T& x) noexcept;
  223. /**
  224. Set start Y value as @a y.
  225. */
  226. void setStartY(const T& y) noexcept;
  227. /**
  228. Set start X and Y values as @a x and @a y respectively.
  229. */
  230. void setStartPos(const T& x, const T& y) noexcept;
  231. /**
  232. Set start X and Y values according to @a pos.
  233. */
  234. void setStartPos(const Point<T>& pos) noexcept;
  235. /**
  236. Set end X value as @a x.
  237. */
  238. void setEndX(const T& x) noexcept;
  239. /**
  240. Set end Y value as @a y.
  241. */
  242. void setEndY(const T& y) noexcept;
  243. /**
  244. Set end X and Y values as @a x and @a y respectively.
  245. */
  246. void setEndPos(const T& x, const T& y) noexcept;
  247. /**
  248. Set end X and Y values according to @a pos.
  249. */
  250. void setEndPos(const Point<T>& pos) noexcept;
  251. /**
  252. Move this line by @a x and @a y values.
  253. */
  254. void moveBy(const T& x, const T& y) noexcept;
  255. /**
  256. Move this line by @a pos.
  257. */
  258. void moveBy(const Point<T>& pos) noexcept;
  259. /**
  260. Draw this line using the current OpenGL state.
  261. */
  262. void draw();
  263. Line<T>& operator=(const Line<T>& line) noexcept;
  264. bool operator==(const Line<T>& line) const noexcept;
  265. bool operator!=(const Line<T>& line) const noexcept;
  266. private:
  267. Point<T> fPosStart, fPosEnd;
  268. };
  269. // -----------------------------------------------------------------------
  270. // Circle
  271. template<typename T>
  272. class Circle
  273. {
  274. public:
  275. /**
  276. Constructor for a null circle.
  277. */
  278. Circle() noexcept;
  279. /**
  280. Constructor using custom X, Y and size values.
  281. */
  282. Circle(const T& x, const T& y, const float size, const uint numSegments = 300);
  283. /**
  284. Constructor using custom position and size values.
  285. */
  286. Circle(const Point<T>& pos, const float size, const uint numSegments = 300);
  287. /**
  288. Constructor using another Circle class values.
  289. */
  290. Circle(const Circle<T>& cir) noexcept;
  291. /**
  292. Get X value.
  293. */
  294. const T& getX() const noexcept;
  295. /**
  296. Get Y value.
  297. */
  298. const T& getY() const noexcept;
  299. /**
  300. Get position.
  301. */
  302. const Point<T>& getPos() const noexcept;
  303. /**
  304. Set X value as @a x.
  305. */
  306. void setX(const T& x) noexcept;
  307. /**
  308. Set Y value as @a y.
  309. */
  310. void setY(const T& y) noexcept;
  311. /**
  312. Set X and Y values as @a x and @a y respectively.
  313. */
  314. void setPos(const T& x, const T& y) noexcept;
  315. /**
  316. Set X and Y values according to @a pos.
  317. */
  318. void setPos(const Point<T>& pos) noexcept;
  319. /**
  320. Get size.
  321. */
  322. float getSize() const noexcept;
  323. /**
  324. Set size.
  325. @note Must always be > 0
  326. */
  327. void setSize(const float size) noexcept;
  328. /**
  329. Get the current number of line segments that make this circle.
  330. */
  331. uint getNumSegments() const noexcept;
  332. /**
  333. Set the number of line segments that will make this circle.
  334. @note Must always be >= 3
  335. */
  336. void setNumSegments(const uint num);
  337. /**
  338. Draw this circle using the current OpenGL state.
  339. */
  340. void draw();
  341. /**
  342. Draw lines (outline of this circle) using the current OpenGL state.
  343. */
  344. void drawOutline();
  345. Circle<T>& operator=(const Circle<T>& cir) noexcept;
  346. bool operator==(const Circle<T>& cir) const noexcept;
  347. bool operator!=(const Circle<T>& cir) const noexcept;
  348. private:
  349. Point<T> fPos;
  350. float fSize;
  351. uint fNumSegments;
  352. // cached values
  353. float fTheta, fCos, fSin;
  354. void _draw(const bool isOutline);
  355. };
  356. // -----------------------------------------------------------------------
  357. // Triangle
  358. template<typename T>
  359. class Triangle
  360. {
  361. public:
  362. /**
  363. Constructor for a null triangle.
  364. */
  365. Triangle() noexcept;
  366. /**
  367. Constructor using custom X and Y values.
  368. */
  369. Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
  370. /**
  371. Constructor using custom position values.
  372. */
  373. Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
  374. /**
  375. Constructor using another Triangle class values.
  376. */
  377. Triangle(const Triangle<T>& tri) noexcept;
  378. /**
  379. Draw this triangle using the current OpenGL state.
  380. */
  381. void draw();
  382. /**
  383. Draw lines (outline of this triangle) using the current OpenGL state.
  384. */
  385. void drawOutline();
  386. Triangle<T>& operator=(const Triangle<T>& tri) noexcept;
  387. bool operator==(const Triangle<T>& tri) const noexcept;
  388. bool operator!=(const Triangle<T>& tri) const noexcept;
  389. private:
  390. Point<T> fPos1, fPos2, fPos3;
  391. void _draw(const bool isOutline);
  392. };
  393. // -----------------------------------------------------------------------
  394. // Rectangle
  395. template<typename T>
  396. class Rectangle
  397. {
  398. public:
  399. /**
  400. Constructor for a null rectangle.
  401. */
  402. Rectangle() noexcept;
  403. /**
  404. Constructor using custom X, Y, width and height values.
  405. */
  406. Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
  407. /**
  408. Constructor using custom X, Y and size values.
  409. */
  410. Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
  411. /**
  412. Constructor using custom pos, width and height values.
  413. */
  414. Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
  415. /**
  416. Constructor using custom position and size.
  417. */
  418. Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  419. /**
  420. Constructor using another Rectangle class values.
  421. */
  422. Rectangle(const Rectangle<T>& rect) noexcept;
  423. /**
  424. Get X value.
  425. */
  426. const T& getX() const noexcept;
  427. /**
  428. Get Y value.
  429. */
  430. const T& getY() const noexcept;
  431. /**
  432. Get width.
  433. */
  434. const T& getWidth() const noexcept;
  435. /**
  436. Get height.
  437. */
  438. const T& getHeight() const noexcept;
  439. /**
  440. Get position.
  441. */
  442. const Point<T>& getPos() const noexcept;
  443. /**
  444. Get size.
  445. */
  446. const Size<T>& getSize() const noexcept;
  447. /**
  448. Set X value as @a x.
  449. */
  450. void setX(const T& x) noexcept;
  451. /**
  452. Set Y value as @a y.
  453. */
  454. void setY(const T& y) noexcept;
  455. /**
  456. Set X and Y values as @a x and @a y respectively.
  457. */
  458. void setPos(const T& x, const T& y) noexcept;
  459. /**
  460. Set X and Y values according to @a pos.
  461. */
  462. void setPos(const Point<T>& pos) noexcept;
  463. /**
  464. Move this rectangle by @a x and @a y values.
  465. */
  466. void moveBy(const T& x, const T& y) noexcept;
  467. /**
  468. Move this rectangle by @a pos.
  469. */
  470. void moveBy(const Point<T>& pos) noexcept;
  471. /**
  472. Set width.
  473. */
  474. void setWidth(const T& width) noexcept;
  475. /**
  476. Set height.
  477. */
  478. void setHeight(const T& height) noexcept;
  479. /**
  480. Set size using @a width and @a height.
  481. */
  482. void setSize(const T& width, const T& height) noexcept;
  483. /**
  484. Set size.
  485. */
  486. void setSize(const Size<T>& size) noexcept;
  487. /**
  488. Grow size by @a multiplier.
  489. */
  490. void growBy(const T& multiplier) noexcept;
  491. /**
  492. Shrink size by @a divider.
  493. */
  494. void shrinkBy(const T& divider) noexcept;
  495. /**
  496. Set rectangle using @a pos and @a size.
  497. */
  498. void setRectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  499. /**
  500. Set rectangle.
  501. */
  502. void setRectangle(const Rectangle<T>& rect) noexcept;
  503. /**
  504. Check if this rectangle contains the point defined by @a X and @a Y.
  505. */
  506. bool contains(const T& x, const T& y) const noexcept;
  507. /**
  508. Check if this rectangle contains the point @a pos.
  509. */
  510. bool contains(const Point<T>& pos) const noexcept;
  511. /**
  512. Check if this rectangle contains X.
  513. */
  514. bool containsX(const T& x) const noexcept;
  515. /**
  516. Check if this rectangle contains Y.
  517. */
  518. bool containsY(const T& y) const noexcept;
  519. /**
  520. Draw this rectangle using the current OpenGL state.
  521. */
  522. void draw();
  523. /**
  524. Draw lines (outline of this rectangle) using the current OpenGL state.
  525. */
  526. void drawOutline();
  527. Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
  528. Rectangle<T>& operator*=(const T& m) noexcept;
  529. Rectangle<T>& operator/=(const T& d) noexcept;
  530. bool operator==(const Rectangle<T>& size) const noexcept;
  531. bool operator!=(const Rectangle<T>& size) const noexcept;
  532. private:
  533. Point<T> fPos;
  534. Size<T> fSize;
  535. void _draw(const bool isOutline);
  536. };
  537. // -----------------------------------------------------------------------
  538. END_NAMESPACE_DGL
  539. #endif // DGL_GEOMETRY_HPP_INCLUDED