DVector3.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using UnityEngine;
  2. using M = System.Math;
  3. // ReSharper disable InconsistentNaming
  4. namespace ARLocation
  5. {
  6. [System.Serializable]
  7. public struct DVector3
  8. {
  9. public double x;
  10. public double y;
  11. public double z;
  12. public DVector3(Vector3 v)
  13. {
  14. x = v.x;
  15. y = v.y;
  16. z = v.z;
  17. }
  18. /// <summary>
  19. /// Gets the magnitude of the vector.
  20. /// </summary>
  21. /// <value>The magnitude.</value>
  22. public double magnitude
  23. {
  24. get
  25. {
  26. return M.Sqrt(x * x + y * y + z * z);
  27. }
  28. }
  29. /// <summary>
  30. /// Gets the normalized version of this vector.
  31. /// </summary>
  32. /// <value>The normalized.</value>
  33. public DVector3 normalized
  34. {
  35. get
  36. {
  37. var m = magnitude;
  38. if (m < 0.00000001)
  39. {
  40. return new DVector3();
  41. }
  42. return new DVector3(x, y, z) / magnitude;
  43. }
  44. }
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="T:DVector3"/> struct.
  47. /// </summary>
  48. public DVector3(double newX = 0.0, double newY = 0.0, double newZ = 0.0)
  49. {
  50. x = newX;
  51. y = newY;
  52. z = newZ;
  53. }
  54. /// <summary>
  55. /// Converts to a Vector3.
  56. /// </summary>
  57. /// <returns>The vector2.</returns>
  58. public Vector3 toVector3()
  59. {
  60. return new Vector3((float)x, (float)y, (float)z);
  61. }
  62. /// <summary>
  63. /// Equals the specified v and e.
  64. /// </summary>
  65. /// <returns>The equals.</returns>
  66. /// <param name="v">V.</param>
  67. /// <param name="e">E.</param>
  68. public bool Equals(DVector3 v, double e = 0.00005)
  69. {
  70. return (M.Abs(x - v.x) <= e) && (M.Abs(y - v.y) <= e) && (M.Abs(z - v.z) <= e);
  71. }
  72. /// <summary>
  73. /// Normalize this instance.
  74. /// </summary>
  75. public void Normalize()
  76. {
  77. double m = magnitude;
  78. x /= m;
  79. y /= m;
  80. z /= m;
  81. }
  82. /// <summary>
  83. /// Set the specified x and y.
  84. /// </summary>
  85. /// <param name="xx"></param>
  86. /// <param name="yy"></param>
  87. /// <param name="zz"></param>
  88. public void Set(double xx = 0.0, double yy = 0.0, double zz = 0.0)
  89. {
  90. x = xx;
  91. y = yy;
  92. z = zz;
  93. }
  94. /// <summary>
  95. /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:DVector3"/>.
  96. /// </summary>
  97. /// <returns>A <see cref="T:System.String"/> that represents the current <see cref="T:DVector3"/>.</returns>
  98. override public string ToString()
  99. {
  100. return "(" + x + ", " + y + ", " + z + ")";
  101. }
  102. /// <summary>
  103. /// Dot the specified a and b.
  104. /// </summary>
  105. /// <returns>The dot.</returns>
  106. /// <param name="a">The alpha component.</param>
  107. /// <param name="b">The blue component.</param>
  108. public static double Dot(DVector3 a, DVector3 b)
  109. {
  110. return a.x * b.x + a.y * b.y + a.z * b.z;
  111. }
  112. /// <summary>
  113. /// Distance the specified a and b.
  114. /// </summary>
  115. /// <returns>The distance.</returns>
  116. /// <param name="a">The alpha component.</param>
  117. /// <param name="b">The blue component.</param>
  118. public static double Distance(DVector3 a, DVector3 b)
  119. {
  120. return M.Sqrt(a.x * b.x + a.y * b.y + a.z * b.z);
  121. }
  122. /// <summary>
  123. /// Lerp the specified a, b and t.
  124. /// </summary>
  125. /// <returns>The lerp.</returns>
  126. /// <param name="a">The alpha component.</param>
  127. /// <param name="b">The blue component.</param>
  128. /// <param name="t">T.</param>
  129. public static DVector3 Lerp(DVector3 a, DVector3 b, double t)
  130. {
  131. double s = M.Max(0, M.Min(t, 1));
  132. return a * (1 - s) + b * s;
  133. }
  134. /// <summary>
  135. /// Computes the product of <c>a</c> and <c>b</c>, yielding a new <see cref="T:DVector3"/>.
  136. /// </summary>
  137. /// <param name="a">The <see cref="DVector3"/> to multiply.</param>
  138. /// <param name="b">The <see cref="double"/> to multiply.</param>
  139. /// <returns>The <see cref="T:DVector3"/> that is the <c>a</c> * <c>b</c>.</returns>
  140. public static DVector3 operator *(DVector3 a, double b)
  141. {
  142. return new DVector3(
  143. b * a.x,
  144. b * a.y,
  145. b * a.z
  146. );
  147. }
  148. /// <summary>
  149. /// Computes the product of <c>a</c> and <c>b</c>, yielding a new <see cref="T:DVector3"/>.
  150. /// </summary>
  151. /// <param name="a">The <see cref="DVector3"/> to multiply.</param>
  152. /// <param name="b">The <see cref="double"/> to multiply.</param>
  153. /// <returns>The <see cref="T:DVector3"/> that is the <c>a</c> * <c>b</c>.</returns>
  154. public static DVector3 operator *(double b, DVector3 a)
  155. {
  156. return new DVector3(
  157. b * a.x,
  158. b * a.y,
  159. b * a.z
  160. );
  161. }
  162. /// <summary>
  163. /// Computes the division of <c>a</c> and <c>b</c>, yielding a new <see cref="T:DVector3"/>.
  164. /// </summary>
  165. /// <param name="a">The <see cref="DVector3"/> to divide (the divident).</param>
  166. /// <param name="b">The <see cref="double"/> to divide (the divisor).</param>
  167. /// <returns>The <see cref="T:DVector3"/> that is the <c>a</c> / <c>b</c>.</returns>
  168. public static DVector3 operator /(DVector3 a, double b)
  169. {
  170. return new DVector3(
  171. a.x / b,
  172. a.y / b,
  173. a.z / b
  174. );
  175. }
  176. /// <summary>
  177. /// Adds a <see cref="DVector3"/> to a <see cref="DVector3"/>, yielding a new <see cref="T:DVector3"/>.
  178. /// </summary>
  179. /// <param name="a">The first <see cref="DVector3"/> to add.</param>
  180. /// <param name="b">The second <see cref="DVector3"/> to add.</param>
  181. /// <returns>The <see cref="T:DVector3"/> that is the sum of the values of <c>a</c> and <c>b</c>.</returns>
  182. public static DVector3 operator +(DVector3 a, DVector3 b)
  183. {
  184. return new DVector3(
  185. a.x + b.x,
  186. a.y + b.y,
  187. a.z + b.z
  188. );
  189. }
  190. /// <summary>
  191. /// Subtracts a <see cref="DVector3"/> from a <see cref="DVector3"/>, yielding a new <see cref="T:DVector3"/>.
  192. /// </summary>
  193. /// <param name="a">The <see cref="DVector3"/> to subtract from (the minuend).</param>
  194. /// <param name="b">The <see cref="DVector3"/> to subtract (the subtrahend).</param>
  195. /// <returns>The <see cref="T:DVector3"/> that is the <c>a</c> minus <c>b</c>.</returns>
  196. public static DVector3 operator -(DVector3 a, DVector3 b)
  197. {
  198. return new DVector3(
  199. a.x - b.x,
  200. a.y - b.y,
  201. a.z - b.z
  202. );
  203. }
  204. }
  205. }