Vector.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace bbiwarg.Utility
  7. {
  8. class Vector<T>
  9. {
  10. private T[] elements;
  11. public T x
  12. {
  13. get
  14. {
  15. return elements[0];
  16. }
  17. set
  18. {
  19. elements[0] = value;
  20. }
  21. }
  22. public T y
  23. {
  24. get
  25. {
  26. return elements[1];
  27. }
  28. set
  29. {
  30. elements[1] = value;
  31. }
  32. }
  33. public T z
  34. {
  35. get
  36. {
  37. return elements[2];
  38. }
  39. set
  40. {
  41. elements[2] = value;
  42. }
  43. }
  44. public Vector(int numberOfElements)
  45. {
  46. this.elements = new T[numberOfElements];
  47. }
  48. public Vector(T[] elements)
  49. {
  50. this.elements = elements;
  51. }
  52. public Vector(Vector<T> vector)
  53. {
  54. this.elements = new T[vector.length()];
  55. for (int i = 0; i < elements.Length; i++)
  56. {
  57. elements[i] = vector[i];
  58. }
  59. }
  60. public Vector<T> copy()
  61. {
  62. Vector<T> newVector = new Vector<T>(this);
  63. return newVector;
  64. }
  65. public T this[int index]
  66. {
  67. get
  68. {
  69. return elements[index];
  70. }
  71. set
  72. {
  73. elements[index] = value;
  74. }
  75. }
  76. public int length()
  77. {
  78. return elements.Length;
  79. }
  80. public void add(Vector<T> summand)
  81. {
  82. for (int i = 0; i < elements.Length; i++)
  83. {
  84. elements[i] = (dynamic)elements[i] + summand[i];
  85. }
  86. }
  87. public void subtract(Vector<T> subtrahend)
  88. {
  89. for (int i = 0; i < elements.Length; i++)
  90. {
  91. elements[i] = (dynamic)elements[i] - subtrahend[i];
  92. }
  93. }
  94. public void multiply(T scalar)
  95. {
  96. for (int i = 0; i < elements.Length; i++)
  97. {
  98. elements[i] = (dynamic)scalar * elements[i];
  99. }
  100. }
  101. public T norm()
  102. {
  103. return subNorm(elements.Length);
  104. }
  105. public T subNorm(int subLength)
  106. {
  107. T result = (dynamic)0;
  108. for (int i = 0; i < subLength; i++)
  109. {
  110. result += (dynamic)elements[i] * elements[i];
  111. }
  112. return (T)Math.Sqrt((dynamic)result);
  113. }
  114. public T distance(Vector<T> vector)
  115. {
  116. return (vector - this).norm();
  117. }
  118. public T sum()
  119. {
  120. T result = (dynamic)0;
  121. for (int i = 0; i < this.length(); i++)
  122. {
  123. result += (dynamic)elements[0];
  124. }
  125. return result;
  126. }
  127. public T subDistance(Vector<T> vector, int subLength) {
  128. return (vector - this).subNorm(subLength);
  129. }
  130. private static void checkLength(Vector<T> vector1, Vector<T> vector2)
  131. {
  132. if (vector1.length() != vector2.length())
  133. throw new ArgumentException("The vectors must have the same length");
  134. }
  135. public static Vector<T> operator +(Vector<T> summand1, Vector<T> summand2)
  136. {
  137. checkLength(summand1, summand2);
  138. Vector<T> result = new Vector<T>(summand1);
  139. result.add(summand2);
  140. return result;
  141. }
  142. public static Vector<T> operator -(Vector<T> minuend, Vector<T> subtrahend)
  143. {
  144. checkLength(minuend, subtrahend);
  145. Vector<T> result = new Vector<T>(minuend);
  146. result.subtract(subtrahend);
  147. return result;
  148. }
  149. public static T operator *(Vector<T> vector1, Vector<T> vector2)
  150. {
  151. checkLength(vector1, vector2);
  152. T result = (dynamic)0;
  153. for (int i = 0; i < vector1.length(); i++)
  154. {
  155. result += (dynamic)vector1[i] * vector2[i];
  156. }
  157. return result;
  158. }
  159. public static Vector<T> crossProduct(Vector<T> vector1, Vector<T> vector2)
  160. {
  161. if (vector1.length() != 3 || vector2.length() != 3)
  162. throw new ArgumentException("The vectors' length should be 3");
  163. Vector<T> result = new Vector<T>(vector1.length());
  164. result[0] = (dynamic)vector1.y * vector2.z - (dynamic)vector1.z * vector2.y;
  165. result[1] = (dynamic)vector1.z * vector2.x - (dynamic)vector1.x * vector2.z;
  166. result[2] = (dynamic)vector1.x * vector2.y - (dynamic)vector1.y * vector2.x;
  167. return result;
  168. }
  169. public static Vector<T> pointwiseMultiply(Vector<T> vector1, Vector<T> vector2)
  170. {
  171. checkLength(vector1, vector2);
  172. Vector<T> result = new Vector<T>(vector1);
  173. for (int i = 0; i < vector1.length(); i++)
  174. {
  175. result[i] = (dynamic)result[i] * vector2[i];
  176. }
  177. return result;
  178. }
  179. public static float distancePointPlane(Vector<T> point, Vector<T> planeA, Vector<T> planeB, Vector<T> planeC)
  180. {
  181. if (point.length() != 3 || planeA.length() != 3 || planeB.length() != 3 || planeC.length() != 3)
  182. throw new ArgumentException("The vectors' length should be 3");
  183. Vector<T> ab = planeB - planeA;
  184. Vector<T> ac = planeC - planeA;
  185. Vector<T> normal = crossProduct(ab, ac);
  186. Vector<T> temp = point - planeA;
  187. temp = pointwiseMultiply(temp, normal);
  188. temp = pointwiseMultiply(temp, normal);
  189. T sum = temp.sum();
  190. temp = pointwiseMultiply(normal, normal);
  191. T sumR = temp.sum();
  192. if (sumR == (dynamic)0)
  193. throw new ArgumentException("the points do not clamp an Plane");
  194. float distance = (sum * (dynamic)(-1)) / sumR;
  195. return distance;
  196. }
  197. }
  198. }