2
0

Vector3.cs 11 KB

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