Files
sign-detector/lib/domain/norm_rect.dart
T
2026-09-27 19:12:14 +03:00

54 lines
1.5 KiB
Dart

/// Axis-aligned box in normalized image space, origin at the top-left.
class NormRect {
const NormRect(this.left, this.top, this.width, this.height);
final double left;
final double top;
final double width;
final double height;
double get right => left + width;
double get bottom => top + height;
NormRect clamped() {
final l = left.clamp(0.0, 1.0);
final t = top.clamp(0.0, 1.0);
final r = right.clamp(0.0, 1.0);
final b = bottom.clamp(0.0, 1.0);
return NormRect(l, t, (r - l).clamp(0.0, 1.0), (b - t).clamp(0.0, 1.0));
}
NormRect lerp(NormRect other, double t) {
double mix(double a, double b) => a + (b - a) * t;
return NormRect(
mix(left, other.left),
mix(top, other.top),
mix(width, other.width),
mix(height, other.height),
);
}
/// Horizontal mirror used for the mirrored front-camera preview.
NormRect mirroredHorizontally() {
return NormRect(1 - right, top, width, height);
}
}
double rectIou(NormRect a, NormRect b) {
final interLeft = a.left > b.left ? a.left : b.left;
final interTop = a.top > b.top ? a.top : b.top;
final interRight = a.right < b.right ? a.right : b.right;
final interBottom = a.bottom < b.bottom ? a.bottom : b.bottom;
final w = interRight - interLeft;
final h = interBottom - interTop;
if (w <= 0 || h <= 0) {
return 0;
}
final inter = w * h;
final union = a.width * a.height + b.width * b.height - inter;
if (union <= 0) {
return 0;
}
return inter / union;
}