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.

1054 lines
22KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2021 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. #ifdef _MSC_VER
  17. # pragma warning(disable:4661) /* instantiated template classes whose methods are defined elsewhere */
  18. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  19. # pragma GCC diagnostic push
  20. # pragma GCC diagnostic ignored "-Wconversion"
  21. #endif
  22. #include "../Geometry.hpp"
  23. #include <cmath>
  24. START_NAMESPACE_DGL
  25. static const float M_2PIf = 3.14159265358979323846f*2.0f;
  26. // -----------------------------------------------------------------------
  27. // Point
  28. template<typename T>
  29. Point<T>::Point() noexcept
  30. : x(0),
  31. y(0) {}
  32. template<typename T>
  33. Point<T>::Point(const T& x2, const T& y2) noexcept
  34. : x(x2),
  35. y(y2) {}
  36. template<typename T>
  37. Point<T>::Point(const Point<T>& pos) noexcept
  38. : x(pos.x),
  39. y(pos.y) {}
  40. template<typename T>
  41. const T& Point<T>::getX() const noexcept
  42. {
  43. return x;
  44. }
  45. template<typename T>
  46. const T& Point<T>::getY() const noexcept
  47. {
  48. return y;
  49. }
  50. template<typename T>
  51. void Point<T>::setX(const T& x2) noexcept
  52. {
  53. x = x2;
  54. }
  55. template<typename T>
  56. void Point<T>::setY(const T& y2) noexcept
  57. {
  58. y = y2;
  59. }
  60. template<typename T>
  61. void Point<T>::setPos(const T& x2, const T& y2) noexcept
  62. {
  63. x = x2;
  64. y = y2;
  65. }
  66. template<typename T>
  67. void Point<T>::setPos(const Point<T>& pos) noexcept
  68. {
  69. x = pos.x;
  70. y = pos.y;
  71. }
  72. template<typename T>
  73. void Point<T>::moveBy(const T& x2, const T& y2) noexcept
  74. {
  75. x = static_cast<T>(x+x2);
  76. y = static_cast<T>(y+y2);
  77. }
  78. template<typename T>
  79. void Point<T>::moveBy(const Point<T>& pos) noexcept
  80. {
  81. x = static_cast<T>(x+pos.x);
  82. y = static_cast<T>(y+pos.y);
  83. }
  84. template<typename T>
  85. bool Point<T>::isZero() const noexcept
  86. {
  87. return x == 0 && y == 0;
  88. }
  89. template<typename T>
  90. bool Point<T>::isNotZero() const noexcept
  91. {
  92. return x != 0 || y != 0;
  93. }
  94. template<typename T>
  95. Point<T> Point<T>::operator+(const Point<T>& pos) noexcept
  96. {
  97. return Point<T>(x+pos.x, y+pos.y);
  98. }
  99. template<typename T>
  100. Point<T> Point<T>::operator-(const Point<T>& pos) noexcept
  101. {
  102. return Point<T>(x-pos.x, y-pos.y);
  103. }
  104. template<typename T>
  105. Point<T>& Point<T>::operator=(const Point<T>& pos) noexcept
  106. {
  107. x = pos.x;
  108. y = pos.y;
  109. return *this;
  110. }
  111. template<typename T>
  112. Point<T>& Point<T>::operator+=(const Point<T>& pos) noexcept
  113. {
  114. x = static_cast<T>(x+pos.x);
  115. y = static_cast<T>(y+pos.y);
  116. return *this;
  117. }
  118. template<typename T>
  119. Point<T>& Point<T>::operator-=(const Point<T>& pos) noexcept
  120. {
  121. x = static_cast<T>(x-pos.x);
  122. y = static_cast<T>(y-pos.y);
  123. return *this;
  124. }
  125. template<typename T>
  126. bool Point<T>::operator==(const Point<T>& pos) const noexcept
  127. {
  128. return (x == pos.x && y == pos.y);
  129. }
  130. template<typename T>
  131. bool Point<T>::operator!=(const Point<T>& pos) const noexcept
  132. {
  133. return (x != pos.x || y != pos.y);
  134. }
  135. // -----------------------------------------------------------------------
  136. // Size
  137. template<typename T>
  138. Size<T>::Size() noexcept
  139. : fWidth(0),
  140. fHeight(0) {}
  141. template<typename T>
  142. Size<T>::Size(const T& width, const T& height) noexcept
  143. : fWidth(width),
  144. fHeight(height) {}
  145. template<typename T>
  146. Size<T>::Size(const Size<T>& size) noexcept
  147. : fWidth(size.fWidth),
  148. fHeight(size.fHeight) {}
  149. template<typename T>
  150. const T& Size<T>::getWidth() const noexcept
  151. {
  152. return fWidth;
  153. }
  154. template<typename T>
  155. const T& Size<T>::getHeight() const noexcept
  156. {
  157. return fHeight;
  158. }
  159. template<typename T>
  160. void Size<T>::setWidth(const T& width) noexcept
  161. {
  162. fWidth = width;
  163. }
  164. template<typename T>
  165. void Size<T>::setHeight(const T& height) noexcept
  166. {
  167. fHeight = height;
  168. }
  169. template<typename T>
  170. void Size<T>::setSize(const T& width, const T& height) noexcept
  171. {
  172. fWidth = width;
  173. fHeight = height;
  174. }
  175. template<typename T>
  176. void Size<T>::setSize(const Size<T>& size) noexcept
  177. {
  178. fWidth = size.fWidth;
  179. fHeight = size.fHeight;
  180. }
  181. template<typename T>
  182. void Size<T>::growBy(double multiplier) noexcept
  183. {
  184. fWidth = static_cast<T>(static_cast<double>(fWidth)*multiplier);
  185. fHeight = static_cast<T>(static_cast<double>(fHeight)*multiplier);
  186. }
  187. template<typename T>
  188. void Size<T>::shrinkBy(double divider) noexcept
  189. {
  190. fWidth = static_cast<T>(static_cast<double>(fWidth)/divider);
  191. fHeight = static_cast<T>(static_cast<double>(fHeight)/divider);
  192. }
  193. template<typename T>
  194. bool Size<T>::isNull() const noexcept
  195. {
  196. return fWidth == 0 && fHeight == 0;
  197. }
  198. template<typename T>
  199. bool Size<T>::isNotNull() const noexcept
  200. {
  201. return fWidth != 0 || fHeight != 0;
  202. }
  203. template<typename T>
  204. bool Size<T>::isValid() const noexcept
  205. {
  206. return fWidth > 0 && fHeight > 0;
  207. }
  208. template<typename T>
  209. bool Size<T>::isInvalid() const noexcept
  210. {
  211. return fWidth <= 0 || fHeight <= 0;
  212. }
  213. template<typename T>
  214. Size<int> Size<T>::toInt() const noexcept
  215. {
  216. return Size<int>(static_cast<int>(fWidth),
  217. static_cast<int>(fHeight));
  218. }
  219. template<>
  220. Size<int> Size<double>::toInt() const noexcept
  221. {
  222. return Size<int>(static_cast<int>(fWidth + 0.5),
  223. static_cast<int>(fHeight + 0.5));
  224. }
  225. template<>
  226. Size<int> Size<float>::toInt() const noexcept
  227. {
  228. return Size<int>(static_cast<int>(fWidth + 0.5f),
  229. static_cast<int>(fHeight + 0.5f));
  230. }
  231. template<typename T>
  232. Size<T> Size<T>::operator+(const Size<T>& size) noexcept
  233. {
  234. return Size<T>(fWidth+size.fWidth, fHeight+size.fHeight);
  235. }
  236. template<typename T>
  237. Size<T> Size<T>::operator-(const Size<T>& size) noexcept
  238. {
  239. return Size<T>(fWidth-size.fWidth, fHeight-size.fHeight);
  240. }
  241. template<typename T>
  242. Size<T>& Size<T>::operator=(const Size<T>& size) noexcept
  243. {
  244. fWidth = size.fWidth;
  245. fHeight = size.fHeight;
  246. return *this;
  247. }
  248. template<typename T>
  249. Size<T>& Size<T>::operator+=(const Size<T>& size) noexcept
  250. {
  251. fWidth = static_cast<T>(fWidth+size.fWidth);
  252. fHeight = static_cast<T>(fHeight+size.fHeight);
  253. return *this;
  254. }
  255. template<typename T>
  256. Size<T>& Size<T>::operator-=(const Size<T>& size) noexcept
  257. {
  258. fWidth = static_cast<T>(fWidth-size.fWidth);
  259. fHeight = static_cast<T>(fHeight-size.fHeight);
  260. return *this;
  261. }
  262. template<typename T>
  263. Size<T>& Size<T>::operator*=(double m) noexcept
  264. {
  265. fWidth = static_cast<T>(static_cast<double>(fWidth)*m);
  266. fHeight = static_cast<T>(static_cast<double>(fHeight)*m);
  267. return *this;
  268. }
  269. template<typename T>
  270. Size<T>& Size<T>::operator/=(double d) noexcept
  271. {
  272. fWidth = static_cast<T>(static_cast<double>(fWidth)/d);
  273. fHeight = static_cast<T>(static_cast<double>(fHeight)/d);
  274. return *this;
  275. }
  276. template<typename T>
  277. Size<T> Size<T>::operator*(const double m) const noexcept
  278. {
  279. Size<T> size(fWidth, fHeight);
  280. size *= m;
  281. return size;
  282. }
  283. template<typename T>
  284. Size<T> Size<T>::operator/(const double m) const noexcept
  285. {
  286. Size<T> size(fWidth, fHeight);
  287. size /= m;
  288. return size;
  289. }
  290. template<typename T>
  291. bool Size<T>::operator==(const Size<T>& size) const noexcept
  292. {
  293. return (fWidth == size.fWidth && fHeight == size.fHeight);
  294. }
  295. template<typename T>
  296. bool Size<T>::operator!=(const Size<T>& size) const noexcept
  297. {
  298. return (fWidth != size.fWidth || fHeight != size.fHeight);
  299. }
  300. // -----------------------------------------------------------------------
  301. // Line
  302. template<typename T>
  303. Line<T>::Line() noexcept
  304. : posStart(0, 0),
  305. posEnd(0, 0) {}
  306. template<typename T>
  307. Line<T>::Line(const T& startX, const T& startY, const T& endX, const T& endY) noexcept
  308. : posStart(startX, startY),
  309. posEnd(endX, endY) {}
  310. template<typename T>
  311. Line<T>::Line(const T& startX, const T& startY, const Point<T>& endPos) noexcept
  312. : posStart(startX, startY),
  313. posEnd(endPos) {}
  314. template<typename T>
  315. Line<T>::Line(const Point<T>& startPos, const T& endX, const T& endY) noexcept
  316. : posStart(startPos),
  317. posEnd(endX, endY) {}
  318. template<typename T>
  319. Line<T>::Line(const Point<T>& startPos, const Point<T>& endPos) noexcept
  320. : posStart(startPos),
  321. posEnd(endPos) {}
  322. template<typename T>
  323. Line<T>::Line(const Line<T>& line) noexcept
  324. : posStart(line.posStart),
  325. posEnd(line.posEnd) {}
  326. template<typename T>
  327. const T& Line<T>::getStartX() const noexcept
  328. {
  329. return posStart.x;
  330. }
  331. template<typename T>
  332. const T& Line<T>::getStartY() const noexcept
  333. {
  334. return posStart.y;
  335. }
  336. template<typename T>
  337. const T& Line<T>::getEndX() const noexcept
  338. {
  339. return posEnd.x;
  340. }
  341. template<typename T>
  342. const T& Line<T>::getEndY() const noexcept
  343. {
  344. return posEnd.y;
  345. }
  346. template<typename T>
  347. const Point<T>& Line<T>::getStartPos() const noexcept
  348. {
  349. return posStart;
  350. }
  351. template<typename T>
  352. const Point<T>& Line<T>::getEndPos() const noexcept
  353. {
  354. return posEnd;
  355. }
  356. template<typename T>
  357. void Line<T>::setStartX(const T& x) noexcept
  358. {
  359. posStart.x = x;
  360. }
  361. template<typename T>
  362. void Line<T>::setStartY(const T& y) noexcept
  363. {
  364. posStart.y = y;
  365. }
  366. template<typename T>
  367. void Line<T>::setStartPos(const T& x, const T& y) noexcept
  368. {
  369. posStart = Point<T>(x, y);
  370. }
  371. template<typename T>
  372. void Line<T>::setStartPos(const Point<T>& pos) noexcept
  373. {
  374. posStart = pos;
  375. }
  376. template<typename T>
  377. void Line<T>::setEndX(const T& x) noexcept
  378. {
  379. posEnd.x = x;
  380. }
  381. template<typename T>
  382. void Line<T>::setEndY(const T& y) noexcept
  383. {
  384. posEnd.y = y;
  385. }
  386. template<typename T>
  387. void Line<T>::setEndPos(const T& x, const T& y) noexcept
  388. {
  389. posEnd = Point<T>(x, y);
  390. }
  391. template<typename T>
  392. void Line<T>::setEndPos(const Point<T>& pos) noexcept
  393. {
  394. posEnd = pos;
  395. }
  396. template<typename T>
  397. void Line<T>::moveBy(const T& x, const T& y) noexcept
  398. {
  399. posStart.moveBy(x, y);
  400. posEnd.moveBy(x, y);
  401. }
  402. template<typename T>
  403. void Line<T>::moveBy(const Point<T>& pos) noexcept
  404. {
  405. posStart.moveBy(pos);
  406. posEnd.moveBy(pos);
  407. }
  408. template<typename T>
  409. bool Line<T>::isNull() const noexcept
  410. {
  411. return posStart == posEnd;
  412. }
  413. template<typename T>
  414. bool Line<T>::isNotNull() const noexcept
  415. {
  416. return posStart != posEnd;
  417. }
  418. template<typename T>
  419. Line<T>& Line<T>::operator=(const Line<T>& line) noexcept
  420. {
  421. posStart = line.posStart;
  422. posEnd = line.posEnd;
  423. return *this;
  424. }
  425. template<typename T>
  426. bool Line<T>::operator==(const Line<T>& line) const noexcept
  427. {
  428. return (posStart == line.posStart && posEnd == line.posEnd);
  429. }
  430. template<typename T>
  431. bool Line<T>::operator!=(const Line<T>& line) const noexcept
  432. {
  433. return (posStart != line.posStart || posEnd != line.posEnd);
  434. }
  435. // -----------------------------------------------------------------------
  436. // Circle
  437. template<typename T>
  438. Circle<T>::Circle() noexcept
  439. : fPos(0, 0),
  440. fSize(0.0f),
  441. fNumSegments(0),
  442. fTheta(0.0f),
  443. fCos(0.0f),
  444. fSin(0.0f) {}
  445. template<typename T>
  446. Circle<T>::Circle(const T& x, const T& y, const float size, const uint numSegments)
  447. : fPos(x, y),
  448. fSize(size),
  449. fNumSegments(numSegments >= 3 ? numSegments : 3),
  450. fTheta(M_2PIf / static_cast<float>(fNumSegments)),
  451. fCos(std::cos(fTheta)),
  452. fSin(std::sin(fTheta))
  453. {
  454. DISTRHO_SAFE_ASSERT(fSize > 0.0f);
  455. }
  456. template<typename T>
  457. Circle<T>::Circle(const Point<T>& pos, const float size, const uint numSegments)
  458. : fPos(pos),
  459. fSize(size),
  460. fNumSegments(numSegments >= 3 ? numSegments : 3),
  461. fTheta(M_2PIf / static_cast<float>(fNumSegments)),
  462. fCos(std::cos(fTheta)),
  463. fSin(std::sin(fTheta))
  464. {
  465. DISTRHO_SAFE_ASSERT(fSize > 0.0f);
  466. }
  467. template<typename T>
  468. Circle<T>::Circle(const Circle<T>& cir) noexcept
  469. : fPos(cir.fPos),
  470. fSize(cir.fSize),
  471. fNumSegments(cir.fNumSegments),
  472. fTheta(cir.fTheta),
  473. fCos(cir.fCos),
  474. fSin(cir.fSin)
  475. {
  476. DISTRHO_SAFE_ASSERT(fSize > 0.0f);
  477. }
  478. template<typename T>
  479. const T& Circle<T>::getX() const noexcept
  480. {
  481. return fPos.x;
  482. }
  483. template<typename T>
  484. const T& Circle<T>::getY() const noexcept
  485. {
  486. return fPos.y;
  487. }
  488. template<typename T>
  489. const Point<T>& Circle<T>::getPos() const noexcept
  490. {
  491. return fPos;
  492. }
  493. template<typename T>
  494. void Circle<T>::setX(const T& x) noexcept
  495. {
  496. fPos.x = x;
  497. }
  498. template<typename T>
  499. void Circle<T>::setY(const T& y) noexcept
  500. {
  501. fPos.y = y;
  502. }
  503. template<typename T>
  504. void Circle<T>::setPos(const T& x, const T& y) noexcept
  505. {
  506. fPos.x = x;
  507. fPos.y = y;
  508. }
  509. template<typename T>
  510. void Circle<T>::setPos(const Point<T>& pos) noexcept
  511. {
  512. fPos = pos;
  513. }
  514. template<typename T>
  515. float Circle<T>::getSize() const noexcept
  516. {
  517. return fSize;
  518. }
  519. template<typename T>
  520. void Circle<T>::setSize(const float size) noexcept
  521. {
  522. DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,);
  523. fSize = size;
  524. }
  525. template<typename T>
  526. uint Circle<T>::getNumSegments() const noexcept
  527. {
  528. return fNumSegments;
  529. }
  530. template<typename T>
  531. void Circle<T>::setNumSegments(const uint num)
  532. {
  533. DISTRHO_SAFE_ASSERT_RETURN(num >= 3,);
  534. if (fNumSegments == num)
  535. return;
  536. fNumSegments = num;
  537. fTheta = M_2PIf / static_cast<float>(fNumSegments);
  538. fCos = std::cos(fTheta);
  539. fSin = std::sin(fTheta);
  540. }
  541. template<typename T>
  542. Circle<T>& Circle<T>::operator=(const Circle<T>& cir) noexcept
  543. {
  544. fPos = cir.fPos;
  545. fSize = cir.fSize;
  546. fTheta = cir.fTheta;
  547. fCos = cir.fCos;
  548. fSin = cir.fSin;
  549. fNumSegments = cir.fNumSegments;
  550. return *this;
  551. }
  552. template<typename T>
  553. bool Circle<T>::operator==(const Circle<T>& cir) const noexcept
  554. {
  555. return (fPos == cir.fPos && d_isEqual(fSize, cir.fSize) && fNumSegments == cir.fNumSegments);
  556. }
  557. template<typename T>
  558. bool Circle<T>::operator!=(const Circle<T>& cir) const noexcept
  559. {
  560. return (fPos != cir.fPos || d_isNotEqual(fSize, cir.fSize) || fNumSegments != cir.fNumSegments);
  561. }
  562. // -----------------------------------------------------------------------
  563. // Triangle
  564. template<typename T>
  565. Triangle<T>::Triangle() noexcept
  566. : pos1(0, 0),
  567. pos2(0, 0),
  568. pos3(0, 0) {}
  569. template<typename T>
  570. Triangle<T>::Triangle(const T& x1, const T& y1, const T& x2, const T& y2, const T& x3, const T& y3) noexcept
  571. : pos1(x1, y1),
  572. pos2(x2, y2),
  573. pos3(x3, y3) {}
  574. template<typename T>
  575. Triangle<T>::Triangle(const Point<T>& p1, const Point<T>& p2, const Point<T>& p3) noexcept
  576. : pos1(p1),
  577. pos2(p2),
  578. pos3(p3) {}
  579. template<typename T>
  580. Triangle<T>::Triangle(const Triangle<T>& tri) noexcept
  581. : pos1(tri.pos1),
  582. pos2(tri.pos2),
  583. pos3(tri.pos3) {}
  584. template<typename T>
  585. bool Triangle<T>::isNull() const noexcept
  586. {
  587. return pos1 == pos2 && pos1 == pos3;
  588. }
  589. template<typename T>
  590. bool Triangle<T>::isNotNull() const noexcept
  591. {
  592. return pos1 != pos2 || pos1 != pos3;
  593. }
  594. template<typename T>
  595. bool Triangle<T>::isValid() const noexcept
  596. {
  597. return pos1 != pos2 && pos1 != pos3;
  598. }
  599. template<typename T>
  600. bool Triangle<T>::isInvalid() const noexcept
  601. {
  602. return pos1 == pos2 || pos1 == pos3;
  603. }
  604. template<typename T>
  605. Triangle<T>& Triangle<T>::operator=(const Triangle<T>& tri) noexcept
  606. {
  607. pos1 = tri.pos1;
  608. pos2 = tri.pos2;
  609. pos3 = tri.pos3;
  610. return *this;
  611. }
  612. template<typename T>
  613. bool Triangle<T>::operator==(const Triangle<T>& tri) const noexcept
  614. {
  615. return (pos1 == tri.pos1 && pos2 == tri.pos2 && pos3 == tri.pos3);
  616. }
  617. template<typename T>
  618. bool Triangle<T>::operator!=(const Triangle<T>& tri) const noexcept
  619. {
  620. return (pos1 != tri.pos1 || pos2 != tri.pos2 || pos3 != tri.pos3);
  621. }
  622. // -----------------------------------------------------------------------
  623. // Rectangle
  624. template<typename T>
  625. Rectangle<T>::Rectangle() noexcept
  626. : pos(0, 0),
  627. size(0, 0) {}
  628. template<typename T>
  629. Rectangle<T>::Rectangle(const T& x, const T& y, const T& w, const T& h) noexcept
  630. : pos(x, y),
  631. size(w, h) {}
  632. template<typename T>
  633. Rectangle<T>::Rectangle(const T& x, const T& y, const Size<T>& s) noexcept
  634. : pos(x, y),
  635. size(s) {}
  636. template<typename T>
  637. Rectangle<T>::Rectangle(const Point<T>& p, const T& w, const T& h) noexcept
  638. : pos(p),
  639. size(w, h) {}
  640. template<typename T>
  641. Rectangle<T>::Rectangle(const Point<T>& p, const Size<T>& s) noexcept
  642. : pos(p),
  643. size(s) {}
  644. template<typename T>
  645. Rectangle<T>::Rectangle(const Rectangle<T>& rect) noexcept
  646. : pos(rect.pos),
  647. size(rect.size) {}
  648. template<typename T>
  649. const T& Rectangle<T>::getX() const noexcept
  650. {
  651. return pos.x;
  652. }
  653. template<typename T>
  654. const T& Rectangle<T>::getY() const noexcept
  655. {
  656. return pos.y;
  657. }
  658. template<typename T>
  659. const T& Rectangle<T>::getWidth() const noexcept
  660. {
  661. return size.fWidth;
  662. }
  663. template<typename T>
  664. const T& Rectangle<T>::getHeight() const noexcept
  665. {
  666. return size.fHeight;
  667. }
  668. template<typename T>
  669. const Point<T>& Rectangle<T>::getPos() const noexcept
  670. {
  671. return pos;
  672. }
  673. template<typename T>
  674. const Size<T>& Rectangle<T>::getSize() const noexcept
  675. {
  676. return size;
  677. }
  678. template<typename T>
  679. void Rectangle<T>::setX(const T& x) noexcept
  680. {
  681. pos.x = x;
  682. }
  683. template<typename T>
  684. void Rectangle<T>::setY(const T& y) noexcept
  685. {
  686. pos.y = y;
  687. }
  688. template<typename T>
  689. void Rectangle<T>::setPos(const T& x, const T& y) noexcept
  690. {
  691. pos.x = x;
  692. pos.y = y;
  693. }
  694. template<typename T>
  695. void Rectangle<T>::setPos(const Point<T>& pos2) noexcept
  696. {
  697. pos = pos2;
  698. }
  699. template<typename T>
  700. void Rectangle<T>::moveBy(const T& x, const T& y) noexcept
  701. {
  702. pos.moveBy(x, y);
  703. }
  704. template<typename T>
  705. void Rectangle<T>::moveBy(const Point<T>& pos2) noexcept
  706. {
  707. pos.moveBy(pos2);
  708. }
  709. template<typename T>
  710. void Rectangle<T>::setWidth(const T& width) noexcept
  711. {
  712. size.fWidth = width;
  713. }
  714. template<typename T>
  715. void Rectangle<T>::setHeight(const T& height) noexcept
  716. {
  717. size.fHeight = height;
  718. }
  719. template<typename T>
  720. void Rectangle<T>::setSize(const T& width, const T& height) noexcept
  721. {
  722. size.fWidth = width;
  723. size.fHeight = height;
  724. }
  725. template<typename T>
  726. void Rectangle<T>::setSize(const Size<T>& size2) noexcept
  727. {
  728. size = size2;
  729. }
  730. template<typename T>
  731. void Rectangle<T>::growBy(double multiplier) noexcept
  732. {
  733. size.growBy(multiplier);
  734. }
  735. template<typename T>
  736. void Rectangle<T>::shrinkBy(double divider) noexcept
  737. {
  738. size.shrinkBy(divider);
  739. }
  740. template<typename T>
  741. void Rectangle<T>::setRectangle(const Point<T>& pos2, const Size<T>& size2) noexcept
  742. {
  743. pos = pos2;
  744. size = size2;
  745. }
  746. template<typename T>
  747. void Rectangle<T>::setRectangle(const Rectangle<T>& rect) noexcept
  748. {
  749. pos = rect.pos;
  750. size = rect.size;
  751. }
  752. template<typename T>
  753. bool Rectangle<T>::contains(const T& x, const T& y) const noexcept
  754. {
  755. return (x >= pos.x && y >= pos.y && x <= pos.x+size.fWidth && y <= pos.y+size.fHeight);
  756. }
  757. template<typename T>
  758. bool Rectangle<T>::contains(const Point<T>& p) const noexcept
  759. {
  760. return contains(p.x, p.y);
  761. }
  762. template<typename T>
  763. template<typename T2>
  764. bool Rectangle<T>::contains(const Point<T2>& p) const noexcept
  765. {
  766. return (p.x >= pos.x && p.y >= pos.y && p.x <= pos.x+size.fWidth && p.y <= pos.y+size.fHeight);
  767. }
  768. template<> template<>
  769. bool Rectangle<int>::contains(const Point<double>& p) const noexcept
  770. {
  771. return (p.x >= pos.x && p.y >= pos.y && p.x <= pos.x+size.fWidth && p.y <= pos.y+size.fHeight);
  772. }
  773. template<> template<>
  774. bool Rectangle<uint>::contains(const Point<double>& p) const noexcept
  775. {
  776. return (p.x >= pos.x && p.y >= pos.y && p.x <= pos.x+size.fWidth && p.y <= pos.y+size.fHeight);
  777. }
  778. template<typename T>
  779. bool Rectangle<T>::containsAfterScaling(const Point<T>& p, const double scaling) const noexcept
  780. {
  781. return (p.x >= pos.x && p.y >= pos.y && p.x/scaling <= pos.x+size.fWidth && p.y/scaling <= pos.y+size.fHeight);
  782. }
  783. template<typename T>
  784. bool Rectangle<T>::containsX(const T& x) const noexcept
  785. {
  786. return (x >= pos.x && x <= pos.x + size.fWidth);
  787. }
  788. template<typename T>
  789. bool Rectangle<T>::containsY(const T& y) const noexcept
  790. {
  791. return (y >= pos.y && y <= pos.y + size.fHeight);
  792. }
  793. template<typename T>
  794. bool Rectangle<T>::isNull() const noexcept
  795. {
  796. return size.isNull();
  797. }
  798. template<typename T>
  799. bool Rectangle<T>::isNotNull() const noexcept
  800. {
  801. return size.isNotNull();
  802. }
  803. template<typename T>
  804. bool Rectangle<T>::isValid() const noexcept
  805. {
  806. return size.isValid();
  807. }
  808. template<typename T>
  809. bool Rectangle<T>::isInvalid() const noexcept
  810. {
  811. return size.isInvalid();
  812. }
  813. template<typename T>
  814. Rectangle<T>& Rectangle<T>::operator=(const Rectangle<T>& rect) noexcept
  815. {
  816. pos = rect.pos;
  817. size = rect.size;
  818. return *this;
  819. }
  820. template<typename T>
  821. Rectangle<T>& Rectangle<T>::operator*=(double m) noexcept
  822. {
  823. size *= m;
  824. return *this;
  825. }
  826. template<typename T>
  827. Rectangle<T>& Rectangle<T>::operator/=(double d) noexcept
  828. {
  829. size /= d;
  830. return *this;
  831. }
  832. template<typename T>
  833. bool Rectangle<T>::operator==(const Rectangle<T>& rect) const noexcept
  834. {
  835. return (pos == rect.pos && size == rect.size);
  836. }
  837. template<typename T>
  838. bool Rectangle<T>::operator!=(const Rectangle<T>& rect) const noexcept
  839. {
  840. return (pos != rect.pos || size != rect.size);
  841. }
  842. // -----------------------------------------------------------------------
  843. // Possible template data types
  844. template class Point<double>;
  845. template class Point<float>;
  846. template class Point<int>;
  847. template class Point<uint>;
  848. template class Point<short>;
  849. template class Point<ushort>;
  850. template class Size<double>;
  851. template class Size<float>;
  852. template class Size<int>;
  853. template class Size<uint>;
  854. template class Size<short>;
  855. template class Size<ushort>;
  856. template class Line<double>;
  857. template class Line<float>;
  858. template class Line<int>;
  859. template class Line<uint>;
  860. template class Line<short>;
  861. template class Line<ushort>;
  862. template class Circle<double>;
  863. template class Circle<float>;
  864. template class Circle<int>;
  865. template class Circle<uint>;
  866. template class Circle<short>;
  867. template class Circle<ushort>;
  868. template class Triangle<double>;
  869. template class Triangle<float>;
  870. template class Triangle<int>;
  871. template class Triangle<uint>;
  872. template class Triangle<short>;
  873. template class Triangle<ushort>;
  874. template class Rectangle<double>;
  875. template class Rectangle<float>;
  876. template class Rectangle<int>;
  877. template class Rectangle<uint>;
  878. template class Rectangle<short>;
  879. template class Rectangle<ushort>;
  880. // -----------------------------------------------------------------------
  881. END_NAMESPACE_DGL