-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working on track statistics, work in progress
- Loading branch information
Showing
11 changed files
with
328 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using PiLot.Model.Nav; | ||
|
||
namespace PiLot.API.Helpers { | ||
|
||
/// <summary> | ||
/// This class helps analyzing a track, especially finding the fastest periods. | ||
/// </summary> | ||
public class TrackAnalyzer { | ||
|
||
private Track track; | ||
private List<EnhancedTrackPoint> trackPoints; | ||
|
||
public TrackAnalyzer(Track pTrack) { | ||
this.track = pTrack; | ||
this.PreprocessTrack(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns the fastest segment either for a minimal distance or a minimal duration. | ||
/// The resulting segment can be slightly longer than required, because of course | ||
/// the TrackPoints can have any distance between them. If the track is shorter than | ||
/// the required distance or time, the result is null. | ||
/// </summary> | ||
/// <param name="pType">The type of track segment to find</param> | ||
/// <returns>The fastest segment for the type, or null</returns> | ||
public TrackSegment GetFastestTrackSegment(TrackSegmentType pType) { | ||
if (pType.IsDistance) { | ||
return this.GetFastestSegmentByDistance(pType); | ||
} else { | ||
return this.GetFastestSegmentByDuration(pType); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This gets the fastest segment having a certain duration. E.g. the fastest hour. | ||
/// </summary> | ||
/// <param name="pType">The type defining the minimal duration</param> | ||
/// <returns>The fastest segment or null</returns> | ||
private TrackSegment GetFastestSegmentByDuration(TrackSegmentType pType) { | ||
TrackSegment result = null; | ||
Int32 startIndex = 0; | ||
Int32 endIndex = 1; | ||
Double duration; // seconds | ||
Double distance; // meters | ||
Double speed; // m/s | ||
Double topSpeed = 0; // m/s | ||
while (endIndex < this.trackPoints.Count) { | ||
duration = (this.trackPoints[endIndex].UTC - this.trackPoints[startIndex].UTC) / 1000; | ||
if (duration < pType.Duration) { | ||
endIndex++; | ||
} else { | ||
distance = this.trackPoints[endIndex].TotalDistance - this.trackPoints[startIndex].TotalDistance; | ||
speed = distance / duration; | ||
if (speed > topSpeed) { | ||
result = result ?? new TrackSegment(this.track, pType.ID); | ||
result.StartUTC = this.trackPoints[startIndex].UTC; | ||
result.StartBoatTime = this.trackPoints[startIndex].BoatTime ?? this.trackPoints[startIndex].UTC; | ||
result.EndUTC = this.trackPoints[endIndex].UTC; | ||
result.EndBoatTime = this.trackPoints[endIndex].BoatTime ?? this.trackPoints[endIndex].UTC; | ||
result.Distance = distance; | ||
topSpeed = speed; | ||
} | ||
startIndex++; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// This gets the fastest segment having a certain distance. E.g. the fastest mile. | ||
/// </summary> | ||
/// <param name="pType">The type defining the minimal distance</param> | ||
/// <returns>The fastest segment or null</returns> | ||
private TrackSegment GetFastestSegmentByDistance(TrackSegmentType pType) { | ||
TrackSegment result = null; | ||
Int32 startIndex = 0; | ||
Int32 endIndex = 1; | ||
Double distance; // meters | ||
Double speed; // m/s | ||
Double topSpeed = 0; // m/s | ||
while (endIndex < this.trackPoints.Count) { | ||
distance = this.trackPoints[endIndex].TotalDistance - this.trackPoints[startIndex].TotalDistance; | ||
if (distance < pType.Distance) { | ||
endIndex++; | ||
} else { | ||
speed = distance / ((this.trackPoints[endIndex].UTC - this.trackPoints[startIndex].UTC) / 1000); | ||
if(speed > topSpeed) { | ||
result = result ?? new TrackSegment(this.track, pType.ID); | ||
result.StartUTC = this.trackPoints[startIndex].UTC; | ||
result.StartBoatTime = this.trackPoints[startIndex].BoatTime ?? this.trackPoints[startIndex].UTC; | ||
result.EndUTC = this.trackPoints[endIndex].UTC; | ||
result.EndBoatTime = this.trackPoints[endIndex].BoatTime ?? this.trackPoints[endIndex].UTC; | ||
result.Distance = distance; | ||
topSpeed = speed; | ||
} | ||
startIndex++; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a list of enhanced track points, which not only contain the | ||
/// positions and timestamps, but also the total distance from the first | ||
/// position. This makes calculating the total distance between any two | ||
/// track points much faster. | ||
/// </summary> | ||
private void PreprocessTrack() { | ||
this.trackPoints = new List<EnhancedTrackPoint>(); | ||
if(track.TrackPoints.Count > 1) { | ||
EnhancedTrackPoint currentTrackPoint = null, previousTrackPoint = null; | ||
foreach (TrackPoint aTrackPoint in this.track.TrackPoints) { | ||
if (currentTrackPoint != null) { | ||
previousTrackPoint = currentTrackPoint; | ||
} | ||
currentTrackPoint = new EnhancedTrackPoint(aTrackPoint); | ||
if (previousTrackPoint != null) { | ||
currentTrackPoint.TotalDistance = previousTrackPoint.TotalDistance + currentTrackPoint.DistanceTo(previousTrackPoint); | ||
} | ||
this.trackPoints.Add(currentTrackPoint); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
|
||
namespace PiLot.Model.Nav { | ||
|
||
/// <summary> | ||
/// This is a special type of TrackPoint, which additionally has information about | ||
/// the total distance, which helps for track analysis. | ||
/// </summary> | ||
public class EnhancedTrackPoint: TrackPoint { | ||
|
||
public EnhancedTrackPoint(TrackPoint pTrackPoint) : base(pTrackPoint.Latitude, pTrackPoint.Longitude) { | ||
this.UTC = pTrackPoint.UTC; | ||
this.BoatTime = pTrackPoint.BoatTime; | ||
this.Altitude = pTrackPoint.Altitude; | ||
this.LatitudeError = pTrackPoint.LatitudeError; | ||
this.LongitudeError = pTrackPoint.LongitudeError; | ||
this.AltitudeError = pTrackPoint.AltitudeError; | ||
} | ||
|
||
public Double TotalDistance { get; set; } = 0; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.