Browse Source

Добавлен класс, занимающийся созданием выборок, а также добавлен интерфейся для создания выборок в отдельный файл

master
defend 6 years ago
parent
commit
b83bc18121
  1. 5
      Pores/Controllers/BaseSaveController.cs
  2. 41
      Pores/Controllers/BatchGenerator.cs
  3. 27
      Pores/Controllers/PoresToFileController.cs
  4. 47
      Pores/Controllers/PoresToFileSaver.cs
  5. 32
      Pores/MainWindow.xaml
  6. 22
      Pores/MainWindowViewModel.cs
  7. 4
      Pores/Models/Localizations/NormalLocalization.cs
  8. 2
      Pores/Models/Material.cs
  9. 9
      Pores/Models/Point.cs
  10. 7
      Pores/Models/Point2D.cs
  11. 4
      Pores/Models/Point3D.cs
  12. 2
      Pores/Models/Pore.cs
  13. 7
      Pores/Pores.csproj

5
Pores/Controllers/BaseSaveController.cs

@ -2,8 +2,7 @@
{ {
public abstract class BaseSaveController<TData> public abstract class BaseSaveController<TData>
{ {
public virtual TData Data { get; set; } = default(TData); public abstract void Save(TData data);
public abstract void Save(); public abstract TData Load();
public abstract void Load();
} }
} }

41
Pores/Controllers/BatchGenerator.cs

@ -0,0 +1,41 @@
using Pores.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace Pores.Controllers
{
public class BatchGenerator
{
public PoresToFileSaver batchSaver = new PoresToFileSaver("batch.log");
public Material Material { get; }
public IList<Pore> Pores { get; }
public BatchGenerator(Material material, IList<Pore> pores)
{
Material = material;
Pores = pores;
}
public void GenerateBatch(Point3D center, double width, double depth)
{
var p1 = new Point3D() { X = center.X - width / 2, Y = center.Y - width / 2, Z = Math.Abs(center.Z - depth) };
var p2 = new Point3D() { X = center.X + width / 2, Y = center.Y + width / 2, Z = center.Z };
batchSaver.FileName = "batch_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log";
batchSaver.Save(Pores.Where(p => IsPointInsideOfCube(p.Point, p1, p2)));
}
public bool IsPointInsideOfCube(Point3D p, Point3D vertex1, Point3D vertex2)
{
if (vertex1.X <= p.X && p.X <= vertex2.X)
if (vertex1.Y <= p.Y && p.Y <= vertex2.Y)
if (vertex1.Z <= p.Z && p.Z <= vertex2.Z)
return true;
return false;
}
}
}

27
Pores/Controllers/PoresToFileController.cs

@ -1,27 +0,0 @@
using Newtonsoft.Json;
using Pores.Models;
using System.Collections.Generic;
using System.IO;
namespace Pores.Controllers
{
public class PoresToFileController : BaseSaveController<IList<Pore>>
{
public string FileName { get; }
public override void Load()
{
}
public override void Save()
{
var output = JsonConvert.SerializeObject(Data);
File.WriteAllText(FileName, output);
}
public PoresToFileController(string fileName)
{
FileName = fileName;
}
}
}

47
Pores/Controllers/PoresToFileSaver.cs

@ -0,0 +1,47 @@
using Newtonsoft.Json;
using Pores.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
namespace Pores.Controllers
{
public class PoresToFileSaver : BaseSaveController<IEnumerable<Pore>>
{
public string FileName { get; set; }
public override IEnumerable<Pore> Load()
{
try
{
var data = File.ReadAllText(FileName);
var output = JsonConvert.DeserializeObject<IList<Pore>>(data);
return output;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
public override void Save(IEnumerable<Pore> data)
{
var output = JsonConvert.SerializeObject(data);
try
{
File.WriteAllText(FileName, output);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public PoresToFileSaver(string fileName)
{
FileName = fileName;
}
}
}

32
Pores/MainWindow.xaml

@ -9,14 +9,14 @@
Title="Pores" Height="450" Width="800"> Title="Pores" Height="450" Width="800">
<Grid> <Grid>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/> <ColumnDefinition Width="2*"/>
<ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/> <ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="2*"/>
<RowDefinition Height="1*"/> <RowDefinition Height="1*"/>
<RowDefinition Height="1*"/> <RowDefinition Height="1*"/>
<RowDefinition Height="60"/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
@ -33,14 +33,14 @@
</StackPanel> </StackPanel>
<StackPanel VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" Margin="20 0"> <StackPanel VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" Grid.RowSpan="2" Margin="20 0">
<TextBlock Text="Результаты работы"/> <TextBlock Text="Результаты работы"/>
<TextBlock Text="Количество пор: "/> <TextBlock Text="Количество пор: "/>
<TextBox Text="{Binding GenController.PoreCoordinates.Count, Mode=OneWay}" IsEnabled="False"/> <TextBox Text="{Binding GenController.PoreCoordinates.Count, Mode=OneWay}" IsEnabled="False"/>
</StackPanel> </StackPanel>
<StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Center"> <StackPanel Grid.Column="2" Grid.Row="1" VerticalAlignment="Center">
<ComboBox ItemsSource="{Binding Localizations}" SelectedItem="{Binding SelectedLocalization}"></ComboBox> <ComboBox ItemsSource="{Binding Localizations}" SelectedItem="{Binding SelectedLocalization}" HorizontalAlignment="Left" Width="198"></ComboBox>
<ContentControl Content="{Binding SelectedLocalization}"> <ContentControl Content="{Binding SelectedLocalization}">
<ContentControl.Resources> <ContentControl.Resources>
<DataTemplate DataType="{x:Type localizations:NormalLocalization}"> <DataTemplate DataType="{x:Type localizations:NormalLocalization}">
@ -56,8 +56,28 @@
</ContentControl> </ContentControl>
</StackPanel> </StackPanel>
<StackPanel Grid.Column="1" Grid.Row="2" VerticalAlignment="Bottom">
<Button Content="Сгенерировать поры" Command="{Binding GeneratePoresCommand}" Grid.Row="2"/>
<Button Content="Выборка в файл" Command="{Binding GenAndSaveBatchCommand}"
Grid.Row="2" Grid.Column="1"/>
<Button Content="Сохранить в файл" Command="{Binding SaveToFileCommand}" Grid.Row="2" Grid.Column="1"/>
</StackPanel>
<StackPanel VerticalAlignment="Center" Grid.Column="1">
<TextBlock Text="Параметры единичной выборки: "/>
<TextBlock Text="Координаты центра (Х): "/>
<TextBox Text="{Binding BatchCenter.X}"/>
<TextBlock Text="Координаты центра (Y): "/>
<TextBox Text="{Binding BatchCenter.Y}"/>
<TextBlock Text="Координаты центра (Z): "/>
<TextBox Text="{Binding BatchCenter.Z}"/>
<TextBlock Text="Ширина: "/>
<TextBox Text="{Binding BatchWidth}"/>
<TextBlock Text="Глубина:"/>
<TextBox Text="{Binding BatchDepth}"/>
</StackPanel>
<Button Content="Сгенерировать поры" Command="{Binding GeneratePoresCommand}" Grid.Row="2"/>
<Button Content="Сохранить в файл" Command="{Binding SaveToFileCommand}" Grid.Row="2" Grid.Column="1"/>
</Grid> </Grid>
</Window> </Window>

22
Pores/MainWindowViewModel.cs

@ -5,7 +5,6 @@ using Pores.Models.Localizations;
using Pores.Helpers; using Pores.Helpers;
using System.Windows.Input; using System.Windows.Input;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Collections.Generic;
namespace Pores namespace Pores
{ {
@ -15,11 +14,16 @@ namespace Pores
public IPoreLocalization SelectedLocalization { get; set; } public IPoreLocalization SelectedLocalization { get; set; }
public ModelGenerationController GenController { get; set; } public ModelGenerationController GenController { get; set; }
public ObservableCollection<IPoreLocalization> Localizations { get; set; } public ObservableCollection<IPoreLocalization> Localizations { get; set; }
public PoresToFileController saveController = new PoresToFileController("output.log"); public PoresToFileSaver saveController = new PoresToFileSaver("output.log");
public ICommand SaveToFileCommand { get; set; }
public int NumberOfPores { get; set; } public int NumberOfPores { get; set; }
public ICommand GeneratePoresCommand { get; set; } public ICommand GeneratePoresCommand { get; set; }
public ICommand SaveToFileCommand { get; set; }
public ICommand GenAndSaveBatchCommand { get; set; }
public double BatchWidth { get; set; }
public double BatchDepth { get; set; }
public Point3D BatchCenter { get; set; } = new Point3D();
public MainWindowViewModel() public MainWindowViewModel()
{ {
@ -29,6 +33,7 @@ namespace Pores
}; };
GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null); GeneratePoresCommand = new RelayCommand(StartGeneration, () => SelectedLocalization != null);
SaveToFileCommand = new RelayCommand(Save, () => GenController?.PoreCoordinates?.Count > 0); SaveToFileCommand = new RelayCommand(Save, () => GenController?.PoreCoordinates?.Count > 0);
GenAndSaveBatchCommand = new RelayCommand(CreateBatch, () => GenController?.PoreCoordinates?.Count > 0);
} }
public void StartGeneration() public void StartGeneration()
@ -39,8 +44,13 @@ namespace Pores
public void Save() public void Save()
{ {
saveController.Data = GenController.PoreCoordinates; saveController.Save(GenController.PoreCoordinates);
saveController.Save(); }
public void CreateBatch()
{
var batchGenerator = new BatchGenerator(Material, GenController.PoreCoordinates);
batchGenerator.GenerateBatch(BatchCenter, BatchWidth, BatchDepth);
} }
} }
} }

4
Pores/Models/Localizations/NormalLocalization.cs

@ -19,7 +19,7 @@ namespace Pores.Models.Localizations
return part1 * part2; return part1 * part2;
} }
public double GaussFunctionByHeight (Coordinate c) public double GaussFunctionByHeight (Point3D c)
{ {
return GaussFunction(c.Z, sigma, mu); return GaussFunction(c.Z, sigma, mu);
} }
@ -33,7 +33,7 @@ namespace Pores.Models.Localizations
for (int j = 0; j <= Material.NumberOfPores; j++) for (int j = 0; j <= Material.NumberOfPores; j++)
for (int k = 0; k <= Material.NumberOfPores; k++) for (int k = 0; k <= Material.NumberOfPores; k++)
{ {
var c = new Coordinate() var c = new Point3D()
{ {
X = (double) i / Material.NumberOfPores * Material.Width, X = (double) i / Material.NumberOfPores * Material.Width,
Y = (double) j / Material.NumberOfPores * Material.Depth, Y = (double) j / Material.NumberOfPores * Material.Depth,

2
Pores/Models/Material.cs

@ -4,7 +4,7 @@ namespace Pores.Models
{ {
public class Material : PropertyChangedClass public class Material : PropertyChangedClass
{ {
public Coordinate GetCoordinate { get; set; } public Point3D GetCoordinate { get; set; }
public double Height { get; set; } public double Height { get; set; }
public double Width { get; set; } public double Width { get; set; }
public double Depth { get; set; } public double Depth { get; set; }

9
Pores/Models/Point.cs

@ -0,0 +1,9 @@
using Pores.Helpers;
namespace Pores.Models
{
public class Point : PropertyChangedClass
{
public double X { get; set; }
}
}

7
Pores/Models/Point2D.cs

@ -0,0 +1,7 @@
namespace Pores.Models
{
public class Point2D : Point
{
public double Y { get; set; }
}
}

4
Pores/Models/Coordinate.cs → Pores/Models/Point3D.cs

@ -4,10 +4,8 @@ using System.Collections.Generic;
namespace Pores.Models namespace Pores.Models
{ {
[Serializable] [Serializable]
public class Coordinate public class Point3D : Point2D
{ {
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; } public double Z { get; set; }
public List<double> ToList() public List<double> ToList()

2
Pores/Models/Pore.cs

@ -5,7 +5,7 @@ namespace Pores.Models
[Serializable] [Serializable]
public class Pore public class Pore
{ {
public Coordinate Point { get; set; } public Point3D Point { get; set; }
public double Radius { get; set; } public double Radius { get; set; }
} }
} }

7
Pores/Pores.csproj

@ -77,13 +77,16 @@
<SubType>Code</SubType> <SubType>Code</SubType>
</Compile> </Compile>
<Compile Include="Controllers\BaseSaveController.cs" /> <Compile Include="Controllers\BaseSaveController.cs" />
<Compile Include="Controllers\PoresToFileController.cs" /> <Compile Include="Controllers\BatchGenerator.cs" />
<Compile Include="Controllers\PoresToFileSaver.cs" />
<Compile Include="Controllers\ModelGenerationController.cs" /> <Compile Include="Controllers\ModelGenerationController.cs" />
<Compile Include="Helpers\PropertyChanged.cs" /> <Compile Include="Helpers\PropertyChanged.cs" />
<Compile Include="Helpers\RelayCommand.cs" /> <Compile Include="Helpers\RelayCommand.cs" />
<Compile Include="Interfaces\IPoresLocalization.cs" /> <Compile Include="Interfaces\IPoresLocalization.cs" />
<Compile Include="MainWindowViewModel.cs" /> <Compile Include="MainWindowViewModel.cs" />
<Compile Include="Models\Coordinate.cs" /> <Compile Include="Models\Point.cs" />
<Compile Include="Models\Point2D.cs" />
<Compile Include="Models\Point3D.cs" />
<Compile Include="Models\Localizations\BaseLocalization.cs" /> <Compile Include="Models\Localizations\BaseLocalization.cs" />
<Compile Include="Models\Localizations\NormalLocalization.cs" /> <Compile Include="Models\Localizations\NormalLocalization.cs" />
<Compile Include="Models\Material.cs" /> <Compile Include="Models\Material.cs" />

Loading…
Cancel
Save