geom.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* geom.h - Qt <-> 2geom conversions
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef STUDIO_UTIL_GEOM_H_FB758F4A_2FB3_527A_81CE_74C1E91A4DFF
  18. #define STUDIO_UTIL_GEOM_H_FB758F4A_2FB3_527A_81CE_74C1E91A4DFF
  19. #include <QPoint>
  20. #include <QMatrix>
  21. #include <2geom/point.h>
  22. #include <core/std/traits.h>
  23. namespace rainynite::util {
  24. /**
  25. * Convert 2D point between lib2geom and Qt (QPoint/QPointF).
  26. *
  27. * This template function can be called whenever you need the conversion and
  28. * direction will be detected automatically throw template specializations.
  29. */
  30. template <typename S, typename T>
  31. T point(S&& p);
  32. template <typename S>
  33. std::enable_if_t<is_same_v<std::decay_t<S>,Geom::Point>, QPointF>
  34. point(S&& p) {
  35. return { p.x(), p.y() };
  36. }
  37. template <typename S>
  38. std::enable_if_t<is_same_v<std::decay_t<S>,QPointF>, Geom::Point>
  39. point(S&& p) {
  40. return { p.x(), p.y() };
  41. }
  42. template <typename S>
  43. std::enable_if_t<is_same_v<std::decay_t<S>,QPoint>, Geom::Point>
  44. point(S&& p) {
  45. return { (Geom::Coord) p.x(), (Geom::Coord) p.y() };
  46. }
  47. /**
  48. * Convert affine transform matrices between lib2geom (Affine) and Qt (QMatrix).
  49. *
  50. * Template specializations will automatically choose appropriate instance.
  51. *
  52. * NOTE: since QTransform is full 3x3 matrix, it cannot be converted into
  53. * Geom::Affine without possible information loss, so you have to do that
  54. * explicitly.
  55. */
  56. template <typename S, typename T>
  57. T matrix(S&& m);
  58. template <typename S>
  59. std::enable_if_t<is_same_v<std::decay_t<S>,Geom::Affine>, QMatrix>
  60. matrix(S&& m) {
  61. return {m[0], m[1], m[2], m[3], m[4], m[5]};
  62. }
  63. template <typename S>
  64. std::enable_if_t<is_same_v<std::decay_t<S>,QMatrix>, Geom::Affine>
  65. matrix(S&& m) {
  66. return {m.m11(), m.m12(), m.m21(), m.m22(), m.dx(), m.dy()};
  67. }
  68. } // namespace rainynite::util
  69. #endif