Vector4.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace BansheeEngine
  4. {
  5. [StructLayout(LayoutKind.Sequential)]
  6. public struct Vector4
  7. {
  8. public float x;
  9. public float y;
  10. public float z;
  11. public float w;
  12. public static Vector4 zero
  13. {
  14. get
  15. {
  16. return new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
  17. }
  18. }
  19. public static Vector4 one
  20. {
  21. get
  22. {
  23. return new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
  24. }
  25. }
  26. public float this[int index]
  27. {
  28. get
  29. {
  30. switch (index)
  31. {
  32. case 0:
  33. return x;
  34. case 1:
  35. return y;
  36. case 2:
  37. return z;
  38. case 3:
  39. return w;
  40. default:
  41. throw new IndexOutOfRangeException("Invalid Vector4 index.");
  42. }
  43. }
  44. set
  45. {
  46. switch (index)
  47. {
  48. case 0:
  49. x = value;
  50. break;
  51. case 1:
  52. y = value;
  53. break;
  54. case 2:
  55. z = value;
  56. break;
  57. case 3:
  58. w = value;
  59. break;
  60. default:
  61. throw new IndexOutOfRangeException("Invalid Vector4 index.");
  62. }
  63. }
  64. }
  65. public Vector4 normalized
  66. {
  67. get
  68. {
  69. return Normalize(this);
  70. }
  71. }
  72. public float magnitude
  73. {
  74. get
  75. {
  76. return MathEx.Sqrt(x * x + y * y + z * z + w * w);
  77. }
  78. }
  79. public float sqrdMagnitude
  80. {
  81. get
  82. {
  83. return (x * x + y * y + z * z + w * w);
  84. }
  85. }
  86. public Vector4(float x, float y, float z, float w)
  87. {
  88. this.x = x;
  89. this.y = y;
  90. this.z = z;
  91. this.w = w;
  92. }
  93. public static Vector4 operator+ (Vector4 a, Vector4 b)
  94. {
  95. return new Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  96. }
  97. public static Vector4 operator- (Vector4 a, Vector4 b)
  98. {
  99. return new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  100. }
  101. public static Vector4 operator- (Vector4 v)
  102. {
  103. return new Vector4(-v.x, -v.y, -v.z, -v.w);
  104. }
  105. public static Vector4 operator* (Vector4 v, float d)
  106. {
  107. return new Vector4(v.x * d, v.y * d, v.z * d, v.w * d);
  108. }
  109. public static Vector4 operator* (float d, Vector4 v)
  110. {
  111. return new Vector4(v.x * d, v.y * d, v.z * d, v.w * d);
  112. }
  113. public static Vector4 operator /(Vector4 v, float d)
  114. {
  115. return new Vector4(v.x / d, v.y / d, v.z / d, v.w / d);
  116. }
  117. public static Vector4 Scale(Vector4 a, Vector4 b)
  118. {
  119. return new Vector4(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w);
  120. }
  121. public static Vector4 Normalize(Vector4 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(Vector4 lhs, Vector4 rhs)
  129. {
  130. return lhs.x * rhs.x + lhs.y * rhs.y + lhs.z * rhs.z + lhs.w * rhs.w;
  131. }
  132. public static float Distance(Vector4 a, Vector4 b)
  133. {
  134. Vector4 vector4 = new Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  135. return MathEx.Sqrt(vector4.x * vector4.x + vector4.y * vector4.y + vector4.z * vector4.z + vector4.w * vector4.w);
  136. }
  137. public static float Magnitude(Vector4 v)
  138. {
  139. return MathEx.Sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  140. }
  141. public static float SqrMagnitude(Vector4 v)
  142. {
  143. return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
  144. }
  145. public void Scale(Vector4 scale)
  146. {
  147. x *= scale.x;
  148. y *= scale.y;
  149. z *= scale.z;
  150. w *= scale.w;
  151. }
  152. public void Normalize()
  153. {
  154. float num = Magnitude(this);
  155. if (num > 9.999999E-06)
  156. this /= num;
  157. else
  158. this = zero;
  159. }
  160. }
  161. }