animate_morphing.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * animate_morphing.cpp - morphing animation utility
  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 <geom_helpers/knots.h>
  19. #include <morphing/morphing.h>
  20. #include <fmt/format.h>
  21. #include <fmt/ostream.h>
  22. #include <iostream>
  23. #include <fstream>
  24. #include <string>
  25. #include <regex>
  26. using namespace fmt::literals;
  27. std::string slurp(std::string const& fname) {
  28. std::ifstream in(fname);
  29. std::stringstream sstr;
  30. sstr << in.rdbuf();
  31. in.close();
  32. return sstr.str();
  33. }
  34. void parse_keys(std::string const& keys, Geom::BezierKnots& path) {
  35. auto regex = std::regex("([^,]*)[,$]");
  36. std::smatch match;
  37. std::regex_search(keys, match, regex);
  38. auto keys_begin = std::sregex_iterator(
  39. std::begin(keys),
  40. std::end(keys),
  41. regex
  42. );
  43. auto keys_end = std::sregex_iterator();
  44. std::cerr << "Keys: " << std::distance(keys_begin, keys_end) << std::endl;
  45. unsigned index = 0;
  46. for (auto key = keys_begin; key != keys_end; ++key) {
  47. std::cerr << (*key)[1] << std::endl;
  48. path.knots[index].uid = (*key)[1];
  49. ++index;
  50. }
  51. }
  52. void parse_average(std::string const& svg_from, std::string const& svg_to,
  53. std::string const& key_from, std::string const& key_to,
  54. Geom::BezierKnots& path_from, Geom::BezierKnots& path_to) {
  55. auto p_from = Geom::svg_to_knots(svg_from.c_str());
  56. auto p_to = Geom::svg_to_knots(svg_to.c_str());
  57. parse_keys(key_from, p_from);
  58. parse_keys(key_to, p_to);
  59. morphing::prepare_average(p_from, p_to, path_from, path_to);
  60. }
  61. void animate(std::string const& fname, unsigned frames) {
  62. using namespace Geom;
  63. auto input = slurp(fname);
  64. std::string base_regex = R"(\{\{([^]*)\}\}@\[([a-zA-Z_,]*)\])";
  65. auto regex = std::regex("\""+base_regex+R"([\n\s]*)"+base_regex+"\"");
  66. std::smatch match;
  67. std::regex_search(input, match, regex);
  68. if (match.empty()) {
  69. std::cerr << "Failed to detect path to morph" << std::endl;
  70. return;
  71. }
  72. auto svg_from = match[1];
  73. auto key_from = match[2];
  74. auto svg_to = match[3];
  75. auto key_to = match[4];
  76. BezierKnots path_from;
  77. BezierKnots path_to;
  78. parse_average(svg_from, svg_to, key_from, key_to, path_from, path_to);
  79. for (unsigned frame = 0; frame <= frames; ++frame) {
  80. auto tween = morphing::simple_average(path_from, path_to, 1.0*frame/frames);
  81. auto output = std::regex_replace(input, regex, "\""+knots_to_svg(tween)+"\"");
  82. std::ofstream out("{}.{:0>3}.svg"_format(fname, frame));
  83. out << output;
  84. out.close();
  85. }
  86. }
  87. int main(int argc, char** argv) {
  88. std::string fname;
  89. unsigned frames = 12;
  90. if (argc < 2) {
  91. fmt::print(std::cerr, "Usage: {} [<file-name> [<frame-count>]]\n", argv[0]);
  92. fname = "fight-source.svg";
  93. } else {
  94. fname = argv[1];
  95. if (argc >= 3) {
  96. frames = (unsigned) std::stoi(argv[2]);
  97. if (argc > 3)
  98. fmt::print(std::cerr, "Extra arguments ignored.\n");
  99. }
  100. }
  101. animate(fname, frames);
  102. return 0;
  103. }