Vector3.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential), SerializeObject]
  6. public struct Vector3
  7. {
  8. public static readonly Vector3 zero = new Vector3(0.0f, 0.0f, 0.0f);
  9. public static readonly Vector3 one = new Vector3(1.0f, 1.0f, 1.0f);
  10. public static readonly Vector3 xAxis = new Vector3(1.0f, 0.0f, 0.0f);
  11. public static readonly Vector3 yAxis = new Vector3(0.0f, 1.0f, 0.0f);
  12. public static readonly Vector3 zAxis = new Vector3(0.0f, 0.0f, 1.0f);
  13. public float x;
  14. public float y;
  15. public float z;
  16. public float this[int index]
  17. {
  18. get
  19. {
  20. switch (index)
  21. {
  22. case 0:
  23. return x;
  24. case 1:
  25. return y;
  26. case 2:
  27. return z;
  28. default:
  29. throw new IndexOutOfRangeException("Invalid Vector3 index.");
  30. }
  31. }
  32. set
  33. {
  34. switch (index)
  35. {
  36. case 0:
  37. x = value;
  38. break;
  39. case 1:
  40. y = value;
  41. break;
  42. case 2:
  43. z = value;
  44. break;
  45. default:
  46. throw new IndexOutOfRangeException("Invalid Vector3 index.");
  47. }
  48. }
  49. }
  50. public Vector3 normalized
  51. {
  52. get
  53. {
  54. return Normalize(this);
  55. }
  56. }
  57. public float magnitude
  58. {
  59. get
  60. {
  61. return (float)MathEx.Sqrt(x * x + y * y + z * z);
  62. }
  63. }
  64. public float sqrdMagnitude
  65. {
  66. get
  67. {
  68. return (x * x + y * y + z * z);
  69. }
  70. }
  71. public Vector3(float x, float y, float z)
  72. {
  73. this.x = x;
  74. this.y = y;
  75. this.z = z;
  76. }
  77. public static explicit operator Vector4(Vector3 vec)
  78. {
  79. return new Vector4(vec.x, vec.y, vec.z, 0.0f);
  80. }
  81. public static Vector3 operator+ (Vector3 a, Vector3 b)
  82. {
  83. return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
  84. }
  85. public static Vector3 operator- (Vector3 a, Vector3 b)
  86. {
  87. return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  88. }
  89. public static Vector3 operator- (Vector3 v)
  90. {
  91. return new Vector3(-v.x, -v.y, -v.z);
  92. }
  93. public static Vector3 operator* (Vector3 v, float d)
  94. {
  95. return new Vector3(v.x * d, v.y * d, v.z * d);
  96. }
  97. public static Vector3 operator* (float d, Vector3 v)
  98. {
  99. return new Vector3(v.x * d, v.y * d, v.z * d);
  100. }
  101. public static Vector3 operator/ (Vector3 v, float d)
  102. {
  103. return new Vector3(v.x / d, v.y / d, v.z / d);
  104. }
  105. public static Vector3 Scale(Vector3 a, Vector3 b)
  106. {
  107. return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
  108. }
  109. public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
  110. {
  111. 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);
  112. }
  113. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  114. {
  115. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  116. }
  117. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  118. {
  119. return !(lhs == rhs);
  120. }
  121. public static Vector3 Normalize(Vector3 value)
  122. {
  123. float num = Magnitude(value);
  124. if (num > 9.999999E-06)
  125. return value / num;
  126. return zero;
  127. }
  128. public static float Dot(Vector3 lhs, Vector3 rhs)
  129. {
  130. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  131. }
  132. public static float Distance(Vector3 a, Vector3 b)
  133. {
  134. Vector3 vector3 = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  135. return MathEx.Sqrt(vector3.x * vector3.x + vector3.y * vector3.y + vector3.z * vector3.z);
  136. }
  137. public static float Magnitude(Vector3 v)
  138. {
  139. return (float)MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  140. }
  141. public static float SqrMagnitude(Vector3 v)
  142. {
  143. return (v.x * v.x + v.y * v.y + v.z * v.z);
  144. }
  145. public void Scale(Vector3 scale)
  146. {
  147. x *= scale.x;
  148. y *= scale.y;
  149. z *= scale.z;
  150. }
  151. public void Normalize()
  152. {
  153. float num = Magnitude(this);
  154. if (num > 9.999999E-06)
  155. this /= num;
  156. else
  157. this = zero;
  158. }
  159. public override int GetHashCode()
  160. {
  161. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
  162. }
  163. public override bool Equals(object other)
  164. {
  165. if (!(other is Vector3))
  166. return false;
  167. Vector3 vec = (Vector3)other;
  168. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z))
  169. return true;
  170. return false;
  171. }
  172. public override string ToString()
  173. {
  174. return "(" + x + ", " + y + ", " + z + ")";
  175. }
  176. public static void OrthogonalComplement(Vector3 x, out Vector3 y, out Vector3 z)
  177. {
  178. if (MathEx.Abs(x.x) > MathEx.Abs(x.y))
  179. y = new Vector3(-x.z, 0, x.x);
  180. else
  181. y = new Vector3(0, x.z, -x.y);
  182. z = Cross(x, y);
  183. Orthonormalize(ref x, ref y, ref z);
  184. }
  185. public static void Orthonormalize(ref Vector3 x, ref Vector3 y, ref Vector3 z)
  186. {
  187. x.Normalize();
  188. float dot0 = Vector3.Dot(x, y);
  189. y -= dot0 * x;
  190. y.Normalize();
  191. float dot1 = Vector3.Dot(y, z);
  192. dot0 = Vector3.Dot(x, z);
  193. z -= dot0 * x + dot1 * y;
  194. z.Normalize();
  195. }
  196. }
  197. }