< Summary - ReFlex - Library

Information
Class: ReFlex.Core.Calibration.Components.Calibrator
Assembly: ReFlex.Core.Calibration
File(s): D:\a\reflex\reflex\library\src\Core\Calibration\Components\Calibrator.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 196
Coverable lines: 196
Total lines: 326
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 62
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_CalibrationMatrix()0%620%
get_CalibrationValues()100%210%
get_CalibrationStage()100%210%
get_Width()100%210%
set_Width(...)0%620%
get_Height()100%210%
set_Height(...)0%620%
get_StartX()100%210%
set_StartX(...)0%620%
get_StartY()100%210%
set_StartY(...)0%620%
.ctor()100%210%
SetFrameSize(...)100%210%
UpdateSource()0%2040%
UpdateTargetValue(...)0%156120%
AddTargetValue(...)0%7280%
ComputeTransformation()0%620%
Reset()0%620%
Load(...)100%210%
Load(...)100%210%
SetCalibrationValues(...)0%7280%
Save(...)100%210%
GenerateMappingMatrix()0%7280%
ValidateCalibrationMatrix()0%7280%

File(s)

D:\a\reflex\reflex\library\src\Core\Calibration\Components\Calibrator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.IO;
 4using System.Xml.Serialization;
 5using MathNet.Numerics.LinearAlgebra;
 6
 7namespace ReFlex.Core.Calibration.Components
 8{
 9    public class Calibrator
 10    {
 11        public const string DefaultCalibrationFile = "DefaultCalibrationFile.xml";
 12
 13        #region Fields
 14
 15        private int _width;
 16        private int _height;
 17        private int _startX;
 18        private int _startY;
 19
 020        private readonly float[,] _unitMat4X4 = {
 021            { 1.0f, 0.0f, 0.0f, 0.0f },
 022            { 0.0f, 1.0f, 0.0f, 0.0f },
 023            { 0.0f, 0.0f, 1.0f, 0.0f },
 024            { 0.0f, 0.0f, 0.0f, 1.0f }
 025        };
 26
 27        private Matrix<float> _calibrationMatrix;
 28        private readonly Matrix<float> _unitMatrix;
 29
 30        #endregion
 31
 32        #region Properties
 33
 034        public Matrix<float> CalibrationMatrix => _calibrationMatrix ?? _unitMatrix;
 35
 036        public Util.Calibration CalibrationValues { get; private set; } = new Util.Calibration
 037        {
 038            SourceValues = new List<int[]>(),
 039            TargetValues = new List<int[]>(),
 040            UpperThreshold = 1010,
 041            LowerThreshold = 960,
 042            LastUpdated = new List<DateTime>()
 043        };
 44
 045        public int CalibrationStage { get; private set; }
 46
 47        public int Width
 48        {
 049            get => _width;
 50            private set
 051            {
 052                if (_width == value)
 053                    return;
 054                _width = value;
 055                UpdateSource();
 056            }
 57        }
 58
 59        public int Height
 60        {
 061            get => _height;
 62            private set
 063            {
 064                if (_height == value)
 065                    return;
 066                _height = value;
 067                UpdateSource();
 068            }
 69        }
 70
 71        public int StartX
 72        {
 073            get => _startX;
 074            private set {
 075                if (_startX == value)
 076                    return;
 077                _startX = value;
 078                UpdateSource();
 079            }
 80        }
 81
 82        public int StartY
 83        {
 084            get => _startY;
 85            private set
 086            {
 087                if (_startY == value)
 088                    return;
 089                _startY = value;
 090                UpdateSource();
 091            }
 92        }
 93
 94        #endregion
 95
 96        #region Events
 97
 98        public EventHandler<Matrix<float>> CalibrationUpdated;
 99        public EventHandler<Matrix<float>> CalibrationFinished;
 100        public EventHandler<Matrix<float>> CalibrationLoaded;
 101
 102
 103        #endregion
 104
 105        #region Constructor
 106
 0107        public Calibrator() : this(500, 500, 10, 100)
 0108        {
 109
 0110        }
 111
 0112        public Calibrator(int width, int height, int startX, int startY)
 0113        {
 0114            _width = width;
 0115            _height = height;
 0116            _startX = startX;
 0117            _startY = startY;
 118
 0119            _unitMatrix = Matrix<float>.Build.DenseOfArray(_unitMat4X4);
 120
 0121            UpdateSource();
 0122        }
 123
 124        #endregion
 125
 126        #region Public Methods
 127
 128        public void SetFrameSize(int width, int height, int top, int left)
 0129        {
 0130            _startX = left;
 0131            _startY = top;
 0132            _width = width;
 0133            _height = height;
 134
 0135            UpdateSource();
 0136        }
 137
 138        public void UpdateSource()
 0139        {
 0140            int[] init1 = { Width / 8 * 5 + StartX, Height / 3 + StartY, -1 };
 0141            int[] init2 = { Width / 8 * 5 + StartX, Height / 3 * 2 + StartY, -1 };
 0142            int[] init3 = { Width / 16 * 5 + StartX, Height / 3 * 2 + StartY, -1 };
 143
 0144            CalibrationValues.SourceValues.Clear();
 145
 0146            CalibrationValues.SourceValues.Add(init1);
 0147            CalibrationValues.SourceValues.Add(init2);
 0148            CalibrationValues.SourceValues.Add(init3);
 149
 0150            if (CalibrationValues.TargetValues.Count == 0)
 0151            {
 0152                CalibrationValues.TargetValues.Add(init1);
 0153                CalibrationValues.TargetValues.Add(init2);
 0154                CalibrationValues.TargetValues.Add(init3);
 155
 0156                CalibrationValues.LastUpdated.Add(DateTime.Now);
 0157                CalibrationValues.LastUpdated.Add(DateTime.Now);
 0158                CalibrationValues.LastUpdated.Add(DateTime.Now);
 0159            }
 160
 0161            CalibrationUpdated?.Invoke(this, CalibrationMatrix);
 0162        }
 163
 164        public void UpdateTargetValue(int idx, int x, int y, int id)
 0165        {
 0166            if (CalibrationValues.TargetValues.Count <= idx)
 0167                return;
 168
 0169            CalibrationValues.TargetValues[idx] = new[] { x, y, id };
 170
 0171            if (CalibrationValues.LastUpdated.Count > idx)
 0172            {
 0173                var lastUpdate = CalibrationValues.LastUpdated[idx];
 174
 0175                if (DateTime.Now - lastUpdate < TimeSpan.FromMilliseconds(500))
 0176                    return;
 0177            }
 178
 0179            if (CalibrationValues.TargetValues.Count == 3)
 0180            {
 0181                _calibrationMatrix = GenerateMappingMatrix();
 0182                if (CalibrationValues.LastUpdated.Count > idx)
 0183                    CalibrationValues.LastUpdated[idx] = DateTime.Now;
 0184                Save();
 0185                CalibrationFinished?.Invoke(this, CalibrationMatrix);
 0186            }
 0187        }
 188
 189        public void AddTargetValue(int x, int y, int id)
 0190        {
 0191            if (CalibrationStage >= 0 && CalibrationStage < 3)
 0192            {
 0193                CalibrationValues.TargetValues.Add(new[] { x, y, id });
 0194                CalibrationValues.LastUpdated.Add(DateTime.Now);
 0195                CalibrationStage++;
 0196                CalibrationUpdated?.Invoke(this, CalibrationMatrix);
 0197            }
 198
 0199            if (CalibrationStage != 3)
 0200                return;
 201
 0202            ComputeTransformation();
 0203            CalibrationStage++;
 0204            Save();
 0205        }
 206
 207        public void ComputeTransformation()
 0208        {
 0209            _calibrationMatrix = GenerateMappingMatrix();
 210
 0211            ValidateCalibrationMatrix();
 212
 0213            CalibrationFinished?.Invoke(this, CalibrationMatrix);
 0214        }
 215
 216        public void Reset()
 0217        {
 0218            _calibrationMatrix = null;
 0219            CalibrationStage = 0;
 0220            CalibrationValues.TargetValues.Clear();
 0221            CalibrationValues.LastUpdated.Clear();
 0222            CalibrationUpdated?.Invoke(this, CalibrationMatrix);
 0223        }
 224
 225        [Obsolete]
 226        public void Load(string xmlSerializedText)
 0227        {
 0228            var ser = new XmlSerializer(typeof(Util.Calibration));
 0229            using (var text = new StringReader(xmlSerializedText))
 0230            {
 0231               var result = (Util.Calibration)ser.Deserialize(text);
 0232               SetCalibrationValues(result);
 0233            }
 0234        }
 235
 236        public void Load(Util.Calibration calibration)
 0237        {
 0238            SetCalibrationValues(calibration);
 0239        }
 240
 241        private void SetCalibrationValues(Util.Calibration calibration)
 0242        {
 243            try
 0244            {
 245
 0246                CalibrationValues = new Util.Calibration
 0247                {
 0248                    LastUpdated = calibration.LastUpdated ?? new List<DateTime>(),
 0249                    UpperThreshold = calibration.UpperThreshold,
 0250                    LowerThreshold = calibration.LowerThreshold,
 0251                    SourceValues = CalibrationValues.SourceValues ?? calibration.SourceValues,
 0252                    TargetValues = calibration.TargetValues ?? new List<int[]>()
 0253                };
 254
 0255                _calibrationMatrix = GenerateMappingMatrix();
 0256                CalibrationLoaded?.Invoke(this, CalibrationMatrix);
 257
 0258            }
 0259            catch (Exception exc)
 0260            {
 0261                Console.Write(exc.Message);
 0262            }
 0263        }
 264
 265        [Obsolete]
 266        public string Save(string fileName = DefaultCalibrationFile)
 0267        {
 0268            var ser = new XmlSerializer(typeof(Util.Calibration));
 0269            using (var text = new StringWriter())
 0270            {
 271                try
 0272                {
 0273                    ser.Serialize(text, CalibrationValues);
 0274                }
 0275                catch (Exception exc)
 0276                {
 0277                    Console.Write(exc.Message);
 0278                }
 279
 0280                return text.ToString();
 281            }
 0282        }
 283
 284        #endregion
 285
 286        #region Auxiliary Methods
 287
 288        private Matrix<float> GenerateMappingMatrix()
 0289        {
 0290            if (CalibrationValues.SourceValues == null
 0291                || CalibrationValues.SourceValues.Count != 3
 0292                || CalibrationValues.TargetValues == null
 0293                || CalibrationValues.TargetValues.Count != 3)
 0294                return _unitMatrix;
 295
 0296            float[,] oPoints = {
 0297                { CalibrationValues.SourceValues[0][0], CalibrationValues.SourceValues[1][0], CalibrationValues.SourceVa
 0298                { CalibrationValues.SourceValues[0][1], CalibrationValues.SourceValues[1][1], CalibrationValues.SourceVa
 0299                { 1.0f, 1.0f, 1.0f, 0.0f },
 0300                { 0.0f, 0.0f, 0.0f, 1.0f }
 0301            };
 302
 0303            float[,] nPoints = {
 0304                { CalibrationValues.TargetValues[0][0], CalibrationValues.TargetValues[1][0], CalibrationValues.TargetVa
 0305                { CalibrationValues.TargetValues[0][1], CalibrationValues.TargetValues[1][1], CalibrationValues.TargetVa
 0306                { 1.0f, 1.0f, 1.0f, 0.0f },
 0307                { 0.0f, 0.0f, 0.0f, 1.0f }
 0308            };
 309
 0310            var oPointsMat = Matrix<float>.Build.DenseOfArray(oPoints);
 0311            var nPointsMat = Matrix<float>.Build.DenseOfArray(nPoints);
 312
 0313            return nPointsMat.Multiply(oPointsMat.Inverse()).Inverse();
 0314        }
 315
 316        private void ValidateCalibrationMatrix()
 0317        {
 0318            var isValid = _calibrationMatrix.ForAll(val => !float.IsInfinity(val) && !float.IsNaN(val) && val >= float.M
 0319            if (!isValid)
 0320                _calibrationMatrix = _unitMatrix;
 0321        }
 322
 323        #endregion
 324    }
 325
 326}