Skip to content

Commit

Permalink
Refactor geometric and geodetic interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmada committed Oct 29, 2023
1 parent f04f507 commit 3562832
Show file tree
Hide file tree
Showing 30 changed files with 462 additions and 300 deletions.
32 changes: 26 additions & 6 deletions src/NetFabric.Numerics.Geography/Datum.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
namespace NetFabric.Numerics.Geography;
using System.Numerics;

public readonly record struct Datum<TDatum>
where TDatum : IDatum<TDatum>?
namespace NetFabric.Numerics.Geography;

public abstract class Datum<T>
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>
{
public abstract string Name { get; }

public abstract Offset<T> Offset { get; }
public abstract Ellipsoid<T> Ellipsoid { get; }
}

public sealed class Datum<TDatum, T>
: Datum<T>
where TDatum : IDatum<T>
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>
{
public string Name
static Lazy<Datum<TDatum, T>> lazyInstance = new(() => new());

private Datum() {}

internal static Datum<TDatum, T> Instance
=> lazyInstance.Value;

public override string Name
=> TDatum.Name;
public Offset<double> Offset
public override Offset<T> Offset
=> TDatum.Offset;
public Ellipsoid<double> Ellipsoid
public override Ellipsoid<T> Ellipsoid
=> TDatum.Ellipsoid;
}
18 changes: 7 additions & 11 deletions src/NetFabric.Numerics.Geography/Geodetic2/CoordinateSystem.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using System.Collections.ObjectModel;
using System.Numerics;

namespace NetFabric.Numerics.Geography.Geodetic2;

public readonly record struct CoordinateSystem<TDatum, TAngle>
: IGeodeticCoordinateSystem<TDatum>
where TDatum : IDatum<TDatum>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>
public readonly record struct CoordinateSystem<TDatum, T>
: IGeodeticCoordinateSystem<CoordinateSystem<TDatum, T>, TDatum, T>
where TDatum : IDatum<T>
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>
{
public Datum<TDatum> Datum
=> new();

static readonly IReadOnlyList<Coordinate> coordinates
= new[] {
new Coordinate("Latitude", typeof(Angle<Degrees, TAngle>)),
new Coordinate("Longitude", typeof(Angle<Degrees, TAngle>)),
new Coordinate("Latitude", typeof(Angle<Degrees, T>)),
new Coordinate("Longitude", typeof(Angle<Degrees, T>)),
};

public IReadOnlyList<Coordinate> Coordinates
public static IReadOnlyList<Coordinate> Coordinates
=> coordinates;
}
27 changes: 11 additions & 16 deletions src/NetFabric.Numerics.Geography/Geodetic2/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace NetFabric.Numerics.Geography.Geodetic2;
[System.Diagnostics.DebuggerDisplay("Latitude = {Latitude}, Longitude = {Longitude}")]
[SkipLocalsInit]
public readonly record struct Point<TDatum, TAngleUnits, T>(Angle<TAngleUnits, T> Latitude, Angle<TAngleUnits, T> Longitude)
: IGeodeticPoint<Point<TDatum, TAngleUnits, T>, TDatum>
where TDatum : IDatum<TDatum>
where TAngleUnits : struct, IAngleUnits<TAngleUnits>
: IGeodeticPoint<Point<TDatum, TAngleUnits, T>, CoordinateSystem<TDatum, T>, TDatum, T>
where TDatum : IDatum<T>
where TAngleUnits : IAngleUnits
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>
{
public Angle<TAngleUnits, T> Latitude { get; }
Expand All @@ -30,7 +30,8 @@ public readonly record struct Point<TDatum, TAngleUnits, T>(Angle<TAngleUnits, T
/// <returns>An instance of <see cref="Point{TDatum, TAngleUnits, TAngle}"/> created from <paramref name="point" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="point" /> is not representable by <see cref="Point{TDatum, TAngleUnits, TAngle}"/>.</exception>
public static Point<TDatum, TAngleUnits, T> CreateChecked<TOther>(in Point<TDatum, TAngleUnits, TOther> point)
public static Point<TDatum, TAngleUnits, T> CreateChecked<TDatumOther, TOther>(in Point<TDatumOther, TAngleUnits, TOther> point)
where TDatumOther : IDatum<TOther>
where TOther : struct, IFloatingPoint<TOther>, IMinMaxValue<TOther>
=> new(
Angle<TAngleUnits, T>.CreateChecked(point.Latitude),
Expand All @@ -46,7 +47,8 @@ public static Point<TDatum, TAngleUnits, T> CreateChecked<TOther>(in Point<TDatu
/// <returns>An instance of <see cref="Point{TDatum, TAngleUnits, TAngle}"/> created from <paramref name="point" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="point" /> is not representable by <see cref="Point{TDatum, TAngleUnits, TAngle}"/>.</exception>
public static Point<TDatum, TAngleUnits, T> CreateSaturating<TOther>(in Point<TDatum, TAngleUnits, TOther> point)
public static Point<TDatum, TAngleUnits, T> CreateSaturating<TDatumOther, TOther>(in Point<TDatumOther, TAngleUnits, TOther> point)
where TDatumOther : IDatum<TOther>
where TOther : struct, IFloatingPoint<TOther>, IMinMaxValue<TOther>
=> new(
Angle<TAngleUnits, T>.CreateSaturating(point.Latitude),
Expand All @@ -62,7 +64,8 @@ public static Point<TDatum, TAngleUnits, T> CreateSaturating<TOther>(in Point<TD
/// <returns>An instance of <see cref="Point{TDatum, TAngleUnits, TAngle}"/> created from <paramref name="point" />.</returns>
/// <exception cref="NotSupportedException"><typeparamref name="TOther" /> is not supported.</exception>
/// <exception cref="OverflowException"><paramref name="point" /> is not representable by <see cref="Point{TDatum, TAngleUnits, TAngle}"/>.</exception>
public static Point<TDatum, TAngleUnits, T> CreateTruncating<TOther>(in Point<TDatum, TAngleUnits, TOther> point)
public static Point<TDatum, TAngleUnits, T> CreateTruncating<TDatumOther, TOther>(in Point<TDatumOther, TAngleUnits, TOther> point)
where TDatumOther : IDatum<TOther>
where TOther : struct, IFloatingPoint<TOther>, IMinMaxValue<TOther>
=> new(
Angle<TAngleUnits, T>.CreateTruncating(point.Latitude),
Expand All @@ -74,7 +77,7 @@ public static Point<TDatum, TAngleUnits, T> CreateTruncating<TOther>(in Point<TD
public static readonly Point<TDatum, TAngleUnits, T> Zero
= new(Angle<TAngleUnits, T>.Zero, Angle<TAngleUnits, T>.Zero);

static Point<TDatum, TAngleUnits, T> IGeometricBase<Point<TDatum, TAngleUnits, T>>.Zero
static Point<TDatum, TAngleUnits, T> IGeometricBase<Point<TDatum, TAngleUnits, T>, CoordinateSystem<TDatum, T>>.Zero
=> Zero;

/// <summary>
Expand All @@ -96,15 +99,7 @@ static Point<TDatum, TAngleUnits, T> IMinMaxValue<Point<TDatum, TAngleUnits, T>>

#endregion

/// <summary>
/// Gets the coordinate system.
/// </summary>
public CoordinateSystem<TDatum, T> CoordinateSystem
=> new();
ICoordinateSystem IGeometricBase<Point<TDatum, TAngleUnits, T>>.CoordinateSystem
=> CoordinateSystem;

object IGeometricBase<Point<TDatum, TAngleUnits, T>>.this[int index]
object IGeometricBase.this[int index]
=> index switch
{
0 => Latitude,
Expand Down
20 changes: 8 additions & 12 deletions src/NetFabric.Numerics.Geography/Geodetic3/CoordinateSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@

namespace NetFabric.Numerics.Geography.Geodetic3;

public readonly record struct CoordinateSystem<TDatum, TAngle, THeight>
: IGeodeticCoordinateSystem<TDatum>
where TDatum : IDatum<TDatum>
where TAngle : struct, IFloatingPoint<TAngle>, IMinMaxValue<TAngle>
where THeight : struct, INumber<THeight>
public readonly record struct CoordinateSystem<TDatum, T>
: IGeodeticCoordinateSystem<CoordinateSystem<TDatum, T>, TDatum, T>
where TDatum : IDatum<T>
where T : struct, IFloatingPoint<T>, IMinMaxValue<T>
{
public Datum<TDatum> Datum
=> new();

static readonly IReadOnlyList<Coordinate> coordinates
= new[] {
new Coordinate("Latitude", typeof(Angle<Degrees, TAngle>)),
new Coordinate("Longitude", typeof(Angle<Degrees, TAngle>)),
new Coordinate("Height", typeof(THeight)),
new Coordinate("Latitude", typeof(Angle<Degrees, T>)),
new Coordinate("Longitude", typeof(Angle<Degrees, T>)),
new Coordinate("Height", typeof(T)),
};

public IReadOnlyList<Coordinate> Coordinates
public static IReadOnlyList<Coordinate> Coordinates
=> coordinates;
}
Loading

0 comments on commit 3562832

Please sign in to comment.