morph.cpp 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. auto path_avg = simple_average(path_from_fixed, path_to_fixed, 0.5);
  48. CHECK(path_from_fixed.size() == path_avg.size());
  49. CHECK(path_from_fixed.closed == path_avg.closed);
  50. // TODO: actual tests?
  51. std::cerr << path_from_fixed << std::endl;
  52. std::cerr << path_to_fixed << std::endl;
  53. std::cerr << knots_to_svg(path_avg) << std::endl;
  54. }
  55. TEST_CASE("Prepare average", "") {
  56. SECTION("Simple") {
  57. auto src_from = "m 100,100 c 60,-40 -40,-20 0,0 z";
  58. 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";
  59. auto path_from = svg_to_knots(src_from);
  60. auto path_to = svg_to_knots(src_to);
  61. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  62. test_average(path_from, path_to);
  63. }
  64. SECTION("M to N") {
  65. 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";
  66. 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";
  67. auto path_from = svg_to_knots(src_from);
  68. auto path_to = svg_to_knots(src_to);
  69. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  70. test_average(path_from, path_to);
  71. }
  72. SECTION("Two keys") {
  73. 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";
  74. 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";
  75. auto path_from = svg_to_knots(src_from);
  76. auto path_to = svg_to_knots(src_to);
  77. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  78. path_from.knots[1].uid = path_to.knots[2].uid = "other";
  79. test_average(path_from, path_to);
  80. }
  81. SECTION("Open") {
  82. auto src_from = "m 0,0 c 20,0 40,40 100,0";
  83. auto src_to = "m 0,20 c 20,0 30,-10 80,-20";
  84. auto src_avg = "m 0,10 c 20,0 35,25 90,0";
  85. auto path_from = svg_to_knots(src_from);
  86. auto path_to = svg_to_knots(src_to);
  87. path_from.knots[0].uid = path_to.knots[0].uid = "main";
  88. test_average(path_from, path_to);
  89. }
  90. }