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.

533 lines
12KB

  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 Triangle;
  24. template<typename> class Rectangle;
  25. // -----------------------------------------------------------------------
  26. // Point
  27. template<typename T>
  28. class Point
  29. {
  30. public:
  31. /**
  32. Constructor for (0, 0) point.
  33. */
  34. Point() noexcept;
  35. /**
  36. Constructor using custom x and y values.
  37. */
  38. Point(const T& x, const T& y) noexcept;
  39. /**
  40. Constructor using another Point class values.
  41. */
  42. Point(const Point<T>& pos) noexcept;
  43. /**
  44. Get X value.
  45. */
  46. const T& getX() const noexcept;
  47. /**
  48. Get Y value.
  49. */
  50. const T& getY() const noexcept;
  51. /**
  52. Set X value as @a x.
  53. */
  54. void setX(const T& x) noexcept;
  55. /**
  56. Set Y value as @a y.
  57. */
  58. void setY(const T& y) noexcept;
  59. /**
  60. Set X and Y values as @a x and @a y respectively.
  61. */
  62. void setPos(const T& x, const T& y) noexcept;
  63. /**
  64. Set X and Y values according to @a pos.
  65. */
  66. void setPos(const Point<T>& pos) noexcept;
  67. /**
  68. Move this point by @a x and @a y values.
  69. */
  70. void moveBy(const T& x, const T& y) noexcept;
  71. /**
  72. Move this point by @a pos.
  73. */
  74. void moveBy(const Point<T>& pos) noexcept;
  75. Point<T>& operator=(const Point<T>& pos) noexcept;
  76. Point<T>& operator+=(const Point<T>& pos) noexcept;
  77. Point<T>& operator-=(const Point<T>& pos) noexcept;
  78. bool operator==(const Point<T>& pos) const noexcept;
  79. bool operator!=(const Point<T>& pos) const noexcept;
  80. private:
  81. T fX, fY;
  82. template<typename> friend class Line;
  83. template<typename> friend class Triangle;
  84. template<typename> friend class Rectangle;
  85. };
  86. // -----------------------------------------------------------------------
  87. // Size
  88. template<typename T>
  89. class Size
  90. {
  91. public:
  92. /**
  93. Constructor for null size (0x0).
  94. */
  95. Size() noexcept;
  96. /**
  97. Constructor using custom width and height values.
  98. */
  99. Size(const T& width, const T& height) noexcept;
  100. /**
  101. Constructor using another Size class values.
  102. */
  103. Size(const Size<T>& size) noexcept;
  104. /**
  105. Get width.
  106. */
  107. const T& getWidth() const noexcept;
  108. /**
  109. Get height.
  110. */
  111. const T& getHeight() const noexcept;
  112. /**
  113. Set width.
  114. */
  115. void setWidth(const T& width) noexcept;
  116. /**
  117. Set height.
  118. */
  119. void setHeight(const T& height) noexcept;
  120. /**
  121. Set size using @a width and @a height.
  122. */
  123. void setSize(const T& width, const T& height) noexcept;
  124. /**
  125. Set size.
  126. */
  127. void setSize(const Size<T>& size) noexcept;
  128. /**
  129. Grow size by @a multiplier.
  130. */
  131. void growBy(const T& multiplier) noexcept;
  132. /**
  133. Shrink size by @a divider.
  134. */
  135. void shrinkBy(const T& divider) noexcept;
  136. Size<T>& operator=(const Size<T>& size) noexcept;
  137. Size<T>& operator+=(const Size<T>& size) noexcept;
  138. Size<T>& operator-=(const Size<T>& size) noexcept;
  139. Size<T>& operator*=(const T& m) noexcept;
  140. Size<T>& operator/=(const T& d) noexcept;
  141. bool operator==(const Size<T>& size) const noexcept;
  142. bool operator!=(const Size<T>& size) const noexcept;
  143. private:
  144. T fWidth, fHeight;
  145. template<typename> friend class Rectangle;
  146. };
  147. // -----------------------------------------------------------------------
  148. // Line
  149. template<typename T>
  150. class Line
  151. {
  152. public:
  153. /**
  154. Constructor for null line ([0, 0] to [0, 0]).
  155. */
  156. Line() noexcept;
  157. /**
  158. Constructor using custom start X, start Y, end X and end Y values.
  159. */
  160. Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept;
  161. /**
  162. Constructor using custom start X, start Y, end pos values.
  163. */
  164. Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept;
  165. /**
  166. Constructor using custom start pos, end X and end Y values.
  167. */
  168. Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept;
  169. /**
  170. Constructor using custom start and end pos values.
  171. */
  172. Line(const Point<T>& startPos, const Point<T>& endPos) noexcept;
  173. /**
  174. Constructor using another Line class values.
  175. */
  176. Line(const Line<T>& line) noexcept;
  177. /**
  178. Get start X value.
  179. */
  180. const T& getStartX() const noexcept;
  181. /**
  182. Get start Y value.
  183. */
  184. const T& getStartY() const noexcept;
  185. /**
  186. Get end X value.
  187. */
  188. const T& getEndX() const noexcept;
  189. /**
  190. Get end Y value.
  191. */
  192. const T& getEndY() const noexcept;
  193. /**
  194. Get start position.
  195. */
  196. const Point<T>& getStartPos() const noexcept;
  197. /**
  198. Get end position.
  199. */
  200. const Point<T>& getEndPos() const noexcept;
  201. /**
  202. Set start X value as @a x.
  203. */
  204. void setStartX(const T& x) noexcept;
  205. /**
  206. Set start Y value as @a y.
  207. */
  208. void setStartY(const T& y) noexcept;
  209. /**
  210. Set start X and Y values as @a x and @a y respectively.
  211. */
  212. void setStartPos(const T& x, const T& y) noexcept;
  213. /**
  214. Set start X and Y values according to @a pos.
  215. */
  216. void setStartPos(const Point<T>& pos) noexcept;
  217. /**
  218. Set end X value as @a x.
  219. */
  220. void setEndX(const T& x) noexcept;
  221. /**
  222. Set end Y value as @a y.
  223. */
  224. void setEndY(const T& y) noexcept;
  225. /**
  226. Set end X and Y values as @a x and @a y respectively.
  227. */
  228. void setEndPos(const T& x, const T& y) noexcept;
  229. /**
  230. Set end X and Y values according to @a pos.
  231. */
  232. void setEndPos(const Point<T>& pos) noexcept;
  233. /**
  234. Move this line by @a x and @a y values.
  235. */
  236. void moveBy(const T& x, const T& y) noexcept;
  237. /**
  238. Move this line by @a pos.
  239. */
  240. void moveBy(const Point<T>& pos) noexcept;
  241. /**
  242. Draw this line using the current OpenGL state.
  243. */
  244. void draw();
  245. Line<T>& operator=(const Line<T>& line) noexcept;
  246. bool operator==(const Line<T>& line) const noexcept;
  247. bool operator!=(const Line<T>& line) const noexcept;
  248. private:
  249. Point<T> fPosStart, fPosEnd;
  250. };
  251. // -----------------------------------------------------------------------
  252. // Triangle
  253. template<typename T>
  254. class Triangle
  255. {
  256. public:
  257. /**
  258. Constructor for null triangle.
  259. */
  260. Triangle() noexcept;
  261. /**
  262. Constructor using custom X and Y values.
  263. */
  264. Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept;
  265. /**
  266. Constructor using custom position values.
  267. */
  268. Triangle(const Point<T>& pos1, const Point<T>& pos2, const Point<T>& pos3) noexcept;
  269. /**
  270. Constructor using another Triangle class values.
  271. */
  272. Triangle(const Triangle<T>& tri) noexcept;
  273. /**
  274. Draw this triangle using the current OpenGL state.
  275. */
  276. void draw();
  277. /**
  278. Draw lines (outline of this triangle) using the current OpenGL state.
  279. */
  280. void drawOutline();
  281. Triangle<T>& operator=(const Triangle<T>& tri) noexcept;
  282. bool operator==(const Triangle<T>& tri) const noexcept;
  283. bool operator!=(const Triangle<T>& tri) const noexcept;
  284. private:
  285. Point<T> fPos1, fPos2, fPos3;
  286. void _draw(const bool isOutline);
  287. };
  288. // -----------------------------------------------------------------------
  289. // Rectangle
  290. template<typename T>
  291. class Rectangle
  292. {
  293. public:
  294. /**
  295. Constructor for null rectangle.
  296. */
  297. Rectangle() noexcept;
  298. /**
  299. Constructor using custom X, Y, width and height values.
  300. */
  301. Rectangle(const T& x, const T& y, const T& width, const T& height) noexcept;
  302. /**
  303. Constructor using custom X, Y and size values.
  304. */
  305. Rectangle(const T& x, const T& y, const Size<T>& size) noexcept;
  306. /**
  307. Constructor using custom pos, width and height values.
  308. */
  309. Rectangle(const Point<T>& pos, const T& width, const T& height) noexcept;
  310. /**
  311. Constructor using custom position and size.
  312. */
  313. Rectangle(const Point<T>& pos, const Size<T>& size) noexcept;
  314. /**
  315. Constructor using another Rectangle class values.
  316. */
  317. Rectangle(const Rectangle<T>& rect) noexcept;
  318. /**
  319. Get X value.
  320. */
  321. const T& getX() const noexcept;
  322. /**
  323. Get Y value.
  324. */
  325. const T& getY() const noexcept;
  326. /**
  327. Get width.
  328. */
  329. const T& getWidth() const noexcept;
  330. /**
  331. Get height.
  332. */
  333. const T& getHeight() const noexcept;
  334. /**
  335. Get position.
  336. */
  337. const Point<T>& getPos() const noexcept;
  338. /**
  339. Get size.
  340. */
  341. const Size<T>& getSize() const noexcept;
  342. /**
  343. Set X value as @a x.
  344. */
  345. void setX(const T& x) noexcept;
  346. /**
  347. Set Y value as @a y.
  348. */
  349. void setY(const T& y) noexcept;
  350. /**
  351. Set X and Y values as @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. Move this rectangle by @a x and @a y values.
  360. */
  361. void moveBy(const T& x, const T& y) noexcept;
  362. /**
  363. Move this rectangle by @a pos.
  364. */
  365. void moveBy(const Point<T>& pos) noexcept;
  366. /**
  367. Set width.
  368. */
  369. void setWidth(const T& width) noexcept;
  370. /**
  371. Set height.
  372. */
  373. void setHeight(const T& height) noexcept;
  374. /**
  375. Set size using @a width and @a height.
  376. */
  377. void setSize(const T& width, const T& height) noexcept;
  378. /**
  379. Set size.
  380. */
  381. void setSize(const Size<T>& size) noexcept;
  382. /**
  383. Grow size by @a multiplier.
  384. */
  385. void growBy(const T& multiplier) noexcept;
  386. /**
  387. Shrink size by @a divider.
  388. */
  389. void shrinkBy(const T& divider) noexcept;
  390. /**
  391. Check if this rectangle contains the point defined by @a X and @a Y.
  392. */
  393. bool contains(const T& x, const T& y) const noexcept;
  394. /**
  395. Check if this rectangle contains the point @a pos.
  396. */
  397. bool contains(const Point<T>& pos) const noexcept;
  398. /**
  399. Check if this rectangle contains X.
  400. */
  401. bool containsX(const T& x) const noexcept;
  402. /**
  403. Check if this rectangle contains Y.
  404. */
  405. bool containsY(const T& y) const noexcept;
  406. /**
  407. Draw this rectangle using the current OpenGL state.
  408. */
  409. void draw();
  410. /**
  411. Draw lines (outline of this rectangle) using the current OpenGL state.
  412. */
  413. void drawOutline();
  414. Rectangle<T>& operator=(const Rectangle<T>& rect) noexcept;
  415. Rectangle<T>& operator*=(const T& m) noexcept;
  416. Rectangle<T>& operator/=(const T& d) noexcept;
  417. bool operator==(const Rectangle<T>& size) const noexcept;
  418. bool operator!=(const Rectangle<T>& size) const noexcept;
  419. private:
  420. Point<T> fPos;
  421. Size<T> fSize;
  422. void _draw(const bool isOutline);
  423. };
  424. // -----------------------------------------------------------------------
  425. END_NAMESPACE_DGL
  426. #endif // DGL_GEOMETRY_HPP_INCLUDED