General Purpose library for Freestanding C++ and POSIX systems
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
4.5 KiB

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