asmrobot
2019-11-25 2aeab450471cb80b59002da7da80faf251a0c4f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using Emgu.CV;
using RichCreator.Utility;
using RichCreator.Utility.PathFindings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ZTImage;
 
namespace RichCreator.Editor.Tools
{
    /// <summary>
    /// AStarTools.xaml 的交互逻辑
    /// </summary>
    public partial class AStarTools : Window
    {
        public AStarTools()
        {
            InitializeComponent();
        }
 
        private bool[] walls = new bool[0];
        private Rectangle[] rects = new Rectangle[0];
 
        /// <summary>
        /// 加载图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenFromFile(object sender, RoutedEventArgs e)
        {
            string imagePath = string.Empty;
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = ".jpg";
            ofd.Filter = "jpg file|*.jpg";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == true)
            {
                imagePath = ofd.FileName;
            }
            else
            {
                return;
            }
 
            Mat source = CvInvoke.Imread(imagePath, Emgu.CV.CvEnum.ImreadModes.Color);
 
            SetImage(this.SourceImage, source.Bitmap);
        }
 
 
 
        /// <summary>
        /// 设置图像
        /// </summary>
        /// <param name="control"></param>
        /// <param name="image"></param>
        private void SetImage(Image control, System.Drawing.Image image)
        {
            BitmapImage t = ImageHelper.BitmapToImageSource(image);
            control.Source = t;
            if (control == this.SourceImage)
            {
                mainContainer.Width = image.Width;
                mainContainer.Height = image.Height;
            }
        }
 
 
        private Int32 rwidth = 0;
        private Int32 rheight = 0;
        private Int32 rrow = 0;
        private Int32 rcol = 0;
 
        /// <summary>
        /// 生成方框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GenericRect(object sender, RoutedEventArgs e)
        {
            rwidth  = TypeConverter.StringToInt(this.rectWidth.Text, 0);
            rheight = TypeConverter.StringToInt(this.rectHeight.Text, 0);
            rrow    = TypeConverter.StringToInt(this.rectRow.Text, 0);
            rcol    = TypeConverter.StringToInt(this.rectCol.Text, 0);
 
            if (rwidth <= 2 || rheight <= 2)
            {
                MessageBox.Show("长宽不能小于2");
                return;
            }
 
            if (rrow <= 0 || rcol <= 0)
            {
                MessageBox.Show("行列不能小于等于0");
                return;
            }
 
 
            Int32 count = rrow * rcol;
            this.walls = new bool[count];
            this.rects = new Rectangle[count];
 
            for (int row = 0; row < rrow; row++)
            {
                for (int col = 0; col < rcol; col++)
                {
                    //生成
                    Rectangle rect=GenericRect(row, col, rwidth, rheight);
                    this.rects[row * rcol + col] = rect;
                }
            }
        }
 
        /// <summary>
        /// 生成方框
        /// </summary>
        /// <param name="row"></param>
        /// <param name="col"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        private Rectangle GenericRect(int row, int col, int width, int height)
        {
            Rectangle rect = new Rectangle();
            rect.Stroke = new SolidColorBrush(Colors.GreenYellow);
            rect.StrokeThickness = 1;
            rect.Width = width;
            rect.Height = height;
            rect.IsHitTestVisible = false;
            rect.Opacity = 0.7f;
 
            Canvas.SetLeft(rect, col*width);
            Canvas.SetTop(rect, row*height);
 
            ImageOverflowLayer.Children.Add(rect);
            return rect;
        }
 
        //是否添加障碍物
        private Int32 OperateAction=0;
        private bool isDown = false;
 
        //开始点
        private Int32 startx = 0;
        private Int32 starty = 0;
 
        //结束点
        private Int32 endx = 0;
        private Int32 endy = 0;
 
        /// <summary>
        /// 鼠标按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SourceImage_MouseDown(object sender, MouseButtonEventArgs e)
        {
            //是否添加障碍物
            OperateAction = ClickOperate.SelectedIndex;
 
            if (e.LeftButton == MouseButtonState.Released)
            {
                return;
            }
            Point point =  e.GetPosition(this.scrollViewer);
 
 
            Int32 row = (Int32)point.Y / rheight;
            Int32 col = (Int32)point.X / rwidth;
 
 
            Int32 index = row * rcol + col;
            if (OperateAction == 0)
            {
                //添加障碍物
                this.rects[index].Fill = new SolidColorBrush(Colors.YellowGreen);
                this.walls[index] = true;
            }
            else if (OperateAction == 1)
            {
                //消除障碍物
                this.rects[index].Fill = new SolidColorBrush(Colors.Transparent);
                this.walls[index] = false;
            }
            else if (OperateAction == 2)
            {
                //添加起点
                this.startx = col;
                this.starty = row;
                this.rects[index].Fill = new SolidColorBrush(Colors.Red);
            }
            else if (OperateAction == 3)
            {
                //添加终点
                this.endx = col;
                this.endy = row;
                this.rects[index].Fill = new SolidColorBrush(Colors.Black);
            }
            isDown = true;
        }
 
        /// <summary>
        /// 鼠标提起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SourceImage_MouseUp(object sender, MouseButtonEventArgs e)
        {
            isDown = false;
        }
 
        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SourceImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isDown)
            {
                return;
            }
            Point point = e.GetPosition(this.scrollViewer);
 
 
            Int32 row = (Int32)point.Y / rheight;
            Int32 col = (Int32)point.X / rwidth;
 
 
            Int32 index = row * rcol + col;
            if (OperateAction==0)
            {
                this.rects[index].Fill = new SolidColorBrush(Colors.YellowGreen);
                this.walls[index] = true;
            }
            else if(OperateAction==1)
            {
                this.rects[index].Fill = new SolidColorBrush(Colors.Transparent);
                this.walls[index] = false;
            }
        }
 
        /// <summary>
        /// 开始寻路
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FindingPath(object sender, RoutedEventArgs e)
        {
            List<Point2> points = new List<Point2>();
            for (int i = 0; i < this.walls.Length; i++)
            {
                if (this.walls[i])
                {
                    int y = i / rcol;
                    int x = i % rcol;
                    points.Add(new Point2(x, y));
                }
            }
            PathGrid grid = new PathGrid(rcol, rrow, points);
            List<Point2> paths=grid.FindPath(new Point2(startx, starty), new Point2(endx, endy));
            //paths=grid.GetPathDiversion(paths);
            for (int i = 0; i < paths.Count; i++)
            {
                Int32 index = paths[i].y * rrow + paths[i].x;
                this.rects[index].Fill = new SolidColorBrush(Colors.Blue);
            }
 
        }
 
        /// <summary>
        /// 清空标记
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ClearTag(object sender, RoutedEventArgs e)
        {
 
        }
    }
}