samplingErrorEstimate function

double samplingErrorEstimate(
  1. List<CubicPath> paths,
  2. int sampleCount
)

Estimates normalized geometric error left by a fixed sample budget. The value is a conservative control-polygon excess divided by the square of the sample count, so diagnostics can explain adaptive decisions.

Implementation

double samplingErrorEstimate(List<CubicPath> paths, int sampleCount) {
  if (sampleCount < 1) {
    throw MeldException(
        'sample-count-too-small', 'Sample count must be positive.');
  }
  var maximum = 0.0;
  for (final path in paths) {
    for (var segment = 0; segment < path.segmentCount; segment++) {
      final index = segment * 6;
      final chord = math.sqrt(
        math.pow(path.points[index + 6] - path.points[index], 2) +
            math.pow(path.points[index + 7] - path.points[index + 1], 2),
      );
      if (chord > 1e-9) {
        maximum = math.max(
          maximum,
          (_controlPolygonLength(path.points, index) / chord - 1).abs(),
        );
      }
    }
  }
  return maximum / (sampleCount * sampleCount);
}