catmull_rom.hpp 8.5 KB

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