Vector4.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 v, float d)
  132. {
  133. return new Vector4(v.x * d, v.y * d, v.z * d, v.w * d);
  134. }
  135. public static Vector4 operator* (float d, Vector4 v)
  136. {
  137. return new Vector4(v.x * d, v.y * d, v.z * d, v.w * d);
  138. }
  139. public static Vector4 operator /(Vector4 v, float d)
  140. {
  141. return new Vector4(v.x / d, v.y / d, v.z / d, v.w / d);
  142. }
  143. public static bool operator== (Vector4 lhs, Vector4 rhs)
  144. {
  145. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z && lhs.w == rhs.w;
  146. }
  147. public static bool operator!= (Vector4 lhs, Vector4 rhs)
  148. {
  149. return !(lhs == rhs);
  150. }
  151. /// <summary>
  152. /// Scales one vector by another.
  153. /// </summary>
  154. /// <param name="a">First four dimensional vector.</param>
  155. /// <param name="b">Second four dimensional vector.</param>
  156. /// <returns>One vector scaled by another.</returns>
  157. public static Vector4 Scale(Vector4 a, Vector4 b)
  158. {
  159. return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
  160. }
  161. /// <summary>
  162. /// Normalizes the provided vector and returns the normalized copy.
  163. /// </summary>
  164. /// <param name="value">Vector to normalize.</param>
  165. /// <returns>Normalized copy of the vector.</returns>
  166. public static Vector4 Normalize(Vector4 value)
  167. {
  168. float num = Magnitude(value);
  169. if (num > 9.999999E-06)
  170. return value / num;
  171. return Zero;
  172. }
  173. /// <summary>
  174. /// Calculates the inner product of the two vectors.
  175. /// </summary>
  176. /// <param name="lhs">First four dimensional vector.</param>
  177. /// <param name="rhs">Second four dimensional vector.</param>
  178. /// <returns>Inner product between the two vectors.</returns>
  179. public static float Dot(Vector4 lhs, Vector4 rhs)
  180. {
  181. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
  182. }
  183. /// <summary>
  184. /// Calculates the distance between two points.
  185. /// </summary>
  186. /// <param name="a">First four dimensional point.</param>
  187. /// <param name="b">Second four dimensional point.</param>
  188. /// <returns>Distance between the two points.</returns>
  189. public static float Distance(Vector4 a, Vector4 b)
  190. {
  191. Vector4 vector4 = new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  192. return MathEx.Sqrt(vector4.x * vector4.x + vector4.y * vector4.y + vector4.z * vector4.z + vector4.w * vector4.w);
  193. }
  194. /// <summary>
  195. /// Calculates the magnitude of the provided vector.
  196. /// </summary>
  197. /// <param name="v">Vector to calculate the magnitude for.</param>
  198. /// <returns>Magnitude of the vector.</returns>
  199. public static float Magnitude(Vector4 v)
  200. {
  201. return MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  202. }
  203. /// <summary>
  204. /// Calculates the squared magnitude of the provided vector.
  205. /// </summary>
  206. /// <param name="v">Vector to calculate the magnitude for.</param>
  207. /// <returns>Squared magnitude of the vector.</returns>
  208. public static float SqrMagnitude(Vector4 v)
  209. {
  210. return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  211. }
  212. /// <summary>
  213. /// Scales the components of the vector by specified scale factors.
  214. /// </summary>
  215. /// <param name="scale">Scale factors to multiply components by.</param>
  216. public void Scale(Vector4 scale)
  217. {
  218. x *= scale.x;
  219. y *= scale.y;
  220. z *= scale.z;
  221. w *= scale.w;
  222. }
  223. /// <summary>
  224. /// Normalizes the vector.
  225. /// </summary>
  226. public void Normalize()
  227. {
  228. float num = Magnitude(this);
  229. if (num > 9.999999e-06f)
  230. this /= num;
  231. else
  232. this = Zero;
  233. }
  234. /// <inheritdoc/>
  235. public override int GetHashCode()
  236. {
  237. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2 ^ w.GetHashCode() >> 1;
  238. }
  239. /// <inheritdoc/>
  240. public override bool Equals(object other)
  241. {
  242. if (!(other is Vector4))
  243. return false;
  244. Vector4 vec = (Vector4)other;
  245. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z) && w.Equals(vec.w))
  246. return true;
  247. return false;
  248. }
  249. /// <inheritdoc/>
  250. public override string ToString()
  251. {
  252. return "(" + x + ", " + y + ", " + z + ", " + w + ")";
  253. }
  254. }
  255. }