/** * Return the maximum level such that the metric is at least the given * value, or zero if there is no such level. For example, * S2.kMinWidth.GetMaxLevel(0.1) returns the maximum level such that all * cells have a minimum width of 0.1 or larger. The return value is always a * valid level. */ public function getMaxLevel($value) { if ($value <= 0) { return S2CellId::MAX_LEVEL; } // This code is equivalent to computing a floating-point "level" // value and rounding down. $exponent = S2::exp((1 << $this->dim) * $this->deriv / $value); $level = max(0, min(S2CellId::MAX_LEVEL, $exponent - 1 >> $this->dim - 1)); // assert (level == 0 || getValue(level) >= value); // assert (level == S2CellId.MAX_LEVEL || getValue(level + 1) < value); return $level; }