vector.lua 7.3 KB

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