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.

1052 lines
22KB

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