Quaternion.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. class "Quaternion"
  2. function Quaternion:__index__(name)
  3. if name == "x" then
  4. return Polycore.Quaternion_get_x(self.__ptr)
  5. elseif name == "y" then
  6. return Polycore.Quaternion_get_y(self.__ptr)
  7. elseif name == "z" then
  8. return Polycore.Quaternion_get_z(self.__ptr)
  9. elseif name == "w" then
  10. return Polycore.Quaternion_get_w(self.__ptr)
  11. end
  12. end
  13. function Quaternion:__set_callback(name,value)
  14. if name == "x" then
  15. Polycore.Quaternion_set_x(self.__ptr, value)
  16. return true
  17. elseif name == "y" then
  18. Polycore.Quaternion_set_y(self.__ptr, value)
  19. return true
  20. elseif name == "z" then
  21. Polycore.Quaternion_set_z(self.__ptr, value)
  22. return true
  23. elseif name == "w" then
  24. Polycore.Quaternion_set_w(self.__ptr, value)
  25. return true
  26. end
  27. return false
  28. end
  29. function Quaternion:Quaternion(...)
  30. for k,v in pairs(arg) do
  31. if type(v) == "table" then
  32. if v.__ptr ~= nil then
  33. arg[k] = v.__ptr
  34. end
  35. end
  36. end
  37. if self.__ptr == nil and arg[1] ~= "__skip_ptr__" then
  38. self.__ptr = Polycore.Quaternion(unpack(arg))
  39. Polycore.__ptr_lookup[self.__ptr] = self
  40. end
  41. end
  42. function Quaternion:setFromMatrix(_mat)
  43. local retVal = Polycore.Quaternion_setFromMatrix(self.__ptr, _mat.__ptr)
  44. end
  45. function Quaternion:Slerp(fT, rkP, rkQ, shortestPath)
  46. local retVal = Polycore.Quaternion_Slerp(fT, rkP.__ptr, rkQ.__ptr, shortestPath)
  47. if retVal == nil then return nil end
  48. if Polycore.__ptr_lookup[retVal] ~= nil then
  49. return Polycore.__ptr_lookup[retVal]
  50. else
  51. Polycore.__ptr_lookup[retVal] = Quaternion("__skip_ptr__")
  52. Polycore.__ptr_lookup[retVal].__ptr = retVal
  53. return Polycore.__ptr_lookup[retVal]
  54. end
  55. end
  56. function Quaternion:Dot(rkQ)
  57. local retVal = Polycore.Quaternion_Dot(self.__ptr, rkQ.__ptr)
  58. return retVal
  59. end
  60. function Quaternion:Log()
  61. local retVal = Polycore.Quaternion_Log(self.__ptr)
  62. if retVal == nil then return nil end
  63. if Polycore.__ptr_lookup[retVal] ~= nil then
  64. return Polycore.__ptr_lookup[retVal]
  65. else
  66. Polycore.__ptr_lookup[retVal] = Quaternion("__skip_ptr__")
  67. Polycore.__ptr_lookup[retVal].__ptr = retVal
  68. return Polycore.__ptr_lookup[retVal]
  69. end
  70. end
  71. function Quaternion:Exp()
  72. local retVal = Polycore.Quaternion_Exp(self.__ptr)
  73. if retVal == nil then return nil end
  74. if Polycore.__ptr_lookup[retVal] ~= nil then
  75. return Polycore.__ptr_lookup[retVal]
  76. else
  77. Polycore.__ptr_lookup[retVal] = Quaternion("__skip_ptr__")
  78. Polycore.__ptr_lookup[retVal].__ptr = retVal
  79. return Polycore.__ptr_lookup[retVal]
  80. end
  81. end
  82. function Quaternion:Norm()
  83. local retVal = Polycore.Quaternion_Norm(self.__ptr)
  84. return retVal
  85. end
  86. function Quaternion:normalize()
  87. local retVal = Polycore.Quaternion_normalize(self.__ptr)
  88. return retVal
  89. end
  90. function Quaternion:lookAt(D, upVector)
  91. local retVal = Polycore.Quaternion_lookAt(self.__ptr, D.__ptr, upVector.__ptr)
  92. end
  93. function Quaternion:createFromMatrix(matrix)
  94. local retVal = Polycore.Quaternion_createFromMatrix(self.__ptr, matrix.__ptr)
  95. end
  96. function Quaternion:Squad(fT, rkP, rkA, rkB, rkQ, shortestPath)
  97. local retVal = Polycore.Quaternion_Squad(fT, rkP.__ptr, rkA.__ptr, rkB.__ptr, rkQ.__ptr, shortestPath)
  98. if retVal == nil then return nil end
  99. if Polycore.__ptr_lookup[retVal] ~= nil then
  100. return Polycore.__ptr_lookup[retVal]
  101. else
  102. Polycore.__ptr_lookup[retVal] = Quaternion("__skip_ptr__")
  103. Polycore.__ptr_lookup[retVal].__ptr = retVal
  104. return Polycore.__ptr_lookup[retVal]
  105. end
  106. end
  107. function Quaternion:Inverse()
  108. local retVal = Polycore.Quaternion_Inverse(self.__ptr)
  109. if retVal == nil then return nil end
  110. if Polycore.__ptr_lookup[retVal] ~= nil then
  111. return Polycore.__ptr_lookup[retVal]
  112. else
  113. Polycore.__ptr_lookup[retVal] = Quaternion("__skip_ptr__")
  114. Polycore.__ptr_lookup[retVal].__ptr = retVal
  115. return Polycore.__ptr_lookup[retVal]
  116. end
  117. end
  118. function Quaternion:set(w, x, y, z)
  119. local retVal = Polycore.Quaternion_set(self.__ptr, w, x, y, z)
  120. end
  121. function Quaternion:inverse()
  122. local retVal = Polycore.Quaternion_inverse(self.__ptr)
  123. if retVal == nil then return nil end
  124. if Polycore.__ptr_lookup[retVal] ~= nil then
  125. return Polycore.__ptr_lookup[retVal]
  126. else
  127. Polycore.__ptr_lookup[retVal] = Quaternion("__skip_ptr__")
  128. Polycore.__ptr_lookup[retVal].__ptr = retVal
  129. return Polycore.__ptr_lookup[retVal]
  130. end
  131. end
  132. function Quaternion:InvSqrt(x)
  133. local retVal = Polycore.Quaternion_InvSqrt(self.__ptr, x)
  134. return retVal
  135. end
  136. function Quaternion:fromAxes(az, ay, ax)
  137. local retVal = Polycore.Quaternion_fromAxes(self.__ptr, az, ay, ax)
  138. end
  139. function Quaternion:FromAngleAxis(rfAngle, rkAxis)
  140. local retVal = Polycore.Quaternion_FromAngleAxis(self.__ptr, rfAngle, rkAxis.__ptr)
  141. end
  142. function Quaternion:ToAngleAxis(rfAngle, rkAxis)
  143. local retVal = Polycore.Quaternion_ToAngleAxis(self.__ptr, rfAngle, rkAxis.__ptr)
  144. end
  145. function Quaternion:createFromAxisAngle(x, y, z, degrees)
  146. local retVal = Polycore.Quaternion_createFromAxisAngle(self.__ptr, x, y, z, degrees)
  147. end
  148. function Quaternion:createMatrix()
  149. local retVal = Polycore.Quaternion_createMatrix(self.__ptr)
  150. if retVal == nil then return nil end
  151. if Polycore.__ptr_lookup[retVal] ~= nil then
  152. return Polycore.__ptr_lookup[retVal]
  153. else
  154. Polycore.__ptr_lookup[retVal] = Matrix4("__skip_ptr__")
  155. Polycore.__ptr_lookup[retVal].__ptr = retVal
  156. return Polycore.__ptr_lookup[retVal]
  157. end
  158. end
  159. function Quaternion:__delete()
  160. Polycore.__ptr_lookup[self.__ptr] = nil
  161. Polycore.delete_Quaternion(self.__ptr)
  162. end