vector3.inl - Engine C API Reference

vector3.inl
  1. #include "math.h"
  2. namespace stingray_plugin_foundation {
  3. // Operators
  4. __forceinline Vector3 operator / (const Vector3 &lhs, const Vector3 &rhs) {
  5. return vector3(lhs.x/rhs.x, lhs.y/rhs.y, lhs.z/rhs.z);
  6. }
  7. __forceinline Vector3 operator * (const Vector3 &lhs, const Vector3 &rhs) {
  8. return vector3(lhs.x*rhs.x, lhs.y*rhs.y, lhs.z*rhs.z);
  9. }
  10. __forceinline Vector3 operator + (const Vector3 &lhs, const Vector3 &rhs) {
  11. return vector3(lhs.x+rhs.x, lhs.y+rhs.y, lhs.z+rhs.z);
  12. }
  13. __forceinline Vector3 operator - (const Vector3 &lhs, const Vector3 &rhs) {
  14. return vector3(lhs.x-rhs.x, lhs.y-rhs.y, lhs.z-rhs.z);
  15. }
  16. __forceinline Vector3 operator - (const Vector3 &v) {
  17. return vector3(-v.x, -v.y, -v.z);
  18. }
  19. __forceinline Vector3 operator + (const Vector3 &v) {
  20. return v;
  21. }
  22. __forceinline void operator /= (Vector3 &lhs, const Vector3 &rhs) {
  23. lhs.x/=rhs.x; lhs.y/=rhs.y; lhs.z/=rhs.z;
  24. }
  25. __forceinline void operator *= (Vector3 &lhs, const Vector3 &rhs) {
  26. lhs.x*=rhs.x; lhs.y*=rhs.y; lhs.z*=rhs.z;
  27. }
  28. __forceinline void operator += (Vector3 &lhs, const Vector3 &rhs) {
  29. lhs.x+=rhs.x; lhs.y+=rhs.y; lhs.z+=rhs.z;
  30. }
  31. __forceinline void operator -= (Vector3 &lhs, const Vector3 &rhs) {
  32. lhs.x-=rhs.x; lhs.y-=rhs.y; lhs.z-=rhs.z;
  33. }
  34. __forceinline Vector3 operator/(const Vector3 &lhs, float rhs) {
  35. return vector3(lhs.x/rhs, lhs.y/rhs, lhs.z/rhs);
  36. }
  37. __forceinline Vector3 operator*(const Vector3 &lhs, float rhs) {
  38. return vector3(lhs.x*rhs, lhs.y*rhs, lhs.z*rhs);
  39. }
  40. __forceinline Vector3 operator*(float lhs, const Vector3 &rhs) {
  41. return vector3(rhs.x*lhs, rhs.y*lhs, rhs.z*lhs);
  42. }
  43. __forceinline Vector3 operator+(const Vector3 &lhs, float rhs) {
  44. return vector3(lhs.x+rhs, lhs.y+rhs, lhs.z+rhs);
  45. }
  46. __forceinline Vector3 operator-(const Vector3 &lhs, float rhs) {
  47. return vector3(lhs.x-rhs, lhs.y-rhs, lhs.z-rhs);
  48. }
  49. __forceinline void operator/=(Vector3 &lhs, float rhs) {
  50. lhs.x/=rhs; lhs.y/=rhs; lhs.z/=rhs;
  51. }
  52. __forceinline void operator*=(Vector3 &lhs, float rhs) {
  53. lhs.x*=rhs; lhs.y*=rhs; lhs.z*=rhs;
  54. }
  55. __forceinline void operator+=(Vector3 &lhs, float rhs) {
  56. lhs.x+=rhs; lhs.y+=rhs; lhs.z+=rhs;
  57. }
  58. __forceinline void operator-=(Vector3 &lhs, float rhs) {
  59. lhs.x-=rhs; lhs.y-=rhs; lhs.z-=rhs;
  60. }
  61. __forceinline bool operator==(const Vector3 &lhs, const Vector3 &rhs)
  62. {
  63. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  64. }
  65. __forceinline bool operator!=(const Vector3 &lhs, const Vector3 &rhs)
  66. {
  67. return !(lhs == rhs);
  68. }
  69. __forceinline bool operator< (const Vector3 &lhs, const Vector3 &rhs)
  70. {
  71. if (lhs.x != rhs.x) return lhs.x < rhs.x;
  72. if (lhs.y != rhs.y) return lhs.y < rhs.y;
  73. return lhs.z < rhs.z;
  74. }
  75. __forceinline bool operator<=(const Vector3 &lhs, const Vector3 &rhs)
  76. {
  77. return (lhs < rhs) || (lhs == rhs);
  78. }
  79. __forceinline bool operator> (const Vector3 &lhs, const Vector3 &rhs)
  80. {
  81. return (rhs < lhs);
  82. }
  83. __forceinline bool operator>=(const Vector3 &lhs, const Vector3 &rhs)
  84. {
  85. return !(lhs < rhs);
  86. }
  87. // Methods
  88. __forceinline Vector3 vector3(float x, float y, float z) {
  89. Vector3 v = {x, y, z};
  90. return v;
  91. }
  92. __forceinline Vector3 vector3(const float v[3])
  93. {
  94. Vector3 r = {v[0], v[1], v[2]};
  95. return r;
  96. }
  97. __forceinline Vector3 vector3(const Vector2 &v) {
  98. Vector3 r = { v.x, v.y, 0.f };
  99. return r;
  100. }
  101. __forceinline Vector3 vector3(const Vector2 &v, float z) {
  102. Vector3 r = { v.x, v.y, z };
  103. return r;
  104. }
  105. __forceinline Vector3 vector3_base(int i)
  106. {
  107. if (i==0)
  108. return vector3(1,0,0);
  109. else if (i==1)
  110. return vector3(0,1,0);
  111. else if (i==2)
  112. return vector3(0,0,1);
  113. return vector3(0,0,0);
  114. }
  115. __forceinline float & element(Vector3 &v, int i)
  116. {
  117. return *(&v.x + i);
  118. }
  119. __forceinline const float & element(const Vector3 &v, int i)
  120. {
  121. return *(&v.x + i);
  122. }
  123. __forceinline void zero(Vector3 &v) {
  124. v.x=0.f; v.y=0.f; v.z=0.f;
  125. }
  126. __forceinline bool is_zero(const Vector3 &v)
  127. {
  128. return v.x == 0.0f && v.y == 0.0f && v.z == 0.0f;
  129. }
  130. __forceinline bool is_zero(const Vector3 &v, float eps)
  131. {
  132. return length_squared(v) <= eps*eps;
  133. }
  134. __forceinline float length(const Vector3 &v) {
  135. return math::square_root(v.x*v.x + v.y*v.y + v.z*v.z);
  136. }
  137. __forceinline float length_squared(const Vector3 &v) {
  138. return v.x*v.x + v.y*v.y + v.z*v.z;
  139. }
  140. __forceinline float norm(const Vector3 &v)
  141. {
  142. return math::square_root(v.x*v.x + v.y*v.y + v.z*v.z);
  143. }
  144. __forceinline float one_norm(const Vector3 &v)
  145. {
  146. return math::abs(v.x) + math::abs(v.y) + math::abs(v.z);
  147. }
  148. __forceinline float infinity_norm(const Vector3 &v)
  149. {
  150. return math::max3( math::abs(v.x), math::abs(v.y), math::abs(v.z) );
  151. }
  152. __forceinline Vector3 normalize(const Vector3 &v) {
  153. float l=length(v);
  154. if (l<0.0001)
  155. return vector3(0.f, 0.f, 0.f);
  156. return v / l;
  157. }
  158. __forceinline float dot(const Vector3 &v0, const Vector3 &v1) {
  159. return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
  160. }
  161. __forceinline Vector3 cross(const Vector3 &v0, const Vector3 &v1) {
  162. return vector3((v0.y*v1.z)-(v0.z*v1.y),(v0.z*v1.x)-(v0.x*v1.z),(v0.x*v1.y)-(v0.y*v1.x));
  163. }
  164. __forceinline float distance(const Vector3 &v0, const Vector3 &v1) {
  165. Vector3 tmp = vector3(v0.x-v1.x,v0.y-v1.y,v0.z-v1.z);
  166. return length(tmp);
  167. }
  168. __forceinline float distance_squared(const Vector3 &v0, const Vector3 &v1) {
  169. return (v0.x-v1.x)*(v0.x-v1.x) + (v0.y-v1.y)*(v0.y-v1.y) + (v0.z-v1.z)*(v0.z-v1.z);
  170. }
  171. __forceinline Vector3 normal_vector(const Vector3 &v0,const Vector3 &v1, const Vector3& v2){
  172. return cross(v1-v0,v2-v0);
  173. }
  174. __forceinline Vector3 orthonormalize(const Vector3 &v, const Vector3 &ref)
  175. {
  176. Vector3 res = v - dot(v, ref) * ref;
  177. return normalize(res);
  178. }
  179. __forceinline Vector3 min(const Vector3 &v0, const Vector3 &v1)
  180. {
  181. return vector3(math::min(v0.x,v1.x), math::min(v0.y,v1.y), math::min(v0.z,v1.z));
  182. }
  183. __forceinline Vector3 max(const Vector3 &v0, const Vector3 &v1)
  184. {
  185. return vector3(math::max(v0.x,v1.x), math::max(v0.y,v1.y), math::max(v0.z,v1.z));
  186. }
  187. __forceinline bool all_gt(const Vector3& p1, const Vector3& p2)
  188. {
  189. return p1.x > p2.x && p1.y > p2.y && p1.z > p2.z;
  190. }
  191. __forceinline bool all_lt(const Vector3& p1, const Vector3& p2)
  192. {
  193. return p1.x < p2.x && p1.y < p2.y && p1.z < p2.z;
  194. }
  195. __forceinline void make_axes(const Vector3 &x, Vector3 &y, Vector3 &z)
  196. {
  197. if (x.z > -0.5 && x.z < 0.5) {
  198. y = vector3(-x.y, x.x, 0);
  199. y = orthonormalize(y, x);
  200. z = cross(x, y);
  201. } else {
  202. y = vector3(0, x.z, -x.y);
  203. y = orthonormalize(y, x);
  204. z = cross(x,y);
  205. }
  206. }
  207. __forceinline Vector3 lerp(const Vector3 &a, const Vector3 &b, float p)
  208. {
  209. return a * (1-p) + b * p;
  210. }
  211. __forceinline MagnitudeAndDirection magnitude_and_direction(const Vector3 &v)
  212. {
  213. MagnitudeAndDirection md;
  214. md.magnitude = norm(v);
  215. md.direction = md.magnitude > 0 ? v / md.magnitude : vector3(0,0,0);
  216. return md;
  217. }
  218. }