diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels')
2 files changed, 325 insertions, 0 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs new file mode 100644 index 000000000..dd0c66c58 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs @@ -0,0 +1,313 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.BL.Entities; +using Tango.Core; +using Tango.Core.Commands; +using Tango.MachineStudio.RML.Models; +using Tango.SharedUI; +using OxyPlot; +using OxyPlot.Wpf; +using OxyPlot.Annotations; +using Tango.ColorCalibration; +using Tango.PMR.ColorLab; +using Tango.Logging; +using Tango.MachineStudio.Common.Notifications; + +namespace Tango.MachineStudio.RML.ViewModels +{ + public class ColorCalibrationViewVM : ExtendedObject + { + private IColorCalibrator _calibrator; + private INotificationProvider _notification; + + #region Properties + + private Rml _rml; + public Rml RML + { + get { return _rml; } + set { _rml = value; RaisePropertyChangedAuto(); } + } + + private BL.Entities.LiquidType _liquidType; + + public BL.Entities.LiquidType LiquidType + { + get { return _liquidType; } + set { _liquidType = value; RaisePropertyChangedAuto(); } + } + + private List<BL.Entities.LiquidType> _liquidTypes; + + public List<BL.Entities.LiquidType> LiquidTypes + { + get { return _liquidTypes; } + set { _liquidTypes = value; } + } + + + private ObservableCollection<CalibrationMeasurementModel> _measurements; + public ObservableCollection<CalibrationMeasurementModel> Measurements + { + get + { + return _measurements; + } + set + { + _measurements = value; RaisePropertyChangedAuto(); + } + } + + + private double _factor; + + public double Factor + { + get { return _factor; } + set { _factor = value; } + } + + public RelayCommand CreateGraphCommand { get; set; } + + + public string CalibrationType + { + get { return LiquidType == null ?"" : LiquidType.Name; } + } + + public Plot PlotControl { get; set; } + private IList<DataPoint> _points; + /// <summary> + /// Binding to ItemsSource of line chart. + /// </summary> + public IList<DataPoint> Points + { + get { return _points; } + set + { + _points = value; + RaisePropertyChangedAuto(); + } + } + private IList<DataPoint> _targetPoints; + /// <summary> + /// Binding to ItemsSource of line chart. + /// </summary> + public IList<DataPoint> TargetPoints + { + get { return _targetPoints; } + set + { + _targetPoints = value; + RaisePropertyChangedAuto(); + } + } + private int _step; + public int XStep + { + get { return _step; } + set { _step = value; RaisePropertyChangedAuto(); } + } + + private double _from; + /// <summary> + /// From use to binding to bottom axis min value + /// </summary> + public double From + { + get { return _from; } + set + { + _from = value; RaisePropertyChangedAuto(); + } + } + + private double _to; + /// <summary> + /// To use to binding to bottom axis max value + /// </summary> + public double To + { + get { return _to; } + set + { + _to = value; RaisePropertyChangedAuto(); + } + } + + #endregion + + public ColorCalibrationViewVM(INotificationProvider notification) + { + _notification = notification; + Measurements = new ObservableCollection<CalibrationMeasurementModel>() + { + new CalibrationMeasurementModel(), + new CalibrationMeasurementModel(), + new CalibrationMeasurementModel(), + }; + Factor = 0; + CreateGraphCommand = new RelayCommand(CreateGraph); + this.Points = new List<DataPoint>(); + TargetPoints = new List<DataPoint>(); + } + + public void Loading() + { + LiquidType = LiquidTypes.Count > 0 ? LiquidTypes[0] : null; + _calibrator = new DefaultColorCalibrator(); + + } + + private double GetLiquidFactor() + { + try{ + CalibrationInput conversionInput = new CalibrationInput(); + Measurements.ToList().ForEach(x => conversionInput.Measurements.Add(new CalibrationMeasurement{ L = x.L, A = x.A, B = x.B, NanoliterPerCentimeter = x.Ink })); + conversionInput.LiquidType = PMR.ColorLab.LiquidType.TryParse(_liquidType.Type.ToString(), out PMR.ColorLab.LiquidType outValue) ? outValue : PMR.ColorLab.LiquidType.Black; + + + LAB lab; + if (ColorCalibrationExt.TargetLiquidTypeToLAB.TryGetValue(_liquidType.Type, out lab)) + { + conversionInput.TargetL = lab.L; + conversionInput.TargetA = lab.A; + conversionInput.TargetB = lab.B; + } + CalibrationOutput result = _calibrator.GetLiquidFactor(conversionInput); + return result.LiquidFactor; + } + catch (Exception ex ) + { + LogManager.Log(ex, "Error occurred while trying to call GetLiquidFactor."); + } + return 0.0; + } + + #region CreateGraph + + private async void CreateGraph(object obj) + { + if (_liquidType == null) + return; + + LAB lab; + string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; + await Task.Factory.StartNew(() => + { + Factor = GetLiquidFactor(); + }); + + Points.Clear(); + TargetPoints.Clear(); + + To = 0; + From = 0; + + Measurements.ToList().ForEach(x =>{ + Points.Add(new DataPoint(x.Ink, x.L)); + TargetPoints.Add(new DataPoint(x.Ink,Factor)); }); + + _to = labType == "L"? 100 : 128; + _from = labType == "L" ? 0 : -127; + + RaisePropertyChanged("To"); + RaisePropertyChanged("From"); + XStep = (int)(Points.Count / 6); + RaisePropertyChanged("CalibrationType"); + PlotControl.InvalidatePlot(true); + + //await Task.Factory.StartNew(() => + //{ + // DataPoint ? intersectionpoints = FindIntersection(Points.ToArray(), TargetPoints.ToArray()); + // if(intersectionpoints == null) + // { + // InvokeUI(() => + // { + // _notification.ShowWarning(LogManager.Log($"No intersect target value with input values", LogCategory.Warning)); + // }); + // } + //}); + + } + + #endregion + + #region Intersect + + public DataPoint? FindIntersection(DataPoint[] line1, DataPoint[] line2) + { + for (int i = 0; i < line1.Length; i++) + { + int nextI = i; + nextI++; + if (nextI == line1.Length) break; + + for (int j = 0; j < line2.Length; j++) + { + int nextJ = j; + nextJ++; + if (nextJ == line2.Length) break; + DataPoint? d = CheckIntersecting(line1[i], line1[nextI], line2[j], line2[nextJ]); + return d; + + } + } + return null; + } + + public DataPoint? CheckIntersecting(DataPoint A, DataPoint B, DataPoint C, DataPoint D) + { + // Line AB represented as a1x + b1y = c1 + double a1 = B.Y - A.Y; + double b1 = A.X - B.X; + double c1 = a1 * (A.X) + b1 * (A.Y); + + // Line CD represented as a2x + b2y = c2 + double a2 = D.Y - C.Y; + double b2 = C.X - D.X; + double c2 = a2 * (C.X) + b2 * (C.Y); + + double determinant = a1 * b2 - a2 * b1; + + if (determinant == 0) + { + // The lines are parallel. This is simplified + } + else + { + double x = (b2 * c1 - b1 * c2) / determinant; + double y = (a1 * c2 - a2 * c1) / determinant; + + // check if the point lies on given line segment + /* To check if "x" is between "a" and "b"; + int m = (a+b)/2; + if(Math.abs(x-m) <= (Math.abs(a-m))) + { + } + */ + double mx1 = (A.X + B.X) / 2; + double mx2 = (C.X + D.X) / 2; + double my1 = (A.Y + B.Y) / 2; + double my2 = (C.Y + D.Y) / 2; + if (Math.Abs(x - mx1) <= Math.Abs(A.X - mx1) + && Math.Abs(x - mx2) <= Math.Abs(C.X - mx2) + && Math.Abs(y - my1) <= Math.Abs(A.Y - my1) + && Math.Abs(y - my2) <= Math.Abs(C.Y - my2)) + { + return new DataPoint(x, y); + } + + } + return null; + } + + #endregion + + } +} diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs index 1ceaf07df..36398a593 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/MainViewVM.cs @@ -164,6 +164,12 @@ namespace Tango.MachineStudio.RML.ViewModels set { _selectedSpool = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } + private ColorCalibrationViewVM _colorCalibrationVM; + public ColorCalibrationViewVM ColorCalibrationVM + { + get { return _colorCalibrationVM; } + set { _colorCalibrationVM = value; RaisePropertyChangedAuto(); } + } /// <summary> /// Gets or sets the manage RML command. @@ -374,6 +380,12 @@ namespace Tango.MachineStudio.RML.ViewModels LiquidTypesRmls = LiquidTypesRmls, }; + ColorCalibrationVM = new ColorCalibrationViewVM(_notification) + { + RML = ActiveRML, + LiquidTypes = LiquidTypesRmls.Where(x => x.LiquidType.HasPigment).ToList().Select(y => y.LiquidType).ToList(), + }; + _rmlBeforeSave = RmlDTO.FromObservable(ActiveRML); View.NavigateTo(RmlNavigationView.RmlView); |
