morph.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * morph.cpp - basic morphing tests
  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. #include <catch.hpp>
  19. #include <geom_helpers/knots_io.h>
  20. #include <morphing/morphing.h>
  21. using namespace Geom;
  22. using namespace morphing;
  23. TEST_CASE("Simple morphing with equal amount of knots", "") {
  24. auto src_from = "m 100,100 c 10,10 20,10 20,0 c 20,20 20,10 30,-20 c -10,-10 -30,0 -10,20 c -10,-10 -30,0 -40,0 z";
  25. auto src_to = "m 100,100 c 20,0 30,-10 30,-30 c 0,-20 -10,-30 -30,-30 c -20,0 -30,10 -30,30 c 0,20 10,30 30,30 z";
  26. auto path_from = svg_to_knots(src_from);
  27. auto path_to = svg_to_knots(src_to);
  28. CHECK(
  29. knots_to_svg(simple_average(path_from, path_to, 0.0)) ==
  30. "M 100 100 C 110 110 120 110 120 100 C 140 120 140 110 150 80 C 140 70 120 80 140 100 C 130 90 110 100 100 100 z"
  31. );
  32. CHECK(
  33. knots_to_svg(simple_average(path_from, path_to, 0.5)) ==
  34. "M 100 100 C 115 105 125 100 125 85 C 135 85 130 75 125 60 C 110 55 95 65 105 85 C 100 90 95 100 100 100 z"
  35. );
  36. CHECK(
  37. knots_to_svg(simple_average(path_from, path_to, 1.0)) ==
  38. "M 100 100 C 120 100 130 90 130 70 S 120 40 100 40 S 70 50 70 70 S 80 100 100 100 z"
  39. );
  40. }
  41. void test_average(BezierKnots const& path_from, BezierKnots const& path_to) {
  42. BezierKnots path_from_fixed;
  43. BezierKnots path_to_fixed;
  44. prepare_average(path_from, path_to, path_from_fixed, path_to_fixed);
  45. CHECK(path_from_fixed.size() == path_to_fixed.size());
  46. CHECK(path_from_fixed.closed == path_to_fixed.closed);
  47. std::cerr << path_from_fixed << std::endl;
  48. std::cerr << path_to_fixed << std::endl;
  49. std::cerr << knots_to_svg(simple_average(path_from_fixed, path_to_fixed, 0.5)) << std::endl;
  50. }
  51. TEST_CASE("Prepare average", "") {
  52. SECTION("Simple") {
  53. auto src_from = "m 100,100 c 60,-40 -40,-20 0,0 z";
  54. auto src_to = "m 100,100 c 20,0 30,-10 30,-30 c 0,-20 -10,-30 -30,-30 c -20,0 -30,10 -30,30 c 0,20 10,30 30,30 z";
  55. auto path_from = svg_to_knots(src_from);
  56. auto path_to = svg_to_knots(src_to);
  57. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  58. test_average(path_from, path_to);
  59. }
  60. SECTION("M to N") {
  61. auto src_from = "m 100,100 c 60,-10 60,-20 40,-40 c -20,-10 -40,0 -60,10 c -10,10 -10,20 20,30 z";
  62. auto src_to = "m 100,100 c 20,0 30,-10 30,-30 c 0,-20 -10,-30 -30,-30 c -20,0 -30,10 -30,30 c 0,20 10,30 30,30 z";
  63. auto path_from = svg_to_knots(src_from);
  64. auto path_to = svg_to_knots(src_to);
  65. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  66. test_average(path_from, path_to);
  67. }
  68. SECTION("Two keys") {
  69. auto src_from = "m 100,100 c 60,-10 60,-20 40,-40 c -20,-10 -40,0 -60,10 c -10,10 -10,20 20,30 z";
  70. auto src_to = "m 100,100 c 20,0 30,-10 30,-30 c 0,-20 -10,-30 -30,-30 c -20,0 -30,10 -30,30 c 0,20 10,30 30,30 z";
  71. auto path_from = svg_to_knots(src_from);
  72. auto path_to = svg_to_knots(src_to);
  73. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  74. path_from.knots[1].uid = path_to.knots[2].uid = "other";
  75. test_average(path_from, path_to);
  76. }
  77. }