private bool CheckPair(TickDiffPair pair)
            {
                // Checks whether candidate tick diff pair should be accepted.
                // Not allowing pairs that are "too parallell"

                TickDiff     p0          = pair.TickDiff1;
                TickDiff     p1          = pair.TickDiff2;
                const double thetaThresh = 5;
                const double lambda      = 0.000001;

                // Using  dot product and a threshold on the angle theta
                // abs = ||a|| * ||b||
                // dot = a0*b0 + a1*b1
                //theta = arccos(dot/abs)
                double abs = Math.Sqrt(Math.Pow(p0.mLatDiff, 2) + Math.Pow(p0.mLongDiff, 2)) * Math.Sqrt(Math.Pow(p1.mLatDiff, 2) + Math.Pow(p1.mLongDiff, 2));
                double dot = p0.mLatDiff * p1.mLatDiff + p0.mLongDiff * p1.mLongDiff;

                List <double> diffs = new List <double>()
                {
                    p0.mLatDiff,
                    p1.mLatDiff,
                    p0.mLongDiff,
                    p1.mLongDiff
                };

                if (!diffs.TrueForAll(diff => Math.Abs(diff) > lambda))
                {
                    return(false);
                }

                double theta    = Math.Acos(dot / abs);
                double thetaDeg = toDegrees(theta);

                return(thetaDeg > thetaThresh && thetaDeg < 180 - thetaThresh);
            }
            private List <TickDiffPair> FindCandidates(TrainingSession ts, int n, int ticksDiff = 1)
            {
                // Finds tickDiff pairs suitable for calibration

                int    ticks = ts.mTicks;
                Random rand  = new Random();
                List <TickDiffPair> tickDiffPairs = new List <TickDiffPair> {
                };
                HashSet <int> usedPoints          = new HashSet <int> {
                };

                while (tickDiffPairs.Count < n)
                {
                    TickDiff[] candidatePair = new TickDiff[2];
                    int[]      tickNumbers   = new int[2];
                    for (int k = 0; k < 2; k++)
                    {
                        bool foundCandidate = false;

                        while (!foundCandidate)
                        {
                            // Comparing the tick with the next tick
                            tickNumbers[k] = rand.Next(0, ticks - (ticksDiff + 1));
                            bool isUsed     = usedPoints.Contains(tickNumbers[k]);
                            bool altSuccess = Math.Abs(ts.mAltVector[tickNumbers[k]] - ts.mAltVector[tickNumbers[k] + ticksDiff]) > AltThresh;
                            if (!isUsed || altSuccess)
                            {
                                TickDiff candidate;
                                try
                                {
                                    candidate = new TickDiff(ts, tickNumbers[k], ticksDiff);
                                }
                                catch (NullReferenceException e)
                                {
                                    continue;
                                }
                                catch (NoDistanceDiffException e)
                                {
                                    continue;
                                }
                                candidatePair[k] = candidate;
                                foundCandidate   = true;
                            }
                        }
                    }
                    TickDiffPair tickDiffPair = new TickDiffPair(candidatePair[0], candidatePair[1]);
                    if (CheckPair(tickDiffPair))
                    {
                        tickDiffPairs.Add(tickDiffPair);
                        // Adding the first tick in each tickDiff used tickDiffs
                        usedPoints.Add(tickNumbers[0]);
                        usedPoints.Add(tickNumbers[1]);
                    }
                }
                return(tickDiffPairs);
            }
 public TickDiffPair(TickDiff td1, TickDiff td2)
 {
     TickDiff1 = td1;
     TickDiff2 = td2;
 }