knots.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * knots.h - Geom paths <-> bezier knots conversion
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef __GEOM_HELPERS__KNOTS_H__B3B45F40
  19. #define __GEOM_HELPERS__KNOTS_H__B3B45F40
  20. #include <2geom/path.h>
  21. namespace Geom {
  22. struct Knot {
  23. using Id = std::string;
  24. Point pos;
  25. Point tg1;
  26. Point tg2;
  27. Id uid;
  28. explicit Knot(Point pos_=Point(), Point tg1_=Point(), Point tg2_=Point(), Id uid_="") :
  29. pos(pos_), tg1(tg1_), tg2(tg2_), uid(uid_)
  30. {}
  31. inline bool operator==(Knot const& other) const {
  32. return pos == other.pos
  33. && tg1 == other.tg1
  34. && tg2 == other.tg2
  35. && uid == other.uid;
  36. }
  37. inline bool operator!=(Knot const& other) const {
  38. return !(*this == other);
  39. }
  40. };
  41. class BezierKnots {
  42. public:
  43. BezierKnots() :
  44. BezierKnots{{}}
  45. {}
  46. explicit BezierKnots(std::vector<Knot> knots_, bool closed_=true) :
  47. knots(knots_), closed(closed_)
  48. {}
  49. public:
  50. inline bool operator==(BezierKnots const& other) const {
  51. return closed == other.closed
  52. && knots == other.knots;
  53. }
  54. inline bool operator!=(BezierKnots const& other) const {
  55. return !(*this == other);
  56. }
  57. public:
  58. inline size_t size() const {
  59. return knots.size();
  60. }
  61. public:
  62. std::vector<Knot> knots;
  63. bool closed;
  64. };
  65. BezierKnots path_to_knots(Geom::Path const& path);
  66. Geom::Path knots_to_path(BezierKnots const& knots);
  67. BezierKnots svg_to_knots(std::string const& str);
  68. BezierKnots svg_to_knots(char const* str);
  69. std::string knots_to_svg(BezierKnots const& knots);
  70. } // namespace Geom
  71. #endif