diff --git a/fastlib/u/garryb/nbr/allnn.cc b/fastlib/u/garryb/nbr/allnn.cc
index 36ed23981b..13daac6704 100644
--- a/fastlib/u/garryb/nbr/allnn.cc
+++ b/fastlib/u/garryb/nbr/allnn.cc
@@ -218,7 +218,7 @@ class Allnn {
*/
static double Heuristic(const Param& param,
const QNode& q_node, const RNode& r_node, const Delta& delta) {
- return q_node.bound().MidDistanceSq(r_node.bound());
+ return r_node.bound().MinToMidSq(q_node.bound());
}
};
};
diff --git a/fastlib/u/garryb/nbr/spbounds.h b/fastlib/u/garryb/nbr/spbounds.h
index 020aa7ac15..aeae062433 100644
--- a/fastlib/u/garryb/nbr/spbounds.h
+++ b/fastlib/u/garryb/nbr/spbounds.h
@@ -554,6 +554,40 @@ class SpHrectBound {
return math::Pow<2, t_pow>(sumsq) / 4;
}
+ /**
+ * Calculates closest-to-their-midpoint bounding box distance,
+ * i.e. calculates their midpoint and finds the minimum box-to-point
+ * distance.
+ *
+ * Equivalent to:
+ *
+ * other.CalcMidpoint(&other_midpoint)
+ * return MinDistanceSqToPoint(other_midpoint)
+ *
+ */
+ double MinToMidSq(const SpHrectBound& other) const {
+ double sumsq = 0;
+ const SpRange *a = this->bounds_;
+ const SpRange *b = other.bounds_;
+
+ DEBUG_ASSERT(dim_ == other.dim_);
+
+ for (index_t d = 0; d < dim_; d++) {
+ double v = b->mid();
+ double v1 = a->lo - v;
+ double v2 = v - a->hi;
+
+ v = (v1 + fabs(v1)) + (v2 + fabs(v2));
+
+ a++;
+ b++;
+
+ sumsq += math::Pow(v);
+ }
+
+ return math::Pow<2, t_pow>(sumsq) / 4;
+ }
+
/**
* Calculates maximum bound-to-point squared distance,
* to the specified power.