catmull_rom_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * Copyright Nick Thompson, 2017
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #define BOOST_TEST_MODULE catmull_rom_test
  8. #include <array>
  9. #include <random>
  10. #include <boost/cstdfloat.hpp>
  11. #include <boost/type_index.hpp>
  12. #include <boost/test/included/unit_test.hpp>
  13. #include <boost/test/tools/floating_point_comparison.hpp>
  14. #include <boost/math/constants/constants.hpp>
  15. #include <boost/math/interpolators/catmull_rom.hpp>
  16. #include <boost/multiprecision/cpp_bin_float.hpp>
  17. #include <boost/multiprecision/cpp_dec_float.hpp>
  18. #include <boost/numeric/ublas/vector.hpp>
  19. using std::abs;
  20. using boost::multiprecision::cpp_bin_float_50;
  21. using boost::math::catmull_rom;
  22. template<class Real>
  23. void test_alpha_distance()
  24. {
  25. Real tol = std::numeric_limits<Real>::epsilon();
  26. std::array<Real, 3> v1 = {0,0,0};
  27. std::array<Real, 3> v2 = {1,0,0};
  28. Real alpha = 0.5;
  29. Real d = boost::math::detail::alpha_distance<std::array<Real, 3>>(v1, v2, alpha);
  30. BOOST_CHECK_CLOSE_FRACTION(d, 1, tol);
  31. d = boost::math::detail::alpha_distance<std::array<Real, 3>>(v1, v2, 0.0);
  32. BOOST_CHECK_CLOSE_FRACTION(d, 1, tol);
  33. d = boost::math::detail::alpha_distance<std::array<Real, 3>>(v1, v2, 1.0);
  34. BOOST_CHECK_CLOSE_FRACTION(d, 1, tol);
  35. v2[0] = 2;
  36. d = boost::math::detail::alpha_distance<std::array<Real, 3>>(v1, v2, alpha);
  37. BOOST_CHECK_CLOSE_FRACTION(d, pow(2, (Real)1/ (Real) 2), tol);
  38. d = boost::math::detail::alpha_distance<std::array<Real, 3>>(v1, v2, 0.0);
  39. BOOST_CHECK_CLOSE_FRACTION(d, 1, tol);
  40. d = boost::math::detail::alpha_distance<std::array<Real, 3>>(v1, v2, 1.0);
  41. BOOST_CHECK_CLOSE_FRACTION(d, 2, tol);
  42. }
  43. template<class Real>
  44. void test_linear()
  45. {
  46. std::cout << "Testing that the Catmull-Rom spline interpolates linear functions correctly on type "
  47. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  48. Real tol = 10*std::numeric_limits<Real>::epsilon();
  49. std::vector<std::array<Real, 3>> v(4);
  50. v[0] = {0,0,0};
  51. v[1] = {1,0,0};
  52. v[2] = {2,0,0};
  53. v[3] = {3,0,0};
  54. catmull_rom<std::array<Real, 3>> cr(std::move(v));
  55. // Test that the interpolation condition is obeyed:
  56. BOOST_CHECK_CLOSE_FRACTION(cr.max_parameter(), 3, tol);
  57. auto p0 = cr(0.0);
  58. BOOST_CHECK_SMALL(p0[0], tol);
  59. BOOST_CHECK_SMALL(p0[1], tol);
  60. BOOST_CHECK_SMALL(p0[2], tol);
  61. auto p1 = cr(1.0);
  62. BOOST_CHECK_CLOSE_FRACTION(p1[0], 1, tol);
  63. BOOST_CHECK_SMALL(p1[1], tol);
  64. BOOST_CHECK_SMALL(p1[2], tol);
  65. auto p2 = cr(2.0);
  66. BOOST_CHECK_CLOSE_FRACTION(p2[0], 2, tol);
  67. BOOST_CHECK_SMALL(p2[1], tol);
  68. BOOST_CHECK_SMALL(p2[2], tol);
  69. auto p3 = cr(3.0);
  70. BOOST_CHECK_CLOSE_FRACTION(p3[0], 3, tol);
  71. BOOST_CHECK_SMALL(p3[1], tol);
  72. BOOST_CHECK_SMALL(p3[2], tol);
  73. Real s = cr.parameter_at_point(0);
  74. BOOST_CHECK_SMALL(s, tol);
  75. s = cr.parameter_at_point(1);
  76. BOOST_CHECK_CLOSE_FRACTION(s, 1, tol);
  77. s = cr.parameter_at_point(2);
  78. BOOST_CHECK_CLOSE_FRACTION(s, 2, tol);
  79. s = cr.parameter_at_point(3);
  80. BOOST_CHECK_CLOSE_FRACTION(s, 3, tol);
  81. // Test that the function is linear on the interval [1,2]:
  82. for (double s = 1; s < 2; s += 0.01)
  83. {
  84. auto p = cr(s);
  85. BOOST_CHECK_CLOSE_FRACTION(p[0], s, tol);
  86. BOOST_CHECK_SMALL(p[1], tol);
  87. BOOST_CHECK_SMALL(p[2], tol);
  88. auto tangent = cr.prime(s);
  89. BOOST_CHECK_CLOSE_FRACTION(tangent[0], 1, tol);
  90. BOOST_CHECK_SMALL(tangent[1], tol);
  91. BOOST_CHECK_SMALL(tangent[2], tol);
  92. }
  93. }
  94. template<class Real>
  95. void test_circle()
  96. {
  97. using boost::math::constants::pi;
  98. using std::cos;
  99. using std::sin;
  100. std::cout << "Testing that the Catmull-Rom spline interpolates circles correctly on type "
  101. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  102. Real tol = 10*std::numeric_limits<Real>::epsilon();
  103. std::vector<std::array<Real, 2>> v(20*sizeof(Real));
  104. std::vector<std::array<Real, 2>> u(20*sizeof(Real));
  105. for (size_t i = 0; i < v.size(); ++i)
  106. {
  107. Real theta = ((Real) i/ (Real) v.size())*2*pi<Real>();
  108. v[i] = {cos(theta), sin(theta)};
  109. u[i] = v[i];
  110. }
  111. catmull_rom<std::array<Real, 2>> circle(std::move(v), true);
  112. // Interpolation condition:
  113. for (size_t i = 0; i < v.size(); ++i)
  114. {
  115. Real s = circle.parameter_at_point(i);
  116. auto p = circle(s);
  117. Real x = p[0];
  118. Real y = p[1];
  119. if (abs(x) < std::numeric_limits<Real>::epsilon())
  120. {
  121. BOOST_CHECK_SMALL(u[i][0], tol);
  122. }
  123. if (abs(y) < std::numeric_limits<Real>::epsilon())
  124. {
  125. BOOST_CHECK_SMALL(u[i][1], tol);
  126. }
  127. else
  128. {
  129. BOOST_CHECK_CLOSE_FRACTION(x, u[i][0], tol);
  130. BOOST_CHECK_CLOSE_FRACTION(y, u[i][1], tol);
  131. }
  132. }
  133. Real max_s = circle.max_parameter();
  134. for(Real s = 0; s < max_s; s += 0.01)
  135. {
  136. auto p = circle(s);
  137. Real x = p[0];
  138. Real y = p[1];
  139. BOOST_CHECK_CLOSE_FRACTION(x*x+y*y, 1, 0.001);
  140. }
  141. }
  142. template<class Real, size_t dimension>
  143. void test_affine_invariance()
  144. {
  145. std::cout << "Testing that the Catmull-Rom spline is affine invariant in dimension "
  146. << dimension << " on type "
  147. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  148. Real tol = 1000*std::numeric_limits<Real>::epsilon();
  149. std::vector<std::array<Real, dimension>> v(100);
  150. std::vector<std::array<Real, dimension>> u(100);
  151. std::mt19937_64 gen(438232);
  152. Real inv_denom = (Real) 100/( (Real) (gen.max)() + (Real) 2);
  153. for(size_t j = 0; j < dimension; ++j)
  154. {
  155. v[0][j] = gen()*inv_denom;
  156. u[0][j] = v[0][j];
  157. }
  158. for (size_t i = 1; i < v.size(); ++i)
  159. {
  160. for(size_t j = 0; j < dimension; ++j)
  161. {
  162. v[i][j] = v[i-1][j] + gen()*inv_denom;
  163. u[i][j] = v[i][j];
  164. }
  165. }
  166. std::array<Real, dimension> affine_shift;
  167. for (size_t j = 0; j < dimension; ++j)
  168. {
  169. affine_shift[j] = gen()*inv_denom;
  170. }
  171. catmull_rom<std::array<Real, dimension>> cr1(std::move(v));
  172. for(size_t i = 0; i< u.size(); ++i)
  173. {
  174. for(size_t j = 0; j < dimension; ++j)
  175. {
  176. u[i][j] += affine_shift[j];
  177. }
  178. }
  179. catmull_rom<std::array<Real, dimension>> cr2(std::move(u));
  180. BOOST_CHECK_CLOSE_FRACTION(cr1.max_parameter(), cr2.max_parameter(), tol);
  181. Real ds = cr1.max_parameter()/1024;
  182. for (Real s = 0; s < cr1.max_parameter(); s += ds)
  183. {
  184. auto p0 = cr1(s);
  185. auto p1 = cr2(s);
  186. auto tangent0 = cr1.prime(s);
  187. auto tangent1 = cr2.prime(s);
  188. for (size_t j = 0; j < dimension; ++j)
  189. {
  190. BOOST_CHECK_CLOSE_FRACTION(p0[j] + affine_shift[j], p1[j], tol);
  191. if (abs(tangent0[j]) > 5000*tol)
  192. {
  193. BOOST_CHECK_CLOSE_FRACTION(tangent0[j], tangent1[j], 5000*tol);
  194. }
  195. }
  196. }
  197. }
  198. template<class Real>
  199. void test_helix()
  200. {
  201. using boost::math::constants::pi;
  202. std::cout << "Testing that the Catmull-Rom spline interpolates helices correctly on type "
  203. << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  204. Real tol = 0.001;
  205. std::vector<std::array<Real, 3>> v(400*sizeof(Real));
  206. for (size_t i = 0; i < v.size(); ++i)
  207. {
  208. Real theta = ((Real) i/ (Real) v.size())*2*pi<Real>();
  209. v[i] = {cos(theta), sin(theta), theta};
  210. }
  211. catmull_rom<std::array<Real, 3>> helix(std::move(v));
  212. // Interpolation condition:
  213. for (size_t i = 0; i < v.size(); ++i)
  214. {
  215. Real s = helix.parameter_at_point(i);
  216. auto p = helix(s);
  217. Real t = p[2];
  218. Real x = p[0];
  219. Real y = p[1];
  220. if (abs(x) < tol)
  221. {
  222. BOOST_CHECK_SMALL(cos(t), tol);
  223. }
  224. if (abs(y) < tol)
  225. {
  226. BOOST_CHECK_SMALL(sin(t), tol);
  227. }
  228. else
  229. {
  230. BOOST_CHECK_CLOSE_FRACTION(x, cos(t), tol);
  231. BOOST_CHECK_CLOSE_FRACTION(y, sin(t), tol);
  232. }
  233. }
  234. Real max_s = helix.max_parameter();
  235. for(Real s = helix.parameter_at_point(1); s < max_s; s += 0.01)
  236. {
  237. auto p = helix(s);
  238. Real x = p[0];
  239. Real y = p[1];
  240. Real t = p[2];
  241. BOOST_CHECK_CLOSE_FRACTION(x*x+y*y, (Real) 1, (Real) 0.01);
  242. if (abs(x) < 0.01)
  243. {
  244. BOOST_CHECK_SMALL(cos(t), (Real) 0.05);
  245. }
  246. if (abs(y) < 0.01)
  247. {
  248. BOOST_CHECK_SMALL(sin(t), (Real) 0.05);
  249. }
  250. else
  251. {
  252. BOOST_CHECK_CLOSE_FRACTION(x, cos(t), (Real) 0.05);
  253. BOOST_CHECK_CLOSE_FRACTION(y, sin(t), (Real) 0.05);
  254. }
  255. }
  256. }
  257. template<class Real>
  258. class mypoint3d
  259. {
  260. public:
  261. // Must define a value_type:
  262. typedef Real value_type;
  263. // Regular constructor:
  264. mypoint3d(Real x, Real y, Real z)
  265. {
  266. m_vec[0] = x;
  267. m_vec[1] = y;
  268. m_vec[2] = z;
  269. }
  270. // Must define a default constructor:
  271. mypoint3d() {}
  272. // Must define array access:
  273. Real operator[](size_t i) const
  274. {
  275. return m_vec[i];
  276. }
  277. // Array element assignment:
  278. Real& operator[](size_t i)
  279. {
  280. return m_vec[i];
  281. }
  282. private:
  283. std::array<Real, 3> m_vec;
  284. };
  285. // Must define the free function "size()":
  286. template<class Real>
  287. BOOST_CONSTEXPR std::size_t size(const mypoint3d<Real>& c)
  288. {
  289. return 3;
  290. }
  291. template<class Real>
  292. void test_data_representations()
  293. {
  294. std::cout << "Testing that the Catmull-Rom spline works with multiple data representations.\n";
  295. mypoint3d<Real> p0(0.1, 0.2, 0.3);
  296. mypoint3d<Real> p1(0.2, 0.3, 0.4);
  297. mypoint3d<Real> p2(0.3, 0.4, 0.5);
  298. mypoint3d<Real> p3(0.4, 0.5, 0.6);
  299. mypoint3d<Real> p4(0.5, 0.6, 0.7);
  300. mypoint3d<Real> p5(0.6, 0.7, 0.8);
  301. // Tests initializer_list:
  302. catmull_rom<mypoint3d<Real>> cat({p0, p1, p2, p3, p4, p5});
  303. Real tol = 0.001;
  304. auto p = cat(cat.parameter_at_point(0));
  305. BOOST_CHECK_CLOSE_FRACTION(p[0], p0[0], tol);
  306. BOOST_CHECK_CLOSE_FRACTION(p[1], p0[1], tol);
  307. BOOST_CHECK_CLOSE_FRACTION(p[2], p0[2], tol);
  308. p = cat(cat.parameter_at_point(1));
  309. BOOST_CHECK_CLOSE_FRACTION(p[0], p1[0], tol);
  310. BOOST_CHECK_CLOSE_FRACTION(p[1], p1[1], tol);
  311. BOOST_CHECK_CLOSE_FRACTION(p[2], p1[2], tol);
  312. }
  313. template<class Real>
  314. void test_random_access_container()
  315. {
  316. std::cout << "Testing that the Catmull-Rom spline works with multiple data representations.\n";
  317. mypoint3d<Real> p0(0.1, 0.2, 0.3);
  318. mypoint3d<Real> p1(0.2, 0.3, 0.4);
  319. mypoint3d<Real> p2(0.3, 0.4, 0.5);
  320. mypoint3d<Real> p3(0.4, 0.5, 0.6);
  321. mypoint3d<Real> p4(0.5, 0.6, 0.7);
  322. mypoint3d<Real> p5(0.6, 0.7, 0.8);
  323. boost::numeric::ublas::vector<mypoint3d<Real>> u(6);
  324. u[0] = p0;
  325. u[1] = p1;
  326. u[2] = p2;
  327. u[3] = p3;
  328. u[4] = p4;
  329. u[5] = p5;
  330. // Tests initializer_list:
  331. catmull_rom<mypoint3d<Real>, decltype(u)> cat(std::move(u));
  332. Real tol = 0.001;
  333. auto p = cat(cat.parameter_at_point(0));
  334. BOOST_CHECK_CLOSE_FRACTION(p[0], p0[0], tol);
  335. BOOST_CHECK_CLOSE_FRACTION(p[1], p0[1], tol);
  336. BOOST_CHECK_CLOSE_FRACTION(p[2], p0[2], tol);
  337. p = cat(cat.parameter_at_point(1));
  338. BOOST_CHECK_CLOSE_FRACTION(p[0], p1[0], tol);
  339. BOOST_CHECK_CLOSE_FRACTION(p[1], p1[1], tol);
  340. BOOST_CHECK_CLOSE_FRACTION(p[2], p1[2], tol);
  341. }
  342. BOOST_AUTO_TEST_CASE(catmull_rom_test)
  343. {
  344. #if !defined(TEST) || (TEST == 1)
  345. test_data_representations<float>();
  346. test_alpha_distance<double>();
  347. test_linear<double>();
  348. test_linear<long double>();
  349. test_circle<float>();
  350. test_circle<double>();
  351. #endif
  352. #if !defined(TEST) || (TEST == 2)
  353. test_helix<double>();
  354. test_affine_invariance<double, 1>();
  355. test_affine_invariance<double, 2>();
  356. test_affine_invariance<double, 3>();
  357. test_affine_invariance<double, 4>();
  358. test_random_access_container<double>();
  359. #endif
  360. #if !defined(TEST) || (TEST == 3)
  361. test_affine_invariance<cpp_bin_float_50, 4>();
  362. #endif
  363. }