vec.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. return false;
  14. return true;
  15. }
  16. void test_ovec3f_normalize_me()
  17. {
  18. vec3f v[][2] = {
  19. { {{1, 0, 0}}, {{1, 0, 0}} },
  20. { {{1, 2, 3}}, {{0.267261241912424, 0.534522483824849, 0.801783725737273}} },
  21. { {{-7, 13, 22}}, {{-0.264197974633739, 0.490653381462658, 0.830336491706037}} },
  22. { {{.1, .1, .1}}, {{0.577350269189626, 0.577350269189626, 0.577350269189626}} },
  23. { {{0, 0, 0}}, {{0, 0, 0}} },
  24. };
  25. int sz = sizeof(vec3f) * 2;
  26. float t = 0.001;
  27. for(int i = 0; i < sizeof(v) / sz; i++){
  28. vec3f norm = v[i][0];
  29. ovec3f_normalize_me(&norm);
  30. TAssert(vec3f_eq(norm, v[i][1], t));
  31. }
  32. }
  33. typedef struct {
  34. vec3f vec;
  35. float f;
  36. } vec_float;
  37. void test_ovec3f_get_length()
  38. {
  39. vec_float vf[] = {
  40. { {{0, 0, 0}}, 0},
  41. { {{1, 0, 0}}, 1},
  42. { {{1, 2, 0}}, 2.23606797749979},
  43. { {{1, -2, 0}}, 2.23606797749979},
  44. { {{1, 2, 3}}, 3.7416573867739413},
  45. { {{-1, -2, -3}}, 3.7416573867739413},
  46. };
  47. int sz = sizeof(vec_float);
  48. float t = 0.001;
  49. for(int i = 0; i < sizeof(vf) / sz; i++){
  50. TAssert(float_eq(ovec3f_get_length(&vf[i].vec), vf[i].f, t));
  51. }
  52. }
  53. typedef struct {
  54. vec3f v1, v2;
  55. float f;
  56. } vec2_float;
  57. void test_ovec3f_get_angle()
  58. {
  59. vec2_float vf[] = {
  60. { {{0, 0, 0}}, {{0, 0, 0}}, 0},
  61. { {{1, 0, 0}}, {{0, 0, 0}}, 0},
  62. { {{2, 4, 3}}, {{1, 2, 3}}, 0.33940126397005316},
  63. { {{2, 4, 3}}, {{-1, 2, 3}}, 0.7311043352203973},
  64. };
  65. int sz = sizeof(vec2_float);
  66. float t = 0.001;
  67. for(int i = 0; i < sizeof(vf) / sz; i++){
  68. TAssert(float_eq(ovec3f_get_angle(&vf[i].v1, &vf[i].v2), vf[i].f, t));
  69. }
  70. }
  71. //float ovec3f_get_dot(const vec3f* me, const vec3f* vec);
  72. void test_ovec3f_get_dot()
  73. {
  74. vec2_float vf[] = {
  75. { {{0, 0, 0}}, {{0, 0, 0}}, 0},
  76. { {{1, 2, 3}}, {{-.30, 2, 25}}, 78.7},
  77. { {{-1, -10000000, 3}}, {{-.30, 2, 25}}, -19999924.7},
  78. };
  79. int sz = sizeof(vec2_float);
  80. float t = 0.001;
  81. for(int i = 0; i < sizeof(vf) / sz; i++){
  82. TAssert(float_eq(ovec3f_get_dot(&vf[i].v1, &vf[i].v2), vf[i].f, t));
  83. }
  84. }