docs: @Reference(authors = "Erich Schubert, Arthur Zimek, Hans-Peter Kriegel", //
        title = "Geodetic Distance Queries on R-Trees for Indexing Geographic Data", //
        booktitle = "Int. Symp. Advances in Spatial and Temporal Databases (SSTD'2013)", //
        url = "https://doi.org/10.1007/978-3-642-40235-7_9", //
        bibkey = "DBLP:conf/ssd/SchubertZK13")
    public static double latlngMinDistRad(double plat, double plng, double rminlat, double rminlng, double rmaxlat, double rmaxlng) {
      // FIXME: add support for rectangles crossing the +-180 deg boundary!
  
      // Degenerate rectangles:
      if((rminlat >= rmaxlat) && (rminlng >= rmaxlng)) {
        return cosineOrHaversineRad(rminlat, rminlng, plat, plng);
      }
  
      // The simplest case is when the query point is in the same "slice":
      if(rminlng <= plng && plng <= rmaxlng) {
        return (rminlat <= plat && plat <= rmaxlat) ? 0 // Inside
            : (plat < rminlat) ? rminlat - plat : plat - rmaxlat; // S, N
      }
  
      // Determine whether going east or west is shorter.
      double lngE = rminlng - plng, lngW = plng - rmaxlng;
      // Ensure delta to be in 0 to 2pi.
      lngE = lngE >= 0 ? lngE : lngE + TWOPI;
      lngW = lngW >= 0 ? lngW : lngW + TWOPI;
  
      // Case distinction east or west:
      final double lngD = (lngE <= lngW) ? lngE : lngW;
      final double rlng = (lngE <= lngW) ? rminlng : rmaxlng;
  
      final DoubleWrapper tmp = new DoubleWrapper(); // To return cosine
      final double slngD = sinAndCos(lngD, tmp), clngD = tmp.value;
      final double tlatQ = tan(plat);
      if(lngD >= HALFPI) { // XTD disappears at 90°
        return cosineOrHaversineRad(plat, plng, //
            tlatQ >= tan((rmaxlat + rminlat) * .5) * clngD // N/S
                ? rmaxlat : rminlat, rlng);
      }
      if(tlatQ >= tan(rmaxlat) * clngD) { // North corner
        return cosineOrHaversineRad(plat, plng, rmaxlat, rlng);
      }
      if(tlatQ <= tan(rminlat) * clngD) { // South corner
        return cosineOrHaversineRad(plat, plng, rminlat, rlng);
      }
      // Cross-track-distance to longitude line.
      return asin(cos(plat) * slngD);
    }