/// <summary> /// Create and return a new SAMPLE based on the index in the mTempList /// </summary> /// <param name="index"></param> /// <returns></returns> private SAMPLE BuildNewSample(int index) { SAMPLE newSample = new SAMPLE(); // Build new sample GeoLoc point1 = new GeoLoc(mTempList[index - 1].LAT, mTempList[index - 1].LON); GeoLoc point2 = new GeoLoc(mTempList[index].LAT, mTempList[index].LON); double newPointSlope = (mTempList[index - 1].SLOPE + mTempList[index].SLOPE) / 2; double newPointSpeed = (mTempList[index - 1].KPH + mTempList[index].KPH) / 2; GeoLocMath geoLocM1 = new GeoLocMath(point1, point2); GeoLoc newPoint = geoLocM1.CalcMidPoint(); geoLocM1.point1 = newPoint; geoLocM1.point2 = point1; double newdistanceTravelled = geoLocM1.CalcDistanceBetweenGeoLocations() + mTempList[index - 1].KM; // Create new sample and add data from above newSample = new SAMPLE() { SECS = 999999, //insertPosition, KM = newdistanceTravelled, KPH = newPointSpeed, ALT = (mTempList[index].ALT + mTempList[index - 1].ALT) / 2, LAT = newPoint.Latitude, LON = newPoint.Longitude, SLOPE = newPointSlope }; return(newSample); }
/// <summary> /// Add a new point between 2 adjacent ones /// </summary> private void AddNewPoint( ref int thisOldRideCnt, ref int thisNewRideCnt, ref int pointsAdded, ref GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide, ref string fullPath) { // Add new sample GeoLoc point1 = new GeoLoc(thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].LAT, thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].LON); GeoLoc point2 = new GeoLoc(thisOldRide.RIDE.SAMPLES[thisOldRideCnt].LAT, thisOldRide.RIDE.SAMPLES[thisOldRideCnt].LON); double newPointSlope = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].SLOPE + thisOldRide.RIDE.SAMPLES[thisOldRideCnt].SLOPE) / 2; double newPointSpeed = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KPH + thisOldRide.RIDE.SAMPLES[thisOldRideCnt].KPH) / 2; GeoLocMath geoLocM1 = new GeoLocMath(point1, point2); GeoLoc newPoint = geoLocM1.CalcMidPoint(); geoLocM1.point1 = newPoint; geoLocM1.point2 = point1; double newdistanceTravelled = geoLocM1.CalcDistanceBetweenGeoLocations() + thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM; // Create new sample and add data from above SAMPLE newSample = new SAMPLE() { SECS = 999, //insertPosition, KM = newdistanceTravelled, KPH = newPointSpeed, ALT = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt].ALT + thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].ALT) / 2, LAT = newPoint.Latitude, LON = newPoint.Longitude, SLOPE = newPointSlope }; // Insert new point in ride thisNewRide.RIDE.SAMPLES.Add(newSample); testSegment.Add(newSample); SaveLineToTxt(newSample, fullPath, -1, thisNewRideCnt); thisNewRideCnt += 1; pointsAdded++; }
} // ***** addPointsToNewRide ENDS /// <summary> /// Process to generate more points that currently available in the segment. /// </summary> /// <param name="startCnt"></param> /// <param name="endCnt"></param> /// <param name="pointsToAdd"></param> /// <param name="pointsAdded"></param> /// <param name="thisOldRideCnt"></param> /// <param name="thisNewRideCnt"></param> /// <param name="pointsMoved"></param> /// <param name="segmentTime"></param> /// <param name="thisOldRide"></param> /// <param name="thisNewRide"></param> /// <param name="fullPath"></param> private void MorePointsThanWeHaveAddProcess(ref int startCnt, ref int endCnt, //ref int pointsInterval, //ref int lastSample, ref int pointsToAdd, ref int pointsAdded, ref int thisOldRideCnt, ref int thisNewRideCnt, //ref int pointsMoved, ref int segmentTime, ref GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide, ref string fullPath) { //Save state int savedStartCnt = startCnt; int savedthisOldRideCnt = thisOldRideCnt; bool finishedMovingPoints = false; // Always start with the second point int startingPoint = 2; while (startCnt <= endCnt) { if (pointsAdded < pointsToAdd && startCnt >= startingPoint) { // Add new sample GeoLoc point1 = new GeoLoc(thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].LAT, thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].LON); GeoLoc point2 = new GeoLoc(thisOldRide.RIDE.SAMPLES[thisOldRideCnt].LAT, thisOldRide.RIDE.SAMPLES[thisOldRideCnt].LON); double newPointSlope = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].SLOPE + thisOldRide.RIDE.SAMPLES[thisOldRideCnt].SLOPE) / 2; double newPointSpeed = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KPH + thisOldRide.RIDE.SAMPLES[thisOldRideCnt].KPH) / 2; GeoLocMath geoLocM1 = new GeoLocMath(point1, point2); GeoLoc newPoint = geoLocM1.CalcMidPoint(); geoLocM1.point1 = newPoint; geoLocM1.point2 = point1; double newdistanceTravelled = geoLocM1.CalcDistanceBetweenGeoLocations() + thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM; // Create new sample and add data from above SAMPLE newSample = new SAMPLE() { SECS = 999, //insertPosition, KM = newdistanceTravelled, KPH = newPointSpeed, ALT = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt].ALT + thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].ALT) / 2, LAT = newPoint.Latitude, LON = newPoint.Longitude, SLOPE = newPointSlope }; // Insert new point in ride thisNewRide.RIDE.SAMPLES.Add(newSample); testSegment.Add(newSample); SaveLineToTxt(newSample, fullPath, -1, thisNewRideCnt); thisNewRideCnt += 1; //cnt = 1; pointsAdded++; } if (!finishedMovingPoints) { // Move sample to new ride CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide); CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment); SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt); thisNewRideCnt += 1; thisOldRideCnt++; //cnt++; } startCnt++; if (pointsAdded < pointsToAdd && startCnt >= startingPoint && startCnt > endCnt) { startCnt = savedStartCnt; thisOldRideCnt = savedthisOldRideCnt; finishedMovingPoints = true; } } // END of adding for loop // Need to check if all points needed were added. // If not, loop and add them //while (pointsAdded < pointsToAdd) //{ // AddNewPoint( // ref thisOldRideCnt, // ref thisNewRideCnt, // ref pointsAdded, // ref thisOldRide, // ref thisNewRide, // ref fullPath); //} //if (pointsAdded < pointsToAdd) //{ // throw new Exception(System.Reflection.MethodBase.GetCurrentMethod().ToString() + // $" -- Only added {pointsAdded} out of {pointsToAdd} "); //} }
/// <summary> /// Add points to the ride for video sync. /// videoTime = The number of seconds the video needs to travel the segment /// rideTime = The # of SECS at the end of the segment /// startTime = The # of SECS at the start of the segment /// endTime = the # of SECS at the end of the segment /// </summary> /// <returns></returns> public List <SAMPLE> AddPointsToNewRide(int videoTime, int startTime, int endTime, ref int thisOldRideCnt, ref int thisNewRideCnt, GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide) { testSegment.Clear(); #if (DEBUG) TextConnector tc = new TextConnector(); string fullPath = tc.FullFilePath("debug" + startTime.ToString() + "-" + endTime.ToString() + ".csv", "debug"); if (File.Exists(fullPath)) { File.Delete(fullPath); } #endif int pointsAdded = 0; int pointsMoved = 0; // Init vars int pointsToAdd = -1; int startCnt = 0; int endCnt = 0; int segmentTime = 0; // If this is not the first record we need to add a second to the video time videoTime++; segmentTime = endTime - startTime + 1; endCnt = segmentTime - 1; // Last point is added manually pointsToAdd = videoTime - segmentTime; //Calculate distance and avg speed for the full segment GeoLoc startpoint = new GeoLoc(thisOldRide.RIDE.SAMPLES[startTime].LAT, thisOldRide.RIDE.SAMPLES[startTime].LON); GeoLoc endpoint = new GeoLoc(thisOldRide.RIDE.SAMPLES[endTime].LAT, thisOldRide.RIDE.SAMPLES[endTime].LON); GeoLocMath geoLocMath = new GeoLocMath(startpoint, endpoint); double distanceTravelled = geoLocMath.CalcDistanceBetweenGeoLocations(); if (thisNewRide.RIDE.SAMPLES.Count() == 0) { // This is the first record. // Move initial point from old route to new route CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide); CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment); #if (DEBUG) SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt); #endif thisNewRideCnt += 1; thisOldRideCnt++; startCnt++; pointsMoved++; endCnt--; // THIS } else { //Check if point exists in new route before adding it if (Math.Abs(thisNewRide.RIDE.SAMPLES[thisNewRideCnt - 1].KM - thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM) > 0.01) { // Move initial point from old route to new route CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide); CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment); #if (DEBUG) SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt); #endif thisNewRideCnt += 1; thisOldRideCnt++; pointsMoved++; } else { // First point is there already, move to the next one and decriment the number of points to move startCnt++; endCnt--; } } // If the last sample to add equals the segment length, add it right before the end //int lastSample = -1; //if ((pointsInterval * pointsToAdd) == endCnt) lastSample = endCnt - 1; if (pointsToAdd > (segmentTime / 2) && pointsToAdd < segmentTime) { //ADD every other point starting with (_segmentTime - _pointsToAdd) / 2 MoreThanHalfPointsAddProcess(ref startCnt, ref endCnt, //ref pointsInterval, //ref lastSample, ref pointsToAdd, ref pointsAdded, ref thisOldRideCnt, ref thisNewRideCnt, ref pointsMoved, ref segmentTime, ref thisOldRide, ref thisNewRide, ref fullPath); } else if (pointsToAdd > (segmentTime / 2) && pointsToAdd > segmentTime) { Debug.WriteLine(" New Method here "); MorePointsThanWeHaveAddProcess(ref startCnt, ref endCnt, //ref pointsInterval, //ref lastSample, ref pointsToAdd, ref pointsAdded, ref thisOldRideCnt, ref thisNewRideCnt, ref segmentTime, ref thisOldRide, ref thisNewRide, ref fullPath); } else { //Calculate the number of points to add and the interval for adding them int pointsInterval = CalculateAddPointsInterval(pointsToAdd, segmentTime, endCnt, videoTime); LessThanHalfPointsAddProcess(ref startCnt, ref endCnt, ref pointsInterval, //ref lastSample, ref pointsToAdd, ref pointsAdded, ref thisOldRideCnt, ref thisNewRideCnt, ref pointsMoved, ref thisOldRide, ref thisNewRide, ref fullPath); } // Move last point from old route to new route CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide); CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment); SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt); thisNewRideCnt += 1; thisOldRideCnt++; return(testSegment); } // ***** addPointsToNewRide ENDS
// This method will add points to a route segment if // the # of points to be added < half of the size of the segment private void LessThanHalfPointsAddProcess(ref int startCnt, ref int endCnt, ref int pointsInterval, //ref int lastSample, ref int pointsToAdd, ref int pointsAdded, ref int thisOldRideCnt, ref int thisNewRideCnt, ref int pointsMoved, //ref double videoSpeed, ref GoldenCheetahRide thisOldRide, ref GoldenCheetahRide thisNewRide, ref string fullPath) { //Loop and add points int cnt = 1; int loopCnt = 0; while (startCnt <= endCnt) // THIS { if (cnt == pointsInterval) // || startCnt == lastSample) // We only add points at pointsInterval { if (pointsAdded < pointsToAdd) // Stop adding points because pointsInterval is converted to integer and not accurate { // Add new sample GeoLoc point1 = new GeoLoc(thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].LAT, thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].LON); GeoLoc point2 = new GeoLoc(thisOldRide.RIDE.SAMPLES[thisOldRideCnt].LAT, thisOldRide.RIDE.SAMPLES[thisOldRideCnt].LON); double newPointSlope = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].SLOPE + thisOldRide.RIDE.SAMPLES[thisOldRideCnt].SLOPE) / 2; double newPointSpeed = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KPH + thisOldRide.RIDE.SAMPLES[thisOldRideCnt].KPH) / 2; GeoLocMath geoLocM1 = new GeoLocMath(point1, point2); GeoLoc newPoint = geoLocM1.CalcMidPoint(); geoLocM1.point1 = newPoint; geoLocM1.point2 = point1; double newdistanceTravelled = geoLocM1.CalcDistanceBetweenGeoLocations() + thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].KM; // Create new sample and add data from above SAMPLE newSample = new SAMPLE() { SECS = 999, //insertPosition, KM = newdistanceTravelled, KPH = newPointSpeed, ALT = (thisOldRide.RIDE.SAMPLES[thisOldRideCnt].ALT + thisOldRide.RIDE.SAMPLES[thisOldRideCnt - 1].ALT) / 2, LAT = newPoint.Latitude, LON = newPoint.Longitude, SLOPE = newPointSlope }; // Insert new point in ride thisNewRide.RIDE.SAMPLES.Add(newSample); testSegment.Add(newSample); SaveLineToTxt(newSample, fullPath, -1, thisNewRideCnt); thisNewRideCnt += 1; cnt = 1; pointsAdded++; } } // Move sample to new ride CopySampleToNewlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref thisNewRide); CopySampleToTestlist(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], ref testSegment); SaveLineToTxt(thisOldRide.RIDE.SAMPLES[thisOldRideCnt], fullPath, thisOldRideCnt, thisNewRideCnt); thisNewRideCnt += 1; thisOldRideCnt++; cnt++; pointsMoved++; loopCnt++; startCnt++; } // END of addong for loop }