public GPSGrid(GPSGrid refGrid, IGPSRoute route, double BWidthFactor, double DistFactor, bool isDist) { m_Distance = DistFactor * Settings.Radius; m_Grid = new Dictionary<int, IDictionary<int, IList<int>>>(); m_Route = route; //Just copy the reference if (refGrid == null) { //Set grid size from aprox distance for reference //See Trails plugin, TrailsGPSLocation.getGPSBounds() m_latWidth = BWidthFactor * Settings.Radius / 110574 * 1.005F; m_lngWidth = BWidthFactor * Settings.Radius / 111132 / Math.Cos(m_Route[0].Value.LongitudeDegrees * Math.PI / 180); } else { m_latWidth = refGrid.m_latWidth; m_lngWidth = refGrid.m_lngWidth; } if (isDist) { m_Dist = m_Route.GetDistanceMetersTrack(); } else { m_Dist = null; } for (int i = 0; i < m_Route.Count; i++ ) { addGrid(i); } }
public GPSGrid(GPSGrid refGrid, IGPSRoute route) : this(refGrid, route, 1, 1, false) { }
public GPSGrid(GPSGrid refGrid, IActivity activity, double BWidthFactor, bool isDist) : this(refGrid, activity.GPSRoute, BWidthFactor, BWidthFactor, isDist) { }
public GPSGrid(GPSGrid refGrid, IActivity activity) : this(refGrid, activity, 1, false) { }
private static IList<IActivity> findSimilarRoutes(IGPSRoute refRoute, string refId, IList<IActivity> activities, bool activityCompare, System.Windows.Forms.ProgressBar progressBar) { if (progressBar == null) { progressBar = new System.Windows.Forms.ProgressBar(); } IList<IActivity> result = new List<IActivity>(); if (refRoute == null || refRoute.Count == 0) return result; if (activities == null) { activities = getBaseActivities(); } progressBar.Value = 0; progressBar.Minimum = 0; progressBar.Maximum = activities.Count; GPSGrid refGrid = new GPSGrid(null, refRoute); IDictionary<string, int> beginningPoints = new Dictionary<string, int>(); IDictionary<string, int> endPoints = new Dictionary<string, int>(); setBeginningAndEndPoints(refId, refRoute, activityCompare, beginningPoints, endPoints); if (!beginningPoints.ContainsKey(refId) || beginningPoints[refId] <= -1 || endPoints[refId] <= -1) { //The settings does not include any points return result; } foreach (IActivity otherActivity in activities) { if (otherActivity.GPSRoute != null && otherActivity.GPSRoute.Count > 0 && //Simple prune, eliminating routes not possibly common otherActivity.GPSRoute[0].Value.DistanceMetersToPoint(refRoute[0].Value) < otherActivity.GPSRoute.TotalDistanceMeters + refRoute.TotalDistanceMeters) { setBeginningAndEndPoints(otherActivity, activityCompare, beginningPoints, endPoints); //int noOfPoints = otherActivity.GPSRoute.Count; if (beginningPoints[otherActivity.ReferenceId] > -1 && endPoints[otherActivity.ReferenceId] > -1) { int pointsOutside = 0; bool inBand = true; int direction = 0; int prevMatch = -1; //IGPSBounds otherBounds = GPSBounds.FromGPSRoute(otherActivity.GPSRoute); GPSGrid otherGrid = new GPSGrid(refGrid, otherActivity); //Check if the reference route fits in the other activity for (int i = beginningPoints[refId]; i <= endPoints[refId]; i++) { IGPSPoint point = refRoute[i].Value; int closests = otherGrid.getClosePoint(point); if (closests < 0) { pointsOutside++; if (pointsOutside / ((double)refRoute.Count) > Settings.ErrorMargin) { inBand = false; break; } } else if (Settings.HasDirection) { int currMatch = closests;// grid.getCloseSinglePoint(point); if (currMatch > prevMatch) { direction++; } else if (currMatch < prevMatch) { direction--; } prevMatch = currMatch; ////Find direction. Works well when the routes are not overlapping and have similar length ////Use all matching points to get majority decision ////All matching points from grid.getAllClose would be better but is significantly slower //bool inOtherLowerHalf = i < otherActivity.GPSRoute.Count / 2; //bool inLowerHalf = closests < refRoute.Count / 2; //if ((inLowerHalf && inOtherLowerHalf) || // (!inLowerHalf && !inOtherLowerHalf)) // direction++; //else // direction--; } } //Check if the other activity matches the reference route //Only used when comparing activities if (inBand && activityCompare) { pointsOutside = 0; for (int i = beginningPoints[otherActivity.ReferenceId]; i <= endPoints[otherActivity.ReferenceId]; i++) { IGPSPoint point = otherActivity.GPSRoute[i].Value; int closests = refGrid.getClosePoint(point); if (closests < 0) { pointsOutside++; if (pointsOutside / ((double)otherActivity.GPSRoute.Count) > Settings.ErrorMargin) { inBand = false; break; } } } } if (inBand && direction >= 0) { result.Add(otherActivity); } } } progressBar.Increment(1); } return result; }
public static IDictionary<IActivity, IList<PointInfo[]>> findSimilarPoints(IGPSRoute refRoute, IActivityLaps refLaps, IList<IActivity> activities, System.Windows.Forms.ProgressBar progressBar) { GPSGrid refGrid = new GPSGrid(null, refRoute, 1F, 0.5F, true); IDictionary<IActivity, IList<PointInfo[]>> result = new Dictionary<IActivity, IList<PointInfo[]>>(); double cumulativeAverageDist = 0; int noCumAv = 0; if (activities != null) { if (progressBar != null) { progressBar.Value = 0; progressBar.Minimum = 0; progressBar.Maximum = activities.Count; } foreach (IActivity otherActivity in activities) { const int ExtraGridIndex = 2; const float MaxDistDiffFactor = 0.1F; double minDistStretch = Settings.Radius * 2; result.Add(otherActivity, new List<PointInfo[]>()); IDistanceDataTrack distTrack = otherActivity.GPSRoute.GetDistanceMetersTrack(); int lastMatch = -1; //index of previous match int startMatch = -1; IndexDiffDist lastMatchRef = null; IndexDiffDist startMatchRef = null; IndexDiffDist lastIndex = null; for (int i = 0; i < otherActivity.GPSRoute.Count; i++) { IList<IndexDiffDist> currIndex = new List<IndexDiffDist>(); IndexDiffDist closeIndex = null; //Closest to current point IndexDiffDist nextIndex = null; //The best match in this stretch bool isEnd = true; //This is the end of a stretch, unless a matching point is found currIndex = refGrid.getAllCloseStretch(otherActivity.GPSRoute[i].Value); if (currIndex.Count > 0) { int prio = int.MaxValue; foreach (IndexDiffDist IndDist in currIndex) { if (IndDist.Index > -1) { //Get the closest good enough point - used at start and could restart current stretch if (closeIndex == null || //Close match in distance (Math.Abs(IndDist.Dist - distTrack[i].Value) < 3 * Settings.Radius) || //Close to other matches (Math.Abs(IndDist.Dist - distTrack[i].Value - cumulativeAverageDist) < Math.Abs(closeIndex.Dist - distTrack[i].Value - cumulativeAverageDist))) { closeIndex = IndDist; } //next in relation to previous match if (lastMatch > -1 && lastIndex != null) { //Only check forward, i.e follow the same direction //Use a close enough stretch //This is the iffiest part of the algorithm matching stretches... if ((IndDist.low >= lastIndex.low && IndDist.low <= lastIndex.high || IndDist.Index >= lastIndex.low && IndDist.Index <= lastIndex.high) && IndDist.Index >= lastIndex.Index) { //The grid overlaps the old grid //The matching index may be lower then this is a restart (this or prev match should then be dropped) if ((prio > 0 && (nextIndex == null || IndDist.Diff < nextIndex.Diff) && IndDist.Index >= lastIndex.Index)) { nextIndex = IndDist; prio = 0; } else if (prio >= 0 && (IndDist.Index > lastIndex.Index && (nextIndex == null || nextIndex.Index <= lastIndex.Index))) { nextIndex = IndDist; prio = 0; } else if (IndDist.Index == lastIndex.Index && (nextIndex == null || nextIndex.Index < lastIndex.Index)) { nextIndex = IndDist; prio = 0; } else if (prio > 10) { nextIndex = IndDist; prio = 10; } else if (nextIndex == null) { nextIndex = IndDist; prio = 10; } //else - not better } else if (( IndDist.low >= lastIndex.low && IndDist.low <= lastIndex.high + ExtraGridIndex || IndDist.Index >= lastIndex.low && IndDist.Index <= lastIndex.high + ExtraGridIndex)) { //Grids are not overlapping, but adjacent points match forward if (prio > 20 && IndDist.Index > lastIndex.Index) { nextIndex = IndDist; prio = 20; } else if (prio >= 20) { nextIndex = IndDist; prio = 20; } } else if (( Math.Abs(IndDist.Dist / lastIndex.Dist - 1) < MaxDistDiffFactor) && (prio > 20)) { nextIndex = IndDist; prio = 30; } } } } //Update if close enough if (closeIndex != null) { //TODO: This algorithm needs to prio correct direction better //nextInd is best match for the stretch, but closeIndex could be better if (nextIndex == null || (!nextIndex.Equals(closeIndex)) && ( //back match - prefer closer lastIndex.Index > nextIndex.Index || prio > 10 && (minDistStretch < distTrack[i].Value - distTrack[startMatch].Value || (Math.Abs(nextIndex.Dist - distTrack[i].Value) > Settings.Radius) || (Math.Abs(nextIndex.Dist - distTrack[i].Value - cumulativeAverageDist) > Math.Abs(closeIndex.Dist - distTrack[i].Value - cumulativeAverageDist))))) { //switch to closeIndex and ignore nextIndex nextIndex = closeIndex; //start of new stretch - use best match to reference activity startMatch = i; lastIndex = closeIndex; startMatchRef = closeIndex; } else { //The stretch continues isEnd = false; cumulativeAverageDist += (lastIndex.Diff - distTrack[i].Value - cumulativeAverageDist) / ++noCumAv; } } } if (isEnd && lastMatch >= 0) { // end match PointInfo[] s = new PointInfo[2]; s[0] = new PointInfo(-i - 1); s[1] = new PointInfo(-lastMatchRef.Index - 1); result[otherActivity].Add(s); } if (nextIndex != null) { lastIndex = nextIndex; lastMatchRef = lastIndex; lastMatch = i; PointInfo[] s = new PointInfo[2]; s[0] = new PointInfo(i, distTrack[i].Value, otherActivity.GPSRoute[i].ElapsedSeconds, getRestLap(i, otherActivity), startMatch + " " + lastMatch + " " + startMatchRef + " " + lastMatchRef); s[1] = new PointInfo(lastIndex.Index, lastIndex.Dist, refRoute[lastIndex.Index].ElapsedSeconds, getRestLap(lastIndex.Index, refRoute, refLaps)); result[otherActivity].Add(s); } else { lastMatch = -1; } } //add end marker if needed if (lastMatch > -1) { PointInfo[] s = new PointInfo[2]; s[0] = new PointInfo(-otherActivity.GPSRoute.Count); s[1] = new PointInfo(-1); result[otherActivity].Add(s); } if (progressBar != null) { progressBar.Increment(1); } } } return result; }