Skip to content

Commit ae85aa6

Browse files
committed
more C# styled
1 parent 17f950d commit ae85aa6

File tree

5 files changed

+76
-61
lines changed

5 files changed

+76
-61
lines changed

GeographicLib/GeoMath.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,19 @@ public static double Atan2d(double y, double x)
310310
// converting it to degrees and mapping the result to the correct
311311
// quadrant.
312312
int q = 0;
313-
if (Math.Abs(y) > Math.Abs(x)) { double t; t = x; x = y; y = t; q = 2; }
314-
if (x < 0) { x = -x; ++q; }
313+
if (Math.Abs(y) > Math.Abs(x))
314+
{
315+
double t;
316+
t = x;
317+
x = y;
318+
y = t;
319+
q = 2;
320+
}
321+
if (x < 0)
322+
{
323+
x = -x;
324+
++q;
325+
}
315326
// here x >= 0 and x >= abs(y), so angle is in [-pi/4, pi/4]
316327
double ang = ToDegrees(Math.Atan2(y, x));
317328
switch (q)
@@ -336,7 +347,7 @@ public static double Atan2d(double y, double x)
336347
* @return true if number is finite, false if NaN or infinite.
337348
**********************************************************************/
338349
[MethodImpl(MethodImplOptions.AggressiveInlining)]
339-
public static bool isfinite(double x) =>
350+
public static bool IsFinite(double x) =>
340351
Math.Abs(x) <= Double.MaxValue;
341352

342353
[MethodImpl(MethodImplOptions.AggressiveInlining)]

GeographicLib/Geodesic.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -280,9 +280,9 @@ public Geodesic(double a, double f)
280280
_etol2 = 0.1 * tol2_ /
281281
Math.Sqrt(Math.Max(0.001, Math.Abs(_f)) *
282282
Math.Min(1.0, 1 - _f / 2) / 2);
283-
if (!(GeoMath.isfinite(_a) && _a > 0))
283+
if (!(GeoMath.IsFinite(_a) && _a > 0))
284284
throw new GeographicErr("Equatorial radius is not positive");
285-
if (!(GeoMath.isfinite(_b) && _b > 0))
285+
if (!(GeoMath.IsFinite(_b) && _b > 0))
286286
throw new GeographicErr("Polar semi-axis is not positive");
287287
_A3x = new double[nA3x_];
288288
_C3x = new double[nC3x_];
@@ -641,13 +641,14 @@ internal struct InverseData
641641
internal GeodesicData g;
642642
internal double salp1, calp1, salp2, calp2;
643643

644+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
644645
private void Init()
645646
{
646647
g = new GeodesicData();
647648
salp1 = calp1 = salp2 = calp2 = Double.NaN;
648649
}
649650

650-
internal static InverseData Empty
651+
internal static InverseData NaN
651652
{
652653
get
653654
{
@@ -661,7 +662,7 @@ internal static InverseData Empty
661662
private InverseData InverseInt(double lat1, double lon1,
662663
double lat2, double lon2, int outmask)
663664
{
664-
InverseData result = InverseData.Empty;
665+
InverseData result = InverseData.NaN;
665666
GeodesicData r = result.g;
666667
// Compute longitude difference (AngDiff does this carefully). Result is
667668
// in [-180, 180] but -180 is only for west-going geodesics. 180 is for
@@ -1269,13 +1270,13 @@ public GeodesicLine Line(double lat1, double lon1, double azi1, int caps)
12691270
* @return <i>a</i> the equatorial radius of the ellipsoid (meters). This is
12701271
* the value used in the constructor.
12711272
**********************************************************************/
1272-
public double MajorRadius() { return _a; }
1273+
public double MajorRadius => _a;
12731274

12741275
/**
12751276
* @return <i>f</i> the flattening of the ellipsoid. This is the
12761277
* value used in the constructor.
12771278
**********************************************************************/
1278-
public double Flattening() { return _f; }
1279+
public double Flattening => _f;
12791280

12801281
/**
12811282
* @return total area of ellipsoid in meters<sup>2</sup>. The area of a
@@ -1288,8 +1289,9 @@ public GeodesicLine Line(double lat1, double lon1, double azi1, int caps)
12881289
* A global instantiation of Geodesic with the parameters for the WGS84
12891290
* ellipsoid.
12901291
**********************************************************************/
1291-
public static readonly Geodesic WGS84 =
1292-
new Geodesic(Constants.WGS84_a, Constants.WGS84_f);
1292+
private static readonly Lazy<Geodesic> _wgs84 = new Lazy<Geodesic>(() => new Geodesic(Constants.WGS84_a, Constants.WGS84_f), true);
1293+
1294+
public static Geodesic WGS84 => _wgs84.Value;
12931295

12941296
// This is a reformulation of the geodesic problem. The notation is as
12951297
// follows:
@@ -1341,11 +1343,14 @@ internal static double SinCosSeries(bool sinp,
13411343
internal struct LengthsV
13421344
{
13431345
internal double s12b, m12b, m0, M12, M21;
1346+
1347+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13441348
private void Init()
13451349
{
13461350
s12b = m12b = m0 = M12 = M21 = Double.NaN;
13471351
}
1348-
internal static LengthsV Empty
1352+
1353+
internal static LengthsV NaN
13491354
{
13501355
get
13511356
{
@@ -1367,7 +1372,7 @@ private LengthsV Lengths(double eps, double sig12,
13671372
// Return m12b = (reduced length)/_b; also calculate s12b = distance/_b,
13681373
// and m0 = coefficient of secular term in expression for reduced length.
13691374
outmask &= GeodesicMask.OUT_MASK;
1370-
LengthsV v = LengthsV.Empty; // To hold s12b, m12b, m0, M12, M21;
1375+
LengthsV v = LengthsV.NaN; // To hold s12b, m12b, m0, M12, M21;
13711376

13721377
double m0x = 0, J12 = 0, A1 = 0, A2 = 0;
13731378
if ((outmask & (GeodesicMask.DISTANCE | GeodesicMask.REDUCEDLENGTH |
@@ -1494,12 +1499,13 @@ internal struct InverseStartV
14941499
// Only updated for short lines
14951500
dnm;
14961501

1502+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14971503
private void Init()
14981504
{
14991505
sig12 = salp1 = calp1 = salp2 = calp2 = dnm = Double.NaN;
15001506
}
15011507

1502-
internal static InverseStartV Empty
1508+
internal static InverseStartV NaN
15031509
{
15041510
get
15051511
{
@@ -1522,7 +1528,7 @@ private InverseStartV InverseStart(double sbet1, double cbet1, double dn1,
15221528
// salp2 and calp2 and function value is sig12.
15231529

15241530
// To hold sig12, salp1, calp1, salp2, calp2, dnm.
1525-
InverseStartV w = InverseStartV.Empty;
1531+
InverseStartV w = InverseStartV.NaN;
15261532
w.sig12 = -1; // Return value
15271533
double
15281534
// bet12 = bet2 - bet1 in [0, pi); bet12a = bet2 + bet1 in (-pi, 0]
@@ -1697,13 +1703,15 @@ internal struct Lambda12V
16971703
{
16981704
internal double lam12, salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
16991705
eps, domg12, dlam12;
1706+
1707+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17001708
private void Init()
17011709
{
17021710
lam12 = salp2 = calp2 = sig12 = ssig1 = csig1 = ssig2 = csig2
17031711
= eps = domg12 = dlam12 = Double.NaN;
17041712
}
17051713

1706-
internal static Lambda12V Empty
1714+
internal static Lambda12V NaN
17071715
{
17081716
get
17091717
{
@@ -1725,7 +1733,7 @@ private Lambda12V Lambda12(double sbet1, double cbet1, double dn1,
17251733
// Object to hold lam12, salp2, calp2, sig12, ssig1, csig1, ssig2, csig2,
17261734
// eps, domg12, dlam12;
17271735

1728-
Lambda12V w = Lambda12V.Empty;
1736+
Lambda12V w = Lambda12V.NaN;
17291737

17301738
if (sbet1 == 0 && calp1 == 0)
17311739
// Break degeneracy of equatorial line. This case has already been

GeographicLib/GeodesicLine.cs

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,11 @@ public GeodesicData Position(bool arcmode, double s12_a12,
452452
{
453453
outmask &= _caps & GeodesicMask.OUT_MASK;
454454
GeodesicData r = new GeodesicData();
455-
if (!(Init() &&
456-
(arcmode ||
457-
(_caps & (GeodesicMask.OUT_MASK & GeodesicMask.DISTANCE_IN)) != 0)
458-
))
455+
if (!(Init && (arcmode || (_caps & (GeodesicMask.OUT_MASK & GeodesicMask.DISTANCE_IN)) != 0)))
456+
{
459457
// Uninitialized or impossible distance calculation requested
460458
return r;
459+
}
461460
r.lat1 = _lat1; r.azi1 = _azi1;
462461
r.lon1 = ((outmask & GeodesicMask.LONG_UNROLL) != 0) ? _lon1 :
463462
GeoMath.AngNormalize(_lon1);
@@ -637,6 +636,7 @@ public GeodesicData Position(bool arcmode, double s12_a12,
637636
* This is only useful if the GeodesicLine object has been constructed
638637
* with <i>caps</i> |= {@link GeodesicMask#DISTANCE_IN}.
639638
**********************************************************************/
639+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
640640
public void SetDistance(double s13)
641641
{
642642
_s13 = s13;
@@ -653,6 +653,7 @@ public void SetDistance(double s13)
653653
* The distance <i>s13</i> is only set if the GeodesicLine object has been
654654
* constructed with <i>caps</i> |= {@link GeodesicMask#DISTANCE}.
655655
**********************************************************************/
656+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
656657
void SetArc(double a13)
657658
{
658659
_a13 = a13;
@@ -671,6 +672,7 @@ void SetArc(double a13)
671672
* point 1 to point 3 (meters); otherwise it is the arc length from
672673
* point 1 to point 3 (degrees); it can be negative.
673674
**********************************************************************/
675+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
674676
public void GenSetDistance(bool arcmode, double s13_a13)
675677
{
676678
if (arcmode)
@@ -682,90 +684,82 @@ public void GenSetDistance(bool arcmode, double s13_a13)
682684
/**
683685
* @return true if the object has been initialized.
684686
**********************************************************************/
685-
private bool Init() { return _caps != 0; }
687+
private bool Init => _caps != 0;
686688

687689
/**
688690
* @return <i>lat1</i> the latitude of point 1 (degrees).
689691
**********************************************************************/
690-
public double Latitude()
691-
{ return Init() ? _lat1 : Double.NaN; }
692+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
693+
public double Latitude() => Init ? _lat1 : double.NaN;
692694

693695
/**
694696
* @return <i>lon1</i> the longitude of point 1 (degrees).
695697
**********************************************************************/
696-
public double Longitude()
697-
{ return Init() ? _lon1 : Double.NaN; }
698+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
699+
public double Longitude() => Init ? _lon1 : double.NaN;
698700

699701
/**
700702
* @return <i>azi1</i> the azimuth (degrees) of the geodesic line at point 1.
701703
**********************************************************************/
702-
public double Azimuth()
703-
{ return Init() ? _azi1 : Double.NaN; }
704+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
705+
public double Azimuth() => Init ? _azi1 : double.NaN;
704706

705707
/**
706708
* @return pair of sine and cosine of <i>azi1</i> the azimuth (degrees) of
707709
* the geodesic line at point 1.
708710
**********************************************************************/
709-
public Pair AzimuthCosines()
710-
{
711-
return new Pair(Init() ? _salp1 : Double.NaN,
712-
Init() ? _calp1 : Double.NaN);
713-
}
711+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
712+
public Pair AzimuthCosines() =>
713+
new Pair(Init ? _salp1 : Double.NaN, Init ? _calp1 : Double.NaN);
714714

715715
/**
716716
* @return <i>azi0</i> the azimuth (degrees) of the geodesic line as it
717717
* crosses the equator in a northward direction.
718718
**********************************************************************/
719-
public double EquatorialAzimuth()
720-
{
721-
return Init() ?
722-
GeoMath.Atan2d(_salp0, _calp0) : Double.NaN;
723-
}
719+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
720+
public double EquatorialAzimuth() =>
721+
Init ? GeoMath.Atan2d(_salp0, _calp0) : Double.NaN;
724722

725723
/**
726724
* @return pair of sine and cosine of <i>azi0</i> the azimuth of the geodesic
727725
* line as it crosses the equator in a northward direction.
728726
**********************************************************************/
729-
public Pair EquatorialAzimuthCosines()
730-
{
731-
return new Pair(Init() ? _salp0 : Double.NaN,
732-
Init() ? _calp0 : Double.NaN);
733-
}
727+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
728+
public Pair EquatorialAzimuthCosines() =>
729+
new Pair(Init ? _salp0 : Double.NaN, Init ? _calp0 : Double.NaN);
734730

735731
/**
736732
* @return <i>a1</i> the arc length (degrees) between the northward
737733
* equatorial crossing and point 1.
738734
**********************************************************************/
739-
public double EquatorialArc()
740-
{
741-
return Init() ?
742-
GeoMath.Atan2d(_ssig1, _csig1) : Double.NaN;
743-
}
735+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
736+
public double EquatorialArc() =>
737+
Init ? GeoMath.Atan2d(_ssig1, _csig1) : double.NaN;
744738

745739
/**
746740
* @return <i>a</i> the equatorial radius of the ellipsoid (meters). This is
747741
* the value inherited from the Geodesic object used in the constructor.
748742
**********************************************************************/
749-
public double MajorRadius()
750-
{ return Init() ? _a : Double.NaN; }
743+
public double MajorRadius => Init ? _a : double.NaN;
751744

752745
/**
753746
* @return <i>f</i> the flattening of the ellipsoid. This is the value
754747
* inherited from the Geodesic object used in the constructor.
755748
**********************************************************************/
756-
public double Flattening()
757-
{ return Init() ? _f : Double.NaN; }
749+
public double Flattening => Init ? _f : double.NaN;
758750

759751
/**
760752
* @return <i>caps</i> the computational capabilities that this object was
761753
* constructed with. LATITUDE and AZIMUTH are always included.
762754
**********************************************************************/
763-
public int Capabilities() { return _caps; }
755+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
756+
public int Capabilities() => _caps;
764757

765758
/**
766759
* @param testcaps a set of bitor'ed {@link GeodesicMask} values.
767760
* @return true if the GeodesicLine object has all these capabilities.
768761
**********************************************************************/
762+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
769763
public bool Capabilities(int testcaps)
770764
{
771765
testcaps &= GeodesicMask.OUT_ALL;
@@ -780,18 +774,20 @@ public bool Capabilities(int testcaps)
780774
* @return <i>s13</i> if <i>arcmode</i> is false; <i>a13</i> if
781775
* <i>arcmode</i> is true.
782776
**********************************************************************/
783-
public double GenDistance(bool arcmode)
784-
{ return Init() ? (arcmode ? _a13 : _s13) : Double.NaN; }
777+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
778+
public double GenDistance(bool arcmode) => Init ? (arcmode ? _a13 : _s13) : double.NaN;
785779

786780
/**
787781
* @return <i>s13</i>, the distance to point 3 (meters).
788782
**********************************************************************/
789-
public double Distance() { return GenDistance(false); }
783+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
784+
public double Distance() => GenDistance(false);
790785

791786
/**
792787
* @return <i>a13</i>, the arc length to point 3 (degrees).
793788
**********************************************************************/
794-
public double Arc() { return GenDistance(true); }
789+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
790+
public double Arc() => GenDistance(true);
795791

796792
}
797793

GeographicLib/Gnomonic.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public readonly struct Gnomonic
153153
public Gnomonic(Geodesic earth)
154154
{
155155
_earth = earth;
156-
_a = _earth.MajorRadius();
157-
_f = _earth.Flattening();
156+
_a = _earth.MajorRadius;
157+
_f = _earth.Flattening;
158158
}
159159

160160
/**

GeographicLib/PolygonArea.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,13 @@ public PolygonResult TestEdge(double azi, double s,
404404
* the value inherited from the Geodesic object used in the constructor.
405405
**********************************************************************/
406406

407-
public double MajorRadius() { return _earth.MajorRadius(); }
407+
public double MajorRadius => _earth.MajorRadius;
408408

409409
/**
410410
* @return <i>f</i> the flattening of the ellipsoid. This is the value
411411
* inherited from the Geodesic object used in the constructor.
412412
**********************************************************************/
413-
public double Flattening() { return _earth.Flattening(); }
413+
public double Flattening => _earth.Flattening;
414414

415415
/**
416416
* Report the previous vertex added to the polygon or polyline.

0 commit comments

Comments
 (0)