vector.lua 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. --[[
  2. Vector helpers
  3. Note: The vector.*-functions must be able to accept old vectors that had no metatables
  4. ]]
  5. -- localize functions
  6. local setmetatable = setmetatable
  7. -- vector.metatable is set by C++.
  8. local metatable = vector.metatable
  9. local xyz = {"x", "y", "z"}
  10. -- only called when rawget(v, key) returns nil
  11. function metatable.__index(v, key)
  12. return rawget(v, xyz[key]) or vector[key]
  13. end
  14. -- only called when rawget(v, key) returns nil
  15. function metatable.__newindex(v, key, value)
  16. rawset(v, xyz[key] or key, value)
  17. end
  18. -- constructors
  19. local function fast_new(x, y, z)
  20. return setmetatable({x = x, y = y, z = z}, metatable)
  21. end
  22. function vector.new(a, b, c)
  23. if a and b and c then
  24. return fast_new(a, b, c)
  25. end
  26. -- deprecated, use vector.copy and vector.zero directly
  27. if type(a) == "table" then
  28. return vector.copy(a)
  29. else
  30. assert(not a, "Invalid arguments for vector.new()")
  31. return vector.zero()
  32. end
  33. end
  34. function vector.zero()
  35. return fast_new(0, 0, 0)
  36. end
  37. function vector.copy(v)
  38. assert(v.x and v.y and v.z, "Invalid vector passed to vector.copy()")
  39. return fast_new(v.x, v.y, v.z)
  40. end
  41. function vector.from_string(s, init)
  42. local x, y, z, np = string.match(s, "^%s*%(%s*([^%s,]+)%s*[,%s]%s*([^%s,]+)%s*[,%s]" ..
  43. "%s*([^%s,]+)%s*[,%s]?%s*%)()", init)
  44. x = tonumber(x)
  45. y = tonumber(y)
  46. z = tonumber(z)
  47. if not (x and y and z) then
  48. return nil
  49. end
  50. return fast_new(x, y, z), np
  51. end
  52. function vector.to_string(v)
  53. return string.format("(%g, %g, %g)", v.x, v.y, v.z)
  54. end
  55. metatable.__tostring = vector.to_string
  56. function vector.equals(a, b)
  57. return a.x == b.x and
  58. a.y == b.y and
  59. a.z == b.z
  60. end
  61. metatable.__eq = vector.equals
  62. -- unary operations
  63. function vector.length(v)
  64. return math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z)
  65. end
  66. -- Note: we can not use __len because it is already used for primitive table length
  67. function vector.normalize(v)
  68. local len = vector.length(v)
  69. if len == 0 then
  70. return fast_new(0, 0, 0)
  71. else
  72. return vector.divide(v, len)
  73. end
  74. end
  75. function vector.floor(v)
  76. return vector.apply(v, math.floor)
  77. end
  78. function vector.round(v)
  79. return fast_new(
  80. math.round(v.x),
  81. math.round(v.y),
  82. math.round(v.z)
  83. )
  84. end
  85. function vector.apply(v, func)
  86. return fast_new(
  87. func(v.x),
  88. func(v.y),
  89. func(v.z)
  90. )
  91. end
  92. function vector.distance(a, b)
  93. local x = a.x - b.x
  94. local y = a.y - b.y
  95. local z = a.z - b.z
  96. return math.sqrt(x * x + y * y + z * z)
  97. end
  98. function vector.direction(pos1, pos2)
  99. return vector.subtract(pos2, pos1):normalize()
  100. end
  101. function vector.angle(a, b)
  102. local dotp = vector.dot(a, b)
  103. local cp = vector.cross(a, b)
  104. local crossplen = vector.length(cp)
  105. return math.atan2(crossplen, dotp)
  106. end
  107. function vector.dot(a, b)
  108. return a.x * b.x + a.y * b.y + a.z * b.z
  109. end
  110. function vector.cross(a, b)
  111. return fast_new(
  112. a.y * b.z - a.z * b.y,
  113. a.z * b.x - a.x * b.z,
  114. a.x * b.y - a.y * b.x
  115. )
  116. end
  117. function metatable.__unm(v)
  118. return fast_new(-v.x, -v.y, -v.z)
  119. end
  120. -- add, sub, mul, div operations
  121. function vector.add(a, b)
  122. if type(b) == "table" then
  123. return fast_new(
  124. a.x + b.x,
  125. a.y + b.y,
  126. a.z + b.z
  127. )
  128. else
  129. return fast_new(
  130. a.x + b,
  131. a.y + b,
  132. a.z + b
  133. )
  134. end
  135. end
  136. function metatable.__add(a, b)
  137. return fast_new(
  138. a.x + b.x,
  139. a.y + b.y,
  140. a.z + b.z
  141. )
  142. end
  143. function vector.subtract(a, b)
  144. if type(b) == "table" then
  145. return fast_new(
  146. a.x - b.x,
  147. a.y - b.y,
  148. a.z - b.z
  149. )
  150. else
  151. return fast_new(
  152. a.x - b,
  153. a.y - b,
  154. a.z - b
  155. )
  156. end
  157. end
  158. function metatable.__sub(a, b)
  159. return fast_new(
  160. a.x - b.x,
  161. a.y - b.y,
  162. a.z - b.z
  163. )
  164. end
  165. function vector.multiply(a, b)
  166. if type(b) == "table" then
  167. return fast_new(
  168. a.x * b.x,
  169. a.y * b.y,
  170. a.z * b.z
  171. )
  172. else
  173. return fast_new(
  174. a.x * b,
  175. a.y * b,
  176. a.z * b
  177. )
  178. end
  179. end
  180. function metatable.__mul(a, b)
  181. if type(a) == "table" then
  182. return fast_new(
  183. a.x * b,
  184. a.y * b,
  185. a.z * b
  186. )
  187. else
  188. return fast_new(
  189. a * b.x,
  190. a * b.y,
  191. a * b.z
  192. )
  193. end
  194. end
  195. function vector.divide(a, b)
  196. if type(b) == "table" then
  197. return fast_new(
  198. a.x / b.x,
  199. a.y / b.y,
  200. a.z / b.z
  201. )
  202. else
  203. return fast_new(
  204. a.x / b,
  205. a.y / b,
  206. a.z / b
  207. )
  208. end
  209. end
  210. function metatable.__div(a, b)
  211. -- scalar/vector makes no sense
  212. return fast_new(
  213. a.x / b,
  214. a.y / b,
  215. a.z / b
  216. )
  217. end
  218. -- misc stuff
  219. function vector.offset(v, x, y, z)
  220. return fast_new(
  221. v.x + x,
  222. v.y + y,
  223. v.z + z
  224. )
  225. end
  226. function vector.sort(a, b)
  227. return fast_new(math.min(a.x, b.x), math.min(a.y, b.y), math.min(a.z, b.z)),
  228. fast_new(math.max(a.x, b.x), math.max(a.y, b.y), math.max(a.z, b.z))
  229. end
  230. function vector.check(v)
  231. return getmetatable(v) == metatable
  232. end
  233. local function sin(x)
  234. if x % math.pi == 0 then
  235. return 0
  236. else
  237. return math.sin(x)
  238. end
  239. end
  240. local function cos(x)
  241. if x % math.pi == math.pi / 2 then
  242. return 0
  243. else
  244. return math.cos(x)
  245. end
  246. end
  247. function vector.rotate_around_axis(v, axis, angle)
  248. local cosangle = cos(angle)
  249. local sinangle = sin(angle)
  250. axis = vector.normalize(axis)
  251. -- https://en.wikipedia.org/wiki/Rodrigues%27_rotation_formula
  252. local dot_axis = vector.multiply(axis, vector.dot(axis, v))
  253. local cross = vector.cross(v, axis)
  254. return vector.new(
  255. cross.x * sinangle + (v.x - dot_axis.x) * cosangle + dot_axis.x,
  256. cross.y * sinangle + (v.y - dot_axis.y) * cosangle + dot_axis.y,
  257. cross.z * sinangle + (v.z - dot_axis.z) * cosangle + dot_axis.z
  258. )
  259. end
  260. function vector.rotate(v, rot)
  261. local sinpitch = sin(-rot.x)
  262. local sinyaw = sin(-rot.y)
  263. local sinroll = sin(-rot.z)
  264. local cospitch = cos(rot.x)
  265. local cosyaw = cos(rot.y)
  266. local cosroll = math.cos(rot.z)
  267. -- Rotation matrix that applies yaw, pitch and roll
  268. local matrix = {
  269. {
  270. sinyaw * sinpitch * sinroll + cosyaw * cosroll,
  271. sinyaw * sinpitch * cosroll - cosyaw * sinroll,
  272. sinyaw * cospitch,
  273. },
  274. {
  275. cospitch * sinroll,
  276. cospitch * cosroll,
  277. -sinpitch,
  278. },
  279. {
  280. cosyaw * sinpitch * sinroll - sinyaw * cosroll,
  281. cosyaw * sinpitch * cosroll + sinyaw * sinroll,
  282. cosyaw * cospitch,
  283. },
  284. }
  285. -- Compute matrix multiplication: `matrix` * `v`
  286. return vector.new(
  287. matrix[1][1] * v.x + matrix[1][2] * v.y + matrix[1][3] * v.z,
  288. matrix[2][1] * v.x + matrix[2][2] * v.y + matrix[2][3] * v.z,
  289. matrix[3][1] * v.x + matrix[3][2] * v.y + matrix[3][3] * v.z
  290. )
  291. end
  292. function vector.dir_to_rotation(forward, up)
  293. forward = vector.normalize(forward)
  294. local rot = vector.new(math.asin(forward.y), -math.atan2(forward.x, forward.z), 0)
  295. if not up then
  296. return rot
  297. end
  298. assert(vector.dot(forward, up) < 0.000001,
  299. "Invalid vectors passed to vector.dir_to_rotation().")
  300. up = vector.normalize(up)
  301. -- Calculate vector pointing up with roll = 0, just based on forward vector.
  302. local forwup = vector.rotate(vector.new(0, 1, 0), rot)
  303. -- 'forwup' and 'up' are now in a plane with 'forward' as normal.
  304. -- The angle between them is the absolute of the roll value we're looking for.
  305. rot.z = vector.angle(forwup, up)
  306. -- Since vector.angle never returns a negative value or a value greater
  307. -- than math.pi, rot.z has to be inverted sometimes.
  308. -- To determine wether this is the case, we rotate the up vector back around
  309. -- the forward vector and check if it worked out.
  310. local back = vector.rotate_around_axis(up, forward, -rot.z)
  311. -- We don't use vector.equals for this because of floating point imprecision.
  312. if (back.x - forwup.x) * (back.x - forwup.x) +
  313. (back.y - forwup.y) * (back.y - forwup.y) +
  314. (back.z - forwup.z) * (back.z - forwup.z) > 0.0000001 then
  315. rot.z = -rot.z
  316. end
  317. return rot
  318. end