Blame view

sources/RoboforkApp/Controls/ScheduleCanvas.cs 7.21 KB
1fe3e8a87   toan   RM2063 : Update s...
1
2
  using RoboforkApp.DataModel;
  using System;
d55d921a3   toan   2045: Draw Schedule
3
4
5
6
7
8
9
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Media;
ec7e1c699   toan   2063 : Commit source
10
  using System.Windows.Media.Animation;
d55d921a3   toan   2045: Draw Schedule
11
12
13
14
15
16
  using System.Windows.Shapes;
  
  namespace RoboforkApp
  {
      public class ScheduleCanvas : Canvas
      {
1db980ace   toan   Update bug schedule
17
          private double COOR_Y = 65;
4bed9f746   toan   2063 : Update source
18
          const double RADIUS_NODE = 25;
1fe3e8a87   toan   RM2063 : Update s...
19
20
          public simulationRobo simulation;
          private VehicleModelList vehicleModelList;
57fdd8bb8   toan   RM2063 : Commit demo
21
22
          private Point _startPoint;
          private Point _endPoint;
4bed9f746   toan   2063 : Update source
23
          private bool _isGoal = false;
1fe3e8a87   toan   RM2063 : Update s...
24
          private double _speed;
57fdd8bb8   toan   RM2063 : Commit demo
25
          private int _index;
d55d921a3   toan   2045: Draw Schedule
26

4bed9f746   toan   2063 : Update source
27
28
29
30
31
          private List<ucNode> _lstNode;
  
          /// <summary>
          /// Create simulation
          /// </summary>
1fe3e8a87   toan   RM2063 : Update s...
32
33
34
          /// <param name="lstNode">List node on schedule</param>
          /// <param name="vehicleModel">Vehicle model on map</param>
          /// <param name="vehicleIndex">Current index of vehicle</param>
5a5c5239e   nguyen_nam   Task: 2095
35
          public void CreateSimulation(List<ucNode> lstNode, ProjectModelList vehicleModel, int vehicleIndex)
d55d921a3   toan   2045: Draw Schedule
36
          {
4bed9f746   toan   2063 : Update source
37
38
39
40
41
              //If node is less than 2 so return
              if (this.Children.Count < 2)
              {
                  return;
              }
6312cbd86   doan   Task 2062
42
              this.Children.Remove(simulation);
1db980ace   toan   Update bug schedule
43
              this.COOR_Y = DesignerCanvas.CanvasScheduleHeight / 2 - 15;
d55d921a3   toan   2045: Draw Schedule
44

4bed9f746   toan   2063 : Update source
45
46
              //Init data
              this._lstNode = lstNode;
1fe3e8a87   toan   RM2063 : Update s...
47
              this.vehicleModelList = vehicleModel.VehicleModelList[vehicleIndex];
57fdd8bb8   toan   RM2063 : Commit demo
48
49
50
51
52
53
54
55
56
57
58
59
              this._startPoint = new Point(Canvas.GetLeft(_lstNode[_index]) + RADIUS_NODE, COOR_Y);
              this._endPoint = new Point(Canvas.GetLeft(_lstNode[_index + 1]) + RADIUS_NODE, COOR_Y);
  
              //Get speed
              double scheDis = GetDistance(_startPoint, _endPoint);
              double mapDis = GetDistance(new Point(vehicleModelList.pointMapList[_index].pointMap_X, vehicleModelList.pointMapList[_index].pointMap_Y),
                                          new Point(vehicleModelList.pointMapList[_index + 1].pointMap_X, vehicleModelList.pointMapList[_index + 1].pointMap_Y + 1));
              this._speed = GetSpeed(mapDis, scheDis, vehicleModelList.pointMapList[_index].speed_Map);
  
              //Check next node is goal
              this._index += 1;
              if (_index == _lstNode.Count - 1)
4bed9f746   toan   2063 : Update source
60
61
62
63
64
65
66
67
68
69
70
71
72
              {
                  _isGoal = true;
              }
  
              // Start simulation
              RoboSimulation();
          }
  
          /// <summary>
          /// Create robo simulation on line
          /// </summary>
          private void RoboSimulation()
          {
6312cbd86   doan   Task 2062
73
              simulation = new simulationRobo();
57fdd8bb8   toan   RM2063 : Commit demo
74
              simulation.storyBoard = CreatPathAnimation(_startPoint, _endPoint, _speed);
6312cbd86   doan   Task 2062
75
              this.Children.Add(simulation);
4bed9f746   toan   2063 : Update source
76
77
78
79
80
81
82
83
84
85
86
          }
  
          /// <summary>
          /// Get storyboard
          /// </summary>
          /// <param name="startPoint">Point start line</param>
          /// <param name="endPoit">Point end line</param>
          /// <param name="speed">speed on line</param>
          /// <returns>Storyboard</returns>
          private Storyboard CreatPathAnimation(Point startPoint, Point endPoit, double speed)
          {
ec7e1c699   toan   2063 : Commit source
87
88
              PathGeometry animationPath = new PathGeometry();
              PathFigure pFigure = new PathFigure();
4bed9f746   toan   2063 : Update source
89
              pFigure.StartPoint = startPoint; //new Point(50, 65);
ec7e1c699   toan   2063 : Commit source
90
              LineSegment lineSegment = new LineSegment();
4bed9f746   toan   2063 : Update source
91
              lineSegment.Point = endPoit; // new Point(800, 65);
ec7e1c699   toan   2063 : Commit source
92
93
94
95
96
97
98
              pFigure.Segments.Add(lineSegment);
              animationPath.Figures.Add(pFigure);
  
              // Freeze the PathGeometry for performance benefits.
              animationPath.Freeze();
  
              // Create a MatrixAnimationUsingPath to move the
4bed9f746   toan   2063 : Update source
99
              // simulation along the path by animating
ec7e1c699   toan   2063 : Commit source
100
101
102
              // its MatrixTransform.
              MatrixAnimationUsingPath matrixAnimation = new MatrixAnimationUsingPath();
              matrixAnimation.PathGeometry = animationPath;
4bed9f746   toan   2063 : Update source
103
              matrixAnimation.SpeedRatio = speed;
ec7e1c699   toan   2063 : Commit source
104
105
              matrixAnimation.AutoReverse = false;
              matrixAnimation.DoesRotateWithTangent = true;
4bed9f746   toan   2063 : Update source
106
              matrixAnimation.Completed += delegate { AnimationCompleted(this._isGoal); };
ec7e1c699   toan   2063 : Commit source
107
108
109
110
111
112
113
114
115
  
              // Set the animation to target the Matrix property
              // of the MatrixTransform named "ButtonMatrixTransform".
              Storyboard.SetTargetName(matrixAnimation, "fl");
              Storyboard.SetTargetProperty(matrixAnimation, new PropertyPath(MatrixTransform.MatrixProperty));
  
              // Create a Storyboard to contain and apply the animation.
              Storyboard pathAnimationStoryboard = new Storyboard();
              pathAnimationStoryboard.Children.Add(matrixAnimation);
4bed9f746   toan   2063 : Update source
116
117
118
119
120
121
122
123
124
125
126
127
              return pathAnimationStoryboard;
          }
  
          /// <summary>
          /// Process when simulation is end line
          /// </summary>
          /// <param name="isGoal">check is node end</param>
          private void AnimationCompleted(bool isGoal)
          {
              // If not end node
              if (!isGoal)
              {
6312cbd86   doan   Task 2062
128
                  this.Children.Remove(simulation);
57fdd8bb8   toan   RM2063 : Commit demo
129
130
131
132
133
134
135
136
137
138
139
140
                  this._startPoint = _endPoint;
                  this._endPoint = new Point(Canvas.GetLeft(_lstNode[_index + 1]) + RADIUS_NODE, COOR_Y);
  
                  //Get speed
                  double scheDis = GetDistance(_startPoint, _endPoint);
                  double mapDis = GetDistance(new Point(vehicleModelList.pointMapList[_index].pointMap_X, vehicleModelList.pointMapList[_index].pointMap_Y),
                                              new Point(vehicleModelList.pointMapList[_index + 1].pointMap_X, vehicleModelList.pointMapList[_index + 1].pointMap_Y + 1));
                  this._speed = GetSpeed(mapDis, scheDis, vehicleModelList.pointMapList[_index].speed_Map);
  
                  //Check next node is goal
                  this._index += 1;
                  if (this._index == this._lstNode.Count - 1)
4bed9f746   toan   2063 : Update source
141
142
143
                  {
                      this._isGoal = true;
                  }
4bed9f746   toan   2063 : Update source
144
145
146
147
148
                  RoboSimulation();
                  return;
              }
  
              // Reset data when finish
57fdd8bb8   toan   RM2063 : Commit demo
149
              this._index = 0;
1fe3e8a87   toan   RM2063 : Update s...
150
              this._speed = 0;
4bed9f746   toan   2063 : Update source
151
              this._isGoal = false;
d55d921a3   toan   2045: Draw Schedule
152
          }
57fdd8bb8   toan   RM2063 : Commit demo
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  
          /// <summary>
          /// Get speed on schedule
          /// </summary>
          /// <param name="mapDis">Distance the line on Map</param>
          /// <param name="scheDis">Distance the line on Schedule</param>
          /// <param name="mapSpeed">Speed the fork on Map</param>
          /// <returns>Speed the fork on schedule</returns>
          private double GetSpeed(double mapDis, double scheDis, double mapSpeed)
          {
              if (mapDis == 0)
                  return 0.0;
  
              return mapSpeed * (scheDis / mapDis);
          }
  
          /// <summary>
          /// Get distance between two point
          /// </summary>
          /// <param name="point1">Point 1</param>
          /// <param name="point2">Point 2</param>
          /// <returns>Distance between two point</returns>
          private double GetDistance(Point point1, Point point2)
          {
              //pythagorean theorem c^2 = a^2 + b^2
              //thus c = square root(a^2 + b^2)
              double a = (double)(point2.X - point1.X);
              double b = (double)(point2.Y - point1.Y);
  
              return Math.Sqrt(a * a + b * b);
          }
d55d921a3   toan   2045: Draw Schedule
184
185
      }
  }