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.

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