| | |
| | | InitializeComponent(); |
| | | } |
| | | |
| | | // 寻路信息 |
| | | private HousePathInfo housePathInfo = null; |
| | | private bool isinit = false;//是否加载图片 |
| | | |
| | | private HousePathInfo housePathInfo = null;//房间信息 |
| | | private bool moveable = false;//可移动的 |
| | | private Line tempLine = null; |
| | | private Polyline tempPolyline = null; |
| | | private Line tempLine = null;//临时线 |
| | | private Polyline tempPolyline = null;//临时障碍物 |
| | | private System.Drawing.Image image = null;// 当前图像 |
| | | private List<ZTPoint> startEndFindPathPoint = new List<ZTPoint>();//测试寻路起点和终点 |
| | | |
| | | // 当前图像 |
| | | private System.Drawing.Image image; |
| | | |
| | | #region 事件 |
| | | private void Window_Loaded(object sender, RoutedEventArgs e) |
| | | { |
| | | |
| | |
| | | this.image = source.Bitmap; |
| | | SetImage(); |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 复制JSON |
| | | /// </summary> |
| | |
| | | /// <param name="e"></param> |
| | | private void CopyJSON_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (!isinit) |
| | | { |
| | | return; |
| | | } |
| | | string json = this.housePathInfo.ToJsonString(); |
| | | Clipboard.SetDataObject(json, true); |
| | | MessageBox.Show("copy success"); |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="e"></param> |
| | | private void CreateFromJSON_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | //SerializeInput input = new SerializeInput(); |
| | | //if (input.ShowDialog() != true) |
| | | //{ |
| | | // return; |
| | | //} |
| | | if (!isinit) |
| | | { |
| | | MessageBox.Show("请先加载图片"); |
| | | return; |
| | | } |
| | | |
| | | //FindPathInfo pi = FindPathInfo.FromJsonString(input.ColorString); |
| | | SerializeInput input = new SerializeInput(); |
| | | if (input.ShowDialog() != true) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | var info = ZTImage.Json.JsonParser.ToObject<HousePathInfo>(input.ColorString); |
| | | if (info == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | //依次加载,障碍物,定位点,寻路点,巡逻线,寻路线 |
| | | //加载障碍物 |
| | | for (int i = 0; i < info.Obstacles.Count; i++) |
| | | { |
| | | Polygon polygon=CreateObstacleUI(info.Obstacles[i]); |
| | | this.HouseInfoLayer.Children.Add(polygon); |
| | | } |
| | | |
| | | //加载定位点 |
| | | for (int i = 0; i < info.LocationPoints.Count; i++) |
| | | { |
| | | Canvas lp = CreateLocationPointUI(info.LocationPoints[i]); |
| | | this.HouseInfoLayer.Children.Add(lp); |
| | | } |
| | | |
| | | |
| | | //加载寻路点 |
| | | for (int i = 0; i < info.FindPathPoints.Count; i++) |
| | | { |
| | | Canvas fp = CreateFindPathPointUI(info.FindPathPoints[i]); |
| | | this.HouseInfoLayer.Children.Add(fp); |
| | | } |
| | | |
| | | ////生成网格 |
| | | //this.rectWidth.Text = pi.Width.ToString(); |
| | | //this.rectHeight.Text = pi.Height.ToString(); |
| | | //GenericRect(pi.Width, pi.Height, pi.RowCount, pi.ColCount); |
| | | //加载巡逻线 |
| | | for (int i = 0; i < info.LoopLines.Count; i++) |
| | | { |
| | | Line llline = CreateLoopLineUI(info.LoopLines[i]); |
| | | this.HouseInfoLayer.Children.Add(llline); |
| | | } |
| | | |
| | | //加载寻路线 |
| | | for (int i = 0; i < info.FindPathLines.Count; i++) |
| | | { |
| | | Line fpline = CreateFindPathLineUI(info.FindPathLines[i]); |
| | | this.HouseInfoLayer.Children.Add(fpline); |
| | | } |
| | | |
| | | |
| | | ////生成顶点 |
| | | //for (int i = 0; i < pi.Vertexs.Count; i++) |
| | | //{ |
| | | // SetVertexUI(pi.Vertexs[i]); |
| | | //} |
| | | |
| | | ////生成连线 |
| | | //for (int i = 0; i < pi.Edges.Count; i++) |
| | | //{ |
| | | // GenericEdge(pi.Edges[i].StartIndex); |
| | | // SetEdge(pi.Edges[i].EndIndex); |
| | | //} |
| | | |
| | | |
| | | //this.PositionOverflowLayer.Children.Clear(); |
| | | |
| | | ////生成定位点 |
| | | //for (int i = 0; i < pi.Positions.Count; i++) |
| | | //{ |
| | | // AddLocationPoint(pi.Positions[i].X, pi.Positions[i].Y, pi.Positions[i].Parameter); |
| | | //} |
| | | this.housePathInfo = info; |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="e"></param> |
| | | private void ClearPosition_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (!isinit) |
| | | { |
| | | return; |
| | | } |
| | | this.LocationPointLayer.Children.Clear(); |
| | | this.housePathInfo.ClearLocationPosiltion(); |
| | | } |
| | | |
| | | |
| | | |
| | | /// <summary> |
| | | /// 清空寻路测试点的起点和终点 |
| | | /// </summary> |
| | | /// <param name="sender"></param> |
| | | /// <param name="e"></param> |
| | | private void ClearStartEndPoint_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (!isinit) |
| | | { |
| | | return; |
| | | } |
| | | this.StartEndPointLayer.Children.Clear(); |
| | | this.startEndFindPathPoint.Clear(); |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 测试寻路 |
| | | /// </summary> |
| | | /// <param name="sender"></param> |
| | | /// <param name="e"></param> |
| | | private void TestFindPath_Click(object sender, RoutedEventArgs e) |
| | | { |
| | | if (this.startEndFindPathPoint.Count < 2) |
| | | { |
| | | MessageBox.Show("请选择测试的起点和终点"); |
| | | } |
| | | ZTPoint start = this.startEndFindPathPoint[0]; |
| | | ZTPoint end = this.startEndFindPathPoint[1]; |
| | | |
| | | List<ZTPoint> path=this.housePathInfo.FindPath(start, end); |
| | | //todo:绘制路径 |
| | | } |
| | | |
| | | |
| | |
| | | /// <param name="e"></param> |
| | | private void SourceImage_MouseDown(object sender, MouseButtonEventArgs e) |
| | | { |
| | | if (!isinit) |
| | | { |
| | | return; |
| | | } |
| | | //是否添加障碍物 |
| | | OperateActionD op = (OperateActionD)(slOperate.SelectedIndex); |
| | | Int32 number = TypeConverter.StringToInt(this.tagNumber.Text, 0); |
| | |
| | | else if (op == OperateActionD.LoopLine) |
| | | { |
| | | //循环线 |
| | | ZTPoint llPoint = GetFindPathPoint(new ZTPoint((Int32)point.X,(Int32)point.Y)); |
| | | if (llPoint != ZTPoint.Empty) |
| | | ZTPoint llPoint = GetFindPathPoint(new ZTPoint((Int32)point.X, (Int32)point.Y)); |
| | | if (!llPoint.Equals(ZTPoint.Empty)) |
| | | { |
| | | tempLine = CreateTempLoopLineUI(llPoint); |
| | | this.HouseInfoLayer.Children.Add(tempLine); |
| | | moveable = true; |
| | | } |
| | | } |
| | | } |
| | | else if (op == OperateActionD.FindPathLine) |
| | | { |
| | | //寻路线 |
| | | ZTPoint fpPoint = GetFindPathPoint(new ZTPoint((Int32)point.X, (Int32)point.Y)); |
| | | if (fpPoint != ZTPoint.Empty) |
| | | if (!fpPoint.Equals(ZTPoint.Empty)) |
| | | { |
| | | tempLine = CreateTempFindPathLineUI(fpPoint); |
| | | this.HouseInfoLayer.Children.Add(tempLine); |
| | | moveable = true; |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | else if (op == OperateActionD.StartEndPoint) |
| | | { |
| | | //画寻路起始和终点 |
| | | ZTPoint sepoint = new ZTPoint((Int32)point.X, (Int32)point.Y); |
| | | Canvas lseControl = CreateStartEndPointUI(sepoint); |
| | | this.StartEndPointLayer.Children.Add(lseControl); |
| | | this.startEndFindPathPoint.Add(sepoint); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | |
| | | /// <param name="e"></param> |
| | | private void SourceImage_MouseMove(object sender, MouseEventArgs e) |
| | | { |
| | | if (!isinit) |
| | | { |
| | | return; |
| | | } |
| | | OperateActionD op = (OperateActionD)(slOperate.SelectedIndex); |
| | | Point point = GetPosition(e); |
| | | if (op == OperateActionD.LoopLine) |
| | |
| | | tempLine.Y2 = point.Y; |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 鼠标提起 |
| | | /// </summary> |
| | |
| | | /// <param name="e"></param> |
| | | private void SourceImage_MouseUp(object sender, MouseButtonEventArgs e) |
| | | { |
| | | if (!isinit) |
| | | { |
| | | return; |
| | | } |
| | | OperateActionD op = (OperateActionD)(slOperate.SelectedIndex); |
| | | Point point = GetPosition(e); |
| | | |
| | |
| | | } |
| | | ZTPoint startPoint = (ZTPoint)tempLine.Tag; |
| | | ZTPoint centerPoint = GetFindPathPoint(new ZTPoint((Int32)point.X, (Int32)point.Y)); |
| | | if (centerPoint != ZTPoint.Empty) |
| | | if (!centerPoint.Equals(ZTPoint.Empty)) |
| | | { |
| | | ZTLinePoint ll = new ZTLinePoint(startPoint, centerPoint); |
| | | if (this.housePathInfo.AddLoopLine(ll)) |
| | |
| | | } |
| | | ZTPoint startPoint = (ZTPoint)tempLine.Tag; |
| | | ZTPoint centerPoint = GetFindPathPoint(new ZTPoint((Int32)point.X, (Int32)point.Y)); |
| | | if (centerPoint != ZTPoint.Empty) |
| | | if (!centerPoint.Equals(ZTPoint.Empty)) |
| | | { |
| | | ZTLinePoint fp = new ZTLinePoint(startPoint, centerPoint); |
| | | if (this.housePathInfo.AddFindPathLine(fp)) |
| | |
| | | { |
| | | //首次画 |
| | | this.points.Add(point); |
| | | this.tempPolyline=CreateTempObstacleUI(point); |
| | | this.tempPolyline = CreateTempObstacleUI(point); |
| | | this.tempLine = CreateObstacleLineUI(new ZTPoint((Int32)point.X, (Int32)point.Y)); |
| | | |
| | | moveable = true; |
| | |
| | | if (IsLastPoint(point)) |
| | | { |
| | | //判断是否跟最后一个点重合,如果重合则不处理 |
| | | //判断是否只有两个点,如果是则不处理 |
| | | return; |
| | | } |
| | | |
| | | if (IsFirstPoint(point)) |
| | | if (IsFirstPoint(point) && this.points.Count > 2) |
| | | { |
| | | //到起点,完成,画折线,向houseinfo里添加障碍物 |
| | | Polygon polygon = CreateObstacleUI(); |
| | | this.HouseInfoLayer.Children.Add(polygon); |
| | | |
| | | //到起点,完成,向houseinfo里添加障碍物 ,画折线, |
| | | ZTPoint[] obstaclePoints = new ZTPoint[this.points.Count]; |
| | | for (int i = 0; i < this.points.Count; i++) |
| | | { |
| | | obstaclePoints[i] = new ZTPoint((Int32)this.points[i].X, (Int32)this.points[i].Y); |
| | | } |
| | | this.housePathInfo.AddObstacle(new ZTPolygon(obstaclePoints)); |
| | | |
| | | ZTPolygon ztpolygon = new ZTPolygon(obstaclePoints); |
| | | this.housePathInfo.AddObstacle(ztpolygon); |
| | | |
| | | Polygon polygon = CreateObstacleUI(ztpolygon); |
| | | this.HouseInfoLayer.Children.Add(polygon); |
| | | |
| | | this.HouseInfoLayer.Children.Remove(tempPolyline); |
| | | tempPolyline = null; |
| | |
| | | |
| | | } |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 计算当前鼠标位置和颜色 |
| | | /// </summary> |
| | | /// <param name="e"></param> |
| | | /// <param name="point"></param> |
| | | /// <param name="color"></param> |
| | | /// <returns></returns> |
| | | private Point GetPosition(MouseEventArgs e) |
| | | { |
| | | Point point = e.GetPosition(this.scrollViewer); |
| | | |
| | | Int32 x = (Int32)Math.Round(this.scrollViewer.HorizontalOffset + point.X); |
| | | Int32 y = (Int32)Math.Round(this.scrollViewer.VerticalOffset + point.Y); |
| | | |
| | | return new Point(x, y); |
| | | } |
| | | |
| | | |
| | | private List<Point> points = new List<Point>(); |
| | | /// <summary> |
| | | /// 是否第一个点 |
| | | /// </summary> |
| | | /// <param name="point"></param> |
| | | /// <returns></returns> |
| | | private bool IsFirstPoint(Point point) |
| | | { |
| | | Point temp =this.points[0]; |
| | | if (Math.Abs(temp.X - point.X) <= 10 && Math.Abs(temp.Y - point.Y) <= 10) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是否最后一个点 |
| | | /// </summary> |
| | | /// <param name="point"></param> |
| | | /// <returns></returns> |
| | | private bool IsLastPoint(Point point) |
| | | { |
| | | Point temp = this.points[this.points.Count-1]; |
| | | if (Math.Abs(temp.X - point.X) <= 10 && Math.Abs(temp.Y - point.Y) <= 10) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 操作类型 |
| | | /// </summary> |
| | | public enum OperateActionD |
| | | { |
| | | Obstacle = 0,//障碍物 |
| | | LocationPoint,//定位点 |
| | | FindPathPoint,//寻路点 |
| | | LoopLine,//循环线 |
| | | FindPathLine,//寻路线 |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region createUI |
| | | |
| | |
| | | this.SourceImage.Source = t; |
| | | mainContainer.Width = image.Width; |
| | | mainContainer.Height = image.Height; |
| | | this.housePathInfo = new HousePathInfo(); |
| | | this.housePathInfo = new HousePathInfo(image.Width, image.Height); |
| | | System.Threading.ThreadPool.QueueUserWorkItem((obj) => |
| | | { |
| | | System.Threading.Thread.Sleep(1000); |
| | | isinit = true; |
| | | }); |
| | | } |
| | | |
| | | |
| | |
| | | |
| | | Canvas main = new Canvas(); |
| | | main.Tag = point; |
| | | Canvas.SetLeft(main,point.Point.X); |
| | | Canvas.SetLeft(main, point.Point.X); |
| | | Canvas.SetTop(main, point.Point.Y); |
| | | |
| | | |
| | | Rectangle rect = new Rectangle(); |
| | | Canvas.SetLeft(rect, -10); |
| | | Canvas.SetTop(rect, -10); |
| | |
| | | rect.Fill = new SolidColorBrush(Colors.Red); |
| | | rect.Opacity = 0.5f; |
| | | main.Children.Add(rect); |
| | | |
| | | |
| | | Ellipse elli = new Ellipse(); |
| | | Canvas.SetLeft(elli, -2); |
| | | Canvas.SetTop(elli, -2); |
| | |
| | | elli.StrokeThickness = 1; |
| | | elli.Fill = new SolidColorBrush(Colors.Black); |
| | | main.Children.Add(elli); |
| | | |
| | | |
| | | TextBlock tb = new TextBlock(); |
| | | tb.Text = point.Parameter.ToString(); |
| | | tb.FontSize = 9; |
| | |
| | | |
| | | return main; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 创建寻路点UI |
| | | /// </summary> |
| | |
| | | |
| | | return main; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 寻路点右击 |
| | | /// </summary> |
| | |
| | | { |
| | | this.HouseInfoLayer.Children.Remove(c); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 创建巡逻线UI |
| | | /// </summary> |
| | |
| | | edgeLine.StrokeEndLineCap = PenLineCap.Square; |
| | | edgeLine.StrokeStartLineCap = PenLineCap.Round; |
| | | |
| | | |
| | | |
| | | return edgeLine; |
| | | } |
| | | |
| | |
| | | edgeLine.StrokeDashCap = PenLineCap.Triangle; |
| | | edgeLine.StrokeEndLineCap = PenLineCap.Square; |
| | | edgeLine.StrokeStartLineCap = PenLineCap.Round; |
| | | |
| | | |
| | | return edgeLine; |
| | | } |
| | | |
| | |
| | | |
| | | edgeLine.X2 = linePoint.X2; |
| | | edgeLine.Y2 = linePoint.Y2; |
| | | |
| | | |
| | | |
| | | |
| | | return edgeLine; |
| | | } |
| | | |
| | |
| | | |
| | | edgeLine.X2 = point.X; |
| | | edgeLine.Y2 = point.Y; |
| | | |
| | | |
| | | |
| | | |
| | | return edgeLine; |
| | | } |
| | | |
| | |
| | | /// 创建障碍物UI |
| | | /// </summary> |
| | | /// <returns></returns> |
| | | private Polygon CreateObstacleUI() |
| | | private Polygon CreateObstacleUI(ZTPolygon ztpolygon) |
| | | { |
| | | //<!--<Polygon StrokeThickness="1" Stroke="Black" Opacity="0.5"> |
| | | // <Polygon.Points>10,50 180,50 180,150 </Polygon.Points> |
| | | // </Polygon>--> |
| | | Polygon polygon = new Polygon(); |
| | | polygon.Tag = ztpolygon; |
| | | polygon.MouseRightButtonUp += Polygon_MouseRightButtonUp; |
| | | polygon.StrokeThickness = 1; |
| | | polygon.Stroke = new SolidColorBrush(Colors.Black); |
| | | polygon.Fill= new SolidColorBrush(Colors.Black); |
| | | polygon.Fill = new SolidColorBrush(Colors.Black); |
| | | polygon.Opacity = 0.7f; |
| | | for (int i = 0; i < this.points.Count; i++) |
| | | { |
| | | polygon.Points.Add(this.points[i]); |
| | | } |
| | | |
| | | |
| | | return polygon; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 障碍物右键删除 |
| | | /// </summary> |
| | | /// <param name="sender"></param> |
| | | /// <param name="e"></param> |
| | | private void Polygon_MouseRightButtonUp(object sender, MouseButtonEventArgs e) |
| | | { |
| | | Polygon c = sender as Polygon; |
| | | if (c == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | ZTPolygon ztp = default(ZTPolygon); |
| | | try |
| | | { |
| | | ztp = (ZTPolygon)c.Tag; |
| | | } |
| | | catch |
| | | { |
| | | return; |
| | | } |
| | | if (this.housePathInfo.RemoveObstacle(ztp)) |
| | | { |
| | | this.HouseInfoLayer.Children.Remove(c); |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | return temp; |
| | | } |
| | | } |
| | | return default(ZTPoint); |
| | | return ZTPoint.Empty; |
| | | } |
| | | |
| | | |
| | | /// <summary> |
| | | /// 创建起始和终止寻路点 |
| | | /// </summary> |
| | | /// <param name="point"></param> |
| | | /// <returns></returns> |
| | | private Canvas CreateStartEndPointUI(ZTPoint point) |
| | | { |
| | | Canvas main = new Canvas(); |
| | | main.Tag = point; |
| | | Canvas.SetLeft(main, point.X); |
| | | Canvas.SetTop(main, point.Y); |
| | | |
| | | Rectangle rect = new Rectangle(); |
| | | Canvas.SetLeft(rect, -10); |
| | | Canvas.SetTop(rect, -10); |
| | | rect.Width = 20; |
| | | rect.Height = 20; |
| | | rect.Stroke = new SolidColorBrush(Colors.Green); |
| | | rect.StrokeThickness = 1; |
| | | rect.Fill = new SolidColorBrush(Colors.Green); |
| | | rect.Opacity = 0.5f; |
| | | main.Children.Add(rect); |
| | | |
| | | Ellipse elli = new Ellipse(); |
| | | Canvas.SetLeft(elli, -2); |
| | | Canvas.SetTop(elli, -2); |
| | | elli.Width = 4; |
| | | elli.Height = 4; |
| | | elli.Stroke = new SolidColorBrush(Colors.Black); |
| | | elli.StrokeThickness = 1; |
| | | elli.Fill = new SolidColorBrush(Colors.Black); |
| | | main.Children.Add(elli); |
| | | |
| | | |
| | | return main; |
| | | } |
| | | #endregion |
| | | |
| | | |
| | | |
| | | |
| | | #region Tools |
| | | //private bool PointInRang(Point) |
| | | |
| | | /// <summary> |
| | | /// 计算当前鼠标位置和颜色 |
| | | /// </summary> |
| | | /// <param name="e"></param> |
| | | /// <param name="point"></param> |
| | | /// <param name="color"></param> |
| | | /// <returns></returns> |
| | | private Point GetPosition(MouseEventArgs e) |
| | | { |
| | | Point point = e.GetPosition(this.scrollViewer); |
| | | |
| | | Int32 x = (Int32)Math.Round(this.scrollViewer.HorizontalOffset + point.X); |
| | | Int32 y = (Int32)Math.Round(this.scrollViewer.VerticalOffset + point.Y); |
| | | |
| | | return new Point(x, y); |
| | | } |
| | | |
| | | |
| | | private List<Point> points = new List<Point>(); |
| | | /// <summary> |
| | | /// 是否第一个点 |
| | | /// </summary> |
| | | /// <param name="point"></param> |
| | | /// <returns></returns> |
| | | private bool IsFirstPoint(Point point) |
| | | { |
| | | Point temp = this.points[0]; |
| | | if (Math.Abs(temp.X - point.X) <= 10 && Math.Abs(temp.Y - point.Y) <= 10) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 是否最后一个点 |
| | | /// </summary> |
| | | /// <param name="point"></param> |
| | | /// <returns></returns> |
| | | private bool IsLastPoint(Point point) |
| | | { |
| | | Point temp = this.points[this.points.Count - 1]; |
| | | if (Math.Abs(temp.X - point.X) <= 10 && Math.Abs(temp.Y - point.Y) <= 10) |
| | | { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | #endregion |
| | | |
| | | /// <summary> |
| | | /// 操作类型 |
| | | /// </summary> |
| | | public enum OperateActionD |
| | | { |
| | | Obstacle = 0,//障碍物 |
| | | LocationPoint,//定位点 |
| | | FindPathPoint,//寻路点 |
| | | LoopLine,//循环线 |
| | | FindPathLine,//寻路线 |
| | | StartEndPoint,//测试起终点 |
| | | } |
| | | |
| | | } |
| | | } |