geom.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* geom.h - Qt <-> 2geom conversions
  2. * Copyright (C) 2017 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 <2geom/point.h>
  21. #include <core/std/traits.h>
  22. namespace rainynite::util {
  23. /**
  24. * Convert 2D point between lib2geom and Qt (QPoint/QPointF).
  25. *
  26. * This template function can be called whenever you need the conversion and
  27. * direction will be detected automatically throw template specializations.
  28. */
  29. template <typename S, typename T>
  30. T point(S&& p);
  31. template <typename S>
  32. std::enable_if_t<is_same_v<std::decay_t<S>,Geom::Point>, QPointF>
  33. point(S&& p) {
  34. return { p.x(), p.y() };
  35. }
  36. template <typename S>
  37. std::enable_if_t<is_same_v<std::decay_t<S>,QPointF>, Geom::Point>
  38. point(S&& p) {
  39. return { p.x(), p.y() };
  40. }
  41. template <typename S>
  42. std::enable_if_t<is_same_v<std::decay_t<S>,QPoint>, Geom::Point>
  43. point(S&& p) {
  44. return { (Geom::Coord) p.x(), (Geom::Coord) p.y() };
  45. }
  46. /**
  47. * Convert affine transform matrices between lib2geom (Affine) and Qt (QMatrix).
  48. *
  49. * Template specializations will automatically choose appropriate instance.
  50. *
  51. * NOTE: since QTransform is full 3x3 matrix, it cannot be converted into
  52. * Geom::Affine without possible information loss, so you have to do that
  53. * explicitly.
  54. */
  55. template <typename S, typename T>
  56. T matrix(S&& m);
  57. template <typename S>
  58. std::enable_if_t<is_same_v<std::decay_t<S>,Geom::Affine>, QMatrix>
  59. matrix(S&& m) {
  60. return {m[0], m[1], m[2], m[3], m[4], m[5]};
  61. }
  62. template <typename S>
  63. std::enable_if_t<is_same_v<std::decay_t<S>,QMatrix>, Geom::Affine>
  64. matrix(S&& m) {
  65. return {m.m11(), m.m12(), m.m21(), m.m22(), m.dx(), m.dy()};
  66. }
  67. } // namespace rainynite::util
  68. #endif