Skip to content

Commit

Permalink
convert some H3 classes to records (#113574)
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase committed Sep 30, 2024
1 parent 3a04f07 commit 71c252c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 75 deletions.
43 changes: 12 additions & 31 deletions libs/h3/src/main/java/org/elasticsearch/h3/BaseCells.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,14 @@
*/
final class BaseCells {

private static class BaseCellData {
// "home" face and normalized ijk coordinates on that face
final int homeFace;
final int homeI;
final int homeJ;
final int homeK;
// is this base cell a pentagon?
final boolean isPentagon;
// if a pentagon, what are its two clockwise offset
final int[] cwOffsetPent;

/// faces?
BaseCellData(int homeFace, int homeI, int homeJ, int homeK, boolean isPentagon, int[] cwOffsetPent) {
this.homeFace = homeFace;
this.homeI = homeI;
this.homeJ = homeJ;
this.homeK = homeK;
this.isPentagon = isPentagon;
this.cwOffsetPent = cwOffsetPent;
}
}
private record BaseCellData(
int homeFace, // "home" face and normalized ijk coordinates on that face
int homeI,
int homeJ,
int homeK,
boolean isPentagon, // is this base cell a pentagon?
int[] cwOffsetPent // if a pentagon, what are its two clockwise offset
) {}

/**
* Resolution 0 base cell data table.
Expand Down Expand Up @@ -185,16 +172,10 @@ private static class BaseCellData {
/**
* base cell at a given ijk and required rotations into its system
*/
private static class BaseCellRotation {
final int baseCell; // base cell number
final int ccwRot60; // number of ccw 60 degree rotations relative to current
/// face

BaseCellRotation(int baseCell, int ccwRot60) {
this.baseCell = baseCell;
this.ccwRot60 = ccwRot60;
}
}
record BaseCellRotation(
int baseCell, // base cell number
int ccwRot60 // number of ccw 60 degree rotations relative to current
) {}

/** @brief Resolution 0 base cell lookup table for each face.
*
Expand Down
26 changes: 7 additions & 19 deletions libs/h3/src/main/java/org/elasticsearch/h3/FaceIJK.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,13 @@ enum Overage {
/**
* Information to transform into an adjacent face IJK system
*/
private static class FaceOrientIJK {
// face number
final int face;
// res 0 translation relative to primary face
final int translateI;
final int translateJ;
final int translateK;
// number of 60 degree ccw rotations relative to primary
final int ccwRot60;

// face
FaceOrientIJK(int face, int translateI, int translateJ, int translateK, int ccwRot60) {
this.face = face;
this.translateI = translateI;
this.translateJ = translateJ;
this.translateK = translateK;
this.ccwRot60 = ccwRot60;
}
}
private record FaceOrientIJK(
int face, // face number
int translateI, // res 0 translation relative to primary face
int translateJ,
int translateK,
int ccwRot60// number of 60 degree ccw rotations relative to primary
) {}

/**
* Definition of which faces neighbor each other.
Expand Down
4 changes: 3 additions & 1 deletion libs/h3/src/main/java/org/elasticsearch/h3/H3Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,9 @@ public static long h3RotatePent60ccw(long h) {
foundFirstNonZeroDigit = true;

// adjust for deleted k-axes sequence
if (h3LeadingNonZeroDigit(h) == CoordIJK.Direction.K_AXES_DIGIT.digit()) h = h3Rotate60ccw(h);
if (h3LeadingNonZeroDigit(h) == CoordIJK.Direction.K_AXES_DIGIT.digit()) {
h = h3Rotate60ccw(h);
}
}
}
return h;
Expand Down
5 changes: 0 additions & 5 deletions libs/h3/src/main/java/org/elasticsearch/h3/HexRing.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,6 @@ final class HexRing {
{ 0, 0, 1, 0, 1, 5, 1 }, // base cell 121
};

private static final int E_SUCCESS = 0; // Success (no error)
private static final int E_PENTAGON = 9; // Pentagon distortion was encountered which the algorithm
private static final int E_CELL_INVALID = 5; // `H3Index` cell argument was not valid
private static final int E_FAILED = 1; // The operation failed but a more specific error is not available

/**
* Directions used for traversing a hexagonal ring counterclockwise around
* {1, 0, 0}
Expand Down
13 changes: 4 additions & 9 deletions libs/h3/src/main/java/org/elasticsearch/h3/Vec2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
/**
* 2D floating-point vector
*/
final class Vec2d {
record Vec2d(
double x, // x component
double y // y component
) {

/** 1/sin(60') **/
private static final double M_RSIN60 = 1.0 / Constants.M_SQRT3_2;
Expand Down Expand Up @@ -90,14 +93,6 @@ final class Vec2d {
{ 2.361378999196363184, 0.266983896803167583, 4.455774101589558636 }, // face 19
};

private final double x; /// < x component
private final double y; /// < y component

Vec2d(double x, double y) {
this.x = x;
this.y = y;
}

/**
* Determines the center point in spherical coordinates of a cell given by this 2D
* hex coordinates on a particular icosahedral face.
Expand Down
11 changes: 1 addition & 10 deletions libs/h3/src/main/java/org/elasticsearch/h3/Vec3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* 3D floating-point vector
*/
final class Vec3d {
record Vec3d(double x, double y, double z) {

/** icosahedron face centers in x/y/z on the unit sphere */
public static final Vec3d[] faceCenterPoint = new Vec3d[] {
Expand All @@ -52,14 +52,6 @@ final class Vec3d {
new Vec3d(-0.1092625278784796, 0.4811951572873210, -0.8697775121287253) // face 19
};

private final double x, y, z;

private Vec3d(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}

/**
* Calculate the square of the distance between two 3D coordinates.
*
Expand Down Expand Up @@ -238,5 +230,4 @@ private static double dotProduct(double x1, double y1, double z1, double x2, dou
private static double magnitude(double x, double y, double z) {
return Math.sqrt(square(x) + square(y) + square(z));
}

}

0 comments on commit 71c252c

Please sign in to comment.