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.

477 lines
10KB

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