Vector3.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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 Vector3 operator+ (Vector3 a, Vector3 b)
  78. {
  79. return new Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
  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 v)
  86. {
  87. return new Vector3(-v.x, -v.y, -v.z);
  88. }
  89. public static Vector3 operator* (Vector3 v, float d)
  90. {
  91. return new Vector3(v.x * d, v.y * d, v.z * d);
  92. }
  93. public static Vector3 operator* (float d, Vector3 v)
  94. {
  95. return new Vector3(v.x * d, v.y * d, v.z * d);
  96. }
  97. public static Vector3 operator/ (Vector3 v, float d)
  98. {
  99. return new Vector3(v.x / d, v.y / d, v.z / d);
  100. }
  101. public static Vector3 Scale(Vector3 a, Vector3 b)
  102. {
  103. return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
  104. }
  105. public static Vector3 Cross(Vector3 lhs, Vector3 rhs)
  106. {
  107. 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);
  108. }
  109. public static bool operator ==(Vector3 lhs, Vector3 rhs)
  110. {
  111. return lhs.x == rhs.x && lhs.y == rhs.y && lhs.z == rhs.z;
  112. }
  113. public static bool operator !=(Vector3 lhs, Vector3 rhs)
  114. {
  115. return !(lhs == rhs);
  116. }
  117. public static Vector3 Normalize(Vector3 value)
  118. {
  119. float num = Magnitude(value);
  120. if (num > 9.999999E-06)
  121. return value / num;
  122. return zero;
  123. }
  124. public static float Dot(Vector3 lhs, Vector3 rhs)
  125. {
  126. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z;
  127. }
  128. public static float Distance(Vector3 a, Vector3 b)
  129. {
  130. Vector3 vector3 = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  131. return MathEx.Sqrt(vector3.x * vector3.x + vector3.y * vector3.y + vector3.z * vector3.z);
  132. }
  133. public static float Magnitude(Vector3 v)
  134. {
  135. return MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
  136. }
  137. public static float SqrMagnitude(Vector3 v)
  138. {
  139. return (v.x * v.x + v.y * v.y + v.z * v.z);
  140. }
  141. public void Scale(Vector3 scale)
  142. {
  143. x *= scale.x;
  144. y *= scale.y;
  145. z *= scale.z;
  146. }
  147. public void Normalize()
  148. {
  149. float num = Magnitude(this);
  150. if (num > 9.999999E-06)
  151. this /= num;
  152. else
  153. this = zero;
  154. }
  155. public override int GetHashCode()
  156. {
  157. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
  158. }
  159. public override bool Equals(object other)
  160. {
  161. if (!(other is Vector3))
  162. return false;
  163. Vector3 vec = (Vector3)other;
  164. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z))
  165. return true;
  166. return false;
  167. }
  168. }
  169. }