TrajectoryGenerator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 readonly int CONSTANT_A = 10;
  12. static readonly double DEADZONE = 3;
  13. /// <summary>
  14. /// the template for the line currently being drawn
  15. /// </summary>
  16. InternalLine currentLine;
  17. /// <summary>
  18. /// the points of the current line template, as an ordered list
  19. /// </summary>
  20. List<Point> currentPoints;
  21. //Point lastCursorPosition;
  22. /// <summary>
  23. /// pointer to the active section of the line template, indexing the ending point of the active section
  24. /// </summary>
  25. int index;
  26. /// <summary>
  27. /// updates the pointer to the template for the line currently being drawn, and resets indices etc.
  28. /// </summary>
  29. /// <param name="newCurrentLine">the new template for the line currently being drawn</param>
  30. public void setCurrentLine(InternalLine newCurrentLine)
  31. {
  32. currentLine = newCurrentLine;
  33. currentPoints = currentLine.GetPoints();
  34. //lastCursorPosition = currentPoints.ElementAt(0);
  35. index = 1;
  36. }
  37. /// <summary>
  38. /// generates the new trajectory back to the template based on the current cursor position
  39. /// </summary>
  40. /// <param name="cursorPosition">the current cursor position</param>
  41. /// <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>
  42. public double GenerateTrajectory(Point cursorPosition)
  43. {
  44. //update index to point to current section if one or more section divideing lines have been passed since last call
  45. while (index < (currentPoints.Count - 1) && SectionDividingLinePassed(cursorPosition, currentPoints.ElementAt(index - 1), currentPoints.ElementAt(index), currentPoints.ElementAt(index + 1)))
  46. {
  47. index++;
  48. }
  49. //lastCursorPosition = cursorPosition;
  50. //project teh point onto the active line segment to be able to compute distances
  51. Point orthogonalProjection = ComputeOrthogonalProjection(cursorPosition, currentPoints.ElementAt(index - 1), currentPoints.ElementAt(index));
  52. //index of the last reachable actual point
  53. int targetIndex = index;
  54. List<Tuple<Point, Point>> strikeZones = new List<Tuple<Point, Point>>();
  55. //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
  56. Point auxiliaryPoint = new Point(-1, -1);
  57. if (ComputeDistance(orthogonalProjection, currentPoints.ElementAt(index)) <= CONSTANT_A)
  58. {
  59. auxiliaryPoint = MoveAlongLine(orthogonalProjection, currentPoints.ElementAt(index - 1), currentPoints.ElementAt(index), CONSTANT_A);
  60. strikeZones.Add(computeStrikeZone(auxiliaryPoint, orthogonalProjection, cursorPosition));
  61. targetIndex--;
  62. }
  63. //aim for the furthest actual point of the line reachable by the descent rate constraints (lower bounds) given by the various strike zones
  64. while (targetIndex < (currentPoints.Count - 1) && allStrikeZonesPassed(strikeZones, cursorPosition, currentPoints.ElementAt(targetIndex + 1)))
  65. {
  66. strikeZones.Add(computeStrikeZone(currentPoints.ElementAt(targetIndex + 1), orthogonalProjection, cursorPosition));
  67. targetIndex++;
  68. }
  69. Point furthestCrossingPoint = new Point(-1, -1); ;
  70. if (targetIndex < index) //auxiliary point created and next actual point not reachable
  71. {
  72. furthestCrossingPoint = ComputeFurthestCrossingPoint(cursorPosition, orthogonalProjection, strikeZones, auxiliaryPoint, currentPoints.ElementAt(targetIndex + 1));
  73. //if such a point exists, use it as target for the new trajectory
  74. if (furthestCrossingPoint.X != -1)
  75. {
  76. Debug_DrawStrikeZones(strikeZones);
  77. Debug_DrawTrajectoryVector(cursorPosition, furthestCrossingPoint);
  78. return computeOrientationOfVector(cursorPosition, furthestCrossingPoint);
  79. }
  80. //else use the last reachable actual point
  81. else
  82. {
  83. Debug_DrawStrikeZones(strikeZones);
  84. Debug_DrawTrajectoryVector(cursorPosition, auxiliaryPoint);
  85. return computeOrientationOfVector(cursorPosition, auxiliaryPoint);
  86. }
  87. }
  88. else
  89. {
  90. //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)
  91. if (targetIndex < (currentPoints.Count - 1))
  92. {
  93. furthestCrossingPoint = ComputeFurthestCrossingPoint(cursorPosition, orthogonalProjection, strikeZones, currentPoints.ElementAt(targetIndex), currentPoints.ElementAt(targetIndex + 1));
  94. }
  95. //if such a point exists, use it as target for the new trajectory
  96. if (furthestCrossingPoint.X != -1)
  97. {
  98. Debug_DrawStrikeZones(strikeZones);
  99. Debug_DrawTrajectoryVector(cursorPosition, furthestCrossingPoint);
  100. return computeOrientationOfVector(cursorPosition, furthestCrossingPoint);
  101. }
  102. //else use the last reachable actual point
  103. else
  104. {
  105. Debug_DrawStrikeZones(strikeZones);
  106. Debug_DrawTrajectoryVector(cursorPosition, currentPoints.ElementAt(targetIndex));
  107. return computeOrientationOfVector(cursorPosition, currentPoints.ElementAt(targetIndex));
  108. }
  109. }
  110. }
  111. /// <summary>
  112. /// prints the trajectory vector on the drawing pane for debugging and calibration purposes
  113. /// </summary>
  114. /// <param name="vectorStartPoint">origin point of the trajectory vector</param>
  115. /// <param name="vectorEndPoint">target point of the trajectory vector</param>
  116. private void Debug_DrawTrajectoryVector(Point vectorStartPoint, Point vectorEndPoint)
  117. {
  118. throw new NotImplementedException();
  119. }
  120. /// <summary>
  121. /// prints all strike zones on the drawing pane for debugging and calibration purposes
  122. /// </summary>
  123. /// <param name="strikeZones">list of all strike zones to be drawn</param>
  124. private void Debug_DrawStrikeZones(List<Tuple<Point, Point>> strikeZones)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. /// <summary>
  129. /// computes the orientation of the given vector on the drawing plane
  130. /// </summary>
  131. /// <param name="vectorStartPoint">origin point of the direction vector</param>
  132. /// <param name="vectorEndPoint">target point of the direction vector</param>
  133. /// <returns>the orientation angle, in degree format</returns>
  134. private double computeOrientationOfVector(Point vectorStartPoint, Point vectorEndPoint)
  135. {
  136. double x = vectorEndPoint.X - vectorStartPoint.X;
  137. double y = vectorEndPoint.Y - vectorStartPoint.Y;
  138. return Math.Atan( y / x );
  139. }
  140. /// <summary>
  141. /// 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
  142. /// </summary>
  143. /// <param name="cursorPosition">the current cursor position</param>
  144. /// <param name="orthogonalProjection">the orthogonal projection of the current cursor position onto the active line segment</param>
  145. /// <param name="strikeZones">list of all strike zones which have to be passed</param>
  146. /// <param name="lineSegmentStartPoint">starting point of the line segment on which the point has to be found</param>
  147. /// <param name="lineSegmentEndPoint">ending point of the line segment on which the point has to be found</param>
  148. /// <returns>the furthest such point or null, if there is no such point on the given segment (start and end point excluded)</returns>
  149. private Point ComputeFurthestCrossingPoint(Point cursorPosition, Point orthogonalProjection, List<Tuple<Point, Point>> strikeZones, Point lineSegmentStartPoint, Point lineSegmentEndPoint)
  150. {
  151. Tuple<Point, Point> bsf= new Tuple<Point, Point>(new Point(-1, -1), new Point(-1, -1));
  152. Double bsfAngle = 180;
  153. for (int j = 0; j < strikeZones.Count; j++)
  154. {
  155. double currentAngle = ComputeAngle(orthogonalProjection, cursorPosition, strikeZones.ElementAt(j).Item2);
  156. if (currentAngle < bsfAngle)
  157. {
  158. bsfAngle = currentAngle;
  159. bsf = strikeZones.ElementAt(j);
  160. }
  161. }
  162. Point furthestCrossingPoint = ComputeCrossingPoint(cursorPosition, bsf.Item2, lineSegmentStartPoint, lineSegmentEndPoint);
  163. return furthestCrossingPoint;
  164. }
  165. private double ComputeAngle(Point point1, Point centerPoint, Point point2)
  166. {
  167. return (Math.Abs(computeOrientationOfVector(centerPoint, point1) - computeOrientationOfVector(centerPoint, point2)) % 180);
  168. }
  169. private Point ComputeCrossingPoint(Point line1Point1, Point lne1Point2, Point line2Point1, Point line2Point2)
  170. {
  171. throw new NotImplementedException();
  172. }
  173. /// <summary>
  174. /// checks if all strike zones are passed by the trajectory given by the straight line from the cursor position to the next target point
  175. /// </summary>
  176. /// <param name="strikeZones">list of all already computed strike zones</param>
  177. /// <param name="cursorPosition">the current cursor position</param>
  178. /// <param name="targetPoint">index of the next target point</param>
  179. /// <returns>true if all strike zones are passed, else false</returns>
  180. private bool allStrikeZonesPassed(List<Tuple<Point, Point>> strikeZones, Point cursorPosition, Point targetPoint)
  181. {
  182. foreach (Tuple<Point, Point> s in strikeZones )
  183. {
  184. if (!SectionsCrossing(cursorPosition, targetPoint, s.Item1, s.Item2)) return false;
  185. }
  186. return true;
  187. }
  188. private bool SectionsCrossing(Point section1StartingPoint, Point section1EndingPoint, Point section2StartingPoint, Point section2EndingPoint)
  189. {
  190. Point crossingPoint = ComputeCrossingPoint(section1StartingPoint, section1EndingPoint, section2StartingPoint, section2EndingPoint);
  191. if (BeyondSection(crossingPoint, section1StartingPoint, section1EndingPoint)) return false;
  192. if (BeyondSection(crossingPoint, section1EndingPoint, section1StartingPoint)) return false;
  193. if (BeyondSection(crossingPoint, section2StartingPoint, section2EndingPoint)) return false;
  194. if (BeyondSection(crossingPoint, section2EndingPoint, section2StartingPoint)) return false;
  195. return true;
  196. }
  197. /// <summary>
  198. /// computes the strike zone for a point using the cursor position, its orthogonal projection onto the active line segment and tunable constants
  199. /// </summary>
  200. /// <param name="targetedPoint">the point to compute the strike zone of</param>
  201. /// <param name="orthogonalProjection">orthogonal projection of the cursor position onto the active line segment</param>
  202. /// <param name="cursorPosition">the current cursor position</param>
  203. /// <returns></returns>
  204. private Tuple<Point, Point> computeStrikeZone(Point targetedPoint, Point orthogonalProjection, Point cursorPosition)
  205. {
  206. if (ComputeDistance(cursorPosition, orthogonalProjection) < DEADZONE) return new Tuple<Point, Point>(targetedPoint, targetedPoint);
  207. throw new NotImplementedException();
  208. }
  209. /// <summary>
  210. /// moves a point a given distance along a vector defined by two points
  211. /// </summary>
  212. /// <param name="pointToBeMoved">the point to be moved along the line</param>
  213. /// <param name="lineStartPoint">origin point of the direction vector</param>
  214. /// <param name="lineEndPoint">target point of the direction vector</param>
  215. /// <param name="distance">distance by which to move the point</param>
  216. /// <returns>a new point that is located distance away from pointToBeMoved in the direction of the given vector</returns>
  217. private Point MoveAlongLine(Point pointToBeMoved, Point lineStartPoint, Point lineEndPoint, double distance)
  218. {
  219. double xOffset = lineEndPoint.X - lineStartPoint.X;
  220. double yOffset = lineEndPoint.Y - lineStartPoint.Y;
  221. double vectorLength = ComputeDistance(lineStartPoint, lineEndPoint);
  222. xOffset /= vectorLength;
  223. xOffset *= distance;
  224. yOffset /= vectorLength;
  225. yOffset *= distance;
  226. return new Point(pointToBeMoved.X + xOffset, pointToBeMoved.Y + yOffset);
  227. }
  228. /// <summary>
  229. /// computes the euclidean distance between two points
  230. /// </summary>
  231. /// <param name="point1">point 1</param>
  232. /// <param name="point2">point 2</param>
  233. /// <returns>euclidean distance between point1 and point2</returns>
  234. private double ComputeDistance(Point point1, Point point2)
  235. {
  236. return Math.Sqrt( (double) ( Math.Pow((point2.X - point1.X), 2) + Math.Pow((point2.Y - point1.Y), 2) ) );
  237. }
  238. /// <summary>
  239. /// computes the orthogonal projection of a point onto the given line segment
  240. /// </summary>
  241. /// <param name="cursorPosition">the current cursor position</param>
  242. /// <param name="lastPoint">beginning point of the current section</param>
  243. /// <param name="currentPoint">ending point of current section</param>
  244. /// <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>
  245. private Point ComputeOrthogonalProjection(Point cursorPosition, Point lastPoint, Point currentPoint)
  246. {
  247. double r = ComputeDistance(cursorPosition, currentPoint);
  248. Point auxiliaryPoint = IntersectCircle(cursorPosition, r, lastPoint, currentPoint);
  249. if (BeyondSection(auxiliaryPoint, lastPoint, currentPoint)) return currentPoint;
  250. Point orthogonalProjection = MoveAlongLine(auxiliaryPoint, auxiliaryPoint, lastPoint, ComputeDistance(auxiliaryPoint, lastPoint) / 2);
  251. if (BeyondSection(orthogonalProjection, currentPoint, lastPoint)) return lastPoint;
  252. return orthogonalProjection;
  253. }
  254. private Point IntersectCircle(Point cursorPosition, double r, Point lastPoint, Point currentPoint)
  255. {
  256. throw new NotImplementedException();
  257. }
  258. /// <summary>
  259. /// checks if a Point lies on a given line section or beyond it, works only if pointToTest lies on the line defined by sectionStartingPoint and sectionEndingPoint
  260. /// </summary>
  261. /// <param name="pointToTest">the Point to test</param>
  262. /// <param name="sectionStartingPoint">the starting point of the section</param>
  263. /// <param name="sectionEndingPoint">the ending point of the section</param>
  264. /// <returns>true iff pointToTest lies beyond sectionEndingPoint, on the line defined by sectionStartingPoint and sectionEndingPoint</returns>
  265. private bool BeyondSection(Point pointToTest, Point sectionStartingPoint, Point sectionEndingPoint)
  266. {
  267. if(sectionEndingPoint.X > sectionStartingPoint.X)
  268. {
  269. if (pointToTest.X > sectionEndingPoint.X) return true;
  270. }
  271. else if(sectionEndingPoint.X < sectionStartingPoint.X)
  272. {
  273. if (pointToTest.X < sectionEndingPoint.X) return true;
  274. }
  275. else
  276. {
  277. if (sectionEndingPoint.Y > sectionStartingPoint.Y)
  278. {
  279. if (pointToTest.Y > sectionEndingPoint.Y) return true;
  280. }
  281. else if (sectionEndingPoint.Y < sectionStartingPoint.Y)
  282. {
  283. if (pointToTest.Y < sectionEndingPoint.Y) return true;
  284. }
  285. }
  286. return false;
  287. }
  288. /// <summary>
  289. /// checks if the angle dividing line between this section and the next one has been passed
  290. /// </summary>
  291. /// <param name="cursorPosition">the current cursor position</param>
  292. /// <param name="lastPoint">beginning point of the current section</param>
  293. /// <param name="currentPoint">ending point of current and beginning point of next section</param>
  294. /// <param name="nextPoint">ending point of next section</param>
  295. /// <returns>true iff cursorPosition is closer to the next section than to the current one</returns>
  296. private bool SectionDividingLinePassed(Point cursorPosition, Point lastPoint, Point currentPoint, Point nextPoint)
  297. {
  298. //compute a point at the same distance to the dividing line as nextPoint, but on the other side of the line
  299. Point auxiliaryPoint = MoveAlongLine(currentPoint, currentPoint, lastPoint, ComputeDistance(currentPoint, nextPoint));
  300. //line passed iff cursorPosition closer to nextPoint than to auxiliaryPoint
  301. return ComputeDistance(cursorPosition, nextPoint) <= ComputeDistance(cursorPosition, auxiliaryPoint);
  302. }
  303. }
  304. }