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 { /// /// AStarTools.xaml 的交互逻辑 /// public partial class AStarTools : Window { public AStarTools() { InitializeComponent(); } private bool[] walls = new bool[0]; private Rectangle[] rects = new Rectangle[0]; /// /// 加载图片 /// /// /// 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); } /// /// 设置图像 /// /// /// 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; /// /// 生成方框 /// /// /// 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; } } } /// /// 生成方框 /// /// /// /// /// 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; /// /// 鼠标按下 /// /// /// 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; } /// /// 鼠标提起 /// /// /// private void SourceImage_MouseUp(object sender, MouseButtonEventArgs e) { isDown = false; } /// /// 鼠标移动 /// /// /// 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; } } /// /// 开始寻路 /// /// /// private void FindingPath(object sender, RoutedEventArgs e) { List points = new List(); 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 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); } } /// /// 清空标记 /// /// /// private void ClearTag(object sender, RoutedEventArgs e) { } } }