|
|
@ -16,48 +16,48 @@ namespace MODELING_DEF_DES_MI_VI.Modules.PoreInspectionUnit |
|
|
|
public class PoreInspectionViewModel : PropertyChangedClass |
|
|
|
{ |
|
|
|
public MaterialModel GlobalMaterial { get; set; } |
|
|
|
public ObservableCollection<Shape> Ellipses { get; set; } = new ObservableCollection<Shape>(); |
|
|
|
public List<FrameworkElement> Ellipses { get; set; } = new List<FrameworkElement>(); |
|
|
|
public Camera2D Camera { get; set; } |
|
|
|
|
|
|
|
private CancellationTokenSource ctSource { get; set; } |
|
|
|
private bool IsCanceled { get; set; } |
|
|
|
private bool IsBusy { get; set; } |
|
|
|
|
|
|
|
public PoreInspectionViewModel(MaterialModel material) |
|
|
|
public PoreInspectionViewModel(MaterialModel material, Camera2D camera) |
|
|
|
{ |
|
|
|
GlobalMaterial = material; |
|
|
|
Camera = new Camera2D() |
|
|
|
{ |
|
|
|
Speed = 1.0, |
|
|
|
CutDepth = 1.0, |
|
|
|
DepthVisibility = 2.0, |
|
|
|
Scale = 40, |
|
|
|
StartPosition = new Point2D(0 * 40, 0 * 40), |
|
|
|
}; |
|
|
|
Camera = camera; |
|
|
|
Application.Current.MainWindow.KeyDown += OnKeyDown; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public async Task UpdateModel() |
|
|
|
{ |
|
|
|
await Task.Run(() => |
|
|
|
await Task.Run(async () => |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
ctSource = new CancellationTokenSource(); |
|
|
|
Application.Current.Dispatcher.Invoke(() => |
|
|
|
while (IsBusy) await Task.Delay(40); |
|
|
|
IsBusy = true; |
|
|
|
IsCanceled = false; |
|
|
|
await Application.Current.Dispatcher.Invoke(async () => |
|
|
|
{ |
|
|
|
List<Shape> bufferedShapes = new List<Shape>(); |
|
|
|
List<FrameworkElement> bufferedShapes = new List<FrameworkElement>(); |
|
|
|
bufferedShapes.Add(CreateMaterial()); |
|
|
|
|
|
|
|
var visibilityRange = Tuple.Create(Camera.CutDepth / Camera.Scale, (Camera.DepthVisibility + Camera.CutDepth) / Camera.Scale); |
|
|
|
var visibilityRange = |
|
|
|
Tuple.Create( |
|
|
|
(Camera.CutDepth + Camera.DepthVisibility) / Camera.Scale, |
|
|
|
(Camera.CutDepth - Camera.DepthVisibility) / Camera.Scale); |
|
|
|
foreach (Pore pore in GlobalMaterial.Pores) |
|
|
|
{ |
|
|
|
if (ctSource.IsCancellationRequested) |
|
|
|
if (IsCanceled) |
|
|
|
return; |
|
|
|
if (visibilityRange.Item1 < pore.Point.Z && pore.Point.Z < visibilityRange.Item2) |
|
|
|
if (visibilityRange.Item2 < pore.Point.Z && pore.Point.Z < visibilityRange.Item1) |
|
|
|
bufferedShapes.Add(CreateEllipse(pore)); |
|
|
|
} |
|
|
|
Ellipses.Clear(); |
|
|
|
bufferedShapes.ForEach(s => Ellipses.Add(s)); |
|
|
|
Ellipses = bufferedShapes; |
|
|
|
await Task.Delay(20); |
|
|
|
IsBusy = false; |
|
|
|
}); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
@ -70,21 +70,27 @@ namespace MODELING_DEF_DES_MI_VI.Modules.PoreInspectionUnit |
|
|
|
private Ellipse CreateEllipse(Pore pore) |
|
|
|
{ |
|
|
|
var e = new Ellipse(); |
|
|
|
var cuttingHeight = Math.Abs(pore.Point.Z * Camera.Scale - Camera.CutDepth); |
|
|
|
if (cuttingHeight < pore.Radius * Camera.Scale) |
|
|
|
var scaledPoreRadius = pore.Radius * Camera.Scale; |
|
|
|
var distanceFromCameraToPore = Camera.CutDepth - pore.Point.Z * Camera.Scale; |
|
|
|
var cuttingHeight = Math.Abs(distanceFromCameraToPore); |
|
|
|
if (cuttingHeight < scaledPoreRadius) |
|
|
|
{ |
|
|
|
e.Fill = Brushes.Violet; |
|
|
|
var cuttedDiameter = Math.Sqrt(Math.Pow(pore.Radius * Camera.Scale, 2) - Math.Pow(cuttingHeight, 2)) * 2; |
|
|
|
var cuttedDiameter = Math.Sqrt(Math.Pow(scaledPoreRadius, 2) - Math.Pow(cuttingHeight, 2)) * 2; |
|
|
|
e.Width = cuttedDiameter; |
|
|
|
e.Height = cuttedDiameter; |
|
|
|
} |
|
|
|
else |
|
|
|
else if (pore.Point.Z < Camera.CutDepth / Camera.Scale) |
|
|
|
{ |
|
|
|
e.Opacity = 0.5; |
|
|
|
byte colorDegree = Convert.ToByte(pore.Point.Z * Camera.Scale / (Camera.DepthVisibility + Camera.CutDepth) * 255); |
|
|
|
byte colorDegree = Convert.ToByte((1 - distanceFromCameraToPore / Camera.DepthVisibility) * 255); |
|
|
|
e.Fill = new SolidColorBrush(Color.FromRgb(colorDegree, colorDegree, colorDegree)); |
|
|
|
e.Width = pore.Radius * 2 * Camera.Scale; |
|
|
|
e.Height = pore.Radius * 2 * Camera.Scale; |
|
|
|
e.Width = scaledPoreRadius * 2; |
|
|
|
e.Height = scaledPoreRadius * 2; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
e.Opacity = 0; |
|
|
|
} |
|
|
|
|
|
|
|
var x = Camera.Scale * pore.Point.X + Camera.StartPosition.X - e.Width / 2; |
|
|
@ -109,61 +115,72 @@ namespace MODELING_DEF_DES_MI_VI.Modules.PoreInspectionUnit |
|
|
|
|
|
|
|
private void OnKeyDown(object sender, KeyEventArgs e) |
|
|
|
{ |
|
|
|
if (!IsBusy) |
|
|
|
switch (e.Key) |
|
|
|
{ |
|
|
|
case Key.A: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.StartPosition.X -= Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.D: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.StartPosition.X += Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.W: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.StartPosition.Y -= Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.S: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.StartPosition.Y += Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.OemMinus: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.Scale /= 2; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.OemPlus: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.Scale *= 2; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.R: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.CutDepth += Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.F: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.CutDepth -= Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.D9: |
|
|
|
if (Camera.Speed > 1) |
|
|
|
{ |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.Speed--; |
|
|
|
UpdateModel(); |
|
|
|
} |
|
|
|
break; |
|
|
|
case Key.D0: |
|
|
|
ctSource.Cancel(); |
|
|
|
IsCanceled = true; |
|
|
|
Camera.Speed++; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.T: |
|
|
|
IsCanceled = true; |
|
|
|
Camera.DepthVisibility -= Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
case Key.G: |
|
|
|
IsCanceled = true; |
|
|
|
Camera.DepthVisibility += Camera.Speed; |
|
|
|
UpdateModel(); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|