Commit 80707c09 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

fixed several bug and some improvements in right-panels

parent 5f82c9e1
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -127,7 +127,6 @@ namespace INAF.Apps.Uwp.SLabDataManager
                new ServiceCollection()
                /* singletons */
                .AddSingleton<ChartAnnotationsHelper>()
                .AddSingleton<ContinuumMissingPointsHelper>()
                .AddSingleton<ContinuumSpectraContainer>()
                .AddSingleton<FitMethodsHelper>()
                .AddSingleton<Logger>(logger)
+12 −0
Original line number Diff line number Diff line
@@ -247,6 +247,18 @@ namespace INAF.Apps.Uwp.SLabDataManager.Extensions
            }
        }

        public static PointModel ToPointModel(this Tuple<object, object> tuple)
        {
            try
            {
                return new PointModel((double)tuple.Item1, (double)tuple.Item2);
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static XmlDocument ToXml(this SpectrumModel spectrum)
        {
            XmlDocument dom = new XmlDocument();
+95 −48
Original line number Diff line number Diff line
@@ -42,8 +42,24 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
            this.logger = logger;
        }

        public void addPointForContinuum(PointModel point,
                                         RadCartesianChart chart,
                                         DataTemplate tappedPointTemplate)
        {
            try
            {
                var points = chart.Annotations.OfType<CartesianCustomAnnotation>();
                if (!points.Any(x => (double)x.HorizontalValue == point.X && (double)x.VerticalValue == point.Y))
                    chart.Annotations.Add(getCustomAnnotation(point.X, point.Y, tappedPointTemplate));
            }
            catch (Exception ex)
            {
                logger.Write<ChartAnnotationsHelper>($"{nameof(addPointForContinuum)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
            }
        }

        public async Task addOrRemoveTappedPointForContinuumAsync(RadCartesianChart chart,
                                                                  Tuple<object, object> point,
                                                                  PointModel point,
                                                                  DataTemplate tappedPointTemplate)
        {
            try
@@ -52,8 +68,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart

                /*  find closest points on spectrum to mouse position */
                (PointModel lowBoundary, PointModel highBoundary) boundaries = getSegmentBoundaries(spectraContainer.getAlignedSpectrum(),
                                                                                                    (double)point.Item1,
                                                                                                    (double)point.Item2);
                                                                                                    point.X);

                var lowerBoundaryPoint = boundaries.lowBoundary;
                var higherBoundaryPoint = boundaries.highBoundary;
@@ -63,8 +78,8 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart

                /* if a point in selected position already exists, then remove it... */
                var existantItem = chart.Annotations.FirstOrDefault(x => x is CartesianCustomAnnotation &&
                                                                    (Convert.ToDouble((x as CartesianCustomAnnotation).HorizontalValue) >= ((double)point.Item1 - 12d)) &&
                                                                    (Convert.ToDouble((x as CartesianCustomAnnotation).HorizontalValue) <= ((double)point.Item1 + 12d)));
                                                                    (Convert.ToDouble((x as CartesianCustomAnnotation).HorizontalValue) >= (point.X - 12d)) &&
                                                                    (Convert.ToDouble((x as CartesianCustomAnnotation).HorizontalValue) <= (point.X + 12d)));
                if (existantItem != null)
                {
                    /* if already existant, then remove point from chart */
@@ -75,26 +90,27 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
                else
                {
                    /* calculate y on spectrum */
                    double yOnSpectrum = (double)point.Item2;
                    double yOnSpectrum = (double)point.Y;
                    if (lowerBoundaryPoint.X != higherBoundaryPoint.X)
                    {
                        var line = new StraightLineModel(lowerBoundaryPoint, higherBoundaryPoint);
                        yOnSpectrum = line.calculateY((double)point.Item1);
                        yOnSpectrum = line.calculateY(point.X);
                    }
                    else
                        yOnSpectrum = lowerBoundaryPoint.Y;

                    /* create new point to be used for new segment(s) */
                    var pointModel = new PointModel((double)point.Item1, yOnSpectrum);

                    /* if new point belongs to an existant segment, then it is removed and 2 new segments are created */
                    if (segmentsFitViewModel.isPointContainedInExistingSegment(pointModel))
                        segmentsFitViewModel.removeOldSegmentAndAddNewOnes(pointModel);
                    /* add the new point on chart */
                    PointModel pointModel = addPoint(point,
                                                     yOnSpectrum,
                                                     chart,
                                                     tappedPointTemplate,
                                                     segmentsFitViewModel);
                    System.Diagnostics.Debug.WriteLine($"new point: {point.X},{yOnSpectrum}");

                    /* ...otherwise add a new point */
                    chart.Annotations.Add(getCustomAnnotation((double)point.Item1, yOnSpectrum, tappedPointTemplate));
                    /* save point */
                    segmentsFitViewModel.addPoint(pointModel);
                }

                //chart.InvalidateUI();

                /* if only a point is added, then avoid trying create segments */
                var pointsNum = chart.Annotations.Count(x => x is CartesianCustomAnnotation);
                if (pointsNum > 1)
@@ -104,7 +120,6 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart

                    /* update UI */
                    SeriesHelper.UpdateUI(chart);
                    //chart.InvalidateUI();
                }
            }
            catch (Exception ex)
@@ -114,11 +129,12 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
        }

        private async Task addNewSegmentsAsync(RadCartesianChart chart)
        {
            try
            {
                /* recover points on chart and order them for creating segments */
                var orderedPoints = chart.Annotations
                                        .Where(x => x is CartesianCustomAnnotation)
                                        .Cast<CartesianCustomAnnotation>()
                                            .OfType<CartesianCustomAnnotation>()
                                            .OrderBy(x => x.HorizontalValue);
                //foreach (var point in orderedPoints)
                //    System.Diagnostics.Debug.WriteLine($"orderedPoint - x,y: {point.HorizontalValue},{point.VerticalValue}");
@@ -143,11 +159,35 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
                    int newSegmentsNum = newSegments.Count();
                    for (int i = 0; i < newSegmentsNum; i++)
                    {
                    //System.Diagnostics.Debug.WriteLine($"fit segment {newSegments.ElementAt(i).P1.X}-{newSegments.ElementAt(i).P2.X}");
                        System.Diagnostics.Debug.WriteLine($"fit segment - i: {i}, (x1,x2): {newSegments.ElementAt(i).P1.X}-{newSegments.ElementAt(i).P2.X}");
                        await viewModel.fitSelectedSegmentAsync(newSegments.ElementAt(i));
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Write<ChartAnnotationsHelper>($"{nameof(addNewSegmentsAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
            }
        }

        private PointModel addPoint(PointModel point,
                                    double yOnSpectrum,
                                    RadCartesianChart chart,
                                    DataTemplate tappedPointTemplate,
                                    SegmentsFitViewModel segmentsFitViewModel)
        {
            /* create new point to be used for new segment(s) */
            var pointModel = new PointModel(point.X, yOnSpectrum);

            /* if new point belongs to an existant segment, then it is removed and 2 new segments are created */
            if (segmentsFitViewModel.isPointContainedInExistingSegment(pointModel))
                segmentsFitViewModel.removeOldSegmentAndAddNewOnes(pointModel);

            /* ...otherwise add a new point */
            chart.Annotations.Add(getCustomAnnotation(point.X, yOnSpectrum, tappedPointTemplate));

            return pointModel;
        }

        public void removePointsForContinuum(RadCartesianChart chart)
        {
@@ -267,11 +307,18 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
        }

        private (PointModel lowBoundary, PointModel highBoundary) getSegmentBoundaries(SpectrumModel spectrum,
                                                                                       double x1,
                                                                                       double x2)
                                                                                       double xVal)
        {
            /* if tapped point belongs to spectrum, then return tapped point... */
            if (spectrum.Elements.Any(x => x.X == xVal))
            {
            var lowerBoundaryPoint = spectrum.Elements.Where(x => x.X <= x1).LastOrDefault().ToPointModel();
            var higherBoundaryPoint = spectrum.Elements.Where(x => x.X >= x2).FirstOrDefault().ToPointModel();
                var point = spectrum.Elements.First(x => x.X == xVal).ToPointModel();
                return (point, point);
            }

            /* ...otherwise, select the closest points to the tapped one and return them */
            var lowerBoundaryPoint = spectrum.Elements.Where(x => x.X < xVal).LastOrDefault().ToPointModel();
            var higherBoundaryPoint = spectrum.Elements.Where(x => x.X > xVal).FirstOrDefault().ToPointModel();
            return (lowerBoundaryPoint, higherBoundaryPoint);
        }
        #endregion
+0 −133
Original line number Diff line number Diff line
using CommunityToolkit.Mvvm.ComponentModel;
using INAF.Libraries.NetStandard.Math.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
{
    public sealed class ContinuumMissingPointsHelper : ObservableObject
    {
        public ContinuumMissingPointsHelper()
        {
            SystemDefinedMissingPoints = new List<PointModel>();
            SystemAddedMissingPoints = new List<PointModel>();

            clear();
        }

        #region properties
        private bool isCompleted;
        public bool IsCompleted
        {
            get { return isCompleted; }
            set { SetProperty(ref isCompleted, value); }
        }

        private int index { get; set; }

        public bool IsChecking { get; private set; }

        private bool isMissingPointAddingAvailable;
        public bool IsMissingPointAddingAvailable
        {
            get { return isMissingPointAddingAvailable; }
            set { SetProperty(ref isMissingPointAddingAvailable, value); }
        }

        private List<PointModel> SystemDefinedMissingPoints { get; set; }

        private List<PointModel> SystemAddedMissingPoints { get; set; }
        #endregion

        #region methods
        public void addMissingPoint(PointModel missingPoint)
        {
            if (!isDefinedMissingPoint(missingPoint))
                SystemDefinedMissingPoints.Add(missingPoint);
        }

        public void clear(bool clearLists = false)
        {
            if (clearLists)
            {
                SystemDefinedMissingPoints.Clear();
                SystemAddedMissingPoints.Clear();
            }

            index = 0;
            IsMissingPointAddingAvailable = false;
        }

        private bool isAlreadyAddedMissingPoint(PointModel candidateMissingPoint)
        {
            return SystemAddedMissingPoints.Any(x => x.X == candidateMissingPoint.X);
        }

        private bool isDefinedMissingPoint(PointModel candidateMissingPoint)
        {
            return SystemDefinedMissingPoints.Any(x => x.X == candidateMissingPoint.X);
        }

        public PointModel getMissingPoint()
        {
            PointModel missingPoint = null;

            if (SystemDefinedMissingPoints.ElementAtOrDefault(index) != null)
            {
                missingPoint = SystemDefinedMissingPoints[index];
                index++;
            }

            return missingPoint;
        }

        private void raiseIsMissingPointAddingAvailable()
        {
            IsMissingPointAddingAvailable = true;
            isMissingPointAddingAvailable = false;
        }

        public void tryIncreaseAddedMissingPointsNum(PointModel candidateMissingPoint)
        {
            if (IsChecking && isDefinedMissingPoint(candidateMissingPoint) && !isAlreadyAddedMissingPoint(candidateMissingPoint))
            {
                System.Diagnostics.Debug.WriteLine($"tryIncreaseAddedMissingPointsNum point: {candidateMissingPoint.X},{candidateMissingPoint.Y}");
                SystemAddedMissingPoints.Add(candidateMissingPoint);
                raiseIsMissingPointAddingAvailable();
            }
        }

        public void raiseIsCompleted()
        {
            IsCompleted = true;
            isCompleted = false;
        }

        public async void startCheckingAreAllMissingPointsAdded()
        {
            bool continueLoop = true;

            clear();
            IsChecking = true;

            await Task.Run(async () =>
            {
                while (continueLoop)
                {
                    System.Diagnostics.Debug.WriteLine($"startCheckingAreAllMissingPointsAdded SystemDefinedMissingPoints.Count: {SystemDefinedMissingPoints.Count}, SystemAddedMissingPoints.Count:{SystemAddedMissingPoints.Count}");
                    if (SystemDefinedMissingPoints.Count > 0 && SystemAddedMissingPoints.Count == SystemDefinedMissingPoints.Count)
                    {
                        IsChecking = false;
                        continueLoop = false;
                    }
                    else
                        await Task.Delay(200);
                }
            });

            raiseIsCompleted();
        }
        #endregion
    }
}
+9 −46
Original line number Diff line number Diff line
@@ -6,6 +6,8 @@ using INAF.Apps.Uwp.SLabDataManager.Models.Spectrum;
using INAF.Apps.Uwp.SLabDataManager.ViewModels.UserControlViewModels;
using INAF.Libraries.NetStandard.Math.Models;
using INAF.Libraries.Uwp.Settings;
using Microsoft.Toolkit.Uwp.UI.Controls.TextToolbarSymbols;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static INAF.Apps.Uwp.SLabDataManager.Constants.Enums;
@@ -18,24 +20,22 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
        private readonly SpectraContainer spectraContainer;
        private readonly SettingsHelper settingsHelper;
        private readonly SpectrumProcessingHelper spectrumProcessingHelper;
        private readonly ContinuumMissingPointsHelper continuumMissingPointsHelper;
        private readonly SegmentsFitViewModel segmentsFitViewModel;

        public ContinuumRemovalHelper(SpectraContainer spectraContainer,
                                      SettingsHelper settingsHelper,
                                      SpectrumProcessingHelper spectrumProcessingHelper,
                                      ContinuumMissingPointsHelper continuumMissingPointsHelper,
                                      SegmentsFitViewModel segmentsFitViewModel)
        {
            this.spectraContainer = spectraContainer;
            this.settingsHelper = settingsHelper;
            this.spectrumProcessingHelper = spectrumProcessingHelper;
            this.continuumMissingPointsHelper = continuumMissingPointsHelper;
            this.segmentsFitViewModel = segmentsFitViewModel;

            tryReadIsAddingPointsForContinuumRemovalAllowed();

            IsContinuumRemovalEnabled = false;
            IsAddingPointsForContinuumRemovalAllowed = false;
        }

        #region properties
@@ -68,12 +68,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
            set { SetProperty(ref isContinuumRemovalEnabled, value); }
        }

        private PointModel missingPoint;
        public PointModel MissingPoint
        {
            get { return missingPoint; }
            private set { SetProperty(ref missingPoint, value); }
        }
        public List<PointModel> MissingPoints { get; private set; }

        public ProcessingResult ProcessingResult { get; private set; }
        #endregion
@@ -81,18 +76,17 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
        #region methods
        private void addMissingSegments()
        {
            MissingPoints = new List<PointModel>();

            if (Continuum.Elements.FirstOrDefault().X > CloneForContinuumRemoval.Elements.FirstOrDefault().X)
            {
                continuumMissingPointsHelper.addMissingPoint(calculateMissingPoint(MissingPointPosition.Beginning));
                MissingPoints.Add(calculateMissingPoint(MissingPointPosition.Beginning));
            }
            //await Task.Delay(1000);
            if (Continuum.Elements.FirstOrDefault().X < CloneForContinuumRemoval.Elements.LastOrDefault().X)
            {
                // add missing segment at the end
                continuumMissingPointsHelper.addMissingPoint(calculateMissingPoint(MissingPointPosition.End));
                MissingPoints.Add(calculateMissingPoint(MissingPointPosition.End));
            }

            setMissingPoint();
        }

        private PointModel calculateMissingPoint(MissingPointPosition missingPointPosition)
@@ -117,7 +111,7 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
                    missingPoint = new PointModel(xValue, yValue);
                    break;
            }

            System.Diagnostics.Debug.WriteLine($"calculateMissingPoint: {missingPoint.X},{missingPoint.Y}");
            return missingPoint;
        }

@@ -138,40 +132,9 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.UI.Chart
                                                                          SpectrumType.ContinuumRemoved);
            Continuum = spectraContainer.tryGetSpectrumOfType(SpectrumType.Continuum);

            /* start check for detectingwhen all missing points have been succesfully added */
            continuumMissingPointsHelper.PropertyChanged += ContinuumMissingPointsHelper_PropertyChanged;


            continuumMissingPointsHelper.startCheckingAreAllMissingPointsAdded();

            /* start procedure for adding missing points */
            addMissingSegments();
        }

        private async void ContinuumMissingPointsHelper_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case nameof(ContinuumMissingPointsHelper.IsCompleted):
                    if (continuumMissingPointsHelper.IsCompleted)
                    {
                        continuumMissingPointsHelper.PropertyChanged -= ContinuumMissingPointsHelper_PropertyChanged;
                        await executeContinuumRemovalAsync();
                    }
                    break;
                case nameof(ContinuumMissingPointsHelper.IsMissingPointAddingAvailable):
                    if (continuumMissingPointsHelper.IsMissingPointAddingAvailable)
                        setMissingPoint();
                    break;
            }
        }

        private void setMissingPoint()
        {
            var missingPoint = continuumMissingPointsHelper.getMissingPoint();
            if (missingPoint != null)
                MissingPoint = missingPoint;
        }
        #endregion

        #region settings
Loading