Commit 1db980acedd503c51f3ac0e4ddef9b606d26e047
1 parent
0de4d7c99d
Exists in
master
Update bug schedule
Showing 1 changed file with 2 additions and 1 deletions Inline Diff
sources/RoboforkApp/Controls/ScheduleCanvas.cs
1 | using RoboforkApp.DataModel; | 1 | using RoboforkApp.DataModel; |
2 | using System; | 2 | using System; |
3 | using System.Collections.Generic; | 3 | using System.Collections.Generic; |
4 | using System.Linq; | 4 | using System.Linq; |
5 | using System.Text; | 5 | using System.Text; |
6 | using System.Threading.Tasks; | 6 | using System.Threading.Tasks; |
7 | using System.Windows; | 7 | using System.Windows; |
8 | using System.Windows.Controls; | 8 | using System.Windows.Controls; |
9 | using System.Windows.Media; | 9 | using System.Windows.Media; |
10 | using System.Windows.Media.Animation; | 10 | using System.Windows.Media.Animation; |
11 | using System.Windows.Shapes; | 11 | using System.Windows.Shapes; |
12 | 12 | ||
13 | namespace RoboforkApp | 13 | namespace RoboforkApp |
14 | { | 14 | { |
15 | public class ScheduleCanvas : Canvas | 15 | public class ScheduleCanvas : Canvas |
16 | { | 16 | { |
17 | const double COOR_Y = 65; | 17 | private double COOR_Y = 65; |
18 | const double RADIUS_NODE = 25; | 18 | const double RADIUS_NODE = 25; |
19 | public simulationRobo simulation; | 19 | public simulationRobo simulation; |
20 | private VehicleModelList vehicleModelList; | 20 | private VehicleModelList vehicleModelList; |
21 | private Point _startPoint; | 21 | private Point _startPoint; |
22 | private Point _endPoint; | 22 | private Point _endPoint; |
23 | private bool _isGoal = false; | 23 | private bool _isGoal = false; |
24 | private double _speed; | 24 | private double _speed; |
25 | private int _index; | 25 | private int _index; |
26 | 26 | ||
27 | private List<ucNode> _lstNode; | 27 | private List<ucNode> _lstNode; |
28 | 28 | ||
29 | /// <summary> | 29 | /// <summary> |
30 | /// Create simulation | 30 | /// Create simulation |
31 | /// </summary> | 31 | /// </summary> |
32 | /// <param name="lstNode">List node on schedule</param> | 32 | /// <param name="lstNode">List node on schedule</param> |
33 | /// <param name="vehicleModel">Vehicle model on map</param> | 33 | /// <param name="vehicleModel">Vehicle model on map</param> |
34 | /// <param name="vehicleIndex">Current index of vehicle</param> | 34 | /// <param name="vehicleIndex">Current index of vehicle</param> |
35 | public void CreateSimulation(List<ucNode> lstNode, VehicleModel vehicleModel, int vehicleIndex) | 35 | public void CreateSimulation(List<ucNode> lstNode, VehicleModel vehicleModel, int vehicleIndex) |
36 | { | 36 | { |
37 | //If node is less than 2 so return | 37 | //If node is less than 2 so return |
38 | if (this.Children.Count < 2) | 38 | if (this.Children.Count < 2) |
39 | { | 39 | { |
40 | return; | 40 | return; |
41 | } | 41 | } |
42 | 42 | ||
43 | this.Children.Remove(simulation); | 43 | this.Children.Remove(simulation); |
44 | this.COOR_Y = DesignerCanvas.CanvasScheduleHeight / 2 - 15; | ||
44 | 45 | ||
45 | //Init data | 46 | //Init data |
46 | this._lstNode = lstNode; | 47 | this._lstNode = lstNode; |
47 | this.vehicleModelList = vehicleModel.VehicleModelList[vehicleIndex]; | 48 | this.vehicleModelList = vehicleModel.VehicleModelList[vehicleIndex]; |
48 | this._startPoint = new Point(Canvas.GetLeft(_lstNode[_index]) + RADIUS_NODE, COOR_Y); | 49 | this._startPoint = new Point(Canvas.GetLeft(_lstNode[_index]) + RADIUS_NODE, COOR_Y); |
49 | this._endPoint = new Point(Canvas.GetLeft(_lstNode[_index + 1]) + RADIUS_NODE, COOR_Y); | 50 | this._endPoint = new Point(Canvas.GetLeft(_lstNode[_index + 1]) + RADIUS_NODE, COOR_Y); |
50 | 51 | ||
51 | //Get speed | 52 | //Get speed |
52 | double scheDis = GetDistance(_startPoint, _endPoint); | 53 | double scheDis = GetDistance(_startPoint, _endPoint); |
53 | double mapDis = GetDistance(new Point(vehicleModelList.pointMapList[_index].pointMap_X, vehicleModelList.pointMapList[_index].pointMap_Y), | 54 | double mapDis = GetDistance(new Point(vehicleModelList.pointMapList[_index].pointMap_X, vehicleModelList.pointMapList[_index].pointMap_Y), |
54 | new Point(vehicleModelList.pointMapList[_index + 1].pointMap_X, vehicleModelList.pointMapList[_index + 1].pointMap_Y + 1)); | 55 | new Point(vehicleModelList.pointMapList[_index + 1].pointMap_X, vehicleModelList.pointMapList[_index + 1].pointMap_Y + 1)); |
55 | this._speed = GetSpeed(mapDis, scheDis, vehicleModelList.pointMapList[_index].speed_Map); | 56 | this._speed = GetSpeed(mapDis, scheDis, vehicleModelList.pointMapList[_index].speed_Map); |
56 | 57 | ||
57 | //Check next node is goal | 58 | //Check next node is goal |
58 | this._index += 1; | 59 | this._index += 1; |
59 | if (_index == _lstNode.Count - 1) | 60 | if (_index == _lstNode.Count - 1) |
60 | { | 61 | { |
61 | _isGoal = true; | 62 | _isGoal = true; |
62 | } | 63 | } |
63 | 64 | ||
64 | // Start simulation | 65 | // Start simulation |
65 | RoboSimulation(); | 66 | RoboSimulation(); |
66 | } | 67 | } |
67 | 68 | ||
68 | /// <summary> | 69 | /// <summary> |
69 | /// Create robo simulation on line | 70 | /// Create robo simulation on line |
70 | /// </summary> | 71 | /// </summary> |
71 | private void RoboSimulation() | 72 | private void RoboSimulation() |
72 | { | 73 | { |
73 | simulation = new simulationRobo(); | 74 | simulation = new simulationRobo(); |
74 | simulation.storyBoard = CreatPathAnimation(_startPoint, _endPoint, _speed); | 75 | simulation.storyBoard = CreatPathAnimation(_startPoint, _endPoint, _speed); |
75 | this.Children.Add(simulation); | 76 | this.Children.Add(simulation); |
76 | } | 77 | } |
77 | 78 | ||
78 | /// <summary> | 79 | /// <summary> |
79 | /// Get storyboard | 80 | /// Get storyboard |
80 | /// </summary> | 81 | /// </summary> |
81 | /// <param name="startPoint">Point start line</param> | 82 | /// <param name="startPoint">Point start line</param> |
82 | /// <param name="endPoit">Point end line</param> | 83 | /// <param name="endPoit">Point end line</param> |
83 | /// <param name="speed">speed on line</param> | 84 | /// <param name="speed">speed on line</param> |
84 | /// <returns>Storyboard</returns> | 85 | /// <returns>Storyboard</returns> |
85 | private Storyboard CreatPathAnimation(Point startPoint, Point endPoit, double speed) | 86 | private Storyboard CreatPathAnimation(Point startPoint, Point endPoit, double speed) |
86 | { | 87 | { |
87 | PathGeometry animationPath = new PathGeometry(); | 88 | PathGeometry animationPath = new PathGeometry(); |
88 | PathFigure pFigure = new PathFigure(); | 89 | PathFigure pFigure = new PathFigure(); |
89 | pFigure.StartPoint = startPoint; //new Point(50, 65); | 90 | pFigure.StartPoint = startPoint; //new Point(50, 65); |
90 | LineSegment lineSegment = new LineSegment(); | 91 | LineSegment lineSegment = new LineSegment(); |
91 | lineSegment.Point = endPoit; // new Point(800, 65); | 92 | lineSegment.Point = endPoit; // new Point(800, 65); |
92 | pFigure.Segments.Add(lineSegment); | 93 | pFigure.Segments.Add(lineSegment); |
93 | animationPath.Figures.Add(pFigure); | 94 | animationPath.Figures.Add(pFigure); |
94 | 95 | ||
95 | // Freeze the PathGeometry for performance benefits. | 96 | // Freeze the PathGeometry for performance benefits. |
96 | animationPath.Freeze(); | 97 | animationPath.Freeze(); |
97 | 98 | ||
98 | // Create a MatrixAnimationUsingPath to move the | 99 | // Create a MatrixAnimationUsingPath to move the |
99 | // simulation along the path by animating | 100 | // simulation along the path by animating |
100 | // its MatrixTransform. | 101 | // its MatrixTransform. |
101 | MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath(); | 102 | MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath(); |
102 | matrixAnimation.PathGeometry = animationPath; | 103 | matrixAnimation.PathGeometry = animationPath; |
103 | matrixAnimation.SpeedRatio = speed; | 104 | matrixAnimation.SpeedRatio = speed; |
104 | matrixAnimation.AutoReverse = false; | 105 | matrixAnimation.AutoReverse = false; |
105 | matrixAnimation.DoesRotateWithTangent = true; | 106 | matrixAnimation.DoesRotateWithTangent = true; |
106 | matrixAnimation.Completed += delegate { AnimationCompleted(this._isGoal); }; | 107 | matrixAnimation.Completed += delegate { AnimationCompleted(this._isGoal); }; |
107 | 108 | ||
108 | // Set the animation to target the Matrix property | 109 | // Set the animation to target the Matrix property |
109 | // of the MatrixTransform named "ButtonMatrixTransform". | 110 | // of the MatrixTransform named "ButtonMatrixTransform". |
110 | Storyboard.SetTargetName(matrixAnimation, "fl"); | 111 | Storyboard.SetTargetName(matrixAnimation, "fl"); |
111 | Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty)); | 112 | Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty)); |
112 | 113 | ||
113 | // Create a Storyboard to contain and apply the animation. | 114 | // Create a Storyboard to contain and apply the animation. |
114 | Storyboard pathAnimationStoryboard = new Storyboard(); | 115 | Storyboard pathAnimationStoryboard = new Storyboard(); |
115 | pathAnimationStoryboard.Children.Add(matrixAnimation); | 116 | pathAnimationStoryboard.Children.Add(matrixAnimation); |
116 | 117 | ||
117 | return pathAnimationStoryboard; | 118 | return pathAnimationStoryboard; |
118 | } | 119 | } |
119 | 120 | ||
120 | /// <summary> | 121 | /// <summary> |
121 | /// Process when simulation is end line | 122 | /// Process when simulation is end line |
122 | /// </summary> | 123 | /// </summary> |
123 | /// <param name="isGoal">check is node end</param> | 124 | /// <param name="isGoal">check is node end</param> |
124 | private void AnimationCompleted(bool isGoal) | 125 | private void AnimationCompleted(bool isGoal) |
125 | { | 126 | { |
126 | // If not end node | 127 | // If not end node |
127 | if (!isGoal) | 128 | if (!isGoal) |
128 | { | 129 | { |
129 | this.Children.Remove(simulation); | 130 | this.Children.Remove(simulation); |
130 | this._startPoint = _endPoint; | 131 | this._startPoint = _endPoint; |
131 | this._endPoint = new Point(Canvas.GetLeft(_lstNode[_index + 1]) + RADIUS_NODE, COOR_Y); | 132 | this._endPoint = new Point(Canvas.GetLeft(_lstNode[_index + 1]) + RADIUS_NODE, COOR_Y); |
132 | 133 | ||
133 | //Get speed | 134 | //Get speed |
134 | double scheDis = GetDistance(_startPoint, _endPoint); | 135 | double scheDis = GetDistance(_startPoint, _endPoint); |
135 | double mapDis = GetDistance(new Point(vehicleModelList.pointMapList[_index].pointMap_X, vehicleModelList.pointMapList[_index].pointMap_Y), | 136 | double mapDis = GetDistance(new Point(vehicleModelList.pointMapList[_index].pointMap_X, vehicleModelList.pointMapList[_index].pointMap_Y), |
136 | new Point(vehicleModelList.pointMapList[_index + 1].pointMap_X, vehicleModelList.pointMapList[_index + 1].pointMap_Y + 1)); | 137 | new Point(vehicleModelList.pointMapList[_index + 1].pointMap_X, vehicleModelList.pointMapList[_index + 1].pointMap_Y + 1)); |
137 | this._speed = GetSpeed(mapDis, scheDis, vehicleModelList.pointMapList[_index].speed_Map); | 138 | this._speed = GetSpeed(mapDis, scheDis, vehicleModelList.pointMapList[_index].speed_Map); |
138 | 139 | ||
139 | //Check next node is goal | 140 | //Check next node is goal |
140 | this._index += 1; | 141 | this._index += 1; |
141 | if (this._index == this._lstNode.Count - 1) | 142 | if (this._index == this._lstNode.Count - 1) |
142 | { | 143 | { |
143 | this._isGoal = true; | 144 | this._isGoal = true; |
144 | } | 145 | } |
145 | RoboSimulation(); | 146 | RoboSimulation(); |
146 | return; | 147 | return; |
147 | } | 148 | } |
148 | 149 | ||
149 | // Reset data when finish | 150 | // Reset data when finish |
150 | this._index = 0; | 151 | this._index = 0; |
151 | this._speed = 0; | 152 | this._speed = 0; |
152 | this._isGoal = false; | 153 | this._isGoal = false; |
153 | } | 154 | } |
154 | 155 | ||
155 | /// <summary> | 156 | /// <summary> |
156 | /// Get speed on schedule | 157 | /// Get speed on schedule |
157 | /// </summary> | 158 | /// </summary> |
158 | /// <param name="mapDis">Distance the line on Map</param> | 159 | /// <param name="mapDis">Distance the line on Map</param> |
159 | /// <param name="scheDis">Distance the line on Schedule</param> | 160 | /// <param name="scheDis">Distance the line on Schedule</param> |
160 | /// <param name="mapSpeed">Speed the fork on Map</param> | 161 | /// <param name="mapSpeed">Speed the fork on Map</param> |
161 | /// <returns>Speed the fork on schedule</returns> | 162 | /// <returns>Speed the fork on schedule</returns> |
162 | private double GetSpeed(double mapDis, double scheDis, double mapSpeed) | 163 | private double GetSpeed(double mapDis, double scheDis, double mapSpeed) |
163 | { | 164 | { |
164 | if (mapDis == 0) | 165 | if (mapDis == 0) |
165 | return 0.0; | 166 | return 0.0; |
166 | 167 | ||
167 | return mapSpeed * (scheDis / mapDis); | 168 | return mapSpeed * (scheDis / mapDis); |
168 | } | 169 | } |
169 | 170 | ||
170 | /// <summary> | 171 | /// <summary> |
171 | /// Get distance between two point | 172 | /// Get distance between two point |
172 | /// </summary> | 173 | /// </summary> |
173 | /// <param name="point1">Point 1</param> | 174 | /// <param name="point1">Point 1</param> |
174 | /// <param name="point2">Point 2</param> | 175 | /// <param name="point2">Point 2</param> |
175 | /// <returns>Distance between two point</returns> | 176 | /// <returns>Distance between two point</returns> |
176 | private double GetDistance(Point point1, Point point2) | 177 | private double GetDistance(Point point1, Point point2) |
177 | { | 178 | { |
178 | //pythagorean theorem c^2 = a^2 + b^2 | 179 | //pythagorean theorem c^2 = a^2 + b^2 |
179 | //thus c = square root(a^2 + b^2) | 180 | //thus c = square root(a^2 + b^2) |
180 | double a = (double)(point2.X - point1.X); | 181 | double a = (double)(point2.X - point1.X); |
181 | double b = (double)(point2.Y - point1.Y); | 182 | double b = (double)(point2.Y - point1.Y); |
182 | 183 | ||
183 | return Math.Sqrt(a * a + b * b); | 184 | return Math.Sqrt(a * a + b * b); |
184 | } | 185 | } |
185 | } | 186 | } |
186 | } | 187 | } |
187 | 188 |