Vector4.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace BansheeEngine
  6. {
  7. /// <summary>
  8. /// A four dimensional vector with a homogeneous w coordinate.
  9. /// </summary>
  10. [StructLayout(LayoutKind.Sequential), SerializeObject]
  11. public struct Vector4
  12. {
  13. public static readonly Vector4 Zero = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
  14. public static readonly Vector4 One = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  15. public static readonly Vector4 XAxis = new Vector4(1.0f, 0.0f, 0.0f, 0.0f);
  16. public static readonly Vector4 YAxis = new Vector4(0.0f, 1.0f, 0.0f, 0.0f);
  17. public static readonly Vector4 ZAxis = new Vector4(0.0f, 0.0f, 1.0f, 0.0f);
  18. public float x;
  19. public float y;
  20. public float z;
  21. public float w;
  22. /// <summary>
  23. /// Accesses a specific component of the vector.
  24. /// </summary>
  25. /// <param name="index">Index of the component.</param>
  26. /// <returns>Value of the specific component.</returns>
  27. public float this[int index]
  28. {
  29. get
  30. {
  31. switch (index)
  32. {
  33. case 0:
  34. return x;
  35. case 1:
  36. return y;
  37. case 2:
  38. return z;
  39. case 3:
  40. return w;
  41. default:
  42. throw new IndexOutOfRangeException("Invalid Vector4 index.");
  43. }
  44. }
  45. set
  46. {
  47. switch (index)
  48. {
  49. case 0:
  50. x = value;
  51. break;
  52. case 1:
  53. y = value;
  54. break;
  55. case 2:
  56. z = value;
  57. break;
  58. case 3:
  59. w = value;
  60. break;
  61. default:
  62. throw new IndexOutOfRangeException("Invalid Vector4 index.");
  63. }
  64. }
  65. }
  66. /// <summary>
  67. /// Returns a normalized copy of the vector.
  68. /// </summary>
  69. public Vector4 Normalized
  70. {
  71. get
  72. {
  73. return Normalize(this);
  74. }
  75. }
  76. /// <summary>
  77. /// Returns the length of the vector.
  78. /// </summary>
  79. public float Length
  80. {
  81. get
  82. {
  83. return MathEx.Sqrt(x * x + y * y + z * z + w * w);
  84. }
  85. }
  86. /// <summary>
  87. /// Returns the squared length of the vector.
  88. /// </summary>
  89. public float SqrdLength
  90. {
  91. get
  92. {
  93. return (x * x + y * y + z * z + w * w);
  94. }
  95. }
  96. /// <summary>
  97. /// Creates a new four dimensional vector.
  98. /// </summary>
  99. /// <param name="x">X coordinate.</param>
  100. /// <param name="y">Y coordinate.</param>
  101. /// <param name="z">Z coordinate.</param>
  102. /// <param name="w">Homogeneous W coordinate.</param>
  103. public Vector4(float x, float y, float z, float w)
  104. {
  105. this.x = x;
  106. this.y = y;
  107. this.z = z;
  108. this.w = w;
  109. }
  110. /// <summary>
  111. /// Converts a homogenous vector into a three dimensional vector. w component is discarded.
  112. /// </summary>
  113. /// <param name="vec">Vector to convert.</param>
  114. /// <returns>A new three dimensional vector.</returns>
  115. public static explicit operator Vector3(Vector4 vec)
  116. {
  117. return new Vector3(vec.x, vec.y, vec.z);
  118. }
  119. public static Vector4 operator+ (Vector4 a, Vector4 b)
  120. {
  121. return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  122. }
  123. public static Vector4 operator- (Vector4 a, Vector4 b)
  124. {
  125. return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  126. }
  127. public static Vector4 operator- (Vector4 v)
  128. {
  129. return new Vector4(-v.x, -v.y, -v.z, -v.w);
  130. }
  131. public static Vector4 operator *(Vector4 a, Vector4 b)
  132. {
  133. return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
  134. }
  135. public static Vector4 operator* (Vector4 v, float d)
  136. {
  137. return new Vector4(v.x * d, v.y * d, v.z * d, v.w * d);
  138. }
  139. public static Vector4 operator* (float d, Vector4 v)
  140. {
  141. return new Vector4(v.x * d, v.y * d, v.z * d, v.w * d);
  142. }
  143. public static Vector4 operator /(Vector4 v, float d)
  144. {
  145. return new Vector4(v.x / d, v.y / d, v.z / d, v.w / d);
  146. }
  147. public static bool operator== (Vector4 lhs, Vector4 rhs)
  148. {
  149. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w;
  150. }
  151. public static bool operator!= (Vector4 lhs, Vector4 rhs)
  152. {
  153. return !(lhs == rhs);
  154. }
  155. /// <summary>
  156. /// Scales one vector by another.
  157. /// </summary>
  158. /// <param name="a">First four dimensional vector.</param>
  159. /// <param name="b">Second four dimensional vector.</param>
  160. /// <returns>One vector scaled by another.</returns>
  161. public static Vector4 Scale(Vector4 a, Vector4 b)
  162. {
  163. return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
  164. }
  165. /// <summary>
  166. /// Normalizes the provided vector and returns the normalized copy.
  167. /// </summary>
  168. /// <param name="value">Vector to normalize.</param>
  169. /// <returns>Normalized copy of the vector.</returns>
  170. public static Vector4 Normalize(Vector4 value)
  171. {
  172. float num = Magnitude(value);
  173. if (num > 9.999999E-06)
  174. return value / num;
  175. return Zero;
  176. }
  177. /// <summary>
  178. /// Calculates the inner product of the two vectors.
  179. /// </summary>
  180. /// <param name="lhs">First four dimensional vector.</param>
  181. /// <param name="rhs">Second four dimensional vector.</param>
  182. /// <returns>Inner product between the two vectors.</returns>
  183. public static float Dot(Vector4 lhs, Vector4 rhs)
  184. {
  185. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
  186. }
  187. /// <summary>
  188. /// Calculates the distance between two points.
  189. /// </summary>
  190. /// <param name="a">First four dimensional point.</param>
  191. /// <param name="b">Second four dimensional point.</param>
  192. /// <returns>Distance between the two points.</returns>
  193. public static float Distance(Vector4 a, Vector4 b)
  194. {
  195. Vector4 vector4 = new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  196. return MathEx.Sqrt(vector4.x * vector4.x + vector4.y * vector4.y + vector4.z * vector4.z + vector4.w * vector4.w);
  197. }
  198. /// <summary>
  199. /// Calculates the magnitude of the provided vector.
  200. /// </summary>
  201. /// <param name="v">Vector to calculate the magnitude for.</param>
  202. /// <returns>Magnitude of the vector.</returns>
  203. public static float Magnitude(Vector4 v)
  204. {
  205. return MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  206. }
  207. /// <summary>
  208. /// Calculates the squared magnitude of the provided vector.
  209. /// </summary>
  210. /// <param name="v">Vector to calculate the magnitude for.</param>
  211. /// <returns>Squared magnitude of the vector.</returns>
  212. public static float SqrMagnitude(Vector4 v)
  213. {
  214. return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  215. }
  216. /// <summary>
  217. /// Scales the components of the vector by specified scale factors.
  218. /// </summary>
  219. /// <param name="scale">Scale factors to multiply components by.</param>
  220. public void Scale(Vector4 scale)
  221. {
  222. x *= scale.x;
  223. y *= scale.y;
  224. z *= scale.z;
  225. w *= scale.w;
  226. }
  227. /// <summary>
  228. /// Normalizes the vector.
  229. /// </summary>
  230. public void Normalize()
  231. {
  232. float num = Magnitude(this);
  233. if (num > 9.999999e-06f)
  234. this /= num;
  235. else
  236. this = Zero;
  237. }
  238. /// <inheritdoc/>
  239. public override int GetHashCode()
  240. {
  241. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2 ^ w.GetHashCode() >> 1;
  242. }
  243. /// <inheritdoc/>
  244. public override bool Equals(object other)
  245. {
  246. if (!(other is Vector4))
  247. return false;
  248. Vector4 vec = (Vector4)other;
  249. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z) && w.Equals(vec.w))
  250. return true;
  251. return false;
  252. }
  253. /// <inheritdoc/>
  254. public override string ToString()
  255. {
  256. return "(" + x + ", " + y + ", " + z + ", " + w + ")";
  257. }
  258. }
  259. }