LuaMath.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "MathUtils.h"
  24. #include "LuaStack.h"
  25. #include "LuaEnvironment.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. CE_EXPORT int32_t math_deg_to_rad(lua_State* L)
  30. {
  31. LuaStack stack(L);
  32. float deg = stack.get_float(1);
  33. stack.push_float(math::deg_to_rad(deg));
  34. return 1;
  35. }
  36. //-----------------------------------------------------------------------------
  37. CE_EXPORT int32_t math_rad_to_deg(lua_State* L)
  38. {
  39. LuaStack stack(L);
  40. float rad = stack.get_float(1);
  41. stack.push_float(math::rad_to_deg(rad));
  42. return 1;
  43. }
  44. //-----------------------------------------------------------------------------
  45. CE_EXPORT int32_t math_next_pow_2(lua_State* L)
  46. {
  47. LuaStack stack(L);
  48. uint32_t x = stack.get_int(1);
  49. stack.push_uint32(math::next_pow_2(x));
  50. return 1;
  51. }
  52. //-----------------------------------------------------------------------------
  53. CE_EXPORT int32_t math_is_pow_2(lua_State* L)
  54. {
  55. LuaStack stack(L);
  56. uint32_t x = stack.get_int(1);
  57. stack.push_bool(math::is_pow_2(x));
  58. return 1;
  59. }
  60. //-----------------------------------------------------------------------------
  61. CE_EXPORT int32_t math_ceil(lua_State* L)
  62. {
  63. LuaStack stack(L);
  64. float x = stack.get_float(1);
  65. stack.push_float(math::ceil(x));
  66. return 1;
  67. }
  68. //-----------------------------------------------------------------------------
  69. CE_EXPORT int32_t math_floor(lua_State* L)
  70. {
  71. LuaStack stack(L);
  72. float x = stack.get_float(1);
  73. stack.push_float(math::floor(x));
  74. return 1;
  75. }
  76. //-----------------------------------------------------------------------------
  77. CE_EXPORT int32_t math_sqrt(lua_State* L)
  78. {
  79. LuaStack stack(L);
  80. float x = stack.get_float(1);
  81. stack.push_float(math::sqrt(x));
  82. return 1;
  83. }
  84. //-----------------------------------------------------------------------------
  85. CE_EXPORT int32_t math_inv_sqrt(lua_State* L)
  86. {
  87. LuaStack stack(L);
  88. float x = stack.get_float(1);
  89. stack.push_float(math::inv_sqrt(x));
  90. return 1;
  91. }
  92. //-----------------------------------------------------------------------------
  93. CE_EXPORT int32_t math_sin(lua_State* L)
  94. {
  95. LuaStack stack(L);
  96. float x = stack.get_float(1);
  97. stack.push_float(math::sin(x));
  98. return 1;
  99. }
  100. //-----------------------------------------------------------------------------
  101. CE_EXPORT int32_t math_cos(lua_State* L)
  102. {
  103. LuaStack stack(L);
  104. float x = stack.get_float(1);
  105. stack.push_float(math::cos(x));
  106. return 1;
  107. }
  108. //-----------------------------------------------------------------------------
  109. CE_EXPORT int32_t math_asin(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. float x = stack.get_float(1);
  113. stack.push_float(math::asin(x));
  114. return 1;
  115. }
  116. //-----------------------------------------------------------------------------
  117. CE_EXPORT int32_t math_acos(lua_State* L)
  118. {
  119. LuaStack stack(L);
  120. float x = stack.get_float(1);
  121. stack.push_float(math::acos(x));
  122. return 1;
  123. }
  124. //-----------------------------------------------------------------------------
  125. CE_EXPORT int32_t math_tan(lua_State* L)
  126. {
  127. LuaStack stack(L);
  128. float x = stack.get_float(1);
  129. stack.push_float(math::tan(x));
  130. return 1;
  131. }
  132. //-----------------------------------------------------------------------------
  133. CE_EXPORT int32_t math_atan2(lua_State* L)
  134. {
  135. LuaStack stack(L);
  136. float x = stack.get_float(1);
  137. float y = stack.get_float(2);
  138. stack.push_float(math::atan2(x, y));
  139. return 1;
  140. }
  141. //-----------------------------------------------------------------------------
  142. CE_EXPORT int32_t math_abs(lua_State* L)
  143. {
  144. LuaStack stack(L);
  145. float x = stack.get_float(1);
  146. stack.push_float(math::abs(x));
  147. return 1;
  148. }
  149. //-----------------------------------------------------------------------------
  150. CE_EXPORT int32_t math_fmod(lua_State* L)
  151. {
  152. LuaStack stack(L);
  153. float x = stack.get_float(1);
  154. float y = stack.get_float(2);
  155. stack.push_float(math::fmod(x, y));
  156. return 1;
  157. }
  158. //-----------------------------------------------------------------------------
  159. void load_math(LuaEnvironment& env)
  160. {
  161. env.load_module_function("Math", "deg_to_rad", math_deg_to_rad);
  162. env.load_module_function("Math", "rad_to_deg", math_rad_to_deg);
  163. env.load_module_function("Math", "next_pow_2", math_next_pow_2);
  164. env.load_module_function("Math", "is_pow_2", math_is_pow_2);
  165. env.load_module_function("Math", "ceil", math_ceil);
  166. env.load_module_function("Math", "floor", math_floor);
  167. env.load_module_function("Math", "sqrt", math_sqrt);
  168. env.load_module_function("Math", "inv_sqrt", math_inv_sqrt);
  169. env.load_module_function("Math", "sin", math_sin);
  170. env.load_module_function("Math", "cos", math_cos);
  171. env.load_module_function("Math", "asin", math_asin);
  172. env.load_module_function("Math", "acos", math_acos);
  173. env.load_module_function("Math", "tan", math_tan);
  174. env.load_module_function("Math", "atan2", math_atan2);
  175. env.load_module_function("Math", "abs", math_abs);
  176. env.load_module_function("Math", "fmod", math_fmod);
  177. }
  178. } // namespace crown