From 487fc03281c2dfde034e73ad99bd74ec4f63f69a Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Wed, 17 Jun 2020 14:43:46 +0300 Subject: RML color calibration. --- .../ViewModels/ColorCalibrationViewVM.cs | 199 +++++++++++++++++++++ .../ViewModels/MainViewVM.cs | 12 ++ 2 files changed, 211 insertions(+) create mode 100644 Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels') 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..a6a2b3f7b --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs @@ -0,0 +1,199 @@ +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; + + +namespace Tango.MachineStudio.RML.ViewModels +{ + public class ColorCalibrationViewVM : ExtendedObject + { + + #region Properties + + private Rml _rml; + public Rml RML + { + get { return _rml; } + set { _rml = value; RaisePropertyChangedAuto(); } + } + + private LiquidType _liquidType; + + public LiquidType LiquidType + { + get { return _liquidType; } + set { _liquidType = value; RaisePropertyChangedAuto(); } + } + + private List _liquidTypes; + + public List LiquidTypes + { + get { return _liquidTypes; } + set { _liquidTypes = value; } + } + + + private ObservableCollection _measurements; + public ObservableCollection 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 _points; + /// + /// Binding to ItemsSource of line chart. + /// + public IList Points + { + get { return _points; } + set + { + _points = value; + RaisePropertyChangedAuto(); + } + } + private IList _targetPoints; + /// + /// Binding to ItemsSource of line chart. + /// + public IList TargetPoints + { + get { return _targetPoints; } + set + { + _targetPoints = value; + RaisePropertyChangedAuto(); + } + } + private int _step; + public int XStep + { + get { return _step; } + set { _step = value; RaisePropertyChangedAuto(); } + } + + private double _from; + /// + /// From use to binding to bottom axis min value + /// + public double From + { + get { return _from; } + set + { + _from = value; RaisePropertyChangedAuto(); + } + } + + private double _to; + /// + /// To use to binding to bottom axis max value + /// + public double To + { + get { return _to; } + set + { + _to = value; RaisePropertyChangedAuto(); + } + } + + #endregion + + public ColorCalibrationViewVM() + { + Measurements = new ObservableCollection() + { + new CalibrationMeasurementModel(), + new CalibrationMeasurementModel(), + new CalibrationMeasurementModel(), + }; + Factor = 0; + CreateGraphCommand = new RelayCommand(CreateGraph); + this.Points = new List(); + TargetPoints = new List(); + } + + public void Loading() + { + LiquidType = LiquidTypes.Count > 0 ? LiquidTypes[0] : null; + } + + private void CreateGraph(object obj) + { + if (_liquidType == null) + return; + + LAB lab; + string color = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; + if( ColorCalibrationExt.TargetLiquidTypeToLAB.TryGetValue(_liquidType.Type, out lab)) + { + var value = lab.GetType().GetProperty(color)?.GetValue(lab, null); + Factor = (double)value; + } + + + + //call color calibration function + //////////////////////// + /// + + + 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 = color == "L"? 100 : 128; + _from = color == "L" ? 0 : -127; + + RaisePropertyChanged("To"); + RaisePropertyChanged("From"); + XStep = (int)(Points.Count / 6); + RaisePropertyChanged("CalibrationType"); + PlotControl.InvalidatePlot(true); + } + } +} 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..4939cbb48 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(); } + } /// /// Gets or sets the manage RML command. @@ -374,6 +380,12 @@ namespace Tango.MachineStudio.RML.ViewModels LiquidTypesRmls = LiquidTypesRmls, }; + ColorCalibrationVM = new ColorCalibrationViewVM() + { + RML = ActiveRML, + LiquidTypes = LiquidTypesRmls.Where(x => x.LiquidType.HasPigment).ToList().Select(y => y.LiquidType).ToList(), + }; + _rmlBeforeSave = RmlDTO.FromObservable(ActiveRML); View.NavigateTo(RmlNavigationView.RmlView); -- cgit v1.3.1 From dd007b87b400427c02b57983ce3c1cce695bc960 Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Wed, 17 Jun 2020 19:04:33 +0300 Subject: Color Calibration. Added calling Native method "GetLiquidFactor". --- .../ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp | 2 + .../Tango.MachineStudio.RML.csproj | 4 ++ .../ViewModels/ColorCalibrationViewVM.cs | 61 ++++++++++++++-------- 3 files changed, 46 insertions(+), 21 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels') diff --git a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp index f2c88f1cc..8d49acf74 100644 --- a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp +++ b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp @@ -20,6 +20,8 @@ size_t Tango::ColorLib::ColorCalibrator::GetLiquidFactor(uint8_t * input_buffer, calibrationInput = (CalibrationInput*)malloc(sizeof(CalibrationInput)); calibrationInput = calibration_input__unpack(NULL, input_buffer_size, input_buffer); + //Call Mirta with input + //Init Output CalibrationOutput *calibrationOutput = (CalibrationOutput*)malloc(sizeof(CalibrationOutput)); calibration_output__init(calibrationOutput); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Tango.MachineStudio.RML.csproj b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Tango.MachineStudio.RML.csproj index 3e2dcc67c..1f9d56f07 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Tango.MachineStudio.RML.csproj +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Tango.MachineStudio.RML.csproj @@ -162,6 +162,10 @@ {40085232-aced-4cbe-945b-90ba8153c151} Tango.BrushPicker + + {b60c695c-61e8-4091-b506-4c45349c04aa} + Tango.ColorCalibration + {b4fe6485-4161-4b36-bc08-67e0b53d01b7} Tango.ColorConversion 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 index a6a2b3f7b..24867122e 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs @@ -12,13 +12,14 @@ using Tango.SharedUI; using OxyPlot; using OxyPlot.Wpf; using OxyPlot.Annotations; - +using Tango.ColorCalibration; +using Tango.PMR.ColorLab; namespace Tango.MachineStudio.RML.ViewModels { public class ColorCalibrationViewVM : ExtendedObject { - + private IColorCalibrator _calibrator; #region Properties private Rml _rml; @@ -28,17 +29,17 @@ namespace Tango.MachineStudio.RML.ViewModels set { _rml = value; RaisePropertyChangedAuto(); } } - private LiquidType _liquidType; + private BL.Entities.LiquidType _liquidType; - public LiquidType LiquidType + public BL.Entities.LiquidType LiquidType { get { return _liquidType; } set { _liquidType = value; RaisePropertyChangedAuto(); } } - private List _liquidTypes; + private List _liquidTypes; - public List LiquidTypes + public List LiquidTypes { get { return _liquidTypes; } set { _liquidTypes = value; } @@ -154,27 +155,45 @@ namespace Tango.MachineStudio.RML.ViewModels public void Loading() { LiquidType = LiquidTypes.Count > 0 ? LiquidTypes[0] : null; + _calibrator = new DefaultColorCalibrator(); + } - private void CreateGraph(object obj) + 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; + } + + private async void CreateGraph(object obj) { if (_liquidType == null) return; LAB lab; - string color = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; - if( ColorCalibrationExt.TargetLiquidTypeToLAB.TryGetValue(_liquidType.Type, out lab)) + string labType = ColorCalibrationExt.DisplayLiquidTypeToLABType[_liquidType.Type]; + await Task.Factory.StartNew(() => { - var value = lab.GetType().GetProperty(color)?.GetValue(lab, null); - Factor = (double)value; - } - - - - //call color calibration function - //////////////////////// - /// - + Factor = GetLiquidFactor(); + }); Points.Clear(); TargetPoints.Clear(); @@ -186,8 +205,8 @@ namespace Tango.MachineStudio.RML.ViewModels Points.Add(new DataPoint(x.Ink, x.L)); TargetPoints.Add(new DataPoint(x.Ink,Factor)); }); - _to = color == "L"? 100 : 128; - _from = color == "L" ? 0 : -127; + _to = labType == "L"? 100 : 128; + _from = labType == "L" ? 0 : -127; RaisePropertyChanged("To"); RaisePropertyChanged("From"); -- cgit v1.3.1 From bdce9ac6cda523781bae61e08587df974974ea98 Mon Sep 17 00:00:00 2001 From: Victoria Plitt Date: Sun, 21 Jun 2020 15:19:41 +0300 Subject: Color Calibrator . Added C++ functions. MaximalDispensingRate. --- .../ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp | 259 +++++++++++++++++++-- .../ColorLib/Tango.ColorLib_v4/ColorCalibrator.h | 18 ++ .../PMR/ColorLab/CalibrationOutput.pb-c.c | 37 ++- .../PMR/ColorLab/CalibrationOutput.pb-c.h | 5 +- .../Models/CalibrationMeasurementModel.cs | 21 +- .../ViewModels/ColorCalibrationViewVM.cs | 99 +++++++- .../ViewModels/MainViewVM.cs | 2 +- .../Views/ColorCalibrationView.xaml | 2 +- 8 files changed, 408 insertions(+), 35 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels') diff --git a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp index 8d49acf74..90c023060 100644 --- a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp +++ b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.cpp @@ -2,15 +2,46 @@ #include "CalibrationInput.pb-c.h" #include "CalibrationOutput.pb-c.h" #include "CalibrationMeasurement.pb-c.h" +#include +#include +#include +using namespace std; +#define MDRThr 1.0 +#define epsTol 0.05 +#define PI 4*atan(1.0) +#define dEThr 1.0 -Tango::ColorLib::ColorCalibrator::ColorCalibrator() + +Tango::ColorLib::ColorCalibrator::ColorCalibrator(): m_nsize(0), m_nlcm(NULL), m_LabData(NULL), m_inkChannel(0), m_targetVal(NULL), m_CalIndex(0) { } -Tango::ColorLib::ColorCalibrator::~ColorCalibrator() +Tango::ColorLib::ColorCalibrator::~ColorCalibrator() { + ClearData(); +} + +void Tango::ColorLib::ColorCalibrator::ClearData() +{ + if (m_LabData != NULL) + { + for (int i = 0; i < m_nsize; ++i) + delete[] m_LabData[i]; + delete[] m_LabData; + m_LabData = NULL; + } + if (m_nlcm != NULL) + { + delete[] m_nlcm; + m_nlcm = NULL; + } + if (m_targetVal != NULL) + { + delete[] m_targetVal; + m_targetVal = NULL; + } } size_t Tango::ColorLib::ColorCalibrator::GetLiquidFactor(uint8_t * input_buffer, size_t input_buffer_size, uint8_t *& output_buffer) @@ -21,27 +52,29 @@ size_t Tango::ColorLib::ColorCalibrator::GetLiquidFactor(uint8_t * input_buffer, calibrationInput = calibration_input__unpack(NULL, input_buffer_size, input_buffer); //Call Mirta with input - + InitData((const CalibrationMeasurement**)calibrationInput->measurements, calibrationInput->n_measurements, calibrationInput->liquidtype, calibrationInput->targetl, calibrationInput->targeta, calibrationInput->targetb); + //Init Output CalibrationOutput *calibrationOutput = (CalibrationOutput*)malloc(sizeof(CalibrationOutput)); calibration_output__init(calibrationOutput); //Investigate Input - double targetL = calibrationInput->has_targetl; //Get Target LAB... + //double targetL = calibrationInput->has_targetl; //Get Target LAB... - for (size_t i = 0; i < calibrationInput->n_measurements; i++) //Iterate measurements... - { - CalibrationMeasurement* m = calibrationInput->measurements[i]; + //for (size_t i = 0; i < calibrationInput->n_measurements; i++) //Iterate measurements... + //{ + // CalibrationMeasurement* m = calibrationInput->measurements[i]; - m->nanoliterpercentimeter; //NL PER CM.. - m->l; //L - m->a; //A - m->b; //B - } + // m->nanoliterpercentimeter; //NL PER CM.. + // m->l; //L + // m->a; //A + // m->b; //B + //} //Set Output calibrationOutput->has_liquidfactor = true; - calibrationOutput->liquidfactor = 5.0; + MaximalDispensingRate(calibrationOutput->liquidfactor); + //calibrationOutput->liquidfactor = 5.0; //Pack Output output_buffer = (uint8_t*)malloc(calibration_output__get_packed_size(calibrationOutput)); @@ -53,3 +86,203 @@ size_t Tango::ColorLib::ColorCalibrator::GetLiquidFactor(uint8_t * input_buffer, return size; } + +void Tango::ColorLib::ColorCalibrator::InitData(const CalibrationMeasurement** m, const size_t &nsize, const int &inkChannel, const double& targetl, const double& targeta, const double& targetb) +{ + ClearData(); + + m_nsize = nsize; + m_LabData = new double*[m_nsize]; + m_nlcm = new double[m_nsize]; + + for (int i = 0; i < m_nsize; ++i) + { + m_LabData[i] = new double[3]; + m_LabData[i][0] = m[i]->l; + m_LabData[i][1] = m[i]->a; + m_LabData[i][2] = m[i]->b; + m_nlcm[i] = m[i]->nanoliterpercentimeter; + + } + m_inkChannel = inkChannel; + m_targetVal = new double[3]; + m_targetVal[0] = targetl; + m_targetVal[1] = targeta; + m_targetVal[2] = targetb; + + int m_CalIndex = 0; + if (m_inkChannel == 2) + m_CalIndex = 2; +} +void Tango::ColorLib::ColorCalibrator::MaximalDispensingRate(double &MDispRate) +{ + MDispRate = -1000; + double *yVal = new double[m_nsize]; + + double maxVal = -1000; + double minVal = 1000; + int maxInd = -1; + int minInd = -1; + for (int i = 0; i < m_nsize; ++i) + { + yVal[i] = m_LabData[i][m_CalIndex]; + if (maxVal < yVal[i]) + { + maxVal = yVal[i]; + maxInd = i; + } + if (minVal > yVal[i]) + { + minVal = yVal[i]; + minInd = i; + } + } + if (maxVal - minVal < MDRThr) + { + delete[] yVal; + yVal = NULL; + throw std::exception("Maximal and Minimal Values are too close"); + } + + //increasing or decreasing + int KC = 0; + if (yVal[0] < yVal[m_nsize - 1]) + KC = 1; // increasing + else if (yVal[0] > yVal[m_nsize - 1]) + KC = -1; // decreasing + int indLab = 0; + if (m_inkChannel == 2) //Yellow + indLab = 2; + double dE = 0.0; + if ((m_targetVal[indLab] < minVal) | (m_targetVal[indLab] > maxVal)) + { + //Advice + if (KC == 1) + { + if (m_targetVal[indLab] < minVal) + { + //Increasing Function, dispensing rate above values + dEcmc(m_targetVal, m_LabData[minInd], dE); + if (dE < dEThr) + MDispRate = m_nlcm[minInd]; + else + { + if (yVal != NULL) + { + delete[] yVal; + yVal = NULL; + } + throw std::exception("Target Value does not fall in data interval,decrease dispensing rate"); + } + } + else if (m_targetVal[indLab] > maxVal) + { + //Increasing Function, dispensing rate below values + dEcmc(m_targetVal, m_LabData[maxInd], dE); + if (dE < dEThr) + MDispRate = m_nlcm[maxInd]; + else + { + if (yVal != NULL) + { + delete[] yVal; + yVal = NULL; + } + throw std::exception("Target Value does not fall in data interval,increase dispensing rate"); + } + } + } + else //decreasing //KC = -1 + { + if (m_targetVal[indLab] < minVal) + { + //decreasing Function, dispensing rate above values + dEcmc(m_targetVal, m_LabData[minInd], dE); + if (dE < dEThr) + MDispRate = m_nlcm[minInd]; + else + { + if (yVal != NULL) + { + delete[] yVal; + yVal = NULL; + } + throw std::exception("Target Value does not fall in data interval,increase dispensing rate"); + } + } + else if (m_targetVal[indLab] > maxVal) + { + //Increasing Function, dispensing rate below values + dEcmc(m_targetVal, m_LabData[maxInd], dE); + if (dE < dEThr) + MDispRate = m_nlcm[maxInd]; + else + throw std::exception("Target Value does not fall in data interval,decrease dispensing rate"); + } + } + } + else + { + for (int i = 0; i < m_nsize; ++i) + yVal[i] *= KC; + double targetVal = KC * m_targetVal[indLab]; + for (int i = 0; i < m_nsize - 1; ++i) + { + if ((yVal[i] <= targetVal) & (targetVal < yVal[i + 1])) + MDispRate = ((targetVal - yVal[i])*m_nlcm[i + 1] + (yVal[i + 1] - targetVal)*m_nlcm[i]) / (yVal[i + 1] - yVal[i]); + } + } + +} +void Tango::ColorLib::ColorCalibrator::dEcmc(double *refX, double *samX, double &dECMC) +{ + //dEcmc(refX, samX) calculates color difference CMC(2:1) in + //CIE - L*a*b* colorspace between references(refX) and + // samples(samX) + +// reference parameter calculations + // hue calculation + double h1 = atan2(refX[2], refX[1])*(180 / PI); + if (h1 < 0) + h1 = h1 + 360; + double h2 = atan2(samX[2], samX[1])*(180 / PI); + if (h2 < 0) + h2 = h2 + 360; + double refX_H = h1; + //chroma calculation + double refX_C = sqrt(refX[1] * refX[1] + refX[2] * refX[2]); + //reference SL parameter + double refX_SL; + if (refX[1] <= 16) + refX_SL = 0.511; + else + refX_SL = refX[1] * 0.040975 / (1 + 0.01765*refX[1]); + //reference SC parameter + double refX_SC = (0.638 + 0.0638*refX_C / (1 + 0.0131*refX_C)); + //reference CQ parameter + double refX_CQ = pow(refX_C, 4); + //reference F parameter + double refX_F = sqrt(refX_CQ / (refX_CQ + 1900)); + // reference T parameter + double refX_T = 0; + if ((refX_H > 164) & (refX_H < 345)) + refX_T = 0.56 + abs(0.2*cos(PI*(refX_H + 168) / 180)); + else if ((refX_H >= 345) | (refX_H <= 164)) + refX_T = 0.36 + abs(0.4*cos(PI*(refX_H + 35) / 180)); + // reference SH parameter + double refX_SH = refX_SC * (refX_T*refX_F + 1 - refX_F); + + //sample parameter calculations + //hue calculation + double samX_H = h2; + //chroma calculation + double samX_C = sqrt(samX[1] * samX[1] + samX[2] * samX[2]); + + double dL = refX[0] - samX[0]; + double dC = samX_C - refX_C; + double da = refX[1] - samX[1]; + double db = refX[2] - samX[2]; + double dH = sqrt(max(da*da + db * db - dC * dC, 0.0)); + + dECMC = sqrt(pow(dL / (2 * refX_SL), 2) + pow(dC / refX_SC, 2) + pow(dH / refX_SH, 2)); +} diff --git a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.h b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.h index 652be03f1..711462e2a 100644 --- a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.h +++ b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/ColorCalibrator.h @@ -1,5 +1,8 @@ #include #include +#include "CalibrationInput.pb-c.h" +#include "CalibrationOutput.pb-c.h" +#include "CalibrationMeasurement.pb-c.h" #pragma once @@ -13,6 +16,21 @@ namespace Tango ColorCalibrator(); ~ColorCalibrator(); size_t Tango::ColorLib::ColorCalibrator::GetLiquidFactor(uint8_t * input_buffer, size_t input_buffer_size, uint8_t *& output_buffer); + protected: + void InitData(const CalibrationMeasurement** m, const size_t &nsize, const int &inkChannel, const double& targetl, const double& targeta, const double& targetb); + void MaximalDispensingRate(double &MDispRate); + void dEcmc(double *refX, double *samX, double &dECMC); + + private: + void ClearData(); + + private: + int m_nsize; + double *m_nlcm; + double **m_LabData; + int m_inkChannel; + double *m_targetVal; + int m_CalIndex; }; } } diff --git a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.c b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.c index ab79bc168..dca0c112b 100644 --- a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.c +++ b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.c @@ -52,7 +52,7 @@ void calibration_output__free_unpacked assert(message->base.descriptor == &calibration_output__descriptor); protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator); } -static const ProtobufCFieldDescriptor calibration_output__field_descriptors[1] = +static const ProtobufCFieldDescriptor calibration_output__field_descriptors[3] = { { "LiquidFactor", @@ -66,14 +66,41 @@ static const ProtobufCFieldDescriptor calibration_output__field_descriptors[1] = 0, /* flags */ 0,NULL,NULL /* reserved1,reserved2, etc */ }, + { + "HasError", + 20, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_BOOL, + offsetof(CalibrationOutput, has_haserror), + offsetof(CalibrationOutput, haserror), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, + { + "ErrorMessage", + 21, + PROTOBUF_C_LABEL_OPTIONAL, + PROTOBUF_C_TYPE_STRING, + 0, /* quantifier_offset */ + offsetof(CalibrationOutput, errormessage), + NULL, + NULL, + 0, /* flags */ + 0,NULL,NULL /* reserved1,reserved2, etc */ + }, }; static const unsigned calibration_output__field_indices_by_name[] = { + 2, /* field[2] = ErrorMessage */ + 1, /* field[1] = HasError */ 0, /* field[0] = LiquidFactor */ }; -static const ProtobufCIntRange calibration_output__number_ranges[1 + 1] = +static const ProtobufCIntRange calibration_output__number_ranges[2 + 1] = { { 1, 0 }, - { 0, 1 } + { 20, 1 }, + { 0, 3 } }; const ProtobufCMessageDescriptor calibration_output__descriptor = { @@ -83,10 +110,10 @@ const ProtobufCMessageDescriptor calibration_output__descriptor = "CalibrationOutput", "", sizeof(CalibrationOutput), - 1, + 3, calibration_output__field_descriptors, calibration_output__field_indices_by_name, - 1, calibration_output__number_ranges, + 2, calibration_output__number_ranges, (ProtobufCMessageInit) calibration_output__init, NULL,NULL,NULL /* reserved[123] */ }; diff --git a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.h b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.h index 160f894db..c60f4f3c8 100644 --- a/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.h +++ b/Software/Visual_Studio/ColorLib/Tango.ColorLib_v4/PMR/ColorLab/CalibrationOutput.pb-c.h @@ -28,10 +28,13 @@ struct _CalibrationOutput ProtobufCMessage base; protobuf_c_boolean has_liquidfactor; double liquidfactor; + protobuf_c_boolean has_haserror; + protobuf_c_boolean haserror; + char *errormessage; }; #define CALIBRATION_OUTPUT__INIT \ { PROTOBUF_C_MESSAGE_INIT (&calibration_output__descriptor) \ - , 0, 0 } + , 0, 0, 0, 0, NULL } /* CalibrationOutput methods */ diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Models/CalibrationMeasurementModel.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Models/CalibrationMeasurementModel.cs index 74c6bba3e..87eba8ba2 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Models/CalibrationMeasurementModel.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Models/CalibrationMeasurementModel.cs @@ -42,33 +42,33 @@ namespace Tango.MachineStudio.RML.Models public class CalibrationMeasurementModel : ExtendedObject { #region properties - private int _l; + private double _l; - public int L + public double L { get { return _l; } set { _l = value; RaisePropertyChangedAuto(); } } - private int _a; + private double _a; - public int A + public double A { get { return _a; } set { _a = value; RaisePropertyChangedAuto(); } } - private int _b; + private double _b; - public int B + public double B { get { return _b; } set { _b = value; RaisePropertyChangedAuto(); } } - private int _ink; + private double _ink; - public int Ink + public double Ink { get { return _ink; } set { _ink = value; RaisePropertyChangedAuto(); } @@ -77,10 +77,7 @@ namespace Tango.MachineStudio.RML.Models public CalibrationMeasurementModel() { - _ink = 0; - L = 0; - A = 0; - B = 0; + } } } 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 index 24867122e..dd0c66c58 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/ViewModels/ColorCalibrationViewVM.cs @@ -14,12 +14,16 @@ 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; @@ -138,8 +142,9 @@ namespace Tango.MachineStudio.RML.ViewModels #endregion - public ColorCalibrationViewVM() + public ColorCalibrationViewVM(INotificationProvider notification) { + _notification = notification; Measurements = new ObservableCollection() { new CalibrationMeasurementModel(), @@ -165,7 +170,8 @@ namespace Tango.MachineStudio.RML.ViewModels 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)) { @@ -183,6 +189,8 @@ namespace Tango.MachineStudio.RML.ViewModels return 0.0; } + #region CreateGraph + private async void CreateGraph(object obj) { if (_liquidType == null) @@ -213,6 +221,93 @@ namespace Tango.MachineStudio.RML.ViewModels 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 4939cbb48..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 @@ -380,7 +380,7 @@ namespace Tango.MachineStudio.RML.ViewModels LiquidTypesRmls = LiquidTypesRmls, }; - ColorCalibrationVM = new ColorCalibrationViewVM() + ColorCalibrationVM = new ColorCalibrationViewVM(_notification) { RML = ActiveRML, LiquidTypes = LiquidTypesRmls.Where(x => x.LiquidType.HasPigment).ToList().Select(y => y.LiquidType).ToList(), diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Views/ColorCalibrationView.xaml b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Views/ColorCalibrationView.xaml index 815e7a5f9..02cef3c0d 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Views/ColorCalibrationView.xaml +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Views/ColorCalibrationView.xaml @@ -106,7 +106,7 @@ - + -- cgit v1.3.1