Vector3.cs 12 KB

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