summaryrefslogtreecommitdiffstats
path: root/searchlib
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-03-24 12:19:44 +0000
committerArne Juul <arnej@verizonmedia.com>2020-03-24 12:21:13 +0000
commit6e725e88b10d424ff5f96ba702124033fdc86733 (patch)
tree167f28f7fa5bd1ff9aee0cf85eb1c64775a4a0c5 /searchlib
parent2ca2401c18a7f3a6860c85ac133493867847f791 (diff)
add reference and refactor
Diffstat (limited to 'searchlib')
-rw-r--r--searchlib/src/vespa/searchlib/tensor/distance_functions.h17
1 files changed, 10 insertions, 7 deletions
diff --git a/searchlib/src/vespa/searchlib/tensor/distance_functions.h b/searchlib/src/vespa/searchlib/tensor/distance_functions.h
index 973d296c6d5..79f987c740c 100644
--- a/searchlib/src/vespa/searchlib/tensor/distance_functions.h
+++ b/searchlib/src/vespa/searchlib/tensor/distance_functions.h
@@ -89,12 +89,19 @@ template class AngularDistance<double>;
/**
* Calculates great-circle distance between Latitude/Longitude pairs,
- * measured in degrees
+ * measured in degrees. Output distance is measured in meters.
+ * Uses the haversine formula directly from:
+ * https://en.wikipedia.org/wiki/Haversine_formula
**/
template <typename FloatType>
class GeoDegreesDistance : public DistanceFunction {
public:
GeoDegreesDistance() {}
+ // haversine function:
+ static double hav(double angle) {
+ double s = sin(0.5*angle);
+ return s*s;
+ }
double calc(const vespalib::tensor::TypedCells& lhs, const vespalib::tensor::TypedCells& rhs) const override {
auto lhs_vector = lhs.typify<FloatType>();
auto rhs_vector = rhs.typify<FloatType>();
@@ -109,13 +116,9 @@ public:
double lat_diff = lat_A - lat_B;
double lon_diff = lon_A - lon_B;
- // sines of half of differences:
- double sin_half_lat = sin(0.5 * lat_diff);
- double sin_half_lon = sin(0.5 * lon_diff);
-
// haversines of differences:
- double hav_lat = sin_half_lat*sin_half_lat;
- double hav_lon = sin_half_lon*sin_half_lon;
+ double hav_lat = hav(lat_diff);
+ double hav_lon = hav(lon_diff);
// haversine of central angle between the two points:
double hav_central_angle = hav_lat + cos(lat_A)*cos(lat_B)*hav_lon;