Skip to content

Commit

Permalink
Implement Ifly737 format.
Browse files Browse the repository at this point in the history
  • Loading branch information
JetStream96 committed Mar 10, 2018
1 parent f46a90d commit 3db585b
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 35 deletions.
11 changes: 11 additions & 0 deletions src/QSP/MathTools/EarthGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ public static Vector3D GetW(Vector3D v, Vector3D v2)
return v3.Cross(v).Normalize();
}

/// <summary>
/// Calculates the great circle path from x to y, and returns heading departing x.
/// x and y should be unique.
/// </summary>
/// <exception cref="ArgumentException"></exception>
public static double TrueHeading(ICoordinate x, ICoordinate y)
{
if (x.LatLonEquals(y)) throw new ArgumentException();
return TrueHeading(GetW(x.ToVector3D(), y.ToVector3D()), x);
}

/// <summary>
/// Calculates the true heading of the given vector, at the given coordinate.
/// Return value is larger than 0 and smaller or equal to 360. Note: If v is
Expand Down
60 changes: 32 additions & 28 deletions src/QSP/RouteFinding/FileExport/Providers/Ifly737Provider.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
using QSP.RouteFinding.Airports;
using QSP.MathTools;
using QSP.RouteFinding.Routes;
using System;
using System.Collections.Generic;
using System.Linq;
using static CommonLibrary.LibraryExtension.Types;
using static System.Linq.Enumerable;

namespace QSP.RouteFinding.FileExport.Providers
{
// TODO: Add test.
public static class Ifly737Provider
{
public static string GetExportText(Route route, AirportManager airports)
// In the waypoint part, each line contain a waypoint. Let's say the
// waypoint is B, and the previous waypoint is A. The line contains A also
// contains a true heading. If you fly a great circle route from A to B, you
// will have true heading equal to that value.

public static string GetExportText(Route route)
{
if (route.Count < 2) throw new ArgumentException();
var linesPart1 = List(
route.FirstWaypoint.ID.Substring(0, 4),
route.FirstWaypoint.ID.Substring(4),
route.LastWaypoint.ID.Substring(0, 4)
route.LastWaypoint.ID.Substring(0, 4),
route.FirstWaypoint.ID.Substring(4)
).Concat(Repeat("", 7))
.Concat(List("-1", "", "", "", "", "0"));

var node = route.First.Next;
var part2 = List(("DIRECT", node.Value.Waypoint));
node = node.Next;
var linesPart2 = new List<string>();
var prev = route.First;
var current = prev.Next;

while (node != null)
while (current != route.Last)
{
var a = node.Previous.Value.Neighbor.Airway;
var airway = a == "DCT" ? "DIRECT" : a;
part2.Add((a, node.Value.Waypoint));
node = node.Next;
var airway = prev.Value.Neighbor.Airway;
bool isDirect = (prev == route.First) || (airway == "DCT");

var w = current.Value.Waypoint;
var heading = EarthGeometry.TrueHeading(prev.Value.Waypoint, w);

linesPart2.Add(string.Format(
"{0},{1},0, {2} {3},0,0, {4},0,0,1,-1,0.000,0,-1000,-1000,-1,-1,-1,0,0,000.00000,0,0,,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1000,0",
isDirect ? "DIRECT,3" : $"{airway},2",
w.ID,
w.Lat.ToString("###0.000000"),
w.Lon.ToString("###0.000000"),
heading.ToString("###0.00000")));

prev = current;
current = current.Next;
}

var linesPart2 = part2.Select(v =>
{
var (airway, wpt) = v;
var words = List(
airway,
airway == "DIRECT" ? "3" : "2",
wpt.ID,
string.Format("{0} {1}", wpt.Lat, wpt.Lon),
"0",
"0",
"Put heading here"); //TODO: calculate true heading towards next wpt
return string.Join(",", words) +
",0,0,1,-1,0.000,0,-1000,-1000,-1,-1,-1,0,0,000.00000,0,0,,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1000,0";
});
linesPart2.Add("");

return string.Join(",\n", linesPart1.Concat(linesPart2));
}
}
}
}
6 changes: 6 additions & 0 deletions src/Tests/UnitTest/MathTools/EarthGeometryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ public void TureHeadingTest()
Assert.IsTrue(IsZero(TrueHeading(v0, p2)));
Assert.IsTrue(IsZero(TrueHeading(v1, p3)));
}

[Test]
public void TureHeadingCoordinateTest()
{
Assert.AreEqual(90, TrueHeading(new LatLon(0, 20), new LatLon(0, 60)), 1e-7);
}
}
}
16 changes: 9 additions & 7 deletions src/Tests/UnitTest/RouteFinding/Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ namespace UnitTest.RouteFinding
{
public static class Common
{
// Format:
// Waypoint1, AirwayToNext, Distance,
// Waypoint2, AirwayToNext, Distance,
// ...
// WaypointN
//
// Use a negative distance for automatic calculation
/// <summary>
/// Format:
/// Waypoint1, AirwayToNext, Distance,
/// Waypoint2, AirwayToNext, Distance,
/// ...
/// WaypointN
///
/// Use a negative distance for automatic calculation
/// </summary>
public static Route GetRoute(params object[] para)
{
if (para.Length % 3 != 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using CommonLibrary.LibraryExtension;
using NUnit.Framework;
using QSP.RouteFinding.Containers;
using QSP.RouteFinding.FileExport.Providers;

namespace UnitTest.RouteFinding.FileExport.Providers
{
[TestFixture]
public class Ifly737ProviderTest
{
[Test]
public void GetExportTextTest()
{
var route = Common.GetRoute(
new Waypoint("RJBB06L", 0.0, 0.0), "A", -1.0,
new Waypoint("WPT0", 0.0, 1.0), "B", -1.0,
new Waypoint("WPT1", 0.0, 2.0), "DCT", -1.0,
new Waypoint("WPT2", 0.0, 2.5), "C", -1.0,
new Waypoint("RJAA18", 0.0, 3.0));

var text = Ifly737Provider.GetExportText(route);

var expected =
@"RJBB,
RJAA,
06L,
,
,
,
,
,
,
,
-1,
,
,
,
,
0,
DIRECT,3,WPT0,0, 0.000000 1.000000,0,0, 90.00000,0,0,1,-1,0.000,0,-1000,-1000,-1,-1,-1,0,0,000.00000,0,0,,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1000,0,
B,2,WPT1,0, 0.000000 2.000000,0,0, 90.00000,0,0,1,-1,0.000,0,-1000,-1000,-1,-1,-1,0,0,000.00000,0,0,,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1000,0,
DIRECT,3,WPT2,0, 0.000000 2.500000,0,0, 90.00000,0,0,1,-1,0.000,0,-1000,-1000,-1,-1,-1,0,0,000.00000,0,0,,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1,-1,-1000,0,-1000,-1000,0,
";

Assert.IsTrue(expected.EqualsIgnoreNewlineStyle(text));
}

[Test]
public void GetExportTextNoWaypontDoesNotThrow()
{
var route = Common.GetRoute(
new Waypoint("RJBB06L", 0.0, 0.0), "DCT", -1.0,
new Waypoint("RJAA18", 0.0, 3.0));

var text = Ifly737Provider.GetExportText(route);
}
}
}
2 changes: 2 additions & 0 deletions src/Tests/UnitTest/UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
<Compile Include="LibraryExtension\LinkedListsTest.cs" />
<Compile Include="LibraryExtension\ListsTest.cs" />
<Compile Include="LibraryExtension\WebRequestsTest.cs" />
<Compile Include="MathTools\AnglesTest.cs" />
<Compile Include="MathTools\EarthGeometryTest.cs" />
<Compile Include="MathTools\ModuloTest.cs" />
<Compile Include="MathTools\Tables\Readers\TableReader1DTest.cs" />
Expand Down Expand Up @@ -154,6 +155,7 @@
<Compile Include="RouteFinding\FileExport\ExportCommandTest.cs" />
<Compile Include="RouteFinding\FileExport\Providers\Fs9ProviderTest.cs" />
<Compile Include="RouteFinding\FileExport\Providers\FsxProviderTest.cs" />
<Compile Include="RouteFinding\FileExport\Providers\Ifly737ProviderTest.cs" />
<Compile Include="RouteFinding\FileExport\Providers\PmdgProviderTest.cs" />
<Compile Include="RouteFinding\RandomRoutes\RandomRouteFinderTest.cs" />
<Compile Include="RouteFinding\RouteAnalyzers\AnalyzerWithCommandsTest.cs" />
Expand Down

0 comments on commit 3db585b

Please sign in to comment.