Skip to content

Commit

Permalink
#177: Convert 'geom' classes to records.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Nov 7, 2023
1 parent aaf2335 commit 407040d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 134 deletions.
98 changes: 17 additions & 81 deletions jpx/src/main/java/io/jenetics/jpx/geom/Ellipsoid.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static java.util.Objects.requireNonNull;

import java.io.Serial;
import java.io.Serializable;

/**
Expand All @@ -33,22 +32,26 @@
* @see <a href="https://en.wikipedia.org/wiki/Earth_ellipsoid">Earth ellipsoid</a>
* @see Geoid
*
* @param name the name of the earth ellipsoid model
* @param A the equatorial radius, in meter
* @param B the polar radius, in meter
* @param F the inverse flattening
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @version 1.0
* @version !__version__!
* @since 1.0
*/
public final class Ellipsoid implements Serializable {

@Serial
private static final long serialVersionUID = 1L;
public record Ellipsoid(String name, double A, double B, double F)
implements Serializable
{

/**
* The ellipsoid of the <em>World Geodetic System: WGS 84</em>
*
* @see <a href="https://en.wikipedia.org/wiki/World_Geodetic_System#A_new_World_Geodetic_System:_WGS_84">
* WGS-84</a>
*/
public static final Ellipsoid WGS84 = of(
public static final Ellipsoid WGS84 = new Ellipsoid(
"WGS-84",
6_378_137,
6_356_752.314245,
Expand All @@ -61,7 +64,7 @@ public final class Ellipsoid implements Serializable {
*
* @see <a href="https://en.wikipedia.org/wiki/IERS">IERS-89</a>
*/
public static final Ellipsoid IERS_1989 = of(
public static final Ellipsoid IERS_1989 = new Ellipsoid(
"IERS-1989",
6_378_136,
6_356_751.302,
Expand All @@ -74,7 +77,7 @@ public final class Ellipsoid implements Serializable {
*
* @see <a href="https://en.wikipedia.org/wiki/IERS">IERS-89</a>
*/
public static final Ellipsoid IERS_2003 = of(
public static final Ellipsoid IERS_2003 = new Ellipsoid(
"IERS-2003",
6_378_136.6,
6_356_751.9,
Expand All @@ -86,84 +89,17 @@ public final class Ellipsoid implements Serializable {
*/
public static final Ellipsoid DEFAULT = WGS84;

private final String _name;
private final double _a;
private final double _b;
private final double _f;

/**
* Create a new earth ellipsoid with the given parameters.
*
* @param name the name of the earth ellipsoid model
* @param a the equatorial radius, in meter
* @param b the polar radius, in meter
* @param f the inverse flattening
* @param A the equatorial radius, in meter
* @param B the polar radius, in meter
* @param F the inverse flattening
* @throws NullPointerException if the given {@code name} is {@code null}
*/
private Ellipsoid(
final String name,
final double a,
final double b,
final double f
) {
_name = requireNonNull(name);
_a = a;
_b = b;
_f = f;
}

/**
* Return the name of the earth ellipsoid model.
*
* @return the name of the earth ellipsoid model
*/
public String getName() {
return _name;
}

/**
* Return the equatorial radius, in meter.
*
* @return the equatorial radius, in meter
*/
public double A() {
return _a;
}

/**
* Return the polar radius, in meter.
*
* @return the polar radius, in meter
*/
public double B() {
return _b;
}

/**
* Return the inverse flattening.
*
* @return the inverse flattening
*/
public double F() {
return _f;
}

/**
* Create a new earth ellipsoid with the given parameters.
*
* @param name the name of the earth ellipsoid model
* @param a the equatorial radius, in meter
* @param b the polar radius, in meter
* @param f the inverse flattening
* @return a new earth ellipsoid with the given parameters
*/
public static Ellipsoid of(
final String name,
final double a,
final double b,
final double f
) {
return new Ellipsoid(name, a, b, f);
public Ellipsoid {
requireNonNull(name);
}

}
72 changes: 20 additions & 52 deletions jpx/src/main/java/io/jenetics/jpx/geom/Geoid.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static java.util.Objects.requireNonNull;
import static io.jenetics.jpx.geom.MathUtils.equal;

import java.io.Serializable;
import java.util.stream.Collector;

import io.jenetics.jpx.Length;
Expand All @@ -43,11 +44,13 @@
* @see <a href="https://en.wikipedia.org/wiki/Geoid">Wikipedia: Geoid</a>
* @see Ellipsoid
*
* @param ellipsoid the ellipsoid used by the geoid
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @version 1.0
* @version !__version__!
* @since 1.0
*/
public final class Geoid {
public record Geoid(Ellipsoid ellipsoid) implements Serializable {

private static final int EPSILON_ULP = 10_000;

Expand All @@ -57,38 +60,28 @@ public final class Geoid {
* @see <a href="https://en.wikipedia.org/wiki/World_Geodetic_System#A_new_World_Geodetic_System:_WGS_84">
* WGS-84</a>
*/
public static final Geoid WGS84 = of(Ellipsoid.WGS84);
public static final Geoid WGS84 = new Geoid(Ellipsoid.WGS84);

/**
* {@link Geoid} using the <em>International Earth Rotation and Reference
* Systems Service (1989)</em>
*
* @see <a href="https://en.wikipedia.org/wiki/IERS">IERS-89</a>
*/
public static final Geoid IERS_1989 = of(Ellipsoid.IERS_1989);
public static final Geoid IERS_1989 = new Geoid(Ellipsoid.IERS_1989);

/**
* {@link Geoid} using the <em>International Earth Rotation and Reference
* Systems Service (2003)</em>
*
* @see <a href="https://en.wikipedia.org/wiki/IERS">IERS-89</a>
*/
public static final Geoid IERS_2003 = of(Ellipsoid.IERS_2003);
public static final Geoid IERS_2003 = new Geoid(Ellipsoid.IERS_2003);

/**
* {@link Geoid} using the {@link Ellipsoid#DEFAULT} ellipsoid.
*/
public static final Geoid DEFAULT = of(Ellipsoid.DEFAULT);

private final Ellipsoid _ellipsoid;

// Minor semi-axes of the ellipsoid.
private final double B;

private final double AABBBB;

// Flattening (A - B)/A
private final double F;
public static final Geoid DEFAULT = new Geoid(Ellipsoid.DEFAULT);

// The maximal iteration of the 'distance'
private static final int DISTANCE_ITERATION_MAX = 1000;
Expand All @@ -102,26 +95,8 @@ public final class Geoid {
* @param ellipsoid the ellipsoid used by the geoid
* @throws NullPointerException if the given {@code ellipsoid} is {@code null}
*/
private Geoid(final Ellipsoid ellipsoid) {
_ellipsoid = requireNonNull(ellipsoid);

final double a = ellipsoid.A();
final double aa = a*a;

B = ellipsoid.B();
final double bb = B*B;

AABBBB = (aa - bb)/bb;
F = 1.0/ellipsoid.F();
}

/**
* Return the ellipsoid the {@code Geom} object is using.
*
* @return the ellipsoid the {@code Geom} object is using
*/
public Ellipsoid ellipsoid() {
return _ellipsoid;
public Geoid {
requireNonNull(ellipsoid);
}

/**
Expand All @@ -145,6 +120,14 @@ public Ellipsoid ellipsoid() {
* which is the case for a point and its (near) antidote.
*/
public Length distance(final Point start, final Point end) {
final double aa = ellipsoid.A()*ellipsoid.A();

final double B = ellipsoid.B();
final double bb = B*B;

final double AABB_BB = (aa - bb)/bb;
final double F = 1.0/ellipsoid.F();

final double lat1 = start.getLatitude().toRadians();
final double lon1 = start.getLongitude().toRadians();
final double lat2 = end.getLatitude().toRadians();
Expand Down Expand Up @@ -211,7 +194,7 @@ public Length distance(final Point start, final Point end) {
final double cos2sigmam = equal(cos2alpha, 0.0, EPSILON_ULP)
? 0.0
: cossigma - 2*sinU1sinU2/cos2alpha;
final double u2 = cos2alpha*AABBBB;
final double u2 = cos2alpha*AABB_BB;

final double cos2sigmam2 = cos2sigmam*cos2sigmam;

Expand Down Expand Up @@ -253,7 +236,6 @@ public Length distance(final Point start, final Point end) {
/**
* Return a collector which calculates the length of the (open) path which
* is defined by the {@code Point} stream.
*
* {@snippet lang="java":
* final Length length = gpx.tracks()
* .flatMap(Track::segments)
Expand Down Expand Up @@ -282,7 +264,6 @@ public Length distance(final Point start, final Point end) {
* Return a collector which calculates the length of the (closed) tour which
* is defined by the {@code Point} stream. The <em>tour</em> length
* additionally adds the distance of the last point back to the first point.
*
* {@snippet lang="java":
* final Length length = gpx.tracks()
* .flatMap(Track::segments)
Expand All @@ -307,17 +288,4 @@ public Length distance(final Point start, final Point end) {
);
}



/**
* Create a new {@code Geoid} object with the given ellipsoid.
*
* @param ellipsoid the ellipsoid used by the geoid
* @return a new {@code Geoid} object with the given ellipsoid
* @throws NullPointerException if the given {@code ellipsoid} is {@code null}
*/
public static Geoid of(final Ellipsoid ellipsoid) {
return new Geoid(ellipsoid);
}

}
2 changes: 1 addition & 1 deletion jpx/src/main/java/io/jenetics/jpx/geom/MathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static boolean equal(final double x, final double y, final int ulps) {
final long diffPositive = a - POSITIVE_ZERO_BITS;
final long diffNegative = b - NEGATIVE_ZERO_BITS;
equal = diffPositive <= ulps && diffNegative <= (ulps - diffPositive);
} else { // a and b have same sign.
} else { // a and b have the same sign.
equal = a - b <= ulps;
}

Expand Down

0 comments on commit 407040d

Please sign in to comment.