TrajectoryGenerator.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SketchAssistantWPF
  8. {
  9. class TrajectoryGenerator
  10. {
  11. static int constantA= 10;
  12. /// <summary>
  13. /// the template for the line currently being drawn
  14. /// </summary>
  15. InternalLine currentLine;
  16. /// <summary>
  17. /// the points of the current line template, as an ordered list
  18. /// </summary>
  19. List<Point> currentPoints;
  20. //Point lastCursorPosition;
  21. /// <summary>
  22. /// pointer to the active section of the line template, indexing the ending point of the active section
  23. /// </summary>
  24. int index;
  25. /// <summary>
  26. /// updates the pointer to the template for the line currently being drawn, and resets indices etc.
  27. /// </summary>
  28. /// <param name="newCurrentLine">the new template for the line currently being drawn</param>
  29. public void setCurrentLine(InternalLine newCurrentLine)
  30. {
  31. currentLine = newCurrentLine;
  32. currentPoints = currentLine.GetPoints();
  33. //lastCursorPosition = currentPoints.ElementAt(0);
  34. index = 1;
  35. }
  36. /// <summary>
  37. /// generates the new trajectory back to the template based on the current cursor position
  38. /// </summary>
  39. /// <param name="cursorPosition">the current cursor position</param>
  40. /// <returns>the direction in which to go, as an angle on the drawing plane, in degree format, with 0° being the positive X-Axis, increasing counterclockwise</returns>
  41. public double GenerateTrajectory(Point cursorPosition)
  42. {
  43. //update index to point to current section if one or more section divideing lines have been passed since last call
  44. while (index < (currentPoints.Count - 1) && SectionDividingLinePassed(cursorPosition, currentPoints.ElementAt(index - 1), currentPoints.ElementAt(index), currentPoints.ElementAt(index + 1)))
  45. {
  46. index++;
  47. }
  48. //lastCursorPosition = cursorPosition;
  49. //project teh point onto the active line segment to be able to compute distances
  50. Point orthogonalProjection = ComputeOrthogonalProjection(cursorPosition, currentPoints.ElementAt(index - 1), currentPoints.ElementAt(index));
  51. //index of the last reachable actual point
  52. int targetIndex = index;
  53. List<Tuple<Point, Point>> strikeZones = new List<Tuple<Point, Point>>();
  54. //if "far" away from the next actual point of the line, generate an auxiliary point at a constant distance (constantA) on the current line segment
  55. Point auxiliaryPoint = new Point(-1, -1);
  56. if (ComputeDistance(orthogonalProjection, currentPoints.ElementAt(index)) <= constantA)
  57. {
  58. auxiliaryPoint = MoveAlongLine(orthogonalProjection, currentPoints.ElementAt(index - 1), currentPoints.ElementAt(index), constantA);
  59. strikeZones.Add(computeStrikeZone(auxiliaryPoint, orthogonalProjection, cursorPosition));
  60. targetIndex--;
  61. }
  62. //aim for the furthest actual point of the line reachable by the descent rate constraints (lower bounds) given by the various strike zones
  63. while (targetIndex < (currentPoints.Count - 1) && allStrikeZonesPassed(strikeZones, cursorPosition, currentPoints.ElementAt(targetIndex + 1)))
  64. {
  65. strikeZones.Add(computeStrikeZone(currentPoints.ElementAt(targetIndex + 1), orthogonalProjection, cursorPosition));
  66. targetIndex++;
  67. }
  68. Point furthestCrossingPoint = new Point(-1, -1); ;
  69. if (targetIndex < index) //auxiliary point created and next actual point not reachable
  70. {
  71. furthestCrossingPoint = ComputeFurthestCrossingPoint(cursorPosition, strikeZones, auxiliaryPoint, currentPoints.ElementAt(targetIndex + 1));
  72. //if such a point exists, use it as target for the new trajectory
  73. if (furthestCrossingPoint.X != -1)
  74. {
  75. Debug_DrawStrikeZones(strikeZones);
  76. Debug_DrawTrajectoryVector(cursorPosition, furthestCrossingPoint);
  77. return computeOrientationOfVector(cursorPosition, furthestCrossingPoint);
  78. }
  79. //else use the last reachable actual point
  80. else
  81. {
  82. Debug_DrawStrikeZones(strikeZones);
  83. Debug_DrawTrajectoryVector(cursorPosition, auxiliaryPoint);
  84. return computeOrientationOfVector(cursorPosition, auxiliaryPoint);
  85. }
  86. }
  87. else
  88. {
  89. //aim for the furthest (auxiliary) point on the line segment after the last reachable actual point (only if there is such a segment: not if that last reachable point is the last point of the line)
  90. if (targetIndex < (currentPoints.Count - 1))
  91. {
  92. furthestCrossingPoint = ComputeFurthestCrossingPoint(cursorPosition, strikeZones, currentPoints.ElementAt(targetIndex), currentPoints.ElementAt(targetIndex + 1));
  93. }
  94. //if such a point exists, use it as target for the new trajectory
  95. if (furthestCrossingPoint.X != -1)
  96. {
  97. Debug_DrawStrikeZones(strikeZones);
  98. Debug_DrawTrajectoryVector(cursorPosition, furthestCrossingPoint);
  99. return computeOrientationOfVector(cursorPosition, furthestCrossingPoint);
  100. }
  101. //else use the last reachable actual point
  102. else
  103. {
  104. Debug_DrawStrikeZones(strikeZones);
  105. Debug_DrawTrajectoryVector(cursorPosition, currentPoints.ElementAt(targetIndex));
  106. return computeOrientationOfVector(cursorPosition, currentPoints.ElementAt(targetIndex));
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// prints the trajectory vector on the drawing pane for debugging and calibration purposes
  112. /// </summary>
  113. /// <param name="vectorStartPoint">origin point of the trajectory vector</param>
  114. /// <param name="vectorEndPoint">target point of the trajectory vector</param>
  115. private void Debug_DrawTrajectoryVector(Point vectorStartPoint, Point vectorEndPoint)
  116. {
  117. throw new NotImplementedException();
  118. }
  119. /// <summary>
  120. /// prints all strike zones on the drawing pane for debugging and calibration purposes
  121. /// </summary>
  122. /// <param name="strikeZones">list of all strike zones to be drawn</param>
  123. private void Debug_DrawStrikeZones(List<Tuple<Point, Point>> strikeZones)
  124. {
  125. throw new NotImplementedException();
  126. }
  127. /// <summary>
  128. /// computes the orientation of the given vector on the drawing plane
  129. /// </summary>
  130. /// <param name="vectorStartPoint">origin point of the direction vector</param>
  131. /// <param name="vectorEndPoint">target point of the direction vector</param>
  132. /// <returns>the orientation angle, in degree format</returns>
  133. private double computeOrientationOfVector(Point vectorStartPoint, Point vectorEndPoint)
  134. {
  135. double x = vectorEndPoint.X - vectorStartPoint.X;
  136. double y = vectorEndPoint.Y - vectorStartPoint.Y;
  137. return Math.Atan( y / x );
  138. }
  139. /// <summary>
  140. /// computes the furthest point on the given line segment that will still pass all previous strike zones when connecting it with the current cursor position in a straight line
  141. /// </summary>
  142. /// <param name="cursorPosition">the current cursor position</param>
  143. /// <param name="strikeZones">list of all strike zones which have to be passed</param>
  144. /// <param name="lineSegmentStartPoint">starting point of the line segment on which the point has to be found</param>
  145. /// <param name="lineSegmentEndPoint">ending point of the line segment on which the point has to be found</param>
  146. /// <returns>the furthest such point or null, if there is no such point on the given segment (start and end point excluded)</returns>
  147. private Point ComputeFurthestCrossingPoint(Point cursorPosition, List<Tuple<Point, Point>> strikeZones, Point lineSegmentStartPoint, Point lineSegmentEndPoint)
  148. {
  149. throw new NotImplementedException();
  150. }
  151. /// <summary>
  152. /// checks if all strike zones are passed by the trajectory given by the straight line from the cursor position to the next target point
  153. /// </summary>
  154. /// <param name="strikeZones">list of all already computed strike zones</param>
  155. /// <param name="cursorPosition">the current cursor position</param>
  156. /// <param name="targetIndex">index of the next target point</param>
  157. /// <returns>true if all strike zones are passed, else false</returns>
  158. private bool allStrikeZonesPassed(List<Tuple<Point, Point>> strikeZones, Point cursorPosition, Point targetIndex)
  159. {
  160. throw new NotImplementedException();
  161. }
  162. /// <summary>
  163. /// computes the strike zone for a point using the cursor position, its orthogonal projection onto the active line segment and tunable constants
  164. /// </summary>
  165. /// <param name="targetedPoint">the point to compute the strike zone of</param>
  166. /// <param name="orthogonalProjection">orthogonal projection of the cursor position onto the active line segment</param>
  167. /// <param name="cursorPosition">the current cursor position</param>
  168. /// <returns></returns>
  169. private Tuple<Point, Point> computeStrikeZone(Point targetedPoint, Point orthogonalProjection, Point cursorPosition)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. /// <summary>
  174. /// moves a point a given distance along a vector defined by two points
  175. /// </summary>
  176. /// <param name="pointToBeMoved">the point to be moved along the line</param>
  177. /// <param name="lineStartPoint">origin point of the direction vector</param>
  178. /// <param name="lineEndPoint">target point of the direction vector</param>
  179. /// <param name="distance">distance by which to move the point</param>
  180. /// <returns>a new point that is located distance away from pointToBeMoved in the direction of the given vector</returns>
  181. private Point MoveAlongLine(Point pointToBeMoved, Point lineStartPoint, Point lineEndPoint, double distance)
  182. {
  183. throw new NotImplementedException();
  184. }
  185. /// <summary>
  186. /// computes the euclidean distance between two points
  187. /// </summary>
  188. /// <param name="point1">point 1</param>
  189. /// <param name="point2">point 2</param>
  190. /// <returns>euclidean distance between point1 and point2</returns>
  191. private double ComputeDistance(Point point1, Point point2)
  192. {
  193. return Math.Sqrt( (double) ( Math.Pow((point2.X - point1.X), 2) + Math.Pow((point2.Y - point1.Y), 2) ) );
  194. }
  195. /// <summary>
  196. /// computes the orthogonal projection of a point onto the given line segment
  197. /// </summary>
  198. /// <param name="cursorPosition">the current cursor position</param>
  199. /// <param name="lastPoint">beginning point of the current section</param>
  200. /// <param name="currentPoint">ending point of current section</param>
  201. /// <returns>the orthogonal projection of a point onto the given line segment, or the respective segment end point if the orthogonal projection lies outside the specified segment</returns>
  202. private Point ComputeOrthogonalProjection(Point cursorPosition, Point lastPoint, Point currentPoint)
  203. {
  204. throw new NotImplementedException();
  205. }
  206. /// <summary>
  207. /// checks if the angle dividing line between this section and the next one has been passed
  208. /// </summary>
  209. /// <param name="cursorPosition">the current cursor position</param>
  210. /// <param name="lastPoint">beginning point of the current section</param>
  211. /// <param name="currentPoint">ending point of current and beginning point of next section</param>
  212. /// <param name="nextPoint">ending point of next section</param>
  213. /// <returns>true iff cursorPosition is closer to the next section than to the current one</returns>
  214. private bool SectionDividingLinePassed(Point cursorPosition, Point lastPoint, Point currentPoint, Point nextPoint)
  215. {
  216. //compute a point at the same distance to the dividing line as nextPoint, but on the other side of the line
  217. Point auxiliaryPoint = MoveAlongLine(currentPoint, currentPoint, lastPoint, ComputeDistance(currentPoint, nextPoint));
  218. //line passed iff cursorPosition closer to nextPoint than to auxiliaryPoint
  219. return ComputeDistance(cursorPosition, nextPoint) <= ComputeDistance(cursorPosition, auxiliaryPoint);
  220. }
  221. }
  222. }