catmull_rom.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright Nick Thompson, 2017
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. // This computes the Catmull-Rom spline from a list of points.
  7. #ifndef BOOST_MATH_INTERPOLATORS_CATMULL_ROM
  8. #define BOOST_MATH_INTERPOLATORS_CATMULL_ROM
  9. #include <cmath>
  10. #include <vector>
  11. #include <algorithm>
  12. #include <iterator>
  13. namespace boost{ namespace math{
  14. namespace detail
  15. {
  16. template<class Point>
  17. typename Point::value_type alpha_distance(Point const & p1, Point const & p2, typename Point::value_type alpha)
  18. {
  19. using std::pow;
  20. using std::size;
  21. typename Point::value_type dsq = 0;
  22. for (size_t i = 0; i < size(p1); ++i)
  23. {
  24. typename Point::value_type dx = p1[i] - p2[i];
  25. dsq += dx*dx;
  26. }
  27. return pow(dsq, alpha/2);
  28. }
  29. }
  30. template <class Point>
  31. class catmull_rom
  32. {
  33. public:
  34. catmull_rom(std::vector<Point>&& points, bool closed = false, typename Point::value_type alpha = (typename Point::value_type) 1/ (typename Point::value_type) 2);
  35. catmull_rom(std::initializer_list<Point> l, bool closed = false, typename Point::value_type alpha = (typename Point::value_type) 1/ (typename Point::value_type) 2) : catmull_rom(std::vector<Point>(l), closed, alpha) {}
  36. typename Point::value_type max_parameter() const
  37. {
  38. return m_max_s;
  39. }
  40. typename Point::value_type parameter_at_point(size_t i) const
  41. {
  42. return m_s[i+1];
  43. }
  44. Point operator()(const typename Point::value_type s) const;
  45. Point prime(const typename Point::value_type s) const;
  46. std::vector<Point>&& get_points()
  47. {
  48. return std::move(m_pnts);
  49. }
  50. private:
  51. std::vector<Point> m_pnts;
  52. std::vector<typename Point::value_type> m_s;
  53. typename Point::value_type m_max_s;
  54. };
  55. template<class Point>
  56. catmull_rom<Point>::catmull_rom(std::vector<Point>&& points, bool closed, typename Point::value_type alpha) : m_pnts(std::move(points))
  57. {
  58. size_t num_pnts = m_pnts.size();
  59. //std::cout << "Number of points = " << num_pnts << "\n";
  60. if (num_pnts < 4)
  61. {
  62. throw std::domain_error("The Catmull-Rom curve requires at least 4 points.");
  63. }
  64. if (alpha < 0 || alpha > 1)
  65. {
  66. throw std::domain_error("The parametrization alpha must be in the range [0,1].");
  67. }
  68. using std::abs;
  69. m_s.resize(num_pnts+3);
  70. m_pnts.resize(num_pnts+3);
  71. //std::cout << "Number of points now = " << m_pnts.size() << "\n";
  72. m_pnts[num_pnts+1] = m_pnts[0];
  73. m_pnts[num_pnts+2] = m_pnts[1];
  74. auto tmp = m_pnts[num_pnts-1];
  75. for (int64_t i = num_pnts-1; i >= 0; --i)
  76. {
  77. m_pnts[i+1] = m_pnts[i];
  78. }
  79. m_pnts[0] = tmp;
  80. m_s[0] = -detail::alpha_distance<Point>(m_pnts[0], m_pnts[1], alpha);
  81. if (abs(m_s[0]) < std::numeric_limits<typename Point::value_type>::epsilon())
  82. {
  83. throw std::domain_error("The first and last point should not be the same.\n");
  84. }
  85. m_s[1] = 0;
  86. for (size_t i = 2; i < m_s.size(); ++i)
  87. {
  88. typename Point::value_type d = detail::alpha_distance<Point>(m_pnts[i], m_pnts[i-1], alpha);
  89. if (abs(d) < std::numeric_limits<typename Point::value_type>::epsilon())
  90. {
  91. throw std::domain_error("The control points of the Catmull-Rom curve are too close together; this will lead to ill-conditioning.\n");
  92. }
  93. m_s[i] = m_s[i-1] + d;
  94. }
  95. if(closed)
  96. {
  97. m_max_s = m_s[num_pnts+1];
  98. }
  99. else
  100. {
  101. m_max_s = m_s[num_pnts];
  102. }
  103. }
  104. template<class Point>
  105. Point catmull_rom<Point>::operator()(const typename Point::value_type s) const
  106. {
  107. using std::size;
  108. if (s < 0 || s > m_max_s)
  109. {
  110. throw std::domain_error("Parameter outside bounds.");
  111. }
  112. auto it = std::upper_bound(m_s.begin(), m_s.end(), s);
  113. //Now *it >= s. We want the index such that m_s[i] <= s < m_s[i+1]:
  114. size_t i = std::distance(m_s.begin(), it - 1);
  115. // Only denom21 is used twice:
  116. typename Point::value_type denom21 = 1/(m_s[i+1] - m_s[i]);
  117. typename Point::value_type s0s = m_s[i-1] - s;
  118. typename Point::value_type s1s = m_s[i] - s;
  119. typename Point::value_type s2s = m_s[i+1] - s;
  120. typename Point::value_type s3s = m_s[i+2] - s;
  121. Point A1_or_A3;
  122. typename Point::value_type denom = 1/(m_s[i] - m_s[i-1]);
  123. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  124. {
  125. A1_or_A3[j] = denom*(s1s*m_pnts[i-1][j] - s0s*m_pnts[i][j]);
  126. }
  127. Point A2_or_B2;
  128. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  129. {
  130. A2_or_B2[j] = denom21*(s2s*m_pnts[i][j] - s1s*m_pnts[i+1][j]);
  131. }
  132. Point B1_or_C;
  133. denom = 1/(m_s[i+1] - m_s[i-1]);
  134. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  135. {
  136. B1_or_C[j] = denom*(s2s*A1_or_A3[j] - s0s*A2_or_B2[j]);
  137. }
  138. denom = 1/(m_s[i+2] - m_s[i+1]);
  139. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  140. {
  141. A1_or_A3[j] = denom*(s3s*m_pnts[i+1][j] - s2s*m_pnts[i+2][j]);
  142. }
  143. Point B2;
  144. denom = 1/(m_s[i+2] - m_s[i]);
  145. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  146. {
  147. B2[j] = denom*(s3s*A2_or_B2[j] - s1s*A1_or_A3[j]);
  148. }
  149. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  150. {
  151. B1_or_C[j] = denom21*(s2s*B1_or_C[j] - s1s*B2[j]);
  152. }
  153. return B1_or_C;
  154. }
  155. template<class Point>
  156. Point catmull_rom<Point>::prime(const typename Point::value_type s) const
  157. {
  158. using std::size;
  159. // https://math.stackexchange.com/questions/843595/how-can-i-calculate-the-derivative-of-a-catmull-rom-spline-with-nonuniform-param
  160. // http://denkovacs.com/2016/02/catmull-rom-spline-derivatives/
  161. if (s < 0 || s > m_max_s)
  162. {
  163. throw std::domain_error("Parameter outside bounds.\n");
  164. }
  165. auto it = std::upper_bound(m_s.begin(), m_s.end(), s);
  166. //Now *it >= s. We want the index such that m_s[i] <= s < m_s[i+1]:
  167. size_t i = std::distance(m_s.begin(), it - 1);
  168. Point A1;
  169. typename Point::value_type denom = 1/(m_s[i] - m_s[i-1]);
  170. typename Point::value_type k1 = (m_s[i]-s)*denom;
  171. typename Point::value_type k2 = (s - m_s[i-1])*denom;
  172. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  173. {
  174. A1[j] = k1*m_pnts[i-1][j] + k2*m_pnts[i][j];
  175. }
  176. Point A1p;
  177. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  178. {
  179. A1p[j] = denom*(m_pnts[i][j] - m_pnts[i-1][j]);
  180. }
  181. Point A2;
  182. denom = 1/(m_s[i+1] - m_s[i]);
  183. k1 = (m_s[i+1]-s)*denom;
  184. k2 = (s - m_s[i])*denom;
  185. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  186. {
  187. A2[j] = k1*m_pnts[i][j] + k2*m_pnts[i+1][j];
  188. }
  189. Point A2p;
  190. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  191. {
  192. A2p[j] = denom*(m_pnts[i+1][j] - m_pnts[i][j]);
  193. }
  194. Point B1;
  195. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  196. {
  197. B1[j] = k1*A1[j] + k2*A2[j];
  198. }
  199. Point A3;
  200. denom = 1/(m_s[i+2] - m_s[i+1]);
  201. k1 = (m_s[i+2]-s)*denom;
  202. k2 = (s - m_s[i+1])*denom;
  203. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  204. {
  205. A3[j] = k1*m_pnts[i+1][j] + k2*m_pnts[i+2][j];
  206. }
  207. Point A3p;
  208. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  209. {
  210. A3p[j] = denom*(m_pnts[i+2][j] - m_pnts[i+1][j]);
  211. }
  212. Point B2;
  213. denom = 1/(m_s[i+2] - m_s[i]);
  214. k1 = (m_s[i+2]-s)*denom;
  215. k2 = (s - m_s[i])*denom;
  216. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  217. {
  218. B2[j] = k1*A2[j] + k2*A3[j];
  219. }
  220. Point B1p;
  221. denom = 1/(m_s[i+1] - m_s[i-1]);
  222. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  223. {
  224. B1p[j] = denom*(A2[j] - A1[j] + (m_s[i+1]- s)*A1p[j] + (s-m_s[i-1])*A2p[j]);
  225. }
  226. Point B2p;
  227. denom = 1/(m_s[i+2] - m_s[i]);
  228. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  229. {
  230. B2p[j] = denom*(A3[j] - A2[j] + (m_s[i+2] - s)*A2p[j] + (s - m_s[i])*A3p[j]);
  231. }
  232. Point Cp;
  233. denom = 1/(m_s[i+1] - m_s[i]);
  234. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  235. {
  236. Cp[j] = denom*(B2[j] - B1[j] + (m_s[i+1] - s)*B1p[j] + (s - m_s[i])*B2p[j]);
  237. }
  238. return Cp;
  239. }
  240. }}
  241. #endif