该程序满足面阵和线阵的要求。
前端代码
xml
<Window x:Class="相机镜头选型.MainWindow" Loaded="Window_Loaded"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
xmlns:svgc = "http://sharpvectors.codeplex.com/svgc/"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:相机镜头选型"
mc:Ignorable="d"
Title="相机镜头选型工具" Height="500" Width="1000"
>
<Window.Background>
<RadialGradientBrush GradientOrigin="0.5,-0.8" Center="0.5,0" RadiusX="0.7" RadiusY="0.7">
<GradientStop Color="#A02B40" Offset="0"/>
<GradientStop Color="#08113c" Offset="1"/>
</RadialGradientBrush>
</Window.Background>
<Window.Resources>
<Style x:Key="myTextBlock" TargetType="TextBlock" >
<Setter Property="Foreground" Value="White"></Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
</Grid>
<!--Canvas -->
<Grid Grid.Column="1" >
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<!--DataGrid -->
<DataGrid Grid.Row="0" x:Name="dataGrid" ItemsSource="{Binding}" AutoGenerateColumns="False" Height="Auto" Margin="5">
<DataGrid.Columns>
<DataGridTextColumn Header="参数" Binding="{Binding Name}" Width="*"></DataGridTextColumn>
<DataGridTextColumn Header="数值" Binding="{Binding Value}" Width="*"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<!-- -->
<Canvas Grid.Row="1" Width="Auto" Height="Auto" Margin="5">
<svgc:SvgViewbox IsHitTestVisible="False" Source="D:\02_Study\06_WPF\WpfApp1\相机镜头选型\Asset\Lens_Selector.svg" Height="240" Width="450" HorizontalAlignment="Center" VerticalAlignment="Top" Canvas.ZIndex="1" Canvas.Left="10" Canvas.Top="-5"/>
<TextBox Text="" Width="50" Height="18" Canvas.ZIndex="2" Canvas.Left="410" Canvas.Top="31" Background="White" x:Name="vof_w"></TextBox>
<TextBox Text="{Binding Text,ElementName=vof_h,Mode=OneWay}" Width="50" Height="18" Panel.ZIndex="2" Canvas.Left="440" Canvas.Top="141" Background="White" HorizontalAlignment="Left" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.487"/>
<TextBox Text="{Binding Text,ElementName=d1,Mode=OneWay}" Width="50" Height="18" Panel.ZIndex="2" Canvas.Left="261" Canvas.Top="186" Background="White" RenderTransformOrigin="0.433,0.521" HorizontalAlignment="Left" VerticalAlignment="Top"/>
<TextBox Text="" Width="50" Height="18" Panel.ZIndex="2" Canvas.Left="94" Canvas.Top="212" Background="White" RenderTransformOrigin="0.5,0.487" HorizontalAlignment="Left" VerticalAlignment="Top" x:Name="lens"/>
</Canvas>
</Grid>
<!-- -->
<!--ControlPanle -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<GroupBox Header="基本参数" Foreground="White" Margin="5">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel >
<TextBlock Style="{StaticResource myTextBlock}" Height="17" Margin="5,5,5,5" >物距(mm):</TextBlock>
<TextBlock Style="{StaticResource myTextBlock}" Height="17" Margin="5,5,5,5">靶面短边(p):</TextBlock>
<TextBlock Style="{StaticResource myTextBlock}" Height="17" Margin="5,5,5,5">视野短边(mm):</TextBlock>
</StackPanel>
<StackPanel Grid.Column="1" VerticalAlignment="Top" >
<TextBox Height="17" Margin="5,5,5,5" x:Name="d1" TextChanged="Calc" ></TextBox>
<TextBox Height="17" Margin="5,5,5,5" x:Name="res_h" TextChanged="Calc"></TextBox>
<TextBox Height="17" Margin="5,5,5,5" x:Name="vof_h" TextChanged="Calc" ></TextBox>
</StackPanel>
<StackPanel Grid.Column="2">
<Slider Height="17" Margin="5,5,5,5" Width="93" Value="{Binding Text,ElementName=d1,Mode=TwoWay}" TickFrequency="1" IsSnapToTickEnabled="True" x:Name="sld_wj" Maximum="3000.0" ValueChanged="Slider_ValueChanged" />
<Slider Height="17" Margin="5,5,5,5" Width="93" Value="{Binding Text,ElementName=res_h,Mode=TwoWay}" TickFrequency="1" IsSnapToTickEnabled="True" x:Name="sld_res_h" Maximum="{Binding Text,ElementName=res_h,Mode=OneWay}" ValueChanged="Slider_ValueChanged" />
<Slider Height="17" Margin="5,5,5,5" Width="93" Value="{Binding Text,ElementName=vof_h,Mode=TwoWay}" TickFrequency="0.1" IsSnapToTickEnabled="True" x:Name="sld_vof_h" Maximum="1000" ValueChanged="Slider_ValueChanged" />
</StackPanel>
</Grid>
</GroupBox>
<GroupBox Grid.Row="1" Header="其他参数" Foreground="White" Margin="5">
<Grid >
<Grid.ColumnDefinitions >
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel >
<TextBlock Style="{StaticResource myTextBlock}" Height="17" Margin="5,5,5,5" >光圈(F):</TextBlock>
<TextBlock Style="{StaticResource myTextBlock}" Height="17" Margin="5,5,5,5">像元(mm):</TextBlock>
<TextBlock Style="{StaticResource myTextBlock}" Height="17" Margin="5,5,5,5">靶面长边(p):</TextBlock>
</StackPanel>
<StackPanel Grid.Column="1" VerticalAlignment="Top" >
<TextBox Height="17" Margin="5,5,5,5" x:Name="gq" TextChanged="Calc" ></TextBox>
<TextBox Height="17" Margin="5,5,5,5" x:Name="size" TextChanged="Calc"></TextBox>
<TextBox Height="17" Margin="5,5,5,5" x:Name="res_w" TextChanged="Calc"></TextBox>
</StackPanel>
<StackPanel Grid.Column="2">
<Slider Height="17" Margin="5,5,5,5" Width="93" Value="{Binding Text,ElementName=gq,Mode=TwoWay}" TickFrequency="0.1" IsSnapToTickEnabled="True" x:Name="sld_gq" Maximum="6" ValueChanged="Slider_ValueChanged"/>
</StackPanel>
</Grid>
</GroupBox>
</Grid>
<!-- -->
</Grid>
</Window>
参数预加载
html
Loaded="Window_Loaded"
数据绑定
html
<Slider Height="17" Margin="5,5,5,5" Width="93" Value="{Binding Text,ElementName=res_h,Mode=TwoWay}" TickFrequency="1" IsSnapToTickEnabled="True" x:Name="sld_res_h" Maximum="{Binding Text,ElementName=res_h,Mode=OneWay}" ValueChanged="Slider_ValueChanged" />
2.C#代码
c
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;
using System.Collections.ObjectModel;
using LiveCharts;
using LiveCharts.Wpf;
namespace 相机镜头选型
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dataGrid.DataContext = this;
//BindSliderToTextBox(sld_wj, d1);
//BindSliderToTextBox(sld_res_h, res_h);
//BindSliderToTextBox(sld_vof_h, vof_h);
SeriesCollection = new SeriesCollection
{
new LineSeries
{
Title = "Series 1",
Values = new ChartValues<double> { 4, 6, 5, 2 ,4 }
},
new LineSeries
{
Title = "Series 2",
Values = new ChartValues<double> { 6, 7, 3, 4 ,6 },
},
new LineSeries
{
Title = "Series 3",
Values = new ChartValues<double> { 4,2,7,2,7 },
}
};
Labels = new[] { "Jan", "Feb", "Mar", "Apr", "May" };
//YFormatter = value => value.ToString("C");
//modifying the series collection will animate and update the chart
//SeriesCollection.Add(new LineSeries
//{
// Title = "Series 4",
// Values = new ChartValues<double> { 5, 3, 2, 4 },
// LineSmoothness = 0, //0: straight lines, 1: really smooth lines
// PointGeometry = Geometry.Parse("m 25 70.36218 20 -28 -20 22 -8 -6 z"),
// PointGeometrySize = 50,
// PointForeground = Brushes.Gray
//});
//modifying any series values will also animate and update the chart
//SeriesCollection[3].Values.Add(5d);
DataContext = this;
}
public SeriesCollection SeriesCollection { get; set; }
public string[] Labels { get; set; }
//public Func<double, string> YFormatter { get; set; }
private List<double> getData()
{
List<double> res = new List<double>();
if (d1.Text != "" && size.Text != "" && vof_h.Text != "" && res_h.Text != "" && res_w.Text != "" && gq.Text != "")
{
double d_wj = double.Parse(d1.Text);
double pixel_size = double.Parse(size.Text);
double vof_height = double.Parse(vof_h.Text);
double res_height = double.Parse(res_h.Text);
double res_width = double.Parse(res_w.Text);
double guangquan = double.Parse(gq.Text);
res.Add(d_wj);
res.Add(pixel_size);
res.Add(vof_height);
res.Add(res_height);
res.Add(res_width);
res.Add(guangquan);
}
else
{
res.Add(0.0);
res.Add(0.0);
res.Add(0.0);
res.Add(0.0);
res.Add(0.0);
res.Add(0.0);
}
return res;
}
//double d_wj, double pixel_size, double vof_height,double res_height,double res_width,double guangquan
private void Window_Loaded(object sender, RoutedEventArgs e)
{
d1.Text = "2700";
res_h.Text = "4096";
vof_h.Text = "1000";
res_w.Text = "1392";
gq.Text = "2";
size.Text = "0.00465";
}
private void Calc(object sender, RoutedEventArgs e)
{
List<double> data = getData();
List<double> res = calcpara(data[0],data[1],data[2],data[3],data[4],data[5]);
printres(res, data[2]);
}
//List<int> xaxisList = Enumerable.Range(1, 10).ToList();
//d_jsj_list = new ChartValues<double> { };
//foreach (var x in xaxisList )
//{
// d_jsj_list.add(calc_d_jsj);
//}
private void printres(List<double> res, double vof_height)
{
if (vof_w != null && lens != null && res != null)
{
vof_w.Text = res[4].ToString();
lens.Text = res[1].ToString();
}
else
{
// 处理 vof_w、lens 或 res 为 null 或索引越界的情况
}
ObservableCollection<Member> memberData = new ObservableCollection<Member>();
memberData.Add(new Member()
{
Name = "放大倍率",
Value = res[0].ToString(),
});
memberData.Add(new Member()
{
Name = "焦距",
Value = Math.Round(res[1],2).ToString(),
});
memberData.Add(new Member()
{
Name = "景深近",
Value = res[2].ToString(),
});
memberData.Add(new Member()
{
Name = "景深远",
Value = res[3].ToString(),
});
memberData.Add(new Member()
{
Name = "视野大小",
Value =Math.Round(res[4],1).ToString() + " X " + vof_height.ToString(),
});
memberData.Add(new Member()
{
Name = "mm/pixel",
Value = res[5].ToString(),
});
dataGrid.DataContext = memberData;
}
//private void BindSliderToTextBox(Slider slider, TextBox textBox)
//{
// slider.ValueChanged += (s, e) =>
// {
// textBox.Text = (slider.Value).ToString("0.00");
// };
//}
static List<double> calcpara(double d_wj, double pixel_size, double vof_height,double res_height,double res_width,double guangquan)
{
double res_scale = Math.Round(vof_height / (res_height * pixel_size), 2);
double res_diag = Math.Sqrt(res_width * res_width + res_height * res_height);
double misanyuan = res_diag / 1730 * pixel_size;
double d_lens = d_wj / res_scale;
double d_xj = 1 / (1 / d_lens - 1 / d_wj);
double d_djjl = d_xj + d_wj;
double d_jsj = Math.Round(d_wj - guangquan * misanyuan * d_djjl * d_djjl / (d_lens * d_lens + guangquan * misanyuan * d_djjl), 2);
double d_jsy = Math.Round(d_wj + guangquan * misanyuan * d_djjl * d_djjl / (d_lens * d_lens + guangquan * misanyuan * d_djjl), 2);
double js = d_jsy - d_jsj;
double vof_width = Math.Round(res_width * pixel_size * res_scale, 2);
double resolution = Math.Round(vof_width / res_width, 2);
List<double> res = new List<double> { res_scale, d_lens, d_jsj, d_jsy,vof_width,resolution };
return res;
}
public class Member
{
public string Name { get; set; }
public string Value { get; set; }
}
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
List<double> data = getData();
List<double> res = calcpara(data[0], data[1], data[2], data[3], data[4], data[5]);
printres(res, data[2]);
}
private void TextChanged(object sender, TextChangedEventArgs e)
{
List<double> data = getData();
List<double> res = calcpara(data[0], data[1], data[2], data[3], data[4], data[5]);
printres(res, data[2]);
}
}
}