diff --git a/src/h3lib/include/constants.h b/src/h3lib/include/constants.h index db53adaab..fe84842a8 100644 --- a/src/h3lib/include/constants.h +++ b/src/h3lib/include/constants.h @@ -31,37 +31,43 @@ #endif /** 2.0 * PI */ -#define M_2PI 6.28318530717958647692528676655900576839433L +#define M_2PI 6.28318530717958647692528676655900576839433 /** pi / 180 */ -#define M_PI_180 0.0174532925199432957692369076848861271111L +#define M_PI_180 0.0174532925199432957692369076848861271111 /** 180 / pi */ -#define M_180_PI 57.29577951308232087679815481410517033240547L +#define M_180_PI 57.29577951308232087679815481410517033240547 /** threshold epsilon */ -#define EPSILON 0.0000000000000001L +#define EPSILON 0.0000000000000001 /** sqrt(3) / 2.0 */ -#define M_SQRT3_2 0.8660254037844386467637231707529361834714L +#define M_SQRT3_2 0.8660254037844386467637231707529361834714 /** sin(60') */ #define M_SIN60 M_SQRT3_2 +/** 1/sin(60') **/ +#define M_RSIN60 1.1547005383792515290182975610039149112953 + +/** one third **/ +#define M_ONETHIRD 0.333333333333333333333333333333333333333 /** rotation angle between Class II and Class III resolution axes * (asin(sqrt(3.0 / 28.0))) */ -#define M_AP7_ROT_RADS 0.333473172251832115336090755351601070065900389L +#define M_AP7_ROT_RADS 0.333473172251832115336090755351601070065900389 /** sin(M_AP7_ROT_RADS) */ -#define M_SIN_AP7_ROT 0.3273268353539885718950318L +#define M_SIN_AP7_ROT 0.3273268353539885718950318 /** cos(M_AP7_ROT_RADS) */ -#define M_COS_AP7_ROT 0.9449111825230680680167902L +#define M_COS_AP7_ROT 0.9449111825230680680167902 /** earth radius in kilometers using WGS84 authalic radius */ -#define EARTH_RADIUS_KM 6371.007180918475L +#define EARTH_RADIUS_KM 6371.007180918475 /** scaling factor from hex2d resolution 0 unit length * (or distance between adjacent cell center points * on the plane) to gnomonic unit length. */ -#define RES0_U_GNOMONIC 0.38196601125010500003L +#define RES0_U_GNOMONIC 0.38196601125010500003 +#define INV_RES0_U_GNOMONIC 2.61803398874989588842 /** max H3 resolution; H3 version 1 has 16 resolutions, numbered 0 through 15 */ #define MAX_H3_RES 15 diff --git a/src/h3lib/lib/coordijk.c b/src/h3lib/lib/coordijk.c index d4276001d..7f9ff7cb5 100644 --- a/src/h3lib/lib/coordijk.c +++ b/src/h3lib/lib/coordijk.c @@ -66,7 +66,7 @@ void _hex2dToCoordIJK(const Vec2d *v, CoordIJK *h) { a2 = fabsl(v->y); // first do a reverse conversion - x2 = a2 / M_SIN60; + x2 = a2 * M_RSIN60; x1 = a1 + x2 / 2.0; // check if we have the center of a hex diff --git a/src/h3lib/lib/faceijk.c b/src/h3lib/lib/faceijk.c index aee1d92a7..133b5f4bc 100644 --- a/src/h3lib/lib/faceijk.c +++ b/src/h3lib/lib/faceijk.c @@ -32,8 +32,9 @@ #include "latLng.h" #include "vec3d.h" -/** square root of 7 */ +/** square root of 7 and inverse square root of 7 */ #define M_SQRT7 2.6457513110645905905016157536392604257102 +#define M_RSQRT7 0.37796447300922722721451653623418006081576 /** @brief icosahedron face centers in lat/lng radians */ const LatLng faceCenterGeo[NUM_ICOSA_FACES] = { @@ -446,12 +447,12 @@ void _hex2dToGeo(const Vec2d *v, int face, int res, int substrate, LatLng *g) { double theta = atan2(v->y, v->x); // scale for current resolution length u - for (int i = 0; i < res; i++) r /= M_SQRT7; + for (int i = 0; i < res; i++) r *= M_RSQRT7; // scale accordingly if this is a substrate grid if (substrate) { - r /= 3.0; - if (isResolutionClassIII(res)) r /= M_SQRT7; + r *= M_ONETHIRD; + if (isResolutionClassIII(res)) r *= M_RSQRT7; } r *= RES0_U_GNOMONIC;