summaryrefslogtreecommitdiff
path: root/grapher/Models/Calculations/AccelChartData.cs
blob: 71ee927d72dd62d3a905fad77c9753f0d7ade7f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;

namespace grapher.Models.Calculations
{
    public class AccelChartData
    {
        #region Constructors

        public AccelChartData()
        {
            AccelPoints = new SortedDictionary<double, double>();
            VelocityPoints = new SortedDictionary<double, double>();
            GainPoints = new SortedDictionary<double, double>();
            OutVelocityToPoints = new Dictionary<double, (double, double, double)>();
            LogToIndex = new int[701];
        }

        #endregion Constructors

        #region Properties

        public SortedDictionary<double, double> AccelPoints { get; }

        public double MaxAccel { get; set; }

        public double MinAccel { get; set; }

        public double MaxGain { get; set; }

        public double MinGain { get; set; }

        public SortedDictionary<double, double> VelocityPoints { get; }

        public SortedDictionary<double, double> GainPoints { get; }

        public int[] LogToIndex { get; }

        public Dictionary<double, (double, double, double)> OutVelocityToPoints { get; }

        #endregion Properties

        #region Methods

        public void Clear()
        {
            AccelPoints.Clear();
            VelocityPoints.Clear();
            GainPoints.Clear();
            OutVelocityToPoints.Clear();
            Array.Clear(LogToIndex, 0, LogToIndex.Length);
        }

        public (double, double, double) FindPointValuesFromOut(double outVelocityValue)
        {
            if (OutVelocityToPoints.TryGetValue(outVelocityValue, out var values))
            {
                return values;
            }
            else
            {
                var velIdx = GetVelocityIndex(outVelocityValue);

                values = (VelocityPoints.ElementAt(velIdx).Key, AccelPoints.ElementAt(velIdx).Value, GainPoints.ElementAt(velIdx).Value);
                OutVelocityToPoints.Add(outVelocityValue, values);
                return values;
            }
        }

        public (double, double, double) ValuesAtIndex(int index)
        {
            return (AccelPoints.ElementAt(index).Value, VelocityPoints.ElementAt(index).Value, GainPoints.ElementAt(index).Value);
        }

        public (double, double, double) ValuesAtInVelocity(double inVelocity)
        {
            return (AccelPoints[inVelocity], VelocityPoints[inVelocity], GainPoints[inVelocity]);
        }

        public int GetVelocityIndex(double outVelocityValue)
        {
            if (outVelocityValue < 0)
            {
                throw new ArgumentException($"invalid velocity: {outVelocityValue}");
            }

            var log = Math.Log10(outVelocityValue);
            if (log < -2)
            {
                log = -2;
            }
            else if (log > 5)
            {
                log = 5;
            }

            log = log * 100 + 200;

            var velIdx = LogToIndex[(int)log];

            return velIdx;
        }

        #endregion Methods
    }
}