catmull_rom.hpp 8.7 KB

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