catmull_rom.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <boost/config.hpp>
  15. namespace std_workaround {
  16. #if defined(__cpp_lib_nonmember_container_access) || (defined(BOOST_MSVC) && (BOOST_MSVC >= 1900))
  17. using std::size;
  18. #else
  19. template <class C>
  20. inline BOOST_CONSTEXPR std::size_t size(const C& c)
  21. {
  22. return c.size();
  23. }
  24. template <class T, std::size_t N>
  25. inline BOOST_CONSTEXPR std::size_t size(const T(&array)[N]) BOOST_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 (std::ptrdiff_t i = num_pnts-1; i >= 0; --i)
  95. {
  96. m_pnts[i+1] = m_pnts[i];
  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. typename Point::value_type s3s = m_s[i+2] - s;
  140. Point A1_or_A3;
  141. typename Point::value_type denom = 1/(m_s[i] - m_s[i-1]);
  142. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  143. {
  144. A1_or_A3[j] = denom*(s1s*m_pnts[i-1][j] - s0s*m_pnts[i][j]);
  145. }
  146. Point A2_or_B2;
  147. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  148. {
  149. A2_or_B2[j] = denom21*(s2s*m_pnts[i][j] - s1s*m_pnts[i+1][j]);
  150. }
  151. Point B1_or_C;
  152. denom = 1/(m_s[i+1] - m_s[i-1]);
  153. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  154. {
  155. B1_or_C[j] = denom*(s2s*A1_or_A3[j] - s0s*A2_or_B2[j]);
  156. }
  157. denom = 1/(m_s[i+2] - m_s[i+1]);
  158. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  159. {
  160. A1_or_A3[j] = denom*(s3s*m_pnts[i+1][j] - s2s*m_pnts[i+2][j]);
  161. }
  162. Point B2;
  163. denom = 1/(m_s[i+2] - m_s[i]);
  164. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  165. {
  166. B2[j] = denom*(s3s*A2_or_B2[j] - s1s*A1_or_A3[j]);
  167. }
  168. for(size_t j = 0; j < size(m_pnts[0]); ++j)
  169. {
  170. B1_or_C[j] = denom21*(s2s*B1_or_C[j] - s1s*B2[j]);
  171. }
  172. return B1_or_C;
  173. }
  174. template<class Point, class RandomAccessContainer >
  175. Point catmull_rom<Point, RandomAccessContainer>::prime(const typename Point::value_type s) const
  176. {
  177. using std_workaround::size;
  178. // https://math.stackexchange.com/questions/843595/how-can-i-calculate-the-derivative-of-a-catmull-rom-spline-with-nonuniform-param
  179. // http://denkovacs.com/2016/02/catmull-rom-spline-derivatives/
  180. if (s < 0 || s > m_max_s)
  181. {
  182. throw std::domain_error("Parameter outside bounds.\n");
  183. }
  184. auto it = std::upper_bound(m_s.begin(), m_s.end(), s);
  185. //Now *it >= s. We want the index such that m_s[i] <= s < m_s[i+1]:
  186. size_t i = std::distance(m_s.begin(), it - 1);
  187. Point A1;
  188. typename Point::value_type denom = 1/(m_s[i] - m_s[i-1]);
  189. typename Point::value_type k1 = (m_s[i]-s)*denom;
  190. typename Point::value_type k2 = (s - m_s[i-1])*denom;
  191. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  192. {
  193. A1[j] = k1*m_pnts[i-1][j] + k2*m_pnts[i][j];
  194. }
  195. Point A1p;
  196. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  197. {
  198. A1p[j] = denom*(m_pnts[i][j] - m_pnts[i-1][j]);
  199. }
  200. Point A2;
  201. denom = 1/(m_s[i+1] - m_s[i]);
  202. k1 = (m_s[i+1]-s)*denom;
  203. k2 = (s - m_s[i])*denom;
  204. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  205. {
  206. A2[j] = k1*m_pnts[i][j] + k2*m_pnts[i+1][j];
  207. }
  208. Point A2p;
  209. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  210. {
  211. A2p[j] = denom*(m_pnts[i+1][j] - m_pnts[i][j]);
  212. }
  213. Point B1;
  214. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  215. {
  216. B1[j] = k1*A1[j] + k2*A2[j];
  217. }
  218. Point A3;
  219. denom = 1/(m_s[i+2] - m_s[i+1]);
  220. k1 = (m_s[i+2]-s)*denom;
  221. k2 = (s - m_s[i+1])*denom;
  222. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  223. {
  224. A3[j] = k1*m_pnts[i+1][j] + k2*m_pnts[i+2][j];
  225. }
  226. Point A3p;
  227. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  228. {
  229. A3p[j] = denom*(m_pnts[i+2][j] - m_pnts[i+1][j]);
  230. }
  231. Point B2;
  232. denom = 1/(m_s[i+2] - m_s[i]);
  233. k1 = (m_s[i+2]-s)*denom;
  234. k2 = (s - m_s[i])*denom;
  235. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  236. {
  237. B2[j] = k1*A2[j] + k2*A3[j];
  238. }
  239. Point B1p;
  240. denom = 1/(m_s[i+1] - m_s[i-1]);
  241. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  242. {
  243. B1p[j] = denom*(A2[j] - A1[j] + (m_s[i+1]- s)*A1p[j] + (s-m_s[i-1])*A2p[j]);
  244. }
  245. Point B2p;
  246. denom = 1/(m_s[i+2] - m_s[i]);
  247. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  248. {
  249. B2p[j] = denom*(A3[j] - A2[j] + (m_s[i+2] - s)*A2p[j] + (s - m_s[i])*A3p[j]);
  250. }
  251. Point Cp;
  252. denom = 1/(m_s[i+1] - m_s[i]);
  253. for (size_t j = 0; j < size(m_pnts[0]); ++j)
  254. {
  255. Cp[j] = denom*(B2[j] - B1[j] + (m_s[i+1] - s)*B1p[j] + (s - m_s[i])*B2p[j]);
  256. }
  257. return Cp;
  258. }
  259. }}
  260. #endif