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.

765 lines
17KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2016 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. /**
  28. DGL Point class.
  29. This class describes a single point in space, defined by an X and Y value.
  30. */
  31. template<typename T>
  32. class Point
  33. {
  34. public:
  35. /**
  36. Constructor for (0, 0) point.
  37. */
  38. Point() noexcept;
  39. /**
  40. Constructor using custom X and Y values.
  41. */
  42. Point(const T& x, const T& y) noexcept;
  43. /**
  44. Constructor using another Point class values.
  45. */
  46. Point(const Point<T>& pos) noexcept;
  47. /**
  48. Get X value.
  49. */
  50. const T& getX() const noexcept;
  51. /**
  52. Get Y value.
  53. */
  54. const T& getY() const noexcept;
  55. /**
  56. Set X value to @a x.
  57. */
  58. void setX(const T& x) noexcept;
  59. /**
  60. Set Y value to @a y.
  61. */
  62. void setY(const T& y) noexcept;
  63. /**
  64. Set X and Y values to @a x and @a y respectively.
  65. */
  66. void setPos(const T& x, const T& y) noexcept;
  67. /**
  68. Set X and Y values according to @a pos.
  69. */
  70. void setPos(const Point<T>& pos) noexcept;
  71. /**
  72. Move this point by @a x and @a y values.
  73. */
  74. void moveBy(const T& x, const T& y) noexcept;
  75. /**
  76. Move this point by @a pos.
  77. */
  78. void moveBy(const Point<T>& pos) noexcept;
  79. /**
  80. Return true if point is (0, 0).
  81. */
  82. bool isZero() const noexcept;
  83. /**
  84. Return true if point is not (0, 0).
  85. */
  86. bool isNotZero() const noexcept;
  87. Point<T> operator+(const Point<T>& pos) noexcept;
  88. Point<T> operator-(const Point<T>& pos) noexcept;
  89. Point<T>& operator=(const Point<T>& pos) noexcept;
  90. Point<T>& operator+=(const Point<T>& pos) noexcept;
  91. Point<T>& operator-=(const Point<T>& pos) noexcept;
  92. bool operator==(const Point<T>& pos) const noexcept;
  93. bool operator!=(const Point<T>& pos) const noexcept;
  94. private:
  95. T fX, fY;
  96. template<typename> friend class Line;
  97. template<typename> friend class Circle;
  98. template<typename> friend class Triangle;
  99. template<typename> friend class Rectangle;
  100. };
  101. // -----------------------------------------------------------------------
  102. /**
  103. DGL Size class.
  104. This class describes a size, defined by a width and height value.
  105. */
  106. template<typename T>
  107. class Size
  108. {
  109. public:
  110. /**
  111. Constructor for null size (0x0).
  112. */
  113. Size() noexcept;
  114. /**
  115. Constructor using custom width and height values.
  116. */
  117. Size(const T& width, const T& height) noexcept;
  118. /**
  119. Constructor using another Size class values.
  120. */
  121. Size(const Size<T>& size) noexcept;
  122. /**
  123. Get width.
  124. */
  125. const T& getWidth() const noexcept;
  126. /**
  127. Get height.
  128. */
  129. const T& getHeight() const noexcept;
  130. /**
  131. Set width.
  132. */
  133. void setWidth(const T& width) noexcept;
  134. /**
  135. Set height.
  136. */
  137. void setHeight(const T& height) noexcept;
  138. /**
  139. Set size to @a width and @a height.
  140. */
  141. void setSize(const T& width, const T& height) noexcept;
  142. /**
  143. Set size.
  144. */
  145. void setSize(const Size<T>& size) noexcept;
  146. /**
  147. Grow size by @a multiplier.
  148. */
  149. void growBy(double multiplier) noexcept;
  150. /**
  151. Shrink size by @a divider.
  152. */
  153. void shrinkBy(double divider) noexcept;
  154. /**
  155. Return true if size is null (0x0).
  156. An null size is also invalid.
  157. */
  158. bool isNull() const noexcept;
  159. /**
  160. Return true if size is not null (0x0).
  161. A non-null size is still invalid if its width or height is negative.
  162. */
  163. bool isNotNull() const noexcept;
  164. /**
  165. Return true if size is valid (width and height are higher than zero).
  166. */
  167. bool isValid() const noexcept;
  168. /**
  169. Return true if size is invalid (width or height are lower or equal to zero).
  170. An invalid size might not be null under some circumstances.
  171. */
  172. bool isInvalid() const noexcept;
  173. Size<T> operator+(const Size<T>& size) noexcept;
  174. Size<T> operator-(const Size<T>& size) noexcept;
  175. Size<T>& operator=(const Size<T>& size) noexcept;
  176. Size<T>& operator+=(const Size<T>& size) noexcept;
  177. Size<T>& operator-=(const Size<T>& size) noexcept;
  178. Size<T>& operator*=(double m) noexcept;
  179. Size<T>& operator/=(double d) noexcept;
  180. bool operator==(const Size<T>& size) const noexcept;
  181. bool operator!=(const Size<T>& size) const noexcept;
  182. private:
  183. T fWidth, fHeight;
  184. template<typename> friend class Rectangle;
  185. };
  186. // -----------------------------------------------------------------------
  187. /**
  188. DGL Line class.
  189. This class describes a line, defined by two points.
  190. */
  191. template<typename T>
  192. class Line
  193. {
  194. public:
  195. /**
  196. Constructor for a null line ([0,0] to [0,0]).
  197. */
  198. Line() noexcept;
  199. /**
  200. Constructor using custom start X, start Y, end X and end Y values.
  201. */
  202. Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept;
  203. /**
  204. Constructor using custom start X, start Y and end pos values.
  205. */
  206. Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept;
  207. /**
  208. Constructor using custom start pos, end X and end Y values.
  209. */
  210. Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept;
  211. /**
  212. Constructor using custom start and end pos values.
  213. */
  214. Line(const Point<T>& startPos, const Point<T>& endPos) noexcept;
  215. /**
  216. Constructor using another Line class values.
  217. */
  218. Line(const Line<T>& line) noexcept;
  219. /**
  220. Get start X value.
  221. */
  222. const T& getStartX() const noexcept;
  223. /**
  224. Get start Y value.
  225. */
  226. const T& getStartY() const noexcept;
  227. /**
  228. Get end X value.
  229. */
  230. const T& getEndX() const noexcept;
  231. /**
  232. Get end Y value.
  233. */
  234. const T& getEndY() const noexcept;
  235. /**
  236. Get start position.
  237. */
  238. const Point<T>& getStartPos() const noexcept;
  239. /**
  240. Get end position.
  241. */
  242. const Point<T>& getEndPos() const noexcept;
  243. /**
  244. Set start X value to @a x.
  245. */
  246. void setStartX(const T& x) noexcept;
  247. /**
  248. Set start Y value to @a y.
  249. */
  250. void setStartY(const T& y) noexcept;
  251. /**
  252. Set start X and Y values to @a x and @a y respectively.
  253. */
  254. void setStartPos(const T& x, const T& y) noexcept;
  255. /**
  256. Set start X and Y values according to @a pos.
  257. */
  258. void setStartPos(const Point<T>& pos) noexcept;
  259. /**
  260. Set end X value to @a x.
  261. */
  262. void setEndX(const T& x) noexcept;
  263. /**
  264. Set end Y value to @a y.
  265. */
  266. void setEndY(const T& y) noexcept;
  267. /**
  268. Set end X and Y values to @a x and @a y respectively.
  269. */
  270. void setEndPos(const T& x, const T& y) noexcept;
  271. /**
  272. Set end X and Y values according to @a pos.
  273. */
  274. void setEndPos(const Point<T>& pos) noexcept;
  275. /**
  276. Move this line by @a x and @a y values.
  277. */
  278. void moveBy(const T& x, const T& y) noexcept;
  279. /**
  280. Move this line by @a pos.
  281. */
  282. void moveBy(const Point<T>& pos) noexcept;
  283. #if defined(HAVE_DGL)
  284. /**
  285. Draw this line using the current OpenGL state.
  286. */
  287. void draw();
  288. #endif
  289. /**
  290. Return true if line is null (start and end pos are equal).
  291. */
  292. bool isNull() const noexcept;
  293. /**
  294. Return true if line is not null (start and end pos are different).
  295. */
  296. bool isNotNull() const noexcept;
  297. Line<T>& operator=(const Line<T>& line) noexcept;
  298. bool operator==(const Line<T>& line) const noexcept;
  299. bool operator!=(const Line<T>& line) const noexcept;
  300. private:
  301. Point<T> fPosStart, fPosEnd;
  302. };
  303. // -----------------------------------------------------------------------
  304. /**
  305. DGL Circle class.
  306. This class describes a circle, defined by position, size and a minimum of 3 segments.
  307. TODO: report if circle starts at top-left, bottom-right or center.
  308. and size grows from which point?
  309. */
  310. template<typename T>
  311. class Circle
  312. {
  313. public:
  314. /**
  315. Constructor for a null circle.
  316. */
  317. Circle() noexcept;
  318. /**
  319. Constructor using custom X, Y and size values.
  320. */
  321. Circle(const T& x, const T& y, const float size, const uint numSegments = 300);
  322. /**
  323. Constructor using custom position and size values.
  324. */
  325. Circle(const Point<T>& pos, const float size, const uint numSegments = 300);
  326. /**
  327. Constructor using another Circle class values.
  328. */
  329. Circle(const Circle<T>& cir) noexcept;
  330. /**
  331. Get X value.
  332. */
  333. const T& getX() const noexcept;
  334. /**
  335. Get Y value.
  336. */
  337. const T& getY() const noexcept;
  338. /**
  339. Get position.
  340. */
  341. const Point<T>& getPos() const noexcept;
  342. /**
  343. Set X value to @a x.
  344. */
  345. void setX(const T& x) noexcept;
  346. /**
  347. Set Y value to @a y.
  348. */
  349. void setY(const T& y) noexcept;
  350. /**
  351. Set X and Y values to @a x and @a y respectively.
  352. */
  353. void setPos(const T& x, const T& y) noexcept;
  354. /**
  355. Set X and Y values according to @a pos.
  356. */
  357. void setPos(const Point<T>& pos) noexcept;
  358. /**
  359. Get size.
  360. */
  361. float getSize() const noexcept;
  362. /**
  363. Set size.
  364. @note Must always be > 0
  365. */
  366. void setSize(const float size) noexcept;
  367. /**
  368. Get the current number of line segments that make this circle.
  369. */
  370. uint getNumSegments() const noexcept;
  371. /**
  372. Set the number of line segments that will make this circle.
  373. @note Must always be >= 3
  374. */
  375. void setNumSegments(const uint num);
  376. #if defined(HAVE_DGL)
  377. /**
  378. Draw this circle using the current OpenGL state.
  379. */
  380. void draw();
  381. /**
  382. Draw lines (outline of this circle) using the current OpenGL state.
  383. */
  384. void drawOutline();
  385. #endif
  386. Circle<T>& operator=(const Circle<T>& cir) noexcept;
  387. bool operator==(const Circle<T>& cir) const noexcept;
  388. bool operator!=(const Circle<T>& cir) const noexcept;
  389. private:
  390. Point<T> fPos;
  391. float fSize;
  392. uint fNumSegments;
  393. // cached values
  394. float fTheta, fCos, fSin;
  395. #if defined(HAVE_DGL)
  396. void _draw(const bool outline);
  397. #endif
  398. };
  399. // -----------------------------------------------------------------------
  400. /**
  401. DGL Triangle class.
  402. This class describes a triangle, defined by 3 points.
  403. */
  404. template<typename T>
  405. class Triangle
  406. {
  407. public:
  408. /**
  409. Constructor for a null triangle.
  410. */
  411. Triangle() noexcept;
  412. /**
  413. Constructor using custom X and Y values.
  414. */
  415. Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
  416. /**
  417. Constructor using custom position values.
  418. */
  419. Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
  420. /**
  421. Constructor using another Triangle class values.
  422. */
  423. Triangle(const Triangle<T>& tri) noexcept;
  424. #if defined(HAVE_DGL)
  425. /**
  426. Draw this triangle using the current OpenGL state.
  427. */
  428. void draw();
  429. /**
  430. Draw lines (outline of this triangle) using the current OpenGL state.
  431. */
  432. void drawOutline();
  433. #endif
  434. /**
  435. Return true if triangle is null (all its points are equal).
  436. An null triangle is also invalid.
  437. */
  438. bool isNull() const noexcept;
  439. /**
  440. Return true if triangle is not null (one its points is different from the others).
  441. A non-null triangle is still invalid if two of its points are equal.
  442. */
  443. bool isNotNull() const noexcept;
  444. /**
  445. Return true if triangle is valid (all its points are different).
  446. */
  447. bool isValid() const noexcept;
  448. /**
  449. Return true if triangle is invalid (one or two of its points are equal).
  450. An invalid triangle might not be null under some circumstances.
  451. */
  452. bool isInvalid() const noexcept;
  453. Triangle<T>& operator=(const Triangle<T>& tri) noexcept;
  454. bool operator==(const Triangle<T>& tri) const noexcept;
  455. bool operator!=(const Triangle<T>& tri) const noexcept;
  456. private:
  457. Point<T> fPos1, fPos2, fPos3;
  458. #if defined(HAVE_DGL)
  459. void _draw(const bool outline);
  460. #endif
  461. };
  462. // -----------------------------------------------------------------------
  463. /**
  464. DGL Rectangle class.
  465. This class describes a rectangle, defined by a starting point and a size.
  466. */
  467. template<typename T>
  468. class Rectangle
  469. {
  470. public:
  471. /**
  472. Constructor for a null rectangle.
  473. */
  474. Rectangle() noexcept;
  475. /**
  476. Constructor using custom X, Y, width and height values.
  477. */
  478. Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
  479. /**
  480. Constructor using custom X, Y and size values.
  481. */
  482. Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
  483. /**
  484. Constructor using custom pos, width and height values.
  485. */
  486. Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
  487. /**
  488. Constructor using custom position and size.
  489. */
  490. Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  491. /**
  492. Constructor using another Rectangle class values.
  493. */
  494. Rectangle(const Rectangle<T>& rect) noexcept;
  495. /**
  496. Get X value.
  497. */
  498. const T& getX() const noexcept;
  499. /**
  500. Get Y value.
  501. */
  502. const T& getY() const noexcept;
  503. /**
  504. Get width.
  505. */
  506. const T& getWidth() const noexcept;
  507. /**
  508. Get height.
  509. */
  510. const T& getHeight() const noexcept;
  511. /**
  512. Get position.
  513. */
  514. const Point<T>& getPos() const noexcept;
  515. /**
  516. Get size.
  517. */
  518. const Size<T>& getSize() const noexcept;
  519. /**
  520. Set X value as @a x.
  521. */
  522. void setX(const T& x) noexcept;
  523. /**
  524. Set Y value as @a y.
  525. */
  526. void setY(const T& y) noexcept;
  527. /**
  528. Set X and Y values as @a x and @a y respectively.
  529. */
  530. void setPos(const T& x, const T& y) noexcept;
  531. /**
  532. Set X and Y values according to @a pos.
  533. */
  534. void setPos(const Point<T>& pos) noexcept;
  535. /**
  536. Move this rectangle by @a x and @a y values.
  537. */
  538. void moveBy(const T& x, const T& y) noexcept;
  539. /**
  540. Move this rectangle by @a pos.
  541. */
  542. void moveBy(const Point<T>& pos) noexcept;
  543. /**
  544. Set width.
  545. */
  546. void setWidth(const T& width) noexcept;
  547. /**
  548. Set height.
  549. */
  550. void setHeight(const T& height) noexcept;
  551. /**
  552. Set size using @a width and @a height.
  553. */
  554. void setSize(const T& width, const T& height) noexcept;
  555. /**
  556. Set size.
  557. */
  558. void setSize(const Size<T>& size) noexcept;
  559. /**
  560. Grow size by @a multiplier.
  561. */
  562. void growBy(double multiplier) noexcept;
  563. /**
  564. Shrink size by @a divider.
  565. */
  566. void shrinkBy(double divider) noexcept;
  567. /**
  568. Set rectangle using @a pos and @a size.
  569. */
  570. void setRectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  571. /**
  572. Set rectangle.
  573. */
  574. void setRectangle(const Rectangle<T>& rect) noexcept;
  575. /**
  576. Check if this rectangle contains the point defined by @a X and @a Y.
  577. */
  578. bool contains(const T& x, const T& y) const noexcept;
  579. /**
  580. Check if this rectangle contains the point @a pos.
  581. */
  582. bool contains(const Point<T>& pos) const noexcept;
  583. /**
  584. Check if this rectangle contains X.
  585. */
  586. bool containsX(const T& x) const noexcept;
  587. /**
  588. Check if this rectangle contains Y.
  589. */
  590. bool containsY(const T& y) const noexcept;
  591. #if defined(HAVE_DGL)
  592. /**
  593. Draw this rectangle using the current OpenGL state.
  594. */
  595. void draw();
  596. /**
  597. Draw lines (outline of this rectangle) using the current OpenGL state.
  598. */
  599. void drawOutline();
  600. #endif
  601. Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
  602. Rectangle<T>& operator*=(double m) noexcept;
  603. Rectangle<T>& operator/=(double d) noexcept;
  604. bool operator==(const Rectangle<T>& size) const noexcept;
  605. bool operator!=(const Rectangle<T>& size) const noexcept;
  606. private:
  607. Point<T> fPos;
  608. Size<T> fSize;
  609. #if defined(HAVE_DGL)
  610. void _draw(const bool outline);
  611. #endif
  612. };
  613. // -----------------------------------------------------------------------
  614. END_NAMESPACE_DGL
  615. #endif // DGL_GEOMETRY_HPP_INCLUDED