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.

754 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. /**
  284. Draw this line using the current OpenGL state.
  285. */
  286. void draw();
  287. /**
  288. Return true if line is null (start and end pos are equal).
  289. */
  290. bool isNull() const noexcept;
  291. /**
  292. Return true if line is not null (start and end pos are different).
  293. */
  294. bool isNotNull() const noexcept;
  295. Line<T>& operator=(const Line<T>& line) noexcept;
  296. bool operator==(const Line<T>& line) const noexcept;
  297. bool operator!=(const Line<T>& line) const noexcept;
  298. private:
  299. Point<T> fPosStart, fPosEnd;
  300. };
  301. // -----------------------------------------------------------------------
  302. /**
  303. DGL Circle class.
  304. This class describes a circle, defined by position, size and a minimum of 3 segments.
  305. TODO: report if circle starts at top-left, bottom-right or center.
  306. and size grows from which point?
  307. */
  308. template<typename T>
  309. class Circle
  310. {
  311. public:
  312. /**
  313. Constructor for a null circle.
  314. */
  315. Circle() noexcept;
  316. /**
  317. Constructor using custom X, Y and size values.
  318. */
  319. Circle(const T& x, const T& y, const float size, const uint numSegments = 300);
  320. /**
  321. Constructor using custom position and size values.
  322. */
  323. Circle(const Point<T>& pos, const float size, const uint numSegments = 300);
  324. /**
  325. Constructor using another Circle class values.
  326. */
  327. Circle(const Circle<T>& cir) noexcept;
  328. /**
  329. Get X value.
  330. */
  331. const T& getX() const noexcept;
  332. /**
  333. Get Y value.
  334. */
  335. const T& getY() const noexcept;
  336. /**
  337. Get position.
  338. */
  339. const Point<T>& getPos() const noexcept;
  340. /**
  341. Set X value to @a x.
  342. */
  343. void setX(const T& x) noexcept;
  344. /**
  345. Set Y value to @a y.
  346. */
  347. void setY(const T& y) noexcept;
  348. /**
  349. Set X and Y values to @a x and @a y respectively.
  350. */
  351. void setPos(const T& x, const T& y) noexcept;
  352. /**
  353. Set X and Y values according to @a pos.
  354. */
  355. void setPos(const Point<T>& pos) noexcept;
  356. /**
  357. Get size.
  358. */
  359. float getSize() const noexcept;
  360. /**
  361. Set size.
  362. @note Must always be > 0
  363. */
  364. void setSize(const float size) noexcept;
  365. /**
  366. Get the current number of line segments that make this circle.
  367. */
  368. uint getNumSegments() const noexcept;
  369. /**
  370. Set the number of line segments that will make this circle.
  371. @note Must always be >= 3
  372. */
  373. void setNumSegments(const uint num);
  374. /**
  375. Draw this circle using the current OpenGL state.
  376. */
  377. void draw();
  378. /**
  379. Draw lines (outline of this circle) using the current OpenGL state.
  380. */
  381. void drawOutline();
  382. Circle<T>& operator=(const Circle<T>& cir) noexcept;
  383. bool operator==(const Circle<T>& cir) const noexcept;
  384. bool operator!=(const Circle<T>& cir) const noexcept;
  385. private:
  386. Point<T> fPos;
  387. float fSize;
  388. uint fNumSegments;
  389. // cached values
  390. float fTheta, fCos, fSin;
  391. /** @internal */
  392. void _draw(const bool outline);
  393. };
  394. // -----------------------------------------------------------------------
  395. /**
  396. DGL Triangle class.
  397. This class describes a triangle, defined by 3 points.
  398. */
  399. template<typename T>
  400. class Triangle
  401. {
  402. public:
  403. /**
  404. Constructor for a null triangle.
  405. */
  406. Triangle() noexcept;
  407. /**
  408. Constructor using custom X and Y values.
  409. */
  410. Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
  411. /**
  412. Constructor using custom position values.
  413. */
  414. Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
  415. /**
  416. Constructor using another Triangle class values.
  417. */
  418. Triangle(const Triangle<T>& tri) noexcept;
  419. /**
  420. Return true if triangle is null (all its points are equal).
  421. An null triangle is also invalid.
  422. */
  423. bool isNull() const noexcept;
  424. /**
  425. Return true if triangle is not null (one its points is different from the others).
  426. A non-null triangle is still invalid if two of its points are equal.
  427. */
  428. bool isNotNull() const noexcept;
  429. /**
  430. Return true if triangle is valid (all its points are different).
  431. */
  432. bool isValid() const noexcept;
  433. /**
  434. Return true if triangle is invalid (one or two of its points are equal).
  435. An invalid triangle might not be null under some circumstances.
  436. */
  437. bool isInvalid() const noexcept;
  438. /**
  439. Draw this triangle using the current OpenGL state.
  440. */
  441. void draw();
  442. /**
  443. Draw lines (outline of this triangle) using the current OpenGL state.
  444. */
  445. void drawOutline();
  446. Triangle<T>& operator=(const Triangle<T>& tri) noexcept;
  447. bool operator==(const Triangle<T>& tri) const noexcept;
  448. bool operator!=(const Triangle<T>& tri) const noexcept;
  449. private:
  450. Point<T> fPos1, fPos2, fPos3;
  451. /** @internal */
  452. void _draw(const bool outline);
  453. };
  454. // -----------------------------------------------------------------------
  455. /**
  456. DGL Rectangle class.
  457. This class describes a rectangle, defined by a starting point and a size.
  458. */
  459. template<typename T>
  460. class Rectangle
  461. {
  462. public:
  463. /**
  464. Constructor for a null rectangle.
  465. */
  466. Rectangle() noexcept;
  467. /**
  468. Constructor using custom X, Y, width and height values.
  469. */
  470. Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
  471. /**
  472. Constructor using custom X, Y and size values.
  473. */
  474. Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
  475. /**
  476. Constructor using custom pos, width and height values.
  477. */
  478. Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
  479. /**
  480. Constructor using custom position and size.
  481. */
  482. Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  483. /**
  484. Constructor using another Rectangle class values.
  485. */
  486. Rectangle(const Rectangle<T>& rect) noexcept;
  487. /**
  488. Get X value.
  489. */
  490. const T& getX() const noexcept;
  491. /**
  492. Get Y value.
  493. */
  494. const T& getY() const noexcept;
  495. /**
  496. Get width.
  497. */
  498. const T& getWidth() const noexcept;
  499. /**
  500. Get height.
  501. */
  502. const T& getHeight() const noexcept;
  503. /**
  504. Get position.
  505. */
  506. const Point<T>& getPos() const noexcept;
  507. /**
  508. Get size.
  509. */
  510. const Size<T>& getSize() const noexcept;
  511. /**
  512. Set X value as @a x.
  513. */
  514. void setX(const T& x) noexcept;
  515. /**
  516. Set Y value as @a y.
  517. */
  518. void setY(const T& y) noexcept;
  519. /**
  520. Set X and Y values as @a x and @a y respectively.
  521. */
  522. void setPos(const T& x, const T& y) noexcept;
  523. /**
  524. Set X and Y values according to @a pos.
  525. */
  526. void setPos(const Point<T>& pos) noexcept;
  527. /**
  528. Move this rectangle by @a x and @a y values.
  529. */
  530. void moveBy(const T& x, const T& y) noexcept;
  531. /**
  532. Move this rectangle by @a pos.
  533. */
  534. void moveBy(const Point<T>& pos) noexcept;
  535. /**
  536. Set width.
  537. */
  538. void setWidth(const T& width) noexcept;
  539. /**
  540. Set height.
  541. */
  542. void setHeight(const T& height) noexcept;
  543. /**
  544. Set size using @a width and @a height.
  545. */
  546. void setSize(const T& width, const T& height) noexcept;
  547. /**
  548. Set size.
  549. */
  550. void setSize(const Size<T>& size) noexcept;
  551. /**
  552. Grow size by @a multiplier.
  553. */
  554. void growBy(double multiplier) noexcept;
  555. /**
  556. Shrink size by @a divider.
  557. */
  558. void shrinkBy(double divider) noexcept;
  559. /**
  560. Set rectangle using @a pos and @a size.
  561. */
  562. void setRectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  563. /**
  564. Set rectangle.
  565. */
  566. void setRectangle(const Rectangle<T>& rect) noexcept;
  567. /**
  568. Check if this rectangle contains the point defined by @a X and @a Y.
  569. */
  570. bool contains(const T& x, const T& y) const noexcept;
  571. /**
  572. Check if this rectangle contains the point @a pos.
  573. */
  574. bool contains(const Point<T>& pos) const noexcept;
  575. /**
  576. Check if this rectangle contains X.
  577. */
  578. bool containsX(const T& x) const noexcept;
  579. /**
  580. Check if this rectangle contains Y.
  581. */
  582. bool containsY(const T& y) const noexcept;
  583. /**
  584. Draw this rectangle using the current OpenGL state.
  585. */
  586. void draw();
  587. /**
  588. Draw lines (outline of this rectangle) using the current OpenGL state.
  589. */
  590. void drawOutline();
  591. Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
  592. Rectangle<T>& operator*=(double m) noexcept;
  593. Rectangle<T>& operator/=(double d) noexcept;
  594. bool operator==(const Rectangle<T>& size) const noexcept;
  595. bool operator!=(const Rectangle<T>& size) const noexcept;
  596. private:
  597. Point<T> fPos;
  598. Size<T> fSize;
  599. /** @internal */
  600. void _draw(const bool outline);
  601. };
  602. // -----------------------------------------------------------------------
  603. END_NAMESPACE_DGL
  604. #endif // DGL_GEOMETRY_HPP_INCLUDED