iou method

Implementation

double iou(PyTorchRect a, PyTorchRect b) {
  double areaA = (a.right - a.left) * (a.bottom - a.top);
  if (areaA <= 0.0) {
    return 0.0;
  }

  double areaB = (b.right - b.left) * (b.bottom - b.top);
  if (areaB <= 0.0) {
    return 0.0;
  }

  double intersectionMinX = max(a.left, b.left);
  double intersectionMinY = max(a.top, b.top);
  double intersectionMaxX = min(a.right, b.right);
  double intersectionMaxY = min(a.bottom, b.bottom);
  double intersectionArea = max(intersectionMaxY - intersectionMinY, 0.0) *
      max(intersectionMaxX - intersectionMinX, 0.0);
  return intersectionArea / (areaA + areaB - intersectionArea);
}