vec.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * OpenHMD - Free and Open Source API and drivers for immersive technology.
  3. * Copyright (C) 2013 Fredrik Hultin.
  4. * Copyright (C) 2013 Jakob Bornecrantz.
  5. * Distributed under the Boost 1.0 licence, see LICENSE for full text.
  6. */
  7. /* Unit Tests - Vector3f Tests */
  8. #include "tests.h"
  9. bool vec3f_eq(vec3f v1, vec3f v2, float t)
  10. {
  11. for(int i = 0; i < 3; i++)
  12. if(!float_eq(v1.arr[i], v2.arr[i], t)){
  13. printf("\nvec.arr[%d] == %f, expected %f\n", i, v1.arr[i], v2.arr[i]);
  14. return false;
  15. }
  16. return true;
  17. }
  18. void test_ovec3f_normalize_me()
  19. {
  20. vec3f v[][2] = {
  21. { {{1, 0, 0}}, {{1, 0, 0}} },
  22. { {{1, 2, 3}}, {{0.267261241912424, 0.534522483824849, 0.801783725737273}} },
  23. { {{-7, 13, 22}}, {{-0.264197974633739, 0.490653381462658, 0.830336491706037}} },
  24. { {{.1, .1, .1}}, {{0.577350269189626, 0.577350269189626, 0.577350269189626}} },
  25. { {{0, 0, 0}}, {{0, 0, 0}} },
  26. };
  27. int sz = sizeof(vec3f) * 2;
  28. float t = 0.001;
  29. for(int i = 0; i < sizeof(v) / sz; i++){
  30. vec3f norm = v[i][0];
  31. ovec3f_normalize_me(&norm);
  32. TAssert(vec3f_eq(norm, v[i][1], t));
  33. }
  34. }
  35. typedef struct {
  36. vec3f vec;
  37. float f;
  38. } vec_float;
  39. void test_ovec3f_get_length()
  40. {
  41. vec_float vf[] = {
  42. { {{0, 0, 0}}, 0},
  43. { {{1, 0, 0}}, 1},
  44. { {{1, 2, 0}}, 2.23606797749979},
  45. { {{1, -2, 0}}, 2.23606797749979},
  46. { {{1, 2, 3}}, 3.7416573867739413},
  47. { {{-1, -2, -3}}, 3.7416573867739413},
  48. };
  49. int sz = sizeof(vec_float);
  50. float t = 0.001;
  51. for(int i = 0; i < sizeof(vf) / sz; i++){
  52. TAssert(float_eq(ovec3f_get_length(&vf[i].vec), vf[i].f, t));
  53. }
  54. }
  55. typedef struct {
  56. vec3f v1, v2;
  57. float f;
  58. } vec2_float;
  59. void test_ovec3f_get_angle()
  60. {
  61. vec2_float vf[] = {
  62. { {{0, 0, 0}}, {{0, 0, 0}}, 0},
  63. { {{1, 0, 0}}, {{0, 0, 0}}, 0},
  64. { {{2, 4, 3}}, {{1, 2, 3}}, 0.33940126397005316},
  65. { {{2, 4, 3}}, {{-1, 2, 3}}, 0.7311043352203973},
  66. };
  67. int sz = sizeof(vec2_float);
  68. float t = 0.001;
  69. for(int i = 0; i < sizeof(vf) / sz; i++){
  70. TAssert(float_eq(ovec3f_get_angle(&vf[i].v1, &vf[i].v2), vf[i].f, t));
  71. }
  72. }
  73. //float ovec3f_get_dot(const vec3f* me, const vec3f* vec);
  74. void test_ovec3f_get_dot()
  75. {
  76. vec2_float vf[] = {
  77. { {{0, 0, 0}}, {{0, 0, 0}}, 0},
  78. { {{1, 2, 3}}, {{-.30, 2, 25}}, 78.7},
  79. { {{-1, -10000000, 3}}, {{-.30, 2, 25}}, -19999924.7},
  80. };
  81. int sz = sizeof(vec2_float);
  82. float t = 0.001;
  83. for(int i = 0; i < sizeof(vf) / sz; i++){
  84. TAssert(float_eq(ovec3f_get_dot(&vf[i].v1, &vf[i].v2), vf[i].f, t));
  85. }
  86. }