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.

658 lines
14KB

  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. Point<T> operator+(const Point<T>& pos) noexcept;
  77. Point<T> operator-(const Point<T>& pos) noexcept;
  78. Point<T>& operator=(const Point<T>& pos) noexcept;
  79. Point<T>& operator+=(const Point<T>& pos) noexcept;
  80. Point<T>& operator-=(const Point<T>& pos) noexcept;
  81. bool operator==(const Point<T>& pos) const noexcept;
  82. bool operator!=(const Point<T>& pos) const noexcept;
  83. private:
  84. T fX, fY;
  85. template<typename> friend class Line;
  86. template<typename> friend class Circle;
  87. template<typename> friend class Triangle;
  88. template<typename> friend class Rectangle;
  89. };
  90. // -----------------------------------------------------------------------
  91. // Size
  92. template<typename T>
  93. class Size
  94. {
  95. public:
  96. /**
  97. Constructor for null size (0x0).
  98. */
  99. Size() noexcept;
  100. /**
  101. Constructor using custom width and height values.
  102. */
  103. Size(const T& width, const T& height) noexcept;
  104. /**
  105. Constructor using another Size class values.
  106. */
  107. Size(const Size<T>& size) noexcept;
  108. /**
  109. Get width.
  110. */
  111. const T& getWidth() const noexcept;
  112. /**
  113. Get height.
  114. */
  115. const T& getHeight() const noexcept;
  116. /**
  117. Set width.
  118. */
  119. void setWidth(const T& width) noexcept;
  120. /**
  121. Set height.
  122. */
  123. void setHeight(const T& height) noexcept;
  124. /**
  125. Set size using @a width and @a height.
  126. */
  127. void setSize(const T& width, const T& height) noexcept;
  128. /**
  129. Set size.
  130. */
  131. void setSize(const Size<T>& size) noexcept;
  132. /**
  133. Grow size by @a multiplier.
  134. */
  135. void growBy(const T& multiplier) noexcept;
  136. /**
  137. Shrink size by @a divider.
  138. */
  139. void shrinkBy(const T& divider) noexcept;
  140. Size<T> operator+(const Size<T>& size) noexcept;
  141. Size<T> operator-(const Size<T>& size) noexcept;
  142. Size<T>& operator=(const Size<T>& size) noexcept;
  143. Size<T>& operator+=(const Size<T>& size) noexcept;
  144. Size<T>& operator-=(const Size<T>& size) noexcept;
  145. Size<T>& operator*=(const T& m) noexcept;
  146. Size<T>& operator/=(const T& d) noexcept;
  147. bool operator==(const Size<T>& size) const noexcept;
  148. bool operator!=(const Size<T>& size) const noexcept;
  149. private:
  150. T fWidth, fHeight;
  151. template<typename> friend class Rectangle;
  152. };
  153. // -----------------------------------------------------------------------
  154. // Line
  155. template<typename T>
  156. class Line
  157. {
  158. public:
  159. /**
  160. Constructor for a null line ([0, 0] to [0, 0]).
  161. */
  162. Line() noexcept;
  163. /**
  164. Constructor using custom start X, start Y, end X and end Y values.
  165. */
  166. Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept;
  167. /**
  168. Constructor using custom start X, start Y, end pos values.
  169. */
  170. Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept;
  171. /**
  172. Constructor using custom start pos, end X and end Y values.
  173. */
  174. Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept;
  175. /**
  176. Constructor using custom start and end pos values.
  177. */
  178. Line(const Point<T>& startPos, const Point<T>& endPos) noexcept;
  179. /**
  180. Constructor using another Line class values.
  181. */
  182. Line(const Line<T>& line) noexcept;
  183. /**
  184. Get start X value.
  185. */
  186. const T& getStartX() const noexcept;
  187. /**
  188. Get start Y value.
  189. */
  190. const T& getStartY() const noexcept;
  191. /**
  192. Get end X value.
  193. */
  194. const T& getEndX() const noexcept;
  195. /**
  196. Get end Y value.
  197. */
  198. const T& getEndY() const noexcept;
  199. /**
  200. Get start position.
  201. */
  202. const Point<T>& getStartPos() const noexcept;
  203. /**
  204. Get end position.
  205. */
  206. const Point<T>& getEndPos() const noexcept;
  207. /**
  208. Set start X value as @a x.
  209. */
  210. void setStartX(const T& x) noexcept;
  211. /**
  212. Set start Y value as @a y.
  213. */
  214. void setStartY(const T& y) noexcept;
  215. /**
  216. Set start X and Y values as @a x and @a y respectively.
  217. */
  218. void setStartPos(const T& x, const T& y) noexcept;
  219. /**
  220. Set start X and Y values according to @a pos.
  221. */
  222. void setStartPos(const Point<T>& pos) noexcept;
  223. /**
  224. Set end X value as @a x.
  225. */
  226. void setEndX(const T& x) noexcept;
  227. /**
  228. Set end Y value as @a y.
  229. */
  230. void setEndY(const T& y) noexcept;
  231. /**
  232. Set end X and Y values as @a x and @a y respectively.
  233. */
  234. void setEndPos(const T& x, const T& y) noexcept;
  235. /**
  236. Set end X and Y values according to @a pos.
  237. */
  238. void setEndPos(const Point<T>& pos) noexcept;
  239. /**
  240. Move this line by @a x and @a y values.
  241. */
  242. void moveBy(const T& x, const T& y) noexcept;
  243. /**
  244. Move this line by @a pos.
  245. */
  246. void moveBy(const Point<T>& pos) noexcept;
  247. /**
  248. Draw this line using the current OpenGL state.
  249. */
  250. void draw();
  251. Line<T>& operator=(const Line<T>& line) noexcept;
  252. bool operator==(const Line<T>& line) const noexcept;
  253. bool operator!=(const Line<T>& line) const noexcept;
  254. private:
  255. Point<T> fPosStart, fPosEnd;
  256. };
  257. // -----------------------------------------------------------------------
  258. // Circle
  259. template<typename T>
  260. class Circle
  261. {
  262. public:
  263. /**
  264. Constructor for a null circle.
  265. */
  266. Circle() noexcept;
  267. /**
  268. Constructor using custom X, Y and size values.
  269. */
  270. Circle(const T& x, const T& y, const float size, const uint numSegments = 300);
  271. /**
  272. Constructor using custom position and size values.
  273. */
  274. Circle(const Point<T>& pos, const float size, const uint numSegments = 300);
  275. /**
  276. Constructor using another Circle class values.
  277. */
  278. Circle(const Circle<T>& cir) noexcept;
  279. /**
  280. Get X value.
  281. */
  282. const T& getX() const noexcept;
  283. /**
  284. Get Y value.
  285. */
  286. const T& getY() const noexcept;
  287. /**
  288. Get position.
  289. */
  290. const Point<T>& getPos() const noexcept;
  291. /**
  292. Set X value as @a x.
  293. */
  294. void setX(const T& x) noexcept;
  295. /**
  296. Set Y value as @a y.
  297. */
  298. void setY(const T& y) noexcept;
  299. /**
  300. Set X and Y values as @a x and @a y respectively.
  301. */
  302. void setPos(const T& x, const T& y) noexcept;
  303. /**
  304. Set X and Y values according to @a pos.
  305. */
  306. void setPos(const Point<T>& pos) noexcept;
  307. /**
  308. Get size.
  309. */
  310. float getSize() const noexcept;
  311. /**
  312. Set size.
  313. @note Must always be > 0
  314. */
  315. void setSize(const float size) noexcept;
  316. /**
  317. Get the current number of line segments that make this circle.
  318. */
  319. uint getNumSegments() const noexcept;
  320. /**
  321. Set the number of line segments that will make this circle.
  322. @note Must always be >= 3
  323. */
  324. void setNumSegments(const uint num);
  325. /**
  326. Draw this circle using the current OpenGL state.
  327. */
  328. void draw();
  329. /**
  330. Draw lines (outline of this circle) using the current OpenGL state.
  331. */
  332. void drawOutline();
  333. Circle<T>& operator=(const Circle<T>& cir) noexcept;
  334. bool operator==(const Circle<T>& cir) const noexcept;
  335. bool operator!=(const Circle<T>& cir) const noexcept;
  336. private:
  337. Point<T> fPos;
  338. float fSize;
  339. uint fNumSegments;
  340. // cached values
  341. float fTheta, fCos, fSin;
  342. void _draw(const bool isOutline);
  343. };
  344. // -----------------------------------------------------------------------
  345. // Triangle
  346. template<typename T>
  347. class Triangle
  348. {
  349. public:
  350. /**
  351. Constructor for a null triangle.
  352. */
  353. Triangle() noexcept;
  354. /**
  355. Constructor using custom X and Y values.
  356. */
  357. Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
  358. /**
  359. Constructor using custom position values.
  360. */
  361. Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
  362. /**
  363. Constructor using another Triangle class values.
  364. */
  365. Triangle(const Triangle<T>& tri) noexcept;
  366. /**
  367. Draw this triangle using the current OpenGL state.
  368. */
  369. void draw();
  370. /**
  371. Draw lines (outline of this triangle) using the current OpenGL state.
  372. */
  373. void drawOutline();
  374. Triangle<T>& operator=(const Triangle<T>& tri) noexcept;
  375. bool operator==(const Triangle<T>& tri) const noexcept;
  376. bool operator!=(const Triangle<T>& tri) const noexcept;
  377. private:
  378. Point<T> fPos1, fPos2, fPos3;
  379. void _draw(const bool isOutline);
  380. };
  381. // -----------------------------------------------------------------------
  382. // Rectangle
  383. template<typename T>
  384. class Rectangle
  385. {
  386. public:
  387. /**
  388. Constructor for a null rectangle.
  389. */
  390. Rectangle() noexcept;
  391. /**
  392. Constructor using custom X, Y, width and height values.
  393. */
  394. Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
  395. /**
  396. Constructor using custom X, Y and size values.
  397. */
  398. Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
  399. /**
  400. Constructor using custom pos, width and height values.
  401. */
  402. Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
  403. /**
  404. Constructor using custom position and size.
  405. */
  406. Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  407. /**
  408. Constructor using another Rectangle class values.
  409. */
  410. Rectangle(const Rectangle<T>& rect) noexcept;
  411. /**
  412. Get X value.
  413. */
  414. const T& getX() const noexcept;
  415. /**
  416. Get Y value.
  417. */
  418. const T& getY() const noexcept;
  419. /**
  420. Get width.
  421. */
  422. const T& getWidth() const noexcept;
  423. /**
  424. Get height.
  425. */
  426. const T& getHeight() const noexcept;
  427. /**
  428. Get position.
  429. */
  430. const Point<T>& getPos() const noexcept;
  431. /**
  432. Get size.
  433. */
  434. const Size<T>& getSize() const noexcept;
  435. /**
  436. Set X value as @a x.
  437. */
  438. void setX(const T& x) noexcept;
  439. /**
  440. Set Y value as @a y.
  441. */
  442. void setY(const T& y) noexcept;
  443. /**
  444. Set X and Y values as @a x and @a y respectively.
  445. */
  446. void setPos(const T& x, const T& y) noexcept;
  447. /**
  448. Set X and Y values according to @a pos.
  449. */
  450. void setPos(const Point<T>& pos) noexcept;
  451. /**
  452. Move this rectangle by @a x and @a y values.
  453. */
  454. void moveBy(const T& x, const T& y) noexcept;
  455. /**
  456. Move this rectangle by @a pos.
  457. */
  458. void moveBy(const Point<T>& pos) noexcept;
  459. /**
  460. Set width.
  461. */
  462. void setWidth(const T& width) noexcept;
  463. /**
  464. Set height.
  465. */
  466. void setHeight(const T& height) noexcept;
  467. /**
  468. Set size using @a width and @a height.
  469. */
  470. void setSize(const T& width, const T& height) noexcept;
  471. /**
  472. Set size.
  473. */
  474. void setSize(const Size<T>& size) noexcept;
  475. /**
  476. Grow size by @a multiplier.
  477. */
  478. void growBy(const T& multiplier) noexcept;
  479. /**
  480. Shrink size by @a divider.
  481. */
  482. void shrinkBy(const T& divider) noexcept;
  483. /**
  484. Set rectangle using @a pos and @a size.
  485. */
  486. void setRectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  487. /**
  488. Set rectangle.
  489. */
  490. void setRectangle(const Rectangle<T>& rect) noexcept;
  491. /**
  492. Check if this rectangle contains the point defined by @a X and @a Y.
  493. */
  494. bool contains(const T& x, const T& y) const noexcept;
  495. /**
  496. Check if this rectangle contains the point @a pos.
  497. */
  498. bool contains(const Point<T>& pos) const noexcept;
  499. /**
  500. Check if this rectangle contains X.
  501. */
  502. bool containsX(const T& x) const noexcept;
  503. /**
  504. Check if this rectangle contains Y.
  505. */
  506. bool containsY(const T& y) const noexcept;
  507. /**
  508. Draw this rectangle using the current OpenGL state.
  509. */
  510. void draw();
  511. /**
  512. Draw lines (outline of this rectangle) using the current OpenGL state.
  513. */
  514. void drawOutline();
  515. Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
  516. Rectangle<T>& operator*=(const T& m) noexcept;
  517. Rectangle<T>& operator/=(const T& d) noexcept;
  518. bool operator==(const Rectangle<T>& size) const noexcept;
  519. bool operator!=(const Rectangle<T>& size) const noexcept;
  520. private:
  521. Point<T> fPos;
  522. Size<T> fSize;
  523. void _draw(const bool isOutline);
  524. };
  525. // -----------------------------------------------------------------------
  526. END_NAMESPACE_DGL
  527. #endif // DGL_GEOMETRY_HPP_INCLUDED