Skip to main content
added performance improvement
Source Link

First thank you allthank you all for your help, and specifically @t3chb0t , @JanKuiken , @PeterTaylor and @dfhwze . I learned a lot today.

Not only that but performance was multiplied by 20

using static System.Math;
using UnityEngine;

public static class Lambert2008
{
    public const int a = 6378137;
    public const double f = 1 / 298.257222101;
    public const double rad = PI/180;
    public const double ϕ1 = 49.8333333 * rad;
    public const double ϕ2 = 51.1666667 * rad;
    public const double ϕ0 = 50.797815 * rad;
    public const double λ0 = 4.359215833 * rad;
    public const int x0 = 649328;
    public const int y0 = 665262;

    static readonly double E = Sqrt((2 - f) * f);

    static readonly double M1;
    static readonly double M2;

    static readonly double T1;
    static readonly double T2;
    static readonly double T0;

    static readonly double N;
    static readonly double G;
    static readonly double R0;

    static Lambert2008()
    {
        double _;
        (_, T0) = MT(ϕ0);
        (M1, T1) = MT(ϕ1);
        (M2, T2) = MT(ϕ2);

        N = Log(M1 / M2, T1 / T2);
        G = M1 / (N * Pow(T1, N));
        R0 = a * G * Pow(T0, N);
    }

    static (double, double) MT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        var m = Cos(ϕ) / Sqrt(1 - Pow(e_sinϕ, 2));
        var t = CalculateT(ϕ);
        return (m, t);
    }

    static double R(double t)
    {
        return a * G * Pow(t, N);
    }

    static double CalculateT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        return Tan(PI / 4 - ϕ / 2)
            / Pow((1 - e_sinϕ) / (1 + e_sinϕ), E / 2);
    }

    public static Vector2 FromGeographicRelative(Vector2 coordinates)
    {
        double _, t, r;
        (_, t) = MT(coordinates.x * rad);
        r = R(t);
        var θ = N * (coordinates.y * rad - λ0);
        var res = new Vector2
        {
            x = (float)(r * Sin(θ)),
            y = (float)(R0 - r * Cos(θ))
        };
        return res;
    }

    public static Vector2 FromGeographic(Vector2 coordinates)
    {
        return FromGeographicRelative(coordinates) + new Vector2(x0, y0);
    }
}
```

First thank you all for your help, and specifically @t3chb0t , @JanKuiken , @PeterTaylor and @dfhwze . I learned a lot today.

using static System.Math;
using UnityEngine;

public static class Lambert2008
{
    public const int a = 6378137;
    public const double f = 1 / 298.257222101;
    public const double rad = PI/180;
    public const double ϕ1 = 49.8333333 * rad;
    public const double ϕ2 = 51.1666667 * rad;
    public const double ϕ0 = 50.797815 * rad;
    public const double λ0 = 4.359215833 * rad;
    public const int x0 = 649328;
    public const int y0 = 665262;

    static readonly double E = Sqrt((2 - f) * f);

    static readonly double M1;
    static readonly double M2;

    static readonly double T1;
    static readonly double T2;
    static readonly double T0;

    static readonly double N;
    static readonly double G;
    static readonly double R0;

    static Lambert2008()
    {
        double _;
        (_, T0) = MT(ϕ0);
        (M1, T1) = MT(ϕ1);
        (M2, T2) = MT(ϕ2);

        N = Log(M1 / M2, T1 / T2);
        G = M1 / (N * Pow(T1, N));
        R0 = a * G * Pow(T0, N);
    }

    static (double, double) MT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        var m = Cos(ϕ) / Sqrt(1 - Pow(e_sinϕ, 2));
        var t = CalculateT(ϕ);
        return (m, t);
    }

    static double R(double t)
    {
        return a * G * Pow(t, N);
    }

    static double CalculateT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        return Tan(PI / 4 - ϕ / 2)
            / Pow((1 - e_sinϕ) / (1 + e_sinϕ), E / 2);
    }

    public static Vector2 FromGeographicRelative(Vector2 coordinates)
    {
        double _, t, r;
        (_, t) = MT(coordinates.x * rad);
        r = R(t);
        var θ = N * (coordinates.y * rad - λ0);
        var res = new Vector2
        {
            x = (float)(r * Sin(θ)),
            y = (float)(R0 - r * Cos(θ))
        };
        return res;
    }

    public static Vector2 FromGeographic(Vector2 coordinates)
    {
        return FromGeographicRelative(coordinates) + new Vector2(x0, y0);
    }
}
```

First thank you all for your help, and specifically @t3chb0t , @JanKuiken , @PeterTaylor and @dfhwze . I learned a lot today.

Not only that but performance was multiplied by 20

using static System.Math;
using UnityEngine;

public static class Lambert2008
{
    public const int a = 6378137;
    public const double f = 1 / 298.257222101;
    public const double rad = PI/180;
    public const double ϕ1 = 49.8333333 * rad;
    public const double ϕ2 = 51.1666667 * rad;
    public const double ϕ0 = 50.797815 * rad;
    public const double λ0 = 4.359215833 * rad;
    public const int x0 = 649328;
    public const int y0 = 665262;

    static readonly double E = Sqrt((2 - f) * f);

    static readonly double M1;
    static readonly double M2;

    static readonly double T1;
    static readonly double T2;
    static readonly double T0;

    static readonly double N;
    static readonly double G;
    static readonly double R0;

    static Lambert2008()
    {
        double _;
        (_, T0) = MT(ϕ0);
        (M1, T1) = MT(ϕ1);
        (M2, T2) = MT(ϕ2);

        N = Log(M1 / M2, T1 / T2);
        G = M1 / (N * Pow(T1, N));
        R0 = a * G * Pow(T0, N);
    }

    static (double, double) MT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        var m = Cos(ϕ) / Sqrt(1 - Pow(e_sinϕ, 2));
        var t = CalculateT(ϕ);
        return (m, t);
    }

    static double R(double t)
    {
        return a * G * Pow(t, N);
    }

    static double CalculateT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        return Tan(PI / 4 - ϕ / 2)
            / Pow((1 - e_sinϕ) / (1 + e_sinϕ), E / 2);
    }

    public static Vector2 FromGeographicRelative(Vector2 coordinates)
    {
        double _, t, r;
        (_, t) = MT(coordinates.x * rad);
        r = R(t);
        var θ = N * (coordinates.y * rad - λ0);
        var res = new Vector2
        {
            x = (float)(r * Sin(θ)),
            y = (float)(R0 - r * Cos(θ))
        };
        return res;
    }

    public static Vector2 FromGeographic(Vector2 coordinates)
    {
        return FromGeographicRelative(coordinates) + new Vector2(x0, y0);
    }
}
Source Link

First thank you all for your help, and specifically @t3chb0t , @JanKuiken , @PeterTaylor and @dfhwze . I learned a lot today.

Here is the refactored code taking into account all the comments. I think it looks a lot better already.

using static System.Math;
using UnityEngine;

public static class Lambert2008
{
    public const int a = 6378137;
    public const double f = 1 / 298.257222101;
    public const double rad = PI/180;
    public const double ϕ1 = 49.8333333 * rad;
    public const double ϕ2 = 51.1666667 * rad;
    public const double ϕ0 = 50.797815 * rad;
    public const double λ0 = 4.359215833 * rad;
    public const int x0 = 649328;
    public const int y0 = 665262;

    static readonly double E = Sqrt((2 - f) * f);

    static readonly double M1;
    static readonly double M2;

    static readonly double T1;
    static readonly double T2;
    static readonly double T0;

    static readonly double N;
    static readonly double G;
    static readonly double R0;

    static Lambert2008()
    {
        double _;
        (_, T0) = MT(ϕ0);
        (M1, T1) = MT(ϕ1);
        (M2, T2) = MT(ϕ2);

        N = Log(M1 / M2, T1 / T2);
        G = M1 / (N * Pow(T1, N));
        R0 = a * G * Pow(T0, N);
    }

    static (double, double) MT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        var m = Cos(ϕ) / Sqrt(1 - Pow(e_sinϕ, 2));
        var t = CalculateT(ϕ);
        return (m, t);
    }

    static double R(double t)
    {
        return a * G * Pow(t, N);
    }

    static double CalculateT(double ϕ)
    {
        var e_sinϕ = E * Sin(ϕ);
        return Tan(PI / 4 - ϕ / 2)
            / Pow((1 - e_sinϕ) / (1 + e_sinϕ), E / 2);
    }

    public static Vector2 FromGeographicRelative(Vector2 coordinates)
    {
        double _, t, r;
        (_, t) = MT(coordinates.x * rad);
        r = R(t);
        var θ = N * (coordinates.y * rad - λ0);
        var res = new Vector2
        {
            x = (float)(r * Sin(θ)),
            y = (float)(R0 - r * Cos(θ))
        };
        return res;
    }

    public static Vector2 FromGeographic(Vector2 coordinates)
    {
        return FromGeographicRelative(coordinates) + new Vector2(x0, y0);
    }
}
```