Vector.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 bool operator ==(Vector<T> vector1, Vector<T> vector2)
  154. {
  155. checkLength(vector1, vector2);
  156. bool result = true;
  157. for (int i = 0; i < vector1.length(); i++)
  158. {
  159. if (vector1[i] != (dynamic)vector2[i])
  160. {
  161. result = false;
  162. break;
  163. }
  164. }
  165. return result;
  166. }
  167. public static bool operator !=(Vector<T> vector1, Vector<T> vector2)
  168. {
  169. return !(vector1 == vector2);
  170. }
  171. public static Vector<T> crossProduct(Vector<T> vector1, Vector<T> vector2)
  172. {
  173. if (vector1.length() != 3 || vector2.length() != 3)
  174. throw new ArgumentException("The vectors' length should be 3");
  175. Vector<T> result = new Vector<T>(vector1.length());
  176. result[0] = (dynamic)vector1.y * vector2.z - (dynamic)vector1.z * vector2.y;
  177. result[1] = (dynamic)vector1.z * vector2.x - (dynamic)vector1.x * vector2.z;
  178. result[2] = (dynamic)vector1.x * vector2.y - (dynamic)vector1.y * vector2.x;
  179. return result;
  180. }
  181. public static Vector<T> pointwiseMultiply(Vector<T> vector1, Vector<T> vector2)
  182. {
  183. checkLength(vector1, vector2);
  184. Vector<T> result = new Vector<T>(vector1);
  185. for (int i = 0; i < vector1.length(); i++)
  186. {
  187. result[i] = (dynamic)result[i] * vector2[i];
  188. }
  189. return result;
  190. }
  191. public static float distancePointPlane(Vector<T> point, Vector<T> planeA, Vector<T> planeB, Vector<T> planeC)
  192. {
  193. //TODO - Diese funktion funktioniert nur mit T = float,
  194. //die normalisierte Normale einer Ebene macht nur mit floats sinn (werte zwischen 0 und 1)
  195. if (point.length() != 3 || planeA.length() != 3 || planeB.length() != 3 || planeC.length() != 3)
  196. throw new ArgumentException("The vectors' length should be 3");
  197. Vector<T> ab = planeB - planeA;
  198. Vector<T> ac = planeC - planeA;
  199. Vector<T> normal = crossProduct(ab, ac).normalize();
  200. Vector<T> temp = point - planeA;
  201. temp = pointwiseMultiply(temp, normal);
  202. temp = pointwiseMultiply(temp, normal);
  203. T sum = temp.sum();
  204. temp = pointwiseMultiply(normal, normal);
  205. T sumR = temp.sum();
  206. if (sumR == (dynamic)0)
  207. throw new ArgumentException("the points do not clamp an Plane");
  208. float distance = (sum * (dynamic)(-1)) / sumR;
  209. if (distance < 0)
  210. distance *= (float)(-1);
  211. return distance;
  212. }
  213. public static Vector<float> projectToLine(Vector<int> p, Vector<float> direction, Vector<float> pointOnLine)
  214. {
  215. float px = p.x, py = p.y, dx = direction.x, dy = direction.y, ox = pointOnLine.x, oy = pointOnLine.y;
  216. float diffx = px - ox;
  217. float diffy = py - oy;
  218. float diff_d = (diffx * dx + diffy * dy);
  219. float d_d = (dx * dx + dy * dy);
  220. float q = diff_d / d_d;
  221. float newX = ox + q * dx;
  222. float newY = oy + q * dy;
  223. return new Vector<float>(newX, newY);
  224. }
  225. }
  226. }