quat.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. --- A quaternion and associated utilities.
  2. -- @module quat
  3. local modules = (...):gsub('%.[^%.]+$', '') .. "."
  4. local constants = require(modules .. "constants")
  5. local vec3 = require(modules .. "vec3")
  6. local DOT_THRESHOLD = constants.DOT_THRESHOLD
  7. local DBL_EPSILON = constants.DBL_EPSILON
  8. local acos = math.acos
  9. local cos = math.cos
  10. local sin = math.sin
  11. local min = math.min
  12. local max = math.max
  13. local sqrt = math.sqrt
  14. local quat = {}
  15. local quat_mt = {}
  16. -- Private constructor.
  17. local function new(x, y, z, w)
  18. return setmetatable({
  19. x = x or 0,
  20. y = y or 0,
  21. z = z or 0,
  22. w = w or 1
  23. }, quat_mt)
  24. end
  25. -- Do the check to see if JIT is enabled. If so use the optimized FFI structs.
  26. local status, ffi
  27. if type(jit) == "table" and jit.status() then
  28. status, ffi = pcall(require, "ffi")
  29. if status then
  30. ffi.cdef "typedef struct { double x, y, z, w;} cpml_quat;"
  31. new = ffi.typeof("cpml_quat")
  32. end
  33. end
  34. -- Statically allocate a temporary variable used in some of our functions.
  35. local tmp = new()
  36. local qv, uv, uuv = vec3(), vec3(), vec3()
  37. --- Constants
  38. -- @table quat
  39. -- @field unit Unit quaternion
  40. -- @field zero Empty quaternion
  41. quat.unit = new(0, 0, 0, 1)
  42. quat.zero = new(0, 0, 0, 0)
  43. --- The public constructor.
  44. -- @param x Can be of two types: </br>
  45. -- number x X component
  46. -- table {x, y, z, w} or {x=x, y=y, z=z, w=w}
  47. -- @tparam number y Y component
  48. -- @tparam number z Z component
  49. -- @tparam number w W component
  50. -- @treturn quat out
  51. function quat.new(x, y, z, w)
  52. -- number, number, number, number
  53. if x and y and z and w then
  54. assert(type(x) == "number", "new: Wrong argument type for x (<number> expected)")
  55. assert(type(y) == "number", "new: Wrong argument type for y (<number> expected)")
  56. assert(type(z) == "number", "new: Wrong argument type for z (<number> expected)")
  57. assert(type(w) == "number", "new: Wrong argument type for w (<number> expected)")
  58. return new(x, y, z, w)
  59. -- {x, y, z, w} or {x=x, y=y, z=z, w=w}
  60. elseif type(x) == "table" then
  61. local xx, yy, zz, ww = x.x or x[1], x.y or x[2], x.z or x[3], x.w or x[4]
  62. assert(type(xx) == "number", "new: Wrong argument type for x (<number> expected)")
  63. assert(type(yy) == "number", "new: Wrong argument type for y (<number> expected)")
  64. assert(type(zz) == "number", "new: Wrong argument type for z (<number> expected)")
  65. assert(type(ww) == "number", "new: Wrong argument type for w (<number> expected)")
  66. return new(xx, yy, zz, ww)
  67. end
  68. return new(0, 0, 0, 1)
  69. end
  70. --- Create a quaternion from an angle/axis pair.
  71. -- @tparam number angle Angle (in radians)
  72. -- @param axis/x -- Can be of two types, a vec3 axis, or the x component of that axis
  73. -- @param y axis -- y component of axis (optional, only if x component param used)
  74. -- @param z axis -- z component of axis (optional, only if x component param used)
  75. -- @treturn quat out
  76. function quat.from_angle_axis(angle, axis, a3, a4)
  77. if axis and a3 and a4 then
  78. local x, y, z = axis, a3, a4
  79. local s = sin(angle * 0.5)
  80. local c = cos(angle * 0.5)
  81. return new(x * s, y * s, z * s, c)
  82. else
  83. return quat.from_angle_axis(angle, axis.x, axis.y, axis.z)
  84. end
  85. end
  86. --- Create a quaternion from a normal/up vector pair.
  87. -- @tparam vec3 normal
  88. -- @tparam vec3 up (optional)
  89. -- @treturn quat out
  90. function quat.from_direction(normal, up)
  91. local u = up or vec3.unit_z
  92. local n = normal:normalize()
  93. local a = u:cross(n)
  94. local d = u:dot(n)
  95. return new(a.x, a.y, a.z, d + 1)
  96. end
  97. --- Clone a quaternion.
  98. -- @tparam quat a Quaternion to clone
  99. -- @treturn quat out
  100. function quat.clone(a)
  101. return new(a.x, a.y, a.z, a.w)
  102. end
  103. --- Add two quaternions.
  104. -- @tparam quat a Left hand operand
  105. -- @tparam quat b Right hand operand
  106. -- @treturn quat out
  107. function quat.add(a, b)
  108. return new(
  109. a.x + b.x,
  110. a.y + b.y,
  111. a.z + b.z,
  112. a.w + b.w
  113. )
  114. end
  115. --- Subtract a quaternion from another.
  116. -- @tparam quat a Left hand operand
  117. -- @tparam quat b Right hand operand
  118. -- @treturn quat out
  119. function quat.sub(a, b)
  120. return new(
  121. a.x - b.x,
  122. a.y - b.y,
  123. a.z - b.z,
  124. a.w - b.w
  125. )
  126. end
  127. --- Multiply two quaternions.
  128. -- @tparam quat a Left hand operand
  129. -- @tparam quat b Right hand operand
  130. -- @treturn quat quaternion equivalent to "apply b, then a"
  131. function quat.mul(a, b)
  132. return new(
  133. a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y,
  134. a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z,
  135. a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x,
  136. a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
  137. )
  138. end
  139. --- Multiply a quaternion and a vec3.
  140. -- @tparam quat a Left hand operand
  141. -- @tparam vec3 b Right hand operand
  142. -- @treturn quat out
  143. function quat.mul_vec3(a, b)
  144. qv.x = a.x
  145. qv.y = a.y
  146. qv.z = a.z
  147. uv = qv:cross(b)
  148. uuv = qv:cross(uv)
  149. return b + ((uv * a.w) + uuv) * 2
  150. end
  151. --- Raise a normalized quaternion to a scalar power.
  152. -- @tparam quat a Left hand operand (should be a unit quaternion)
  153. -- @tparam number s Right hand operand
  154. -- @treturn quat out
  155. function quat.pow(a, s)
  156. -- Do it as a slerp between identity and a (code borrowed from slerp)
  157. if a.w < 0 then
  158. a = -a
  159. end
  160. local dot = a.w
  161. if dot > DOT_THRESHOLD then
  162. return a:scale(s)
  163. end
  164. dot = min(max(dot, -1), 1)
  165. local theta = acos(dot) * s
  166. local c = new(a.x, a.y, a.z, 0):normalize() * sin(theta)
  167. c.w = cos(theta)
  168. return c
  169. end
  170. --- Normalize a quaternion.
  171. -- @tparam quat a Quaternion to normalize
  172. -- @treturn quat out
  173. function quat.normalize(a)
  174. if a:is_zero() then
  175. return new(0, 0, 0, 0)
  176. end
  177. return a:scale(1 / a:len())
  178. end
  179. --- Get the dot product of two quaternions.
  180. -- @tparam quat a Left hand operand
  181. -- @tparam quat b Right hand operand
  182. -- @treturn number dot
  183. function quat.dot(a, b)
  184. return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w
  185. end
  186. --- Return the length of a quaternion.
  187. -- @tparam quat a Quaternion to get length of
  188. -- @treturn number len
  189. function quat.len(a)
  190. return sqrt(a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w)
  191. end
  192. --- Return the squared length of a quaternion.
  193. -- @tparam quat a Quaternion to get length of
  194. -- @treturn number len
  195. function quat.len2(a)
  196. return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w
  197. end
  198. --- Multiply a quaternion by a scalar.
  199. -- @tparam quat a Left hand operand
  200. -- @tparam number s Right hand operand
  201. -- @treturn quat out
  202. function quat.scale(a, s)
  203. return new(
  204. a.x * s,
  205. a.y * s,
  206. a.z * s,
  207. a.w * s
  208. )
  209. end
  210. --- Alias of from_angle_axis.
  211. -- @tparam number angle Angle (in radians)
  212. -- @param axis/x -- Can be of two types, a vec3 axis, or the x component of that axis
  213. -- @param y axis -- y component of axis (optional, only if x component param used)
  214. -- @param z axis -- z component of axis (optional, only if x component param used)
  215. -- @treturn quat out
  216. function quat.rotate(angle, axis, a3, a4)
  217. return quat.from_angle_axis(angle, axis, a3, a4)
  218. end
  219. --- Return the conjugate of a quaternion.
  220. -- @tparam quat a Quaternion to conjugate
  221. -- @treturn quat out
  222. function quat.conjugate(a)
  223. return new(-a.x, -a.y, -a.z, a.w)
  224. end
  225. --- Return the inverse of a quaternion.
  226. -- @tparam quat a Quaternion to invert
  227. -- @treturn quat out
  228. function quat.inverse(a)
  229. tmp.x = -a.x
  230. tmp.y = -a.y
  231. tmp.z = -a.z
  232. tmp.w = a.w
  233. return tmp:normalize()
  234. end
  235. --- Return the reciprocal of a quaternion.
  236. -- @tparam quat a Quaternion to reciprocate
  237. -- @treturn quat out
  238. function quat.reciprocal(a)
  239. if a:is_zero() then
  240. error("Cannot reciprocate a zero quaternion")
  241. return false
  242. end
  243. tmp.x = -a.x
  244. tmp.y = -a.y
  245. tmp.z = -a.z
  246. tmp.w = a.w
  247. return tmp:scale(1 / a:len2())
  248. end
  249. --- Lerp between two quaternions.
  250. -- @tparam quat a Left hand operand
  251. -- @tparam quat b Right hand operand
  252. -- @tparam number s Step value
  253. -- @treturn quat out
  254. function quat.lerp(a, b, s)
  255. return (a + (b - a) * s):normalize()
  256. end
  257. --- Slerp between two quaternions.
  258. -- @tparam quat a Left hand operand
  259. -- @tparam quat b Right hand operand
  260. -- @tparam number s Step value
  261. -- @treturn quat out
  262. function quat.slerp(a, b, s)
  263. local dot = a:dot(b)
  264. if dot < 0 then
  265. a = -a
  266. dot = -dot
  267. end
  268. if dot > DOT_THRESHOLD then
  269. return a:lerp(b, s)
  270. end
  271. dot = min(max(dot, -1), 1)
  272. local theta = acos(dot) * s
  273. local c = (b - a * dot):normalize()
  274. return a * cos(theta) + c * sin(theta)
  275. end
  276. --- Unpack a quaternion into individual components.
  277. -- @tparam quat a Quaternion to unpack
  278. -- @treturn number x
  279. -- @treturn number y
  280. -- @treturn number z
  281. -- @treturn number w
  282. function quat.unpack(a)
  283. return a.x, a.y, a.z, a.w
  284. end
  285. --- Return a boolean showing if a table is or is not a quat.
  286. -- @tparam quat a Quaternion to be tested
  287. -- @treturn boolean is_quat
  288. function quat.is_quat(a)
  289. if type(a) == "cdata" then
  290. return ffi.istype("cpml_quat", a)
  291. end
  292. return
  293. type(a) == "table" and
  294. type(a.x) == "number" and
  295. type(a.y) == "number" and
  296. type(a.z) == "number" and
  297. type(a.w) == "number"
  298. end
  299. --- Return a boolean showing if a table is or is not a zero quat.
  300. -- @tparam quat a Quaternion to be tested
  301. -- @treturn boolean is_zero
  302. function quat.is_zero(a)
  303. return
  304. a.x == 0 and
  305. a.y == 0 and
  306. a.z == 0 and
  307. a.w == 0
  308. end
  309. --- Return a boolean showing if a table is or is not a real quat.
  310. -- @tparam quat a Quaternion to be tested
  311. -- @treturn boolean is_real
  312. function quat.is_real(a)
  313. return
  314. a.x == 0 and
  315. a.y == 0 and
  316. a.z == 0
  317. end
  318. --- Return a boolean showing if a table is or is not an imaginary quat.
  319. -- @tparam quat a Quaternion to be tested
  320. -- @treturn boolean is_imaginary
  321. function quat.is_imaginary(a)
  322. return a.w == 0
  323. end
  324. --- Convert a quaternion into an angle plus axis components.
  325. -- @tparam quat a Quaternion to convert
  326. -- @treturn number angle
  327. -- @treturn x axis-x
  328. -- @treturn y axis-y
  329. -- @treturn z axis-z
  330. function quat.to_angle_axis_unpack(a)
  331. if a.w > 1 or a.w < -1 then
  332. a = a:normalize()
  333. end
  334. local x, y, z
  335. local angle = 2 * acos(a.w)
  336. local s = sqrt(1 - a.w * a.w)
  337. if s < DBL_EPSILON then
  338. x = a.x
  339. y = a.y
  340. z = a.z
  341. else
  342. x = a.x / s
  343. y = a.y / s
  344. z = a.z / s
  345. end
  346. return angle, x, y, z
  347. end
  348. --- Convert a quaternion into an angle/axis pair.
  349. -- @tparam quat a Quaternion to convert
  350. -- @treturn number angle
  351. -- @treturn vec3 axis
  352. function quat.to_angle_axis(a)
  353. local angle, x, y, z = a:to_angle_axis_unpack()
  354. return angle, vec3(x, y, z)
  355. end
  356. --- Convert a quaternion into a vec3.
  357. -- @tparam quat a Quaternion to convert
  358. -- @treturn vec3 out
  359. function quat.to_vec3(a)
  360. return vec3(a.x, a.y, a.z)
  361. end
  362. --- Return a formatted string.
  363. -- @tparam quat a Quaternion to be turned into a string
  364. -- @treturn string formatted
  365. function quat.to_string(a)
  366. return string.format("(%+0.3f,%+0.3f,%+0.3f,%+0.3f)", a.x, a.y, a.z, a.w)
  367. end
  368. quat_mt.__index = quat
  369. quat_mt.__tostring = quat.to_string
  370. function quat_mt.__call(_, x, y, z, w)
  371. return quat.new(x, y, z, w)
  372. end
  373. function quat_mt.__unm(a)
  374. return a:scale(-1)
  375. end
  376. function quat_mt.__eq(a,b)
  377. if not quat.is_quat(a) or not quat.is_quat(b) then
  378. return false
  379. end
  380. return a.x == b.x and a.y == b.y and a.z == b.z and a.w == b.w
  381. end
  382. function quat_mt.__add(a, b)
  383. assert(quat.is_quat(a), "__add: Wrong argument type for left hand operand. (<cpml.quat> expected)")
  384. assert(quat.is_quat(b), "__add: Wrong argument type for right hand operand. (<cpml.quat> expected)")
  385. return a:add(b)
  386. end
  387. function quat_mt.__sub(a, b)
  388. assert(quat.is_quat(a), "__sub: Wrong argument type for left hand operand. (<cpml.quat> expected)")
  389. assert(quat.is_quat(b), "__sub: Wrong argument type for right hand operand. (<cpml.quat> expected)")
  390. return a:sub(b)
  391. end
  392. function quat_mt.__mul(a, b)
  393. assert(quat.is_quat(a), "__mul: Wrong argument type for left hand operand. (<cpml.quat> expected)")
  394. assert(quat.is_quat(b) or vec3.is_vec3(b) or type(b) == "number", "__mul: Wrong argument type for right hand operand. (<cpml.quat> or <cpml.vec3> or <number> expected)")
  395. if quat.is_quat(b) then
  396. return a:mul(b)
  397. end
  398. if type(b) == "number" then
  399. return a:scale(b)
  400. end
  401. return a:mul_vec3(b)
  402. end
  403. function quat_mt.__pow(a, n)
  404. assert(quat.is_quat(a), "__pow: Wrong argument type for left hand operand. (<cpml.quat> expected)")
  405. assert(type(n) == "number", "__pow: Wrong argument type for right hand operand. (<number> expected)")
  406. return a:pow(n)
  407. end
  408. if status then
  409. ffi.metatype(new, quat_mt)
  410. end
  411. return setmetatable({}, quat_mt)