hermite.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Hermite Curve Test
  4. **
  5. ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
  6. ** Copyright (c) 2007 Chris Moore
  7. **
  8. ** This package is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License as
  10. ** published by the Free Software Foundation; either version 2 of
  11. ** the License, or (at your option) any later version.
  12. **
  13. ** This package is distributed in the hope that it will be useful,
  14. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ** General Public License for more details.
  17. **
  18. ** === N O T E S ===========================================================
  19. **
  20. ** ========================================================================= */
  21. #include <ETL/hermite>
  22. #include <ETL/angle>
  23. #include <ETL/clock>
  24. #include <ETL/calculus>
  25. #include <ETL/fixed>
  26. #include <stdio.h>
  27. using namespace etl;
  28. int basic_test(void)
  29. {
  30. int ret = 0;
  31. float f;
  32. hermite<float> Hermie;
  33. etl::clock timer;
  34. double t;
  35. Hermie.p1() = 0;
  36. Hermie.t1() = 1;
  37. Hermie.p2() = 0;
  38. Hermie.t2() = 1;
  39. Hermie.sync();
  40. integral<hermite<float> > inte(Hermie);
  41. fprintf(stderr, "integral of curve() on [0,1] = %f\n", inte(0, 1.0));
  42. fprintf(stderr, "integral of curve() on [-1,3] = %f\n", inte(-1.0, 3.0));
  43. Hermie.set_rs(-1.0, 7.0);
  44. inte = integral<hermite<float> >(Hermie);
  45. fprintf(stderr, "integral of curve()[%f,%f] on [-1,7] = %f\n", Hermie.get_r(), Hermie.get_s(), inte(-1.0, 7.0));
  46. fprintf(stderr, "integral of curve()[%f,%f] on [0,1] = %f\n", Hermie.get_r(), Hermie.get_s(), inte(0, 1.0));
  47. Hermie.set_rs(0.0, 1.0);
  48. for (f = 0.0f, timer.reset(); f < 1.001f; f += 0.000005f) {
  49. t += Hermie(f) + Hermie(f + 0.1f);
  50. t += Hermie(f) + Hermie(f + 0.1f);
  51. t += Hermie(f) + Hermie(f + 0.1f);
  52. t += Hermie(f) + Hermie(f + 0.1f);
  53. t += Hermie(f) + Hermie(f + 0.1f);
  54. t += Hermie(f) + Hermie(f + 0.1f);
  55. t += Hermie(f) + Hermie(f + 0.1f);
  56. t += Hermie(f) + Hermie(f + 0.1f);
  57. t += Hermie(f) + Hermie(f + 0.1f);
  58. t += Hermie(f) + Hermie(f + 0.1f);
  59. t += Hermie(f) + Hermie(f + 0.1f);
  60. t += Hermie(f) + Hermie(f + 0.1f);
  61. }
  62. t = timer();
  63. fprintf(stderr, "time=%f milliseconds\n", t * 1000);
  64. return ret;
  65. }
  66. int angle_test(void)
  67. {
  68. int ret = 0;
  69. float f;
  70. hermite<angle> Hermie;
  71. etl::clock timer;
  72. angle tmp;
  73. double t;
  74. Hermie.p1() = angle::degrees(0);
  75. Hermie.t1() = angle::degrees(45);
  76. Hermie.p2() = angle::degrees(-45);
  77. Hermie.t2() = angle::degrees(180);
  78. Hermie.sync();
  79. for (f = 0.0f, timer.reset(); f < 1.001f; f += 0.000005f) {
  80. tmp += Hermie(f) + Hermie(f + 0.1f);
  81. tmp += Hermie(f) + Hermie(f + 0.1f);
  82. tmp += Hermie(f) + Hermie(f + 0.1f);
  83. tmp += Hermie(f) + Hermie(f + 0.1f);
  84. tmp += Hermie(f) + Hermie(f + 0.1f);
  85. tmp += Hermie(f) + Hermie(f + 0.1f);
  86. tmp += Hermie(f) + Hermie(f + 0.1f);
  87. tmp += Hermie(f) + Hermie(f + 0.1f);
  88. tmp += Hermie(f) + Hermie(f + 0.1f);
  89. tmp += Hermie(f) + Hermie(f + 0.1f);
  90. tmp += Hermie(f) + Hermie(f + 0.1f);
  91. tmp += Hermie(f) + Hermie(f + 0.1f);
  92. }
  93. t = timer();
  94. fprintf(stderr, "angle time=%f milliseconds\n", t * 1000);
  95. return ret;
  96. }
  97. int fixed_test(void)
  98. {
  99. int ret = 0;
  100. float f;
  101. hermite<float, fixed> Hermie;
  102. etl::clock timer;
  103. double t;
  104. Hermie.p1() = 0;
  105. Hermie.t1() = 1;
  106. Hermie.p2() = 0;
  107. Hermie.t2() = 1;
  108. Hermie.sync();
  109. for (f = 0.0f, timer.reset(); f < 1.001f; f += 0.005f) {
  110. t += Hermie(f) + Hermie(f + 0.1f);
  111. t += Hermie(f) + Hermie(f + 0.1f);
  112. t += Hermie(f) + Hermie(f + 0.1f);
  113. t += Hermie(f) + Hermie(f + 0.1f);
  114. t += Hermie(f) + Hermie(f + 0.1f);
  115. t += Hermie(f) + Hermie(f + 0.1f);
  116. t += Hermie(f) + Hermie(f + 0.1f);
  117. t += Hermie(f) + Hermie(f + 0.1f);
  118. t += Hermie(f) + Hermie(f + 0.1f);
  119. t += Hermie(f) + Hermie(f + 0.1f);
  120. t += Hermie(f) + Hermie(f + 0.1f);
  121. t += Hermie(f) + Hermie(f + 0.1f);
  122. }
  123. t = timer();
  124. fprintf(stderr, "time=%f milliseconds\n", t * 1000);
  125. return ret;
  126. }
  127. int anglefixed_test(void)
  128. {
  129. int ret = 0;
  130. float f;
  131. hermite<angle, fixed> Hermie;
  132. etl::clock timer;
  133. angle tmp;
  134. double t;
  135. Hermie.p1() = angle::degrees(0);
  136. Hermie.t1() = angle::degrees(45);
  137. Hermie.p2() = angle::degrees(-45);
  138. Hermie.t2() = angle::degrees(180);
  139. Hermie.sync();
  140. for (f = 0.0f, timer.reset(); f < 1.001f; f += 0.0005f) {
  141. tmp += Hermie(f) + Hermie(f + 0.1f);
  142. tmp += Hermie(f) + Hermie(f + 0.1f);
  143. tmp += Hermie(f) + Hermie(f + 0.1f);
  144. tmp += Hermie(f) + Hermie(f + 0.1f);
  145. tmp += Hermie(f) + Hermie(f + 0.1f);
  146. tmp += Hermie(f) + Hermie(f + 0.1f);
  147. tmp += Hermie(f) + Hermie(f + 0.1f);
  148. tmp += Hermie(f) + Hermie(f + 0.1f);
  149. tmp += Hermie(f) + Hermie(f + 0.1f);
  150. tmp += Hermie(f) + Hermie(f + 0.1f);
  151. tmp += Hermie(f) + Hermie(f + 0.1f);
  152. tmp += Hermie(f) + Hermie(f + 0.1f);
  153. }
  154. t = timer();
  155. fprintf(stderr, "angle fixed time=%f milliseconds\n", t * 1000);
  156. return ret;
  157. }
  158. int float_intersection_test()
  159. {
  160. int ret = 0;
  161. hermite<float> curve1(0, 1, 0, 1);
  162. hermite<float> curve2(-1, 2, -1, -2);
  163. double t1, t2;
  164. float d;
  165. t1 = curve1.intersect(curve2);
  166. t2 = curve2.intersect(curve1);
  167. d = curve1(t1) - curve2(t2);
  168. fprintf(stderr, "float:Intersection difference: %f (t1=%f, t2=%f)\n", d, t1, t2);
  169. if (d > 0.01) {
  170. fprintf(stderr, "float:FAILED INTERSECTION TEST.\n");
  171. ret++;
  172. }
  173. return ret;
  174. }
  175. int main()
  176. {
  177. int error = 0;
  178. error += basic_test();
  179. error += angle_test();
  180. error += fixed_test();
  181. error += float_intersection_test();
  182. return error;
  183. }