General Purpose library for Freestanding C++ and POSIX systems
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

270 rader
3.9 KiB

4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
4 år sedan
  1. #pragma once
  2. #include "gp_config.hpp"
  3. #include "gp/math/integer_math.hpp"
  4. #include "gp/math/q_math.hpp"
  5. #if !defined(NO_FP_MATH)
  6. # include "gp/math/fp_math.hpp"
  7. #endif
  8. #include "gp/algorithm/repeat.hpp"
  9. namespace gp {
  10. template<typename T, size_t fixed_passes = 16>
  11. T fixed_sqrt(T value) {
  12. if(value == 0) return 0;
  13. T ret = value / 2;
  14. T tmp;
  15. gp::repeat(fixed_passes, [&](){
  16. tmp = ret;
  17. ret = (value / tmp + tmp) / 2;
  18. });
  19. return ret;
  20. }
  21. template<typename T, size_t cap_passes = 16>
  22. T epsilon_sqrt(T value) {
  23. if(value == 0) return 0;
  24. T ret = value / 2;
  25. T tmp;
  26. constexpr T epsilon = gp_config::rendering::epsilon;
  27. size_t cnt = 0;
  28. while(
  29. !(
  30. (ret+epsilon)*ret > value
  31. && (ret-epsilon)*ret < value
  32. )
  33. && cnt < cap_passes
  34. ){
  35. tmp = ret;
  36. ret = (value / tmp + tmp) / 2;
  37. ++cnt;
  38. };
  39. return ret;
  40. }
  41. template<typename T, size_t cap_passes = 16>
  42. T stable_sqrt(T value) {
  43. if(value == 0) return 0;
  44. T ret = value / 2;
  45. T tmp;
  46. while(ret != tmp){
  47. tmp = ret;
  48. ret = (value / tmp + tmp) / 2;
  49. };
  50. return ret;
  51. }
  52. template<typename T = gp_config::rendering::default_type>
  53. struct vec2_g {
  54. T x;
  55. T y;
  56. vec2_g()
  57. : x{}
  58. , y{}
  59. {}
  60. vec2_g(
  61. T _x,
  62. T _y
  63. )
  64. : x{_x}
  65. , y{_y}
  66. {}
  67. vec2_g operator/(vec2_g rhs) {
  68. return {
  69. x / rhs.x,
  70. y / rhs.y
  71. };
  72. }
  73. vec2_g operator*(vec2_g rhs) {
  74. return {
  75. x * rhs.x,
  76. y * rhs.y
  77. };
  78. }
  79. vec2_g operator+(vec2_g oth) {
  80. return {x+oth.x, y+oth.y};
  81. }
  82. vec2_g operator-(vec2_g oth) {
  83. return {x-oth.x, y-oth.y};
  84. }
  85. vec2_g normalize() {
  86. T ilen = fast_isqrt(x*x+y*y);
  87. return {x*ilen, y*ilen};
  88. }
  89. };
  90. template<typename T = gp_config::rendering::default_type>
  91. struct vec3_g {
  92. T x;
  93. T y;
  94. T z;
  95. vec3_g()
  96. : x{}
  97. , y{}
  98. , z{}
  99. {}
  100. vec3_g(
  101. T _x,
  102. T _y,
  103. T _z
  104. )
  105. : x{_x}
  106. , y{_y}
  107. , z{_z}
  108. {}
  109. vec3_g(vec2_g<T> left, T right)
  110. : x{left.x}
  111. , y{left.y}
  112. , z{right}
  113. {}
  114. vec3_g(T left, vec2_g<T> right)
  115. : x{left}
  116. , y{right.x}
  117. , z{right.y}
  118. {}
  119. vec3_g operator/(vec3_g rhs) {
  120. return {
  121. x / rhs.x,
  122. y / rhs.y,
  123. z / rhs.z
  124. };
  125. }
  126. vec3_g operator*(vec3_g rhs) {
  127. return {
  128. x * rhs.x,
  129. y * rhs.y,
  130. z * rhs.z
  131. };
  132. }
  133. vec3_g operator+(vec3_g oth) {
  134. return {x+oth.x, y+oth.y, z+oth.z};
  135. }
  136. vec3_g operator-(vec3_g oth) {
  137. return {x-oth.x, y-oth.y, z-oth.z};
  138. }
  139. vec3_g normalize() {
  140. T ilen = fast_isqrt(x*x+y*y+z*z);
  141. return {x*ilen, y*ilen, z*ilen};
  142. }
  143. };
  144. template<typename T = gp_config::rendering::default_type>
  145. struct vec4_g {
  146. T x;
  147. T y;
  148. T z;
  149. T w;
  150. vec4_g()
  151. : x{}
  152. , y{}
  153. , z{}
  154. , w{}
  155. {}
  156. vec4_g(
  157. T _x,
  158. T _y,
  159. T _z,
  160. T _w
  161. )
  162. : x{_x}
  163. , y{_y}
  164. , z{_z}
  165. , w{_w}
  166. {}
  167. vec4_g(T left, vec3_g<> right)
  168. : x{left}
  169. , y{right.x}
  170. , z{right.y}
  171. , w{right.z}
  172. {}
  173. vec4_g(vec3_g<> left, T right)
  174. : x{left.x}
  175. , y{left.y}
  176. , z{left.z}
  177. , w{right}
  178. {}
  179. vec4_g operator/(vec4_g rhs) {
  180. return {
  181. x / rhs.x,
  182. y / rhs.y,
  183. z / rhs.z,
  184. w / rhs.w
  185. };
  186. }
  187. vec4_g operator*(vec4_g rhs) {
  188. return {
  189. x * rhs.x,
  190. y * rhs.y,
  191. z * rhs.z,
  192. w * rhs.w
  193. };
  194. }
  195. vec4_g operator+(vec4_g oth) {
  196. return {x+oth.x, y+oth.y, z+oth.z, w+oth.w};
  197. }
  198. vec4_g operator-(vec4_g oth) {
  199. return {x-oth.x, y-oth.y, z-oth.w, z-oth.w};
  200. }
  201. vec4_g normalize() {
  202. T ilen = fast_isqrt(x*x+y*y+z*z+w*w);
  203. return {x*ilen, y*ilen, z*ilen, w*ilen};
  204. }
  205. };
  206. template<typename T>
  207. vec2_g<T> operator*(vec2_g<T> p, T v) {
  208. return {p.x*v, p.y*v};
  209. }
  210. template<typename T>
  211. vec3_g<T> operator*(vec3_g<T> p, T v) {
  212. return {p.x*v, p.y*v, p.z*v};
  213. }
  214. template<typename T>
  215. vec4_g<T> operator*(vec4_g<T> p, T v) {
  216. return {p.x*v, p.y*v, p.z*v, p.w*v};
  217. }
  218. template<typename T>
  219. vec2_g<T> operator*(T v, vec2_g<T> p) {
  220. return p*v;
  221. }
  222. template<typename T>
  223. vec3_g<T> operator*(T v, vec3_g<T> p) {
  224. return p*v;
  225. }
  226. template<typename T>
  227. vec4_g<T> operator*(T v, vec4_g<T> p) {
  228. return p*v;
  229. }
  230. }