vector_spec.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. _G.vector = {}
  2. dofile("builtin/common/vector.lua")
  3. describe("vector", function()
  4. describe("new()", function()
  5. it("constructs", function()
  6. assert.same({x = 0, y = 0, z = 0}, vector.new())
  7. assert.same({x = 1, y = 2, z = 3}, vector.new(1, 2, 3))
  8. assert.same({x = 3, y = 2, z = 1}, vector.new({x = 3, y = 2, z = 1}))
  9. assert.is_true(vector.check(vector.new()))
  10. assert.is_true(vector.check(vector.new(1, 2, 3)))
  11. assert.is_true(vector.check(vector.new({x = 3, y = 2, z = 1})))
  12. local input = vector.new({ x = 3, y = 2, z = 1 })
  13. local output = vector.new(input)
  14. assert.same(input, output)
  15. assert.equal(input, output)
  16. assert.is_false(rawequal(input, output))
  17. assert.equal(input, input:new())
  18. end)
  19. it("throws on invalid input", function()
  20. assert.has.errors(function()
  21. vector.new({ x = 3 })
  22. end)
  23. assert.has.errors(function()
  24. vector.new({ d = 3 })
  25. end)
  26. end)
  27. end)
  28. it("zero()", function()
  29. assert.same({x = 0, y = 0, z = 0}, vector.zero())
  30. assert.same(vector.new(), vector.zero())
  31. assert.equal(vector.new(), vector.zero())
  32. assert.is_true(vector.check(vector.zero()))
  33. end)
  34. it("copy()", function()
  35. local v = vector.new(1, 2, 3)
  36. assert.same(v, vector.copy(v))
  37. assert.same(vector.new(v), vector.copy(v))
  38. assert.equal(vector.new(v), vector.copy(v))
  39. assert.is_true(vector.check(vector.copy(v)))
  40. end)
  41. it("indexes", function()
  42. local some_vector = vector.new(24, 42, 13)
  43. assert.equal(24, some_vector[1])
  44. assert.equal(24, some_vector.x)
  45. assert.equal(42, some_vector[2])
  46. assert.equal(42, some_vector.y)
  47. assert.equal(13, some_vector[3])
  48. assert.equal(13, some_vector.z)
  49. some_vector[1] = 100
  50. assert.equal(100, some_vector.x)
  51. some_vector.x = 101
  52. assert.equal(101, some_vector[1])
  53. some_vector[2] = 100
  54. assert.equal(100, some_vector.y)
  55. some_vector.y = 102
  56. assert.equal(102, some_vector[2])
  57. some_vector[3] = 100
  58. assert.equal(100, some_vector.z)
  59. some_vector.z = 103
  60. assert.equal(103, some_vector[3])
  61. end)
  62. it("direction()", function()
  63. local a = vector.new(1, 0, 0)
  64. local b = vector.new(1, 42, 0)
  65. assert.equal(vector.new(0, 1, 0), vector.direction(a, b))
  66. assert.equal(vector.new(0, 1, 0), a:direction(b))
  67. end)
  68. it("distance()", function()
  69. local a = vector.new(1, 0, 0)
  70. local b = vector.new(3, 42, 9)
  71. assert.is_true(math.abs(43 - vector.distance(a, b)) < 1.0e-12)
  72. assert.is_true(math.abs(43 - a:distance(b)) < 1.0e-12)
  73. assert.equal(0, vector.distance(a, a))
  74. assert.equal(0, b:distance(b))
  75. end)
  76. it("length()", function()
  77. local a = vector.new(0, 0, -23)
  78. assert.equal(0, vector.length(vector.new()))
  79. assert.equal(23, vector.length(a))
  80. assert.equal(23, a:length())
  81. end)
  82. it("normalize()", function()
  83. local a = vector.new(0, 0, -23)
  84. assert.equal(vector.new(0, 0, -1), vector.normalize(a))
  85. assert.equal(vector.new(0, 0, -1), a:normalize())
  86. assert.equal(vector.new(), vector.normalize(vector.new()))
  87. end)
  88. it("floor()", function()
  89. local a = vector.new(0.1, 0.9, -0.5)
  90. assert.equal(vector.new(0, 0, -1), vector.floor(a))
  91. assert.equal(vector.new(0, 0, -1), a:floor())
  92. end)
  93. it("round()", function()
  94. local a = vector.new(0.1, 0.9, -0.5)
  95. assert.equal(vector.new(0, 1, -1), vector.round(a))
  96. assert.equal(vector.new(0, 1, -1), a:round())
  97. end)
  98. it("apply()", function()
  99. local i = 0
  100. local f = function(x)
  101. i = i + 1
  102. return x + i
  103. end
  104. local a = vector.new(0.1, 0.9, -0.5)
  105. assert.equal(vector.new(1, 1, 0), vector.apply(a, math.ceil))
  106. assert.equal(vector.new(1, 1, 0), a:apply(math.ceil))
  107. assert.equal(vector.new(0.1, 0.9, 0.5), vector.apply(a, math.abs))
  108. assert.equal(vector.new(0.1, 0.9, 0.5), a:apply(math.abs))
  109. assert.equal(vector.new(1.1, 2.9, 2.5), vector.apply(a, f))
  110. assert.equal(vector.new(4.1, 5.9, 5.5), a:apply(f))
  111. end)
  112. it("equals()", function()
  113. local function assertE(a, b)
  114. assert.is_true(vector.equals(a, b))
  115. end
  116. local function assertNE(a, b)
  117. assert.is_false(vector.equals(a, b))
  118. end
  119. assertE({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
  120. assertE({x = -1, y = 0, z = 1}, {x = -1, y = 0, z = 1})
  121. assertE({x = -1, y = 0, z = 1}, vector.new(-1, 0, 1))
  122. local a = {x = 2, y = 4, z = -10}
  123. assertE(a, a)
  124. assertNE({x = -1, y = 0, z = 1}, a)
  125. assert.equal(vector.new(1, 2, 3), vector.new(1, 2, 3))
  126. assert.is_true(vector.new(1, 2, 3):equals(vector.new(1, 2, 3)))
  127. assert.not_equal(vector.new(1, 2, 3), vector.new(1, 2, 4))
  128. assert.is_true(vector.new(1, 2, 3) == vector.new(1, 2, 3))
  129. assert.is_false(vector.new(1, 2, 3) == vector.new(1, 3, 3))
  130. end)
  131. it("metatable is same", function()
  132. local a = vector.new()
  133. local b = vector.new(1, 2, 3)
  134. assert.equal(true, vector.check(a))
  135. assert.equal(true, vector.check(b))
  136. assert.equal(vector.metatable, getmetatable(a))
  137. assert.equal(vector.metatable, getmetatable(b))
  138. assert.equal(vector.metatable, a.metatable)
  139. end)
  140. it("sort()", function()
  141. local a = vector.new(1, 2, 3)
  142. local b = vector.new(0.5, 232, -2)
  143. local sorted = {vector.new(0.5, 2, -2), vector.new(1, 232, 3)}
  144. assert.same(sorted, {vector.sort(a, b)})
  145. assert.same(sorted, {a:sort(b)})
  146. end)
  147. it("angle()", function()
  148. assert.equal(math.pi, vector.angle(vector.new(-1, -2, -3), vector.new(1, 2, 3)))
  149. assert.equal(math.pi/2, vector.new(0, 1, 0):angle(vector.new(1, 0, 0)))
  150. end)
  151. it("dot()", function()
  152. assert.equal(-14, vector.dot(vector.new(-1, -2, -3), vector.new(1, 2, 3)))
  153. assert.equal(0, vector.new():dot(vector.new(1, 2, 3)))
  154. end)
  155. it("cross()", function()
  156. local a = vector.new(-1, -2, 0)
  157. local b = vector.new(1, 2, 3)
  158. assert.equal(vector.new(-6, 3, 0), vector.cross(a, b))
  159. assert.equal(vector.new(-6, 3, 0), a:cross(b))
  160. end)
  161. it("offset()", function()
  162. assert.same({x = 41, y = 52, z = 63}, vector.offset(vector.new(1, 2, 3), 40, 50, 60))
  163. assert.equal(vector.new(41, 52, 63), vector.offset(vector.new(1, 2, 3), 40, 50, 60))
  164. assert.equal(vector.new(41, 52, 63), vector.new(1, 2, 3):offset(40, 50, 60))
  165. end)
  166. it("is()", function()
  167. local some_table1 = {foo = 13, [42] = 1, "bar", 2}
  168. local some_table2 = {1, 2, 3}
  169. local some_table3 = {x = 1, 2, 3}
  170. local some_table4 = {1, 2, z = 3}
  171. local old = {x = 1, y = 2, z = 3}
  172. local real = vector.new(1, 2, 3)
  173. assert.is_false(vector.check(nil))
  174. assert.is_false(vector.check(1))
  175. assert.is_false(vector.check(true))
  176. assert.is_false(vector.check("foo"))
  177. assert.is_false(vector.check(some_table1))
  178. assert.is_false(vector.check(some_table2))
  179. assert.is_false(vector.check(some_table3))
  180. assert.is_false(vector.check(some_table4))
  181. assert.is_false(vector.check(old))
  182. assert.is_true(vector.check(real))
  183. assert.is_true(real:check())
  184. end)
  185. it("global pairs", function()
  186. local out = {}
  187. local vec = vector.new(10, 20, 30)
  188. for k, v in pairs(vec) do
  189. out[k] = v
  190. end
  191. assert.same({x = 10, y = 20, z = 30}, out)
  192. end)
  193. it("abusing works", function()
  194. local v = vector.new(1, 2, 3)
  195. v.a = 1
  196. assert.equal(1, v.a)
  197. local a_is_there = false
  198. for key, value in pairs(v) do
  199. if key == "a" then
  200. a_is_there = true
  201. assert.equal(value, 1)
  202. break
  203. end
  204. end
  205. assert.is_true(a_is_there)
  206. end)
  207. it("add()", function()
  208. local a = vector.new(1, 2, 3)
  209. local b = vector.new(1, 4, 3)
  210. local c = vector.new(2, 6, 6)
  211. assert.equal(c, vector.add(a, {x = 1, y = 4, z = 3}))
  212. assert.equal(c, vector.add(a, b))
  213. assert.equal(c, a:add(b))
  214. assert.equal(c, a + b)
  215. assert.equal(c, b + a)
  216. end)
  217. it("subtract()", function()
  218. local a = vector.new(1, 2, 3)
  219. local b = vector.new(2, 4, 3)
  220. local c = vector.new(-1, -2, 0)
  221. assert.equal(c, vector.subtract(a, {x = 2, y = 4, z = 3}))
  222. assert.equal(c, vector.subtract(a, b))
  223. assert.equal(c, a:subtract(b))
  224. assert.equal(c, a - b)
  225. assert.equal(c, -b + a)
  226. end)
  227. it("multiply()", function()
  228. local a = vector.new(1, 2, 3)
  229. local b = vector.new(2, 4, 3)
  230. local c = vector.new(2, 8, 9)
  231. local s = 2
  232. local d = vector.new(2, 4, 6)
  233. assert.equal(c, vector.multiply(a, {x = 2, y = 4, z = 3}))
  234. assert.equal(c, vector.multiply(a, b))
  235. assert.equal(d, vector.multiply(a, s))
  236. assert.equal(d, a:multiply(s))
  237. assert.equal(d, a * s)
  238. assert.equal(d, s * a)
  239. assert.equal(-a, -1 * a)
  240. end)
  241. it("divide()", function()
  242. local a = vector.new(1, 2, 3)
  243. local b = vector.new(2, 4, 3)
  244. local c = vector.new(0.5, 0.5, 1)
  245. local s = 2
  246. local d = vector.new(0.5, 1, 1.5)
  247. assert.equal(c, vector.divide(a, {x = 2, y = 4, z = 3}))
  248. assert.equal(c, vector.divide(a, b))
  249. assert.equal(d, vector.divide(a, s))
  250. assert.equal(d, a:divide(s))
  251. assert.equal(d, a / s)
  252. assert.equal(d, 1/s * a)
  253. assert.equal(-a, a / -1)
  254. end)
  255. it("to_string()", function()
  256. local v = vector.new(1, 2, 3.14)
  257. assert.same("(1, 2, 3.14)", vector.to_string(v))
  258. assert.same("(1, 2, 3.14)", v:to_string())
  259. assert.same("(1, 2, 3.14)", tostring(v))
  260. end)
  261. it("from_string()", function()
  262. local v = vector.new(1, 2, 3.14)
  263. assert.same({v, 13}, {vector.from_string("(1, 2, 3.14)")})
  264. assert.same({v, 12}, {vector.from_string("(1,2 ,3.14)")})
  265. assert.same({v, 12}, {vector.from_string("(1,2,3.14,)")})
  266. assert.same({v, 11}, {vector.from_string("(1 2 3.14)")})
  267. assert.same({v, 15}, {vector.from_string("( 1, 2, 3.14 )")})
  268. assert.same({v, 15}, {vector.from_string(" ( 1, 2, 3.14) ")})
  269. assert.same({vector.new(), 8}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ")})
  270. assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 8)})
  271. assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 9)})
  272. assert.same(nil, vector.from_string("nothing"))
  273. end)
  274. -- This function is needed because of floating point imprecision.
  275. local function almost_equal(a, b)
  276. if type(a) == "number" then
  277. return math.abs(a - b) < 0.00000000001
  278. end
  279. return vector.distance(a, b) < 0.000000000001
  280. end
  281. describe("rotate_around_axis()", function()
  282. it("rotates", function()
  283. assert.True(almost_equal({x = -1, y = 0, z = 0},
  284. vector.rotate_around_axis({x = 1, y = 0, z = 0}, {x = 0, y = 1, z = 0}, math.pi)))
  285. assert.True(almost_equal({x = 0, y = 1, z = 0},
  286. vector.rotate_around_axis({x = 0, y = 0, z = 1}, {x = 1, y = 0, z = 0}, math.pi / 2)))
  287. assert.True(almost_equal({x = 4, y = 1, z = 1},
  288. vector.rotate_around_axis({x = 4, y = 1, z = 1}, {x = 4, y = 1, z = 1}, math.pi / 6)))
  289. end)
  290. it("keeps distance to axis", function()
  291. local rotate1 = {x = 1, y = 3, z = 1}
  292. local axis1 = {x = 1, y = 3, z = 2}
  293. local rotated1 = vector.rotate_around_axis(rotate1, axis1, math.pi / 13)
  294. assert.True(almost_equal(vector.distance(axis1, rotate1), vector.distance(axis1, rotated1)))
  295. local rotate2 = {x = 1, y = 1, z = 3}
  296. local axis2 = {x = 2, y = 6, z = 100}
  297. local rotated2 = vector.rotate_around_axis(rotate2, axis2, math.pi / 23)
  298. assert.True(almost_equal(vector.distance(axis2, rotate2), vector.distance(axis2, rotated2)))
  299. local rotate3 = {x = 1, y = -1, z = 3}
  300. local axis3 = {x = 2, y = 6, z = 100}
  301. local rotated3 = vector.rotate_around_axis(rotate3, axis3, math.pi / 2)
  302. assert.True(almost_equal(vector.distance(axis3, rotate3), vector.distance(axis3, rotated3)))
  303. end)
  304. it("rotates back", function()
  305. local rotate1 = {x = 1, y = 3, z = 1}
  306. local axis1 = {x = 1, y = 3, z = 2}
  307. local rotated1 = vector.rotate_around_axis(rotate1, axis1, math.pi / 13)
  308. rotated1 = vector.rotate_around_axis(rotated1, axis1, -math.pi / 13)
  309. assert.True(almost_equal(rotate1, rotated1))
  310. local rotate2 = {x = 1, y = 1, z = 3}
  311. local axis2 = {x = 2, y = 6, z = 100}
  312. local rotated2 = vector.rotate_around_axis(rotate2, axis2, math.pi / 23)
  313. rotated2 = vector.rotate_around_axis(rotated2, axis2, -math.pi / 23)
  314. assert.True(almost_equal(rotate2, rotated2))
  315. local rotate3 = {x = 1, y = -1, z = 3}
  316. local axis3 = {x = 2, y = 6, z = 100}
  317. local rotated3 = vector.rotate_around_axis(rotate3, axis3, math.pi / 2)
  318. rotated3 = vector.rotate_around_axis(rotated3, axis3, -math.pi / 2)
  319. assert.True(almost_equal(rotate3, rotated3))
  320. end)
  321. it("is right handed", function()
  322. local v_before1 = {x = 0, y = 1, z = -1}
  323. local v_after1 = vector.rotate_around_axis(v_before1, {x = 1, y = 0, z = 0}, math.pi / 4)
  324. assert.True(almost_equal(vector.normalize(vector.cross(v_after1, v_before1)), {x = 1, y = 0, z = 0}))
  325. local v_before2 = {x = 0, y = 3, z = 4}
  326. local v_after2 = vector.rotate_around_axis(v_before2, {x = 1, y = 0, z = 0}, 2 * math.pi / 5)
  327. assert.True(almost_equal(vector.normalize(vector.cross(v_after2, v_before2)), {x = 1, y = 0, z = 0}))
  328. local v_before3 = {x = 1, y = 0, z = -1}
  329. local v_after3 = vector.rotate_around_axis(v_before3, {x = 0, y = 1, z = 0}, math.pi / 4)
  330. assert.True(almost_equal(vector.normalize(vector.cross(v_after3, v_before3)), {x = 0, y = 1, z = 0}))
  331. local v_before4 = {x = 3, y = 0, z = 4}
  332. local v_after4 = vector.rotate_around_axis(v_before4, {x = 0, y = 1, z = 0}, 2 * math.pi / 5)
  333. assert.True(almost_equal(vector.normalize(vector.cross(v_after4, v_before4)), {x = 0, y = 1, z = 0}))
  334. local v_before5 = {x = 1, y = -1, z = 0}
  335. local v_after5 = vector.rotate_around_axis(v_before5, {x = 0, y = 0, z = 1}, math.pi / 4)
  336. assert.True(almost_equal(vector.normalize(vector.cross(v_after5, v_before5)), {x = 0, y = 0, z = 1}))
  337. local v_before6 = {x = 3, y = 4, z = 0}
  338. local v_after6 = vector.rotate_around_axis(v_before6, {x = 0, y = 0, z = 1}, 2 * math.pi / 5)
  339. assert.True(almost_equal(vector.normalize(vector.cross(v_after6, v_before6)), {x = 0, y = 0, z = 1}))
  340. end)
  341. end)
  342. describe("rotate()", function()
  343. it("rotates", function()
  344. assert.True(almost_equal({x = -1, y = 0, z = 0},
  345. vector.rotate({x = 1, y = 0, z = 0}, {x = 0, y = math.pi, z = 0})))
  346. assert.True(almost_equal({x = 0, y = -1, z = 0},
  347. vector.rotate({x = 1, y = 0, z = 0}, {x = 0, y = 0, z = math.pi / 2})))
  348. assert.True(almost_equal({x = 1, y = 0, z = 0},
  349. vector.rotate({x = 1, y = 0, z = 0}, {x = math.pi / 123, y = 0, z = 0})))
  350. end)
  351. it("is counterclockwise", function()
  352. local v_before1 = {x = 0, y = 1, z = -1}
  353. local v_after1 = vector.rotate(v_before1, {x = math.pi / 4, y = 0, z = 0})
  354. assert.True(almost_equal(vector.normalize(vector.cross(v_after1, v_before1)), {x = 1, y = 0, z = 0}))
  355. local v_before2 = {x = 0, y = 3, z = 4}
  356. local v_after2 = vector.rotate(v_before2, {x = 2 * math.pi / 5, y = 0, z = 0})
  357. assert.True(almost_equal(vector.normalize(vector.cross(v_after2, v_before2)), {x = 1, y = 0, z = 0}))
  358. local v_before3 = {x = 1, y = 0, z = -1}
  359. local v_after3 = vector.rotate(v_before3, {x = 0, y = math.pi / 4, z = 0})
  360. assert.True(almost_equal(vector.normalize(vector.cross(v_after3, v_before3)), {x = 0, y = 1, z = 0}))
  361. local v_before4 = {x = 3, y = 0, z = 4}
  362. local v_after4 = vector.rotate(v_before4, {x = 0, y = 2 * math.pi / 5, z = 0})
  363. assert.True(almost_equal(vector.normalize(vector.cross(v_after4, v_before4)), {x = 0, y = 1, z = 0}))
  364. local v_before5 = {x = 1, y = -1, z = 0}
  365. local v_after5 = vector.rotate(v_before5, {x = 0, y = 0, z = math.pi / 4})
  366. assert.True(almost_equal(vector.normalize(vector.cross(v_after5, v_before5)), {x = 0, y = 0, z = 1}))
  367. local v_before6 = {x = 3, y = 4, z = 0}
  368. local v_after6 = vector.rotate(v_before6, {x = 0, y = 0, z = 2 * math.pi / 5})
  369. assert.True(almost_equal(vector.normalize(vector.cross(v_after6, v_before6)), {x = 0, y = 0, z = 1}))
  370. end)
  371. end)
  372. it("dir_to_rotation()", function()
  373. -- Comparing rotations (pitch, yaw, roll) is hard because of certain ambiguities,
  374. -- e.g. (pi, 0, pi) looks exactly the same as (0, pi, 0)
  375. -- So instead we convert the rotation back to vectors and compare these.
  376. local function forward_at_rot(rot)
  377. return vector.rotate(vector.new(0, 0, 1), rot)
  378. end
  379. local function up_at_rot(rot)
  380. return vector.rotate(vector.new(0, 1, 0), rot)
  381. end
  382. local rot1 = vector.dir_to_rotation({x = 1, y = 0, z = 0}, {x = 0, y = 1, z = 0})
  383. assert.True(almost_equal({x = 1, y = 0, z = 0}, forward_at_rot(rot1)))
  384. assert.True(almost_equal({x = 0, y = 1, z = 0}, up_at_rot(rot1)))
  385. local rot2 = vector.dir_to_rotation({x = 1, y = 1, z = 0}, {x = 0, y = 0, z = 1})
  386. assert.True(almost_equal({x = 1/math.sqrt(2), y = 1/math.sqrt(2), z = 0}, forward_at_rot(rot2)))
  387. assert.True(almost_equal({x = 0, y = 0, z = 1}, up_at_rot(rot2)))
  388. for i = 1, 1000 do
  389. local rand_vec = vector.new(math.random(), math.random(), math.random())
  390. if vector.length(rand_vec) ~= 0 then
  391. local rot_1 = vector.dir_to_rotation(rand_vec)
  392. local rot_2 = {
  393. x = math.atan2(rand_vec.y, math.sqrt(rand_vec.z * rand_vec.z + rand_vec.x * rand_vec.x)),
  394. y = -math.atan2(rand_vec.x, rand_vec.z),
  395. z = 0
  396. }
  397. assert.True(almost_equal(rot_1, rot_2))
  398. end
  399. end
  400. end)
  401. end)