tryJumpThreshold function

int tryJumpThreshold(
  1. List<int> gaps,
  2. int maxThreshold, {
  3. int minGapCount = Band._minArtifactsForSpaceDetection,
  4. double minJumpRatio = Band._spaceGapJumpRatio,
  5. int midpointDivisor = Band._gapMidpointDivisor,
})

Returns a threshold when sorted gaps show a clear jump in spacing.

Finds the largest ratio jump in the sorted gaps where the resulting threshold does not exceed maxThreshold. This prevents multi-scale gap distributions from pushing the threshold too high.

Implementation

int tryJumpThreshold(
  List<int> gaps,
  int maxThreshold, {
  int minGapCount = Band._minArtifactsForSpaceDetection,
  double minJumpRatio = Band._spaceGapJumpRatio,
  int midpointDivisor = Band._gapMidpointDivisor,
}) {
  if (gaps.length < minGapCount) {
    return 0;
  }

  double bestRatio = 1.0;
  int bestIndex = -1;

  for (int i = 1; i < gaps.length; i++) {
    final int prev = gaps[i - 1];
    final int current = gaps[i];
    if (prev <= 0) {
      continue;
    }

    final double ratio = current / prev;
    if (ratio <= bestRatio) {
      continue;
    }

    final int candidate = ((prev + current) / midpointDivisor).round();
    if (candidate <= maxThreshold) {
      bestRatio = ratio;
      bestIndex = i;
    }
  }

  if (bestIndex < 0 || bestRatio < minJumpRatio) {
    return 0;
  }

  return ((gaps[bestIndex - 1] + gaps[bestIndex]) / midpointDivisor).round();
}