Vector3.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = value.Magnitude;
  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 void Scale(Vector3 scale)
  138. {
  139. x *= scale.x;
  140. y *= scale.y;
  141. z *= scale.z;
  142. }
  143. public void Normalize()
  144. {
  145. float num = Magnitude;
  146. if (num > 9.999999E-06)
  147. this /= num;
  148. else
  149. this = zero;
  150. }
  151. public override int GetHashCode()
  152. {
  153. return x.GetHashCode() ^ y.GetHashCode() << 2 ^ z.GetHashCode() >> 2;
  154. }
  155. public override bool Equals(object other)
  156. {
  157. if (!(other is Vector3))
  158. return false;
  159. Vector3 vec = (Vector3)other;
  160. if (x.Equals(vec.x) && y.Equals(vec.y) && z.Equals(vec.z))
  161. return true;
  162. return false;
  163. }
  164. public override string ToString()
  165. {
  166. return "(" + x + ", " + y + ", " + z + ")";
  167. }
  168. public static void OrthogonalComplement(Vector3 x, out Vector3 y, out Vector3 z)
  169. {
  170. if (MathEx.Abs(x.x) > MathEx.Abs(x.y))
  171. y = new Vector3(-x.z, 0, x.x);
  172. else
  173. y = new Vector3(0, x.z, -x.y);
  174. z = Cross(x, y);
  175. Orthonormalize(ref x, ref y, ref z);
  176. }
  177. public static void Orthonormalize(ref Vector3 x, ref Vector3 y, ref Vector3 z)
  178. {
  179. x.Normalize();
  180. float dot0 = Vector3.Dot(x, y);
  181. y -= dot0 * x;
  182. y.Normalize();
  183. float dot1 = Vector3.Dot(y, z);
  184. dot0 = Vector3.Dot(x, z);
  185. z -= dot0 * x + dot1 * y;
  186. z.Normalize();
  187. }
  188. }
  189. }