Vector3.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 three dimensional vector.
  9. /// </summary>
  10. [StructLayout(LayoutKind.Sequential), SerializeObject]
  11. public struct Vector3 // Note: Must match C++ class Vector3
  12. {
  13. public static readonly Vector3 Zero = new Vector3(0.0f, 0.0f, 0.0f);
  14. public static readonly Vector3 One = new Vector3(1.0f, 1.0f, 1.0f);
  15. public static readonly Vector3 XAxis = new Vector3(1.0f, 0.0f, 0.0f);
  16. public static readonly Vector3 YAxis = new Vector3(0.0f, 1.0f, 0.0f);
  17. public static readonly Vector3 ZAxis = new Vector3(0.0f, 0.0f, 1.0f);
  18. public float x;
  19. public float y;
  20. public float z;
  21. /// <summary>
  22. /// Accesses a specific component of the vector.
  23. /// </summary>
  24. /// <param name="index">Index of the component.</param>
  25. /// <returns>Value of the specific component.</returns>
  26. public float this[int index]
  27. {
  28. get
  29. {
  30. switch (index)
  31. {
  32. case 0:
  33. return x;
  34. case 1:
  35. return y;
  36. case 2:
  37. return z;
  38. default:
  39. throw new IndexOutOfRangeException("Invalid Vector3 index.");
  40. }
  41. }
  42. set
  43. {
  44. switch (index)
  45. {
  46. case 0:
  47. x = value;
  48. break;
  49. case 1:
  50. y = value;
  51. break;
  52. case 2:
  53. z = value;
  54. break;
  55. default:
  56. throw new IndexOutOfRangeException("Invalid Vector3 index.");
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// Returns a normalized copy of the vector.
  62. /// </summary>
  63. public Vector3 Normalized
  64. {
  65. get
  66. {
  67. return Normalize(this);
  68. }
  69. }
  70. /// <summary>
  71. /// Returns the length of the vector.
  72. /// </summary>
  73. public float Length
  74. {
  75. get
  76. {
  77. return (float)MathEx.Sqrt(x * x + y * y + z * z);
  78. }
  79. }
  80. /// <summary>
  81. /// Returns the squared length of the vector.
  82. /// </summary>
  83. public float SqrdLength
  84. {
  85. get
  86. {
  87. return (x * x + y * y + z * z);
  88. }
  89. }
  90. /// <summary>
  91. /// Creates a new three dimensional vector.
  92. /// </summary>
  93. /// <param name="x">X coordinate.</param>
  94. /// <param name="y">Y coordinate.</param>
  95. /// <param name="z">Z coordinate.</param>
  96. public Vector3(float x, float y, float z)
  97. {
  98. this.x = x;
  99. this.y = y;
  100. this.z = z;
  101. }
  102. /// <summary>
  103. /// Converts a three dimensional vector into a four dimensional vector. w component will be set to zero.
  104. /// </summary>
  105. /// <param name="vec">Vector to convert.</param>
  106. /// <returns>A new four dimensional vector.</returns>
  107. public static explicit operator Vector4(Vector3 vec)
  108. {
  109. return new Vector4(vec.x, vec.y, vec.z, 0.0f);
  110. }
  111. public static Vector3 operator+ (Vector3 a, Vector3 b)
  112. {
  113. return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
  114. }
  115. public static Vector3 operator- (Vector3 a, Vector3 b)
  116. {
  117. return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  118. }
  119. public static Vector3 operator- (Vector3 v)
  120. {
  121. return new Vector3(-v.x, -v.y, -v.z);
  122. }
  123. public static Vector3 operator*(Vector3 a, Vector3 b)
  124. {
  125. return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
  126. }
  127. public static Vector3 operator* (Vector3 v, float d)
  128. {
  129. return new Vector3(v.x * d, v.y * d, v.z * d);
  130. }
  131. public static Vector3 operator* (float d, Vector3 v)
  132. {
  133. return new Vector3(v.x * d, v.y * d, v.z * d);
  134. }
  135. public static Vector3 operator/ (Vector3 v, float d)
  136. {
  137. return new Vector3(v.x / d, v.y / d, v.z / d);
  138. }
  139. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  140. {
  141. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  142. }
  143. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  144. {
  145. return !(lhs == rhs);
  146. }
  147. /// <summary>
  148. /// Calculates the magnitude of the provided vector.
  149. /// </summary>
  150. /// <param name="v">Vector to calculate the magnitude for.</param>
  151. /// <returns>Magnitude of the vector.</returns>
  152. public static float Magnitude(Vector3 v)
  153. {
  154. return MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  155. }
  156. /// <summary>
  157. /// Calculates the squared magnitude of the provided vector.
  158. /// </summary>
  159. /// <param name="v">Vector to calculate the magnitude for.</param>
  160. /// <returns>Squared magnitude of the vector.</returns>
  161. public static float SqrMagnitude(Vector3 v)
  162. {
  163. return (v.x * v.x + v.y * v.y + v.z * v.z);
  164. }
  165. /// <summary>
  166. /// Scales one vector by another.
  167. /// </summary>
  168. /// <param name="a">First three dimensional vector.</param>
  169. /// <param name="b">Second three dimensional vector.</param>
  170. /// <returns>One vector scaled by another.</returns>
  171. public static Vector3 Scale(Vector3 a, Vector3 b)
  172. {
  173. return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
  174. }
  175. /// <summary>
  176. /// Returns the cross product between two vectors.
  177. /// </summary>
  178. /// <param name="lhs">First three dimensional vector.</param>
  179. /// <param name="rhs">Second three dimensional vector.</param>
  180. /// <returns>Cross product between two vectors.</returns>
  181. public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
  182. {
  183. return new Vector3(lhs.y * rhs.z - lhs.z * rhs.y, lhs.z * rhs.x - lhs.x * rhs.z, lhs.x * rhs.y - lhs.y * rhs.x);
  184. }
  185. /// <summary>
  186. /// Normalizes the provided vector and returns the normalized copy.
  187. /// </summary>
  188. /// <param name="value">Vector to normalize.</param>
  189. /// <returns>Normalized copy of the vector.</returns>
  190. public static Vector3 Normalize(Vector3 value)
  191. {
  192. float num = value.Length;
  193. if (num > 9.999999E-06)
  194. return value / num;
  195. return Zero;
  196. }
  197. /// <summary>
  198. /// Calculates the inner product of the two vectors.
  199. /// </summary>
  200. /// <param name="lhs">First three dimensional vector.</param>
  201. /// <param name="rhs">Second three dimensional vector.</param>
  202. /// <returns>Inner product between the two vectors.</returns>
  203. public static float Dot(Vector3 lhs, Vector3 rhs)
  204. {
  205. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  206. }
  207. /// <summary>
  208. /// Calculates the distance between two points.
  209. /// </summary>
  210. /// <param name="a">First three dimensional point.</param>
  211. /// <param name="b">Second three dimensional point.</param>
  212. /// <returns>Distance between the two points.</returns>
  213. public static float Distance(Vector3 a, Vector3 b)
  214. {
  215. Vector3 vector3 = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  216. return MathEx.Sqrt(vector3.x * vector3.x + vector3.y * vector3.y + vector3.z * vector3.z);
  217. }
  218. /// <summary>
  219. /// Scales the components of the vector by specified scale factors.
  220. /// </summary>
  221. /// <param name="scale">Scale factors to multiply components by.</param>
  222. public void Scale(Vector3 scale)
  223. {
  224. x *= scale.x;
  225. y *= scale.y;
  226. z *= scale.z;
  227. }
  228. /// <summary>
  229. /// Normalizes the vector.
  230. /// </summary>
  231. public void Normalize()
  232. {
  233. float num = Length;
  234. if (num > 9.999999E-06)
  235. this /= num;
  236. else
  237. this = Zero;
  238. }
  239. /// <summary>
  240. /// Calculates two vectors orthonormal to the first vector.
  241. /// </summary>
  242. /// <param name="x">Normalized vector to calculate orthonormal vectors for.</param>
  243. /// <param name="y">First orthonormal vector.</param>
  244. /// <param name="z">Second orthonormal vector.</param>
  245. public static void OrthogonalComplement(Vector3 x, out Vector3 y, out Vector3 z)
  246. {
  247. if (MathEx.Abs(x.x) > MathEx.Abs(x.y))
  248. y = new Vector3(-x.z, 0, x.x);
  249. else
  250. y = new Vector3(0, x.z, -x.y);
  251. z = Cross(x, y);
  252. Orthonormalize(ref x, ref y, ref z);
  253. }
  254. /// <summary>
  255. /// Performs Gram-Schmidt orthonormalization on the specified basis, making all the vectors
  256. /// normal and orthogonal to each other.
  257. /// </summary>
  258. /// <param name="x">First vector to orthogonalize.</param>
  259. /// <param name="y">Second vector to orthogonalize.</param>
  260. /// <param name="z">Third vector to orthogonalize.</param>
  261. public static void Orthonormalize(ref Vector3 x, ref Vector3 y, ref Vector3 z)
  262. {
  263. x.Normalize();
  264. float dot0 = Vector3.Dot(x, y);
  265. y -= dot0 * x;
  266. y.Normalize();
  267. float dot1 = Vector3.Dot(y, z);
  268. dot0 = Vector3.Dot(x, z);
  269. z -= dot0 * x + dot1 * y;
  270. z.Normalize();
  271. }
  272. /// <inheritdoc/>
  273. public override int GetHashCode()
  274. {
  275. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
  276. }
  277. /// <inheritdoc/>
  278. public override bool Equals(object other)
  279. {
  280. if (!(other is Vector3))
  281. return false;
  282. Vector3 vec = (Vector3)other;
  283. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z))
  284. return true;
  285. return false;
  286. }
  287. /// <inheritdoc/>
  288. public override string ToString()
  289. {
  290. return "(" + x + ", " + y + ", " + z + ")";
  291. }
  292. }
  293. }