自动按照位置排序虚拟ID

This commit is contained in:
zxd 2019-12-11 20:41:07 +08:00
parent 6e7d33ed91
commit 9133800b62
3 changed files with 297 additions and 0 deletions

View File

@ -0,0 +1,222 @@
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Plane.Copters;
using Plane.FormationCreator.Formation;
using Plane.Geography;
using Plane.Windows.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Plane.FormationCreator.ViewModels
{
public class ConfigVirtualIdViewModel: ViewModelBase
{
CopterManager _copterManager;
public ConfigVirtualIdViewModel(CopterManager copterManager)
{
_copterManager = copterManager;
}
private int _SingleVirtualId = 0;
public int SingleVirtualId
{
get { return _SingleVirtualId; }
set { Set(nameof(SingleVirtualId), ref _SingleVirtualId, value); }
}
private int _SingleRowCount = 0;
public int SingleRowCount
{
get { return _SingleRowCount; }
set { Set(nameof(SingleRowCount), ref _SingleRowCount, value); }
}
/// <summary>
/// 清除所有编号
/// </summary>
private ICommand _ClearVirtualIdCommand;
public ICommand ClearVirtualIdCommand
{
get
{
return _ClearVirtualIdCommand ?? (_ClearVirtualIdCommand = new RelayCommand(() =>
{
foreach (var c in _copterManager.Copters)
{
c.VirtualId = 0;
}
Message.Show($"已清除所有飞机编号");
}));
}
}
/// <summary>
/// 设置单个虚拟ID
/// </summary>
private ICommand _SetSingleVirtualIdCommand;
public ICommand SetSingleVirtualIdCommand
{
get
{
return _SetSingleVirtualIdCommand ?? (_SetSingleVirtualIdCommand = new RelayCommand(() =>
{
if (SingleVirtualId <= 0)
{
System.Windows.MessageBox.Show("编号必须大于0");
return;
}
if (_copterManager.AcceptingControlCopters.Count() == 1)
{
var copter = _copterManager.AcceptingControlCopters.FirstOrDefault();
copter.VirtualId = SingleVirtualId;
Message.Show($"飞机{copter.Name} 设置编号={SingleVirtualId}");
}
}));
}
}
private ICommand _AutoLocationNumsCommand;
public ICommand AutoLocationNumsCommand
{
get
{
return _AutoLocationNumsCommand ?? (_AutoLocationNumsCommand = new RelayCommand(async () =>
{
int colCount = SingleRowCount;
int copterCount = _copterManager.Copters.Count;
if (copterCount % colCount != 0)
{
Alert.Show($"列数设置错误!");
return;
}
int rowCount = copterCount / colCount;
int num = 1;
if (_copterManager.AcceptingControlCopters.Count() == 4)
{
var copters = _copterManager.AcceptingControlCopters.ToList();
var firstCopter = copters[0];
var secondCopter = copters[1];
var thirdCopter = copters[2];
var fourthCopter = copters[3];
//第一列角度
double firstColazimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D(
firstCopter.Latitude, firstCopter.Longitude,
thirdCopter.Latitude, thirdCopter.Longitude));
//最后列角度
double lastColazimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D(
secondCopter.Latitude, secondCopter.Longitude,
fourthCopter.Latitude, fourthCopter.Longitude));
ICopter destCopter = null;
ICopter rowfirstCopter = null;
ICopter rowlastCopter = null;
for (int i = 0; i < rowCount; i++)
{
if (i == 0)
{
rowfirstCopter = firstCopter;
rowlastCopter = secondCopter;
}
else if (i == rowCount - 1)
{
rowfirstCopter = thirdCopter;
rowlastCopter = fourthCopter;
}
else
{
rowfirstCopter = FindNextCopter(rowfirstCopter, firstColazimuth);
rowlastCopter = FindNextCopter(rowlastCopter, lastColazimuth);
}
if (rowfirstCopter == null || rowlastCopter == null)
{
Alert.Show($"在确认第{i+1}列时候无法找到起始或结束飞机");
return;
}
for (int j = 0; j < colCount; j++)
{
double curRowAzimuth = GeographyUtils.RadToDeg(GeographyUtils.CalcDirection2D(
rowfirstCopter.Latitude, rowfirstCopter.Longitude,
rowlastCopter.Latitude, rowlastCopter.Longitude));
if (j == 0)
{
destCopter = rowfirstCopter;
}
else if (j == colCount - 1)
{
destCopter = rowlastCopter;
}
else
{
destCopter = FindNextCopter(destCopter, curRowAzimuth);
if (destCopter == null)
{
Alert.Show($"在寻找第{num}号位置时候无法找到对应位置飞机");
return;
}
}
destCopter.VirtualId = num;
Message.Show($"飞机{destCopter.Name} 虚拟ID = {destCopter.VirtualId}");
num++;
}
}
}
await Task.Delay(10);
}));
}
}
private ICopter FindNextCopter(ICopter startCopter, double azimuth)
{
ICopter retCopter = null;
bool foundCopter = false;
float stepLength = 0;
while (!foundCopter)
{
stepLength += 1.5f;
Tuple<double, double> targetLatLng = GeographyUtils.CalcLatLngSomeMetersAway2D(
startCopter.Latitude, startCopter.Longitude, (float)azimuth, stepLength);
List<ICopter> remainCopters = _copterManager.Copters.Where(o => o.VirtualId == 0).ToList();
foreach (var copter in remainCopters)
{
double temp = GeographyUtils.CalcDistance2D(copter.Latitude, copter.Longitude,
targetLatLng.Item1, targetLatLng.Item2);
if (temp < 1.5)
{
foundCopter = true;
retCopter = copter;
break;
}
}
if (stepLength > 15)
{
break;
}
}
return retCopter;
}
}
}

View File

@ -0,0 +1,44 @@
<UserControl x:Class="Plane.FormationCreator.Views.ConfigVirtualIdView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Plane.FormationCreator.Views"
mc:Ignorable="d"
BorderThickness="1"
Background="#FF2D2D2D" Width="464" Height="260" BorderBrush="#FF006595">
<Grid>
<StackPanel>
<StackPanel>
<TextBlock Margin="10,5" Text="手动编号"/>
<TextBlock Margin="10,5" Text="选中单架无人机输入虚拟ID后点击设置"/>
<StackPanel Margin="10,0" Orientation="Horizontal" HorizontalAlignment="Center">
<TextBox Margin="0,5" Width="80" VerticalAlignment="Center"
Text="{Binding SingleVirtualId}"/>
<Button Margin="10,5" Width="80" Content = "设置" VerticalAlignment="Center"
Command="{Binding SetSingleVirtualIdCommand}"/>
</StackPanel>
</StackPanel>
<StackPanel>
<TextBlock Margin="10,5" Text="自动编号"/>
<StackPanel Margin="10,0" >
<TextBlock Margin="0,5" Text="请从1号机开始按照顺序选中4个顶点的飞机后点击自动配置"/>
<StackPanel Margin="0,5" Orientation="Horizontal" HorizontalAlignment="Center">
<TextBlock Margin="0,0,10,0" Text="每行数量" VerticalAlignment="Center"/>
<TextBox Margin="0,0,10,0" Width="80" VerticalAlignment="Center"
Text="{Binding SingleRowCount}"/>
<Button Margin="0,0,10,0" Width="100" Content = "自动配置" VerticalAlignment="Center"
Command="{Binding AutoLocationNumsCommand}"/>
</StackPanel>
</StackPanel>
</StackPanel>
<StackPanel>
<TextBlock Margin="10,5" Text="清除编号"/>
<Button Margin="0,0" Width="120" Content = "清除所有编号" Command="{Binding ClearVirtualIdCommand}"/>
</StackPanel>
</StackPanel>
</Grid>
</UserControl>

View File

@ -0,0 +1,31 @@
using Microsoft.Practices.ServiceLocation;
using Plane.FormationCreator.ViewModels;
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.Navigation;
using System.Windows.Shapes;
namespace Plane.FormationCreator.Views
{
/// <summary>
/// ConfigVirtualIdView.xaml 的交互逻辑
/// </summary>
public partial class ConfigVirtualIdView : UserControl
{
public ConfigVirtualIdView()
{
InitializeComponent();
this.DataContext = ServiceLocator.Current.GetInstance<ConfigVirtualIdViewModel>();
}
}
}