Vector.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 { get { return elements[0]; } set { elements[0] = value; } }
  12. public T y { get { return elements[1]; } set { elements[1] = value; } }
  13. public T z { get { return elements[2]; } set { elements[2] = value; } }
  14. public Vector(T x, T y)
  15. {
  16. elements = new T[2];
  17. elements[0] = x;
  18. elements[1] = y;
  19. }
  20. public Vector(T x, T y, T z)
  21. {
  22. elements = new T[3];
  23. elements[0] = x;
  24. elements[1] = y;
  25. elements[2] = z;
  26. }
  27. public Vector(int numberOfElements)
  28. {
  29. this.elements = new T[numberOfElements];
  30. }
  31. public Vector(T[] elements)
  32. {
  33. this.elements = elements;
  34. }
  35. public Vector(Vector<T> vector)
  36. {
  37. this.elements = new T[vector.length()];
  38. for (int i = 0; i < elements.Length; i++)
  39. {
  40. elements[i] = vector[i];
  41. }
  42. }
  43. public Vector<T> copy()
  44. {
  45. Vector<T> newVector = new Vector<T>(this);
  46. return newVector;
  47. }
  48. public T this[int index]
  49. {
  50. get
  51. {
  52. return elements[index];
  53. }
  54. set
  55. {
  56. elements[index] = value;
  57. }
  58. }
  59. public int length()
  60. {
  61. return elements.Length;
  62. }
  63. public void add(Vector<T> summand)
  64. {
  65. for (int i = 0; i < elements.Length; i++)
  66. {
  67. elements[i] = (dynamic)elements[i] + summand[i];
  68. }
  69. }
  70. public void subtract(Vector<T> subtrahend)
  71. {
  72. for (int i = 0; i < elements.Length; i++)
  73. {
  74. elements[i] = (dynamic)elements[i] - subtrahend[i];
  75. }
  76. }
  77. public void multiply(T scalar)
  78. {
  79. for (int i = 0; i < elements.Length; i++)
  80. {
  81. elements[i] = (dynamic)scalar * elements[i];
  82. }
  83. }
  84. public float norm()
  85. {
  86. return subNorm(elements.Length);
  87. }
  88. public float subNorm(int subLength)
  89. {
  90. T result = (dynamic)0;
  91. for (int i = 0; i < subLength; i++)
  92. {
  93. result += (dynamic)elements[i] * elements[i];
  94. }
  95. return (float)Math.Sqrt((dynamic)result);
  96. }
  97. public float distance(Vector<T> vector)
  98. {
  99. return (vector - this).norm();
  100. }
  101. public float subDistance(Vector<T> vector, int subLength)
  102. {
  103. return (vector - this).subNorm(subLength);
  104. }
  105. public T sum()
  106. {
  107. T result = (dynamic)0;
  108. for (int i = 0; i < this.length(); i++)
  109. {
  110. result += (dynamic)elements[i];
  111. }
  112. return result;
  113. }
  114. public Vector<T> normalize()
  115. {
  116. float norm = this.norm();
  117. Vector<T> result = new Vector<T>(this);
  118. for (int i = 0; i < result.length(); i++)
  119. {
  120. result[i] = (result[i] / (dynamic)norm);
  121. }
  122. return result;
  123. }
  124. private static void checkLength(Vector<T> vector1, Vector<T> vector2)
  125. {
  126. if (vector1.length() != vector2.length())
  127. throw new ArgumentException("The vectors must have the same length");
  128. }
  129. public static Vector<T> operator +(Vector<T> summand1, Vector<T> summand2)
  130. {
  131. checkLength(summand1, summand2);
  132. Vector<T> result = new Vector<T>(summand1);
  133. result.add(summand2);
  134. return result;
  135. }
  136. public static Vector<T> operator -(Vector<T> minuend, Vector<T> subtrahend)
  137. {
  138. checkLength(minuend, subtrahend);
  139. Vector<T> result = new Vector<T>(minuend);
  140. result.subtract(subtrahend);
  141. return result;
  142. }
  143. public static T operator *(Vector<T> vector1, Vector<T> vector2)
  144. {
  145. checkLength(vector1, vector2);
  146. T result = (dynamic)0;
  147. for (int i = 0; i < vector1.length(); i++)
  148. {
  149. result += (dynamic)vector1[i] * vector2[i];
  150. }
  151. return result;
  152. }
  153. public static Vector<T> operator *(T factor, Vector<T> vector1)
  154. {
  155. Vector<T> result = new Vector<T>(vector1);
  156. result.multiply(factor);
  157. return result;
  158. }
  159. public static bool operator ==(Vector<T> vector1, Vector<T> vector2)
  160. {
  161. checkLength(vector1, vector2);
  162. bool result = true;
  163. for (int i = 0; i < vector1.length(); i++)
  164. {
  165. if (vector1[i] != (dynamic)vector2[i])
  166. {
  167. result = false;
  168. break;
  169. }
  170. }
  171. return result;
  172. }
  173. public static bool operator !=(Vector<T> vector1, Vector<T> vector2)
  174. {
  175. return !(vector1 == vector2);
  176. }
  177. public static Vector<T> crossProduct(Vector<T> vector1, Vector<T> vector2)
  178. {
  179. if (vector1.length() != 3 || vector2.length() != 3)
  180. throw new ArgumentException("The vectors' length should be 3");
  181. Vector<T> result = new Vector<T>(vector1.length());
  182. result[0] = (dynamic)vector1.y * vector2.z - (dynamic)vector1.z * vector2.y;
  183. result[1] = (dynamic)vector1.z * vector2.x - (dynamic)vector1.x * vector2.z;
  184. result[2] = (dynamic)vector1.x * vector2.y - (dynamic)vector1.y * vector2.x;
  185. return result;
  186. }
  187. public static Vector<T> pointwiseMultiply(Vector<T> vector1, Vector<T> vector2)
  188. {
  189. checkLength(vector1, vector2);
  190. Vector<T> result = new Vector<T>(vector1);
  191. for (int i = 0; i < vector1.length(); i++)
  192. {
  193. result[i] = (dynamic)result[i] * vector2[i];
  194. }
  195. return result;
  196. }
  197. public static float distancePointPlane(Vector<T> point, Vector<T> planeA, Vector<T> planeB, Vector<T> planeC)
  198. {
  199. //TODO - Diese funktion funktioniert nur mit T = float,
  200. //die normalisierte Normale einer Ebene macht nur mit floats sinn (werte zwischen 0 und 1)
  201. if (point.length() != 3 || planeA.length() != 3 || planeB.length() != 3 || planeC.length() != 3)
  202. throw new ArgumentException("The vectors' length should be 3");
  203. Vector<T> ab = planeB - planeA;
  204. Vector<T> ac = planeC - planeA;
  205. Vector<T> normal = crossProduct(ab, ac).normalize();
  206. Vector<T> temp = point - planeA;
  207. temp = pointwiseMultiply(temp, normal);
  208. temp = pointwiseMultiply(temp, normal);
  209. T sum = temp.sum();
  210. temp = pointwiseMultiply(normal, normal);
  211. T sumR = temp.sum();
  212. if (sumR == (dynamic)0)
  213. throw new ArgumentException("the points do not clamp an Plane");
  214. float distance = (sum * (dynamic)(-1)) / sumR;
  215. if (distance < 0)
  216. distance *= (float)(-1);
  217. return distance;
  218. }
  219. public static Vector<float> projectToLine(Vector<float> p, Vector<float> direction, Vector<float> pointOnLine)
  220. {
  221. float px = p.x, py = p.y, dx = direction.x, dy = direction.y, ox = pointOnLine.x, oy = pointOnLine.y;
  222. float diffx = px - ox;
  223. float diffy = py - oy;
  224. float diff_d = (diffx * dx + diffy * dy);
  225. float d_d = (dx * dx + dy * dy);
  226. float q = diff_d / d_d;
  227. float newX = ox + q * dx;
  228. float newY = oy + q * dy;
  229. return new Vector<float>(newX, newY);
  230. }
  231. }
  232. }