catmull_rom_example.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright Nick Thompson, 2017
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or
  4. // copy at http://www.boost.org/LICENSE_1_0.txt).
  5. #include <iostream>
  6. #include <vector>
  7. #include <array>
  8. #include <cmath>
  9. #include <boost/math/interpolators/catmull_rom.hpp>
  10. #include <boost/math/constants/constants.hpp>
  11. using std::sin;
  12. using std::cos;
  13. using boost::math::catmull_rom;
  14. int main()
  15. {
  16. std::cout << "This shows how to use Boost's Catmull-Rom spline to create an Archimedean spiral.\n";
  17. // The Archimedean spiral is given by r = a*theta. We have set a = 1.
  18. std::vector<std::array<double, 2>> spiral_points(500);
  19. double theta_max = boost::math::constants::pi<double>();
  20. for (size_t i = 0; i < spiral_points.size(); ++i)
  21. {
  22. double theta = ((double) i/ (double) spiral_points.size())*theta_max;
  23. spiral_points[i] = {theta*cos(theta), theta*sin(theta)};
  24. }
  25. auto archimedean = catmull_rom<std::array<double,2>>(std::move(spiral_points));
  26. double max_s = archimedean.max_parameter();
  27. std::cout << "Max s = " << max_s << std::endl;
  28. for (double s = 0; s < max_s; s += 0.01)
  29. {
  30. auto p = archimedean(s);
  31. double x = p[0];
  32. double y = p[1];
  33. double r = sqrt(x*x + y*y);
  34. double theta = atan2(y/r, x/r);
  35. std::cout << "r = " << r << ", theta = " << theta << ", r - theta = " << r - theta << std::endl;
  36. }
  37. return 0;
  38. }