vector3d_f_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/geometry/vector3d_f.h"
  5. #include <stddef.h>
  6. #include <cmath>
  7. #include <limits>
  8. #include "build/build_config.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace gfx {
  11. TEST(Vector3dFTest, IsZero) {
  12. gfx::Vector3dF float_zero(0, 0, 0);
  13. gfx::Vector3dF float_nonzero(0.1f, -0.1f, 0.1f);
  14. EXPECT_TRUE(float_zero.IsZero());
  15. EXPECT_FALSE(float_nonzero.IsZero());
  16. }
  17. TEST(Vector3dFTest, Add) {
  18. gfx::Vector3dF f1(3.1f, 5.1f, 2.7f);
  19. gfx::Vector3dF f2(4.3f, -1.3f, 8.1f);
  20. const struct {
  21. gfx::Vector3dF expected;
  22. gfx::Vector3dF actual;
  23. } float_tests[] = {
  24. { gfx::Vector3dF(3.1F, 5.1F, 2.7f), f1 + gfx::Vector3dF() },
  25. { gfx::Vector3dF(3.1f + 4.3f, 5.1f - 1.3f, 2.7f + 8.1f), f1 + f2 },
  26. { gfx::Vector3dF(3.1f - 4.3f, 5.1f + 1.3f, 2.7f - 8.1f), f1 - f2 }
  27. };
  28. for (size_t i = 0; i < std::size(float_tests); ++i)
  29. EXPECT_EQ(float_tests[i].expected.ToString(),
  30. float_tests[i].actual.ToString());
  31. }
  32. TEST(Vector3dFTest, Negative) {
  33. const struct {
  34. gfx::Vector3dF expected;
  35. gfx::Vector3dF actual;
  36. } float_tests[] = {
  37. { gfx::Vector3dF(-0.0f, -0.0f, -0.0f), -gfx::Vector3dF(0, 0, 0) },
  38. { gfx::Vector3dF(-0.3f, -0.3f, -0.3f), -gfx::Vector3dF(0.3f, 0.3f, 0.3f) },
  39. { gfx::Vector3dF(0.3f, 0.3f, 0.3f), -gfx::Vector3dF(-0.3f, -0.3f, -0.3f) },
  40. { gfx::Vector3dF(-0.3f, 0.3f, -0.3f), -gfx::Vector3dF(0.3f, -0.3f, 0.3f) },
  41. { gfx::Vector3dF(0.3f, -0.3f, -0.3f), -gfx::Vector3dF(-0.3f, 0.3f, 0.3f) },
  42. { gfx::Vector3dF(-0.3f, -0.3f, 0.3f), -gfx::Vector3dF(0.3f, 0.3f, -0.3f) }
  43. };
  44. for (size_t i = 0; i < std::size(float_tests); ++i)
  45. EXPECT_EQ(float_tests[i].expected.ToString(),
  46. float_tests[i].actual.ToString());
  47. }
  48. TEST(Vector3dFTest, Scale) {
  49. float triple_values[][6] = {
  50. { 4.5f, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f },
  51. { 4.5f, -1.2f, -1.8f, 3.3f, 5.6f, 4.2f },
  52. { 4.5f, 1.2f, -1.8f, 3.3f, 5.6f, 4.2f },
  53. { 4.5f, -1.2f -1.8f, 3.3f, 5.6f, 4.2f },
  54. { 4.5f, 1.2f, 1.8f, 3.3f, -5.6f, -4.2f },
  55. { 4.5f, 1.2f, 1.8f, -3.3f, -5.6f, -4.2f },
  56. { 4.5f, 1.2f, -1.8f, 3.3f, -5.6f, -4.2f },
  57. { 4.5f, 1.2f, -1.8f, -3.3f, -5.6f, -4.2f },
  58. { -4.5f, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f },
  59. { -4.5f, 1.2f, 1.8f, 0, 5.6f, 4.2f },
  60. { -4.5f, 1.2f, -1.8f, 3.3f, 5.6f, 4.2f },
  61. { -4.5f, 1.2f, -1.8f, 0, 5.6f, 4.2f },
  62. { -4.5f, 1.2f, 1.8f, 3.3f, 0, 4.2f },
  63. { 4.5f, 0, 1.8f, 3.3f, 5.6f, 4.2f },
  64. { -4.5f, 1.2f, -1.8f, 3.3f, 0, 4.2f },
  65. { 4.5f, 0, -1.8f, 3.3f, 5.6f, 4.2f },
  66. { -4.5f, 1.2f, 1.8f, 3.3f, 5.6f, 0 },
  67. { -4.5f, 1.2f, -1.8f, 3.3f, 5.6f, 0 },
  68. { 0, 1.2f, 0, 3.3f, 5.6f, 4.2f },
  69. { 0, 1.2f, 1.8f, 3.3f, 5.6f, 4.2f }
  70. };
  71. for (size_t i = 0; i < std::size(triple_values); ++i) {
  72. gfx::Vector3dF v(triple_values[i][0],
  73. triple_values[i][1],
  74. triple_values[i][2]);
  75. v.Scale(triple_values[i][3], triple_values[i][4], triple_values[i][5]);
  76. EXPECT_EQ(triple_values[i][0] * triple_values[i][3], v.x());
  77. EXPECT_EQ(triple_values[i][1] * triple_values[i][4], v.y());
  78. EXPECT_EQ(triple_values[i][2] * triple_values[i][5], v.z());
  79. Vector3dF v2 = ScaleVector3d(
  80. gfx::Vector3dF(triple_values[i][0],
  81. triple_values[i][1],
  82. triple_values[i][2]),
  83. triple_values[i][3], triple_values[i][4], triple_values[i][5]);
  84. EXPECT_EQ(triple_values[i][0] * triple_values[i][3], v2.x());
  85. EXPECT_EQ(triple_values[i][1] * triple_values[i][4], v2.y());
  86. EXPECT_EQ(triple_values[i][2] * triple_values[i][5], v2.z());
  87. }
  88. float single_values[][4] = {
  89. { 4.5f, 1.2f, 1.8f, 3.3f },
  90. { 4.5f, -1.2f, 1.8f, 3.3f },
  91. { 4.5f, 1.2f, -1.8f, 3.3f },
  92. { 4.5f, -1.2f, -1.8f, 3.3f },
  93. { -4.5f, 1.2f, 3.3f },
  94. { -4.5f, 1.2f, 0 },
  95. { -4.5f, 1.2f, 1.8f, 3.3f },
  96. { -4.5f, 1.2f, 1.8f, 0 },
  97. { 4.5f, 0, 1.8f, 3.3f },
  98. { 0, 1.2f, 1.8f, 3.3f },
  99. { 4.5f, 0, 1.8f, 3.3f },
  100. { 0, 1.2f, 1.8f, 3.3f },
  101. { 4.5f, 1.2f, 0, 3.3f },
  102. { 4.5f, 1.2f, 0, 3.3f }
  103. };
  104. for (size_t i = 0; i < std::size(single_values); ++i) {
  105. gfx::Vector3dF v(single_values[i][0],
  106. single_values[i][1],
  107. single_values[i][2]);
  108. v.Scale(single_values[i][3]);
  109. EXPECT_EQ(single_values[i][0] * single_values[i][3], v.x());
  110. EXPECT_EQ(single_values[i][1] * single_values[i][3], v.y());
  111. EXPECT_EQ(single_values[i][2] * single_values[i][3], v.z());
  112. Vector3dF v2 = ScaleVector3d(
  113. gfx::Vector3dF(single_values[i][0],
  114. single_values[i][1],
  115. single_values[i][2]),
  116. single_values[i][3]);
  117. EXPECT_EQ(single_values[i][0] * single_values[i][3], v2.x());
  118. EXPECT_EQ(single_values[i][1] * single_values[i][3], v2.y());
  119. EXPECT_EQ(single_values[i][2] * single_values[i][3], v2.z());
  120. }
  121. }
  122. TEST(Vector3dFTest, Length) {
  123. float float_values[][3] = {
  124. { 0, 0, 0 },
  125. { 10.5f, 20.5f, 8.5f },
  126. { 20.5f, 10.5f, 8.5f },
  127. { 8.5f, 20.5f, 10.5f },
  128. { 10.5f, 8.5f, 20.5f },
  129. { -10.5f, -20.5f, -8.5f },
  130. { -20.5f, 10.5f, -8.5f },
  131. { -8.5f, -20.5f, -10.5f },
  132. { -10.5f, -8.5f, -20.5f },
  133. { 10.5f, -20.5f, 8.5f },
  134. { -10.5f, 20.5f, 8.5f },
  135. { 10.5f, -20.5f, -8.5f },
  136. { -10.5f, 20.5f, -8.5f },
  137. // A large vector that fails if the Length function doesn't use
  138. // double precision internally.
  139. { 1236278317862780234892374893213178027.12122348904204230f,
  140. 335890352589839028212313231225425134332.38123f,
  141. 27861786423846742743236423478236784678.236713617231f }
  142. };
  143. for (size_t i = 0; i < std::size(float_values); ++i) {
  144. double v0 = float_values[i][0];
  145. double v1 = float_values[i][1];
  146. double v2 = float_values[i][2];
  147. double length_squared =
  148. static_cast<double>(v0) * v0 +
  149. static_cast<double>(v1) * v1 +
  150. static_cast<double>(v2) * v2;
  151. double length = std::sqrt(length_squared);
  152. gfx::Vector3dF vector(v0, v1, v2);
  153. EXPECT_DOUBLE_EQ(length_squared, vector.LengthSquared());
  154. EXPECT_FLOAT_EQ(static_cast<float>(length), vector.Length());
  155. }
  156. }
  157. TEST(Vector3dFTest, DotProduct) {
  158. const struct {
  159. float expected;
  160. gfx::Vector3dF input1;
  161. gfx::Vector3dF input2;
  162. } tests[] = {
  163. { 0, gfx::Vector3dF(1, 0, 0), gfx::Vector3dF(0, 1, 1) },
  164. { 0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(1, 0, 1) },
  165. { 0, gfx::Vector3dF(0, 0, 1), gfx::Vector3dF(1, 1, 0) },
  166. { 3, gfx::Vector3dF(1, 1, 1), gfx::Vector3dF(1, 1, 1) },
  167. { 1.2f, gfx::Vector3dF(1.2f, -1.2f, 1.2f), gfx::Vector3dF(1, 1, 1) },
  168. { 1.2f, gfx::Vector3dF(1, 1, 1), gfx::Vector3dF(1.2f, -1.2f, 1.2f) },
  169. { 38.72f,
  170. gfx::Vector3dF(1.1f, 2.2f, 3.3f), gfx::Vector3dF(4.4f, 5.5f, 6.6f) }
  171. };
  172. for (size_t i = 0; i < std::size(tests); ++i) {
  173. float actual = gfx::DotProduct(tests[i].input1, tests[i].input2);
  174. EXPECT_EQ(tests[i].expected, actual);
  175. }
  176. }
  177. TEST(Vector3dFTest, CrossProduct) {
  178. const struct {
  179. gfx::Vector3dF expected;
  180. gfx::Vector3dF input1;
  181. gfx::Vector3dF input2;
  182. } tests[] = {
  183. { Vector3dF(), Vector3dF(), Vector3dF(1, 1, 1) },
  184. { Vector3dF(), Vector3dF(1, 1, 1), Vector3dF() },
  185. { Vector3dF(), Vector3dF(1, 1, 1), Vector3dF(1, 1, 1) },
  186. { Vector3dF(),
  187. Vector3dF(1.6f, 10.6f, -10.6f),
  188. Vector3dF(1.6f, 10.6f, -10.6f) },
  189. { Vector3dF(1, -1, 0), Vector3dF(1, 1, 1), Vector3dF(0, 0, 1) },
  190. { Vector3dF(-1, 0, 1), Vector3dF(1, 1, 1), Vector3dF(0, 1, 0) },
  191. { Vector3dF(0, 1, -1), Vector3dF(1, 1, 1), Vector3dF(1, 0, 0) },
  192. { Vector3dF(-1, 1, 0), Vector3dF(0, 0, 1), Vector3dF(1, 1, 1) },
  193. { Vector3dF(1, 0, -1), Vector3dF(0, 1, 0), Vector3dF(1, 1, 1) },
  194. { Vector3dF(0, -1, 1), Vector3dF(1, 0, 0), Vector3dF(1, 1, 1) }
  195. };
  196. for (size_t i = 0; i < std::size(tests); ++i) {
  197. SCOPED_TRACE(i);
  198. Vector3dF actual = gfx::CrossProduct(tests[i].input1, tests[i].input2);
  199. EXPECT_EQ(tests[i].expected.ToString(), actual.ToString());
  200. }
  201. }
  202. TEST(Vector3dFTest, ClampVector3dF) {
  203. Vector3dF a;
  204. a = Vector3dF(3.5f, 5.5f, 7.5f);
  205. EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
  206. a.SetToMax(Vector3dF(2, 4.5f, 6.5f));
  207. EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
  208. a.SetToMax(Vector3dF(3.5f, 5.5f, 7.5f));
  209. EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
  210. a.SetToMax(Vector3dF(4.5f, 2, 6.5f));
  211. EXPECT_EQ(Vector3dF(4.5f, 5.5f, 7.5f).ToString(), a.ToString());
  212. a.SetToMax(Vector3dF(3.5f, 6.5f, 6.5f));
  213. EXPECT_EQ(Vector3dF(4.5f, 6.5f, 7.5f).ToString(), a.ToString());
  214. a.SetToMax(Vector3dF(3.5f, 5.5f, 8.5f));
  215. EXPECT_EQ(Vector3dF(4.5f, 6.5f, 8.5f).ToString(), a.ToString());
  216. a.SetToMax(Vector3dF(8.5f, 10.5f, 12.5f));
  217. EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
  218. a.SetToMin(Vector3dF(9.5f, 11.5f, 13.5f));
  219. EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
  220. a.SetToMin(Vector3dF(8.5f, 10.5f, 12.5f));
  221. EXPECT_EQ(Vector3dF(8.5f, 10.5f, 12.5f).ToString(), a.ToString());
  222. a.SetToMin(Vector3dF(7.5f, 11.5f, 13.5f));
  223. EXPECT_EQ(Vector3dF(7.5f, 10.5f, 12.5f).ToString(), a.ToString());
  224. a.SetToMin(Vector3dF(9.5f, 9.5f, 13.5f));
  225. EXPECT_EQ(Vector3dF(7.5f, 9.5f, 12.5f).ToString(), a.ToString());
  226. a.SetToMin(Vector3dF(9.5f, 11.5f, 11.5f));
  227. EXPECT_EQ(Vector3dF(7.5f, 9.5f, 11.5f).ToString(), a.ToString());
  228. a.SetToMin(Vector3dF(3.5f, 5.5f, 7.5f));
  229. EXPECT_EQ(Vector3dF(3.5f, 5.5f, 7.5f).ToString(), a.ToString());
  230. }
  231. TEST(Vector3dFTest, AngleBetweenVectorsInDegress) {
  232. const struct {
  233. float expected;
  234. gfx::Vector3dF input1;
  235. gfx::Vector3dF input2;
  236. } tests[] = {{0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 1, 0)},
  237. {90, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 0, 1)},
  238. {45, gfx::Vector3dF(0, 1, 0),
  239. gfx::Vector3dF(0, 0.70710678188f, 0.70710678188f)},
  240. {180, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, -1, 0)},
  241. // Two vectors that are sufficiently close enough together to
  242. // trigger an issue that produces NANs if the value passed to
  243. // acos is not clamped due to floating point precision.
  244. {0, gfx::Vector3dF(0, -0.990842f, -0.003177f),
  245. gfx::Vector3dF(0, -0.999995f, -0.003124f)}};
  246. for (size_t i = 0; i < std::size(tests); ++i) {
  247. float actual =
  248. gfx::AngleBetweenVectorsInDegrees(tests[i].input1, tests[i].input2);
  249. EXPECT_FLOAT_EQ(tests[i].expected, actual);
  250. actual =
  251. gfx::AngleBetweenVectorsInDegrees(tests[i].input2, tests[i].input1);
  252. EXPECT_FLOAT_EQ(tests[i].expected, actual);
  253. }
  254. }
  255. TEST(Vector3dFTest, ClockwiseAngleBetweenVectorsInDegress) {
  256. const struct {
  257. float expected;
  258. gfx::Vector3dF input1;
  259. gfx::Vector3dF input2;
  260. } tests[] = {
  261. {0, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 1, 0)},
  262. {90, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 0, -1)},
  263. {45,
  264. gfx::Vector3dF(0, -1, 0),
  265. gfx::Vector3dF(0, -0.70710678188f, 0.70710678188f)},
  266. {180, gfx::Vector3dF(0, -1, 0), gfx::Vector3dF(0, 1, 0)},
  267. {270, gfx::Vector3dF(0, 1, 0), gfx::Vector3dF(0, 0, 1)},
  268. };
  269. const gfx::Vector3dF normal_vector(1.0f, 0.0f, 0.0f);
  270. for (size_t i = 0; i < std::size(tests); ++i) {
  271. float actual = gfx::ClockwiseAngleBetweenVectorsInDegrees(
  272. tests[i].input1, tests[i].input2, normal_vector);
  273. EXPECT_FLOAT_EQ(tests[i].expected, actual);
  274. actual = -gfx::ClockwiseAngleBetweenVectorsInDegrees(
  275. tests[i].input2, tests[i].input1, normal_vector);
  276. if (actual < 0.0f)
  277. actual += 360.0f;
  278. EXPECT_FLOAT_EQ(tests[i].expected, actual);
  279. }
  280. }
  281. TEST(Vector3dFTest, GetNormalized) {
  282. const struct {
  283. bool expected;
  284. gfx::Vector3dF v;
  285. gfx::Vector3dF normalized;
  286. } tests[] = {
  287. {false, gfx::Vector3dF(0, 0, 0), gfx::Vector3dF(0, 0, 0)},
  288. {false,
  289. gfx::Vector3dF(std::numeric_limits<float>::min(),
  290. std::numeric_limits<float>::min(),
  291. std::numeric_limits<float>::min()),
  292. gfx::Vector3dF(std::numeric_limits<float>::min(),
  293. std::numeric_limits<float>::min(),
  294. std::numeric_limits<float>::min())},
  295. {true, gfx::Vector3dF(1, 0, 0), gfx::Vector3dF(1, 0, 0)},
  296. {true, gfx::Vector3dF(std::numeric_limits<float>::max(), 0, 0),
  297. gfx::Vector3dF(1, 0, 0)},
  298. };
  299. for (size_t i = 0; i < std::size(tests); ++i) {
  300. gfx::Vector3dF n;
  301. EXPECT_EQ(tests[i].expected, tests[i].v.GetNormalized(&n));
  302. EXPECT_EQ(tests[i].normalized.ToString(), n.ToString());
  303. }
  304. }
  305. TEST(Vector3dFTest, ToString) {
  306. EXPECT_EQ("[1.03125 2.5 -3]", Vector3dF(1.03125, 2.5, -3).ToString());
  307. }
  308. } // namespace gfx