Audio plugin host https://kx.studio/carla
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.

Geometry.hpp 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2015 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. void _draw(const bool outline);
  392. };
  393. // -----------------------------------------------------------------------
  394. /**
  395. DGL Triangle class.
  396. This class describes a triangle, defined by 3 points.
  397. */
  398. template<typename T>
  399. class Triangle
  400. {
  401. public:
  402. /**
  403. Constructor for a null triangle.
  404. */
  405. Triangle() noexcept;
  406. /**
  407. Constructor using custom X and Y values.
  408. */
  409. Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
  410. /**
  411. Constructor using custom position values.
  412. */
  413. Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
  414. /**
  415. Constructor using another Triangle class values.
  416. */
  417. Triangle(const Triangle<T>& tri) noexcept;
  418. /**
  419. Draw this triangle using the current OpenGL state.
  420. */
  421. void draw();
  422. /**
  423. Draw lines (outline of this triangle) using the current OpenGL state.
  424. */
  425. void drawOutline();
  426. /**
  427. Return true if triangle is null (all its points are equal).
  428. An null triangle is also invalid.
  429. */
  430. bool isNull() const noexcept;
  431. /**
  432. Return true if triangle is not null (one its points is different from the others).
  433. A non-null triangle is still invalid if two of its points are equal.
  434. */
  435. bool isNotNull() const noexcept;
  436. /**
  437. Return true if triangle is valid (all its points are different).
  438. */
  439. bool isValid() const noexcept;
  440. /**
  441. Return true if triangle is invalid (one or two of its points are equal).
  442. An invalid triangle might not be null under some circumstances.
  443. */
  444. bool isInvalid() const noexcept;
  445. Triangle<T>& operator=(const Triangle<T>& tri) noexcept;
  446. bool operator==(const Triangle<T>& tri) const noexcept;
  447. bool operator!=(const Triangle<T>& tri) const noexcept;
  448. private:
  449. Point<T> fPos1, fPos2, fPos3;
  450. void _draw(const bool outline);
  451. };
  452. // -----------------------------------------------------------------------
  453. /**
  454. DGL Rectangle class.
  455. This class describes a rectangle, defined by a starting point and a size.
  456. */
  457. template<typename T>
  458. class Rectangle
  459. {
  460. public:
  461. /**
  462. Constructor for a null rectangle.
  463. */
  464. Rectangle() noexcept;
  465. /**
  466. Constructor using custom X, Y, width and height values.
  467. */
  468. Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
  469. /**
  470. Constructor using custom X, Y and size values.
  471. */
  472. Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
  473. /**
  474. Constructor using custom pos, width and height values.
  475. */
  476. Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
  477. /**
  478. Constructor using custom position and size.
  479. */
  480. Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  481. /**
  482. Constructor using another Rectangle class values.
  483. */
  484. Rectangle(const Rectangle<T>& rect) noexcept;
  485. /**
  486. Get X value.
  487. */
  488. const T& getX() const noexcept;
  489. /**
  490. Get Y value.
  491. */
  492. const T& getY() const noexcept;
  493. /**
  494. Get width.
  495. */
  496. const T& getWidth() const noexcept;
  497. /**
  498. Get height.
  499. */
  500. const T& getHeight() const noexcept;
  501. /**
  502. Get position.
  503. */
  504. const Point<T>& getPos() const noexcept;
  505. /**
  506. Get size.
  507. */
  508. const Size<T>& getSize() const noexcept;
  509. /**
  510. Set X value as @a x.
  511. */
  512. void setX(const T& x) noexcept;
  513. /**
  514. Set Y value as @a y.
  515. */
  516. void setY(const T& y) noexcept;
  517. /**
  518. Set X and Y values as @a x and @a y respectively.
  519. */
  520. void setPos(const T& x, const T& y) noexcept;
  521. /**
  522. Set X and Y values according to @a pos.
  523. */
  524. void setPos(const Point<T>& pos) noexcept;
  525. /**
  526. Move this rectangle by @a x and @a y values.
  527. */
  528. void moveBy(const T& x, const T& y) noexcept;
  529. /**
  530. Move this rectangle by @a pos.
  531. */
  532. void moveBy(const Point<T>& pos) noexcept;
  533. /**
  534. Set width.
  535. */
  536. void setWidth(const T& width) noexcept;
  537. /**
  538. Set height.
  539. */
  540. void setHeight(const T& height) noexcept;
  541. /**
  542. Set size using @a width and @a height.
  543. */
  544. void setSize(const T& width, const T& height) noexcept;
  545. /**
  546. Set size.
  547. */
  548. void setSize(const Size<T>& size) noexcept;
  549. /**
  550. Grow size by @a multiplier.
  551. */
  552. void growBy(double multiplier) noexcept;
  553. /**
  554. Shrink size by @a divider.
  555. */
  556. void shrinkBy(double divider) noexcept;
  557. /**
  558. Set rectangle using @a pos and @a size.
  559. */
  560. void setRectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  561. /**
  562. Set rectangle.
  563. */
  564. void setRectangle(const Rectangle<T>& rect) noexcept;
  565. /**
  566. Check if this rectangle contains the point defined by @a X and @a Y.
  567. */
  568. bool contains(const T& x, const T& y) const noexcept;
  569. /**
  570. Check if this rectangle contains the point @a pos.
  571. */
  572. bool contains(const Point<T>& pos) const noexcept;
  573. /**
  574. Check if this rectangle contains X.
  575. */
  576. bool containsX(const T& x) const noexcept;
  577. /**
  578. Check if this rectangle contains Y.
  579. */
  580. bool containsY(const T& y) const noexcept;
  581. /**
  582. Draw this rectangle using the current OpenGL state.
  583. */
  584. void draw();
  585. /**
  586. Draw lines (outline of this rectangle) using the current OpenGL state.
  587. */
  588. void drawOutline();
  589. Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
  590. Rectangle<T>& operator*=(double m) noexcept;
  591. Rectangle<T>& operator/=(double d) noexcept;
  592. bool operator==(const Rectangle<T>& size) const noexcept;
  593. bool operator!=(const Rectangle<T>& size) const noexcept;
  594. private:
  595. Point<T> fPos;
  596. Size<T> fSize;
  597. void _draw(const bool outline);
  598. };
  599. // -----------------------------------------------------------------------
  600. END_NAMESPACE_DGL
  601. #endif // DGL_GEOMETRY_HPP_INCLUDED