Vector3.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 v, float d)
  124. {
  125. return new Vector3(v.x * d, v.y * d, v.z * d);
  126. }
  127. public static Vector3 operator* (float d, Vector3 v)
  128. {
  129. return new Vector3(v.x * d, v.y * d, v.z * d);
  130. }
  131. public static Vector3 operator/ (Vector3 v, float d)
  132. {
  133. return new Vector3(v.x / d, v.y / d, v.z / d);
  134. }
  135. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  136. {
  137. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  138. }
  139. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  140. {
  141. return !(lhs == rhs);
  142. }
  143. /// <summary>
  144. /// Calculates the magnitude of the provided vector.
  145. /// </summary>
  146. /// <param name="v">Vector to calculate the magnitude for.</param>
  147. /// <returns>Magnitude of the vector.</returns>
  148. public static float Magnitude(Vector3 v)
  149. {
  150. return MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  151. }
  152. /// <summary>
  153. /// Calculates the squared magnitude of the provided vector.
  154. /// </summary>
  155. /// <param name="v">Vector to calculate the magnitude for.</param>
  156. /// <returns>Squared magnitude of the vector.</returns>
  157. public static float SqrMagnitude(Vector3 v)
  158. {
  159. return (v.x * v.x + v.y * v.y + v.z * v.z);
  160. }
  161. /// <summary>
  162. /// Scales one vector by another.
  163. /// </summary>
  164. /// <param name="a">First three dimensional vector.</param>
  165. /// <param name="b">Second three dimensional vector.</param>
  166. /// <returns>One vector scaled by another.</returns>
  167. public static Vector3 Scale(Vector3 a, Vector3 b)
  168. {
  169. return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
  170. }
  171. /// <summary>
  172. /// Returns the cross product between two vectors.
  173. /// </summary>
  174. /// <param name="lhs">First three dimensional vector.</param>
  175. /// <param name="rhs">Second three dimensional vector.</param>
  176. /// <returns>Cross product between two vectors.</returns>
  177. public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
  178. {
  179. 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);
  180. }
  181. /// <summary>
  182. /// Normalizes the provided vector and returns the normalized copy.
  183. /// </summary>
  184. /// <param name="value">Vector to normalize.</param>
  185. /// <returns>Normalized copy of the vector.</returns>
  186. public static Vector3 Normalize(Vector3 value)
  187. {
  188. float num = value.Length;
  189. if (num > 9.999999E-06)
  190. return value / num;
  191. return Zero;
  192. }
  193. /// <summary>
  194. /// Calculates the inner product of the two vectors.
  195. /// </summary>
  196. /// <param name="lhs">First three dimensional vector.</param>
  197. /// <param name="rhs">Second three dimensional vector.</param>
  198. /// <returns>Inner product between the two vectors.</returns>
  199. public static float Dot(Vector3 lhs, Vector3 rhs)
  200. {
  201. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  202. }
  203. /// <summary>
  204. /// Calculates the distance between two points.
  205. /// </summary>
  206. /// <param name="a">First three dimensional point.</param>
  207. /// <param name="b">Second three dimensional point.</param>
  208. /// <returns>Distance between the two points.</returns>
  209. public static float Distance(Vector3 a, Vector3 b)
  210. {
  211. Vector3 vector3 = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  212. return MathEx.Sqrt(vector3.x * vector3.x + vector3.y * vector3.y + vector3.z * vector3.z);
  213. }
  214. /// <summary>
  215. /// Scales the components of the vector by specified scale factors.
  216. /// </summary>
  217. /// <param name="scale">Scale factors to multiply components by.</param>
  218. public void Scale(Vector3 scale)
  219. {
  220. x *= scale.x;
  221. y *= scale.y;
  222. z *= scale.z;
  223. }
  224. /// <summary>
  225. /// Normalizes the vector.
  226. /// </summary>
  227. public void Normalize()
  228. {
  229. float num = Length;
  230. if (num > 9.999999E-06)
  231. this /= num;
  232. else
  233. this = Zero;
  234. }
  235. /// <summary>
  236. /// Calculates two vectors orthonormal to the first vector.
  237. /// </summary>
  238. /// <param name="x">Normalized vector to calculate orthonormal vectors for.</param>
  239. /// <param name="y">First orthonormal vector.</param>
  240. /// <param name="z">Second orthonormal vector.</param>
  241. public static void OrthogonalComplement(Vector3 x, out Vector3 y, out Vector3 z)
  242. {
  243. if (MathEx.Abs(x.x) > MathEx.Abs(x.y))
  244. y = new Vector3(-x.z, 0, x.x);
  245. else
  246. y = new Vector3(0, x.z, -x.y);
  247. z = Cross(x, y);
  248. Orthonormalize(ref x, ref y, ref z);
  249. }
  250. /// <summary>
  251. /// Performs Gram-Schmidt orthonormalization on the specified basis, making all the vectors
  252. /// normal and orthogonal to each other.
  253. /// </summary>
  254. /// <param name="x">First vector to orthogonalize.</param>
  255. /// <param name="y">Second vector to orthogonalize.</param>
  256. /// <param name="z">Third vector to orthogonalize.</param>
  257. public static void Orthonormalize(ref Vector3 x, ref Vector3 y, ref Vector3 z)
  258. {
  259. x.Normalize();
  260. float dot0 = Vector3.Dot(x, y);
  261. y -= dot0 * x;
  262. y.Normalize();
  263. float dot1 = Vector3.Dot(y, z);
  264. dot0 = Vector3.Dot(x, z);
  265. z -= dot0 * x + dot1 * y;
  266. z.Normalize();
  267. }
  268. /// <inheritdoc/>
  269. public override int GetHashCode()
  270. {
  271. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
  272. }
  273. /// <inheritdoc/>
  274. public override bool Equals(object other)
  275. {
  276. if (!(other is Vector3))
  277. return false;
  278. Vector3 vec = (Vector3)other;
  279. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z))
  280. return true;
  281. return false;
  282. }
  283. /// <inheritdoc/>
  284. public override string ToString()
  285. {
  286. return "(" + x + ", " + y + ", " + z + ")";
  287. }
  288. }
  289. }