PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ box2df_distance_node_centroid()

static double box2df_distance_node_centroid ( const BOX2DF *  node,
const BOX2DF *  query 
)
static

Calculate the The node_box_edge->query_centroid distance between the boxes.

Definition at line 417 of file gserialized_gist_2d.c.

References box2df_overlaps(), and LW_TRUE.

Referenced by gserialized_gist_distance_2d().

418 {
419  BOX2DF q;
420  double qx, qy;
421  double d = 0.0;
422 
423  /* Turn query into point */
424  q.xmin = q.xmax = (query->xmin + query->xmax) / 2.0;
425  q.ymin = q.ymax = (query->ymin + query->ymax) / 2.0;
426  qx = q.xmin;
427  qy = q.ymin;
428 
429  /* Check for overlap */
430  if ( box2df_overlaps(node, &q) == LW_TRUE )
431  return 0.0;
432 
433  /* Above or below */
434  if ( qx >= node->xmin && qx <= node->xmax )
435  {
436  if( qy > node->ymax )
437  d = qy - node->ymax;
438  else if ( qy < node->ymin )
439  d = node->ymin - qy;
440  return d;
441  }
442  /* Left or right */
443  else if ( qy >= node->ymin && qy <= node->ymax )
444  {
445  if ( qx > node->xmax )
446  d = qx - node->xmax;
447  else if ( qx < node->xmin )
448  d = node->xmin - qx;
449  return d;
450  }
451  /* Corner quadrants */
452  else
453  {
454  /* below/left of xmin/ymin */
455  if ( qx < node->xmin && qy < node->ymin )
456  {
457  d = (node->xmin - qx) * (node->xmin - qx) +
458  (node->ymin - qy) * (node->ymin - qy);
459  }
460  /* above/left of xmin/ymax */
461  else if ( qx < node->xmin && qy > node->ymax )
462  {
463  d = (node->xmin - qx) * (node->xmin - qx) +
464  (node->ymax - qy) * (node->ymax - qy);
465  }
466  /* above/right of xmax/ymax */
467  else if ( qx > node->xmax && qy > node->ymax )
468  {
469  d = (node->xmax - qx) * (node->xmax - qx) +
470  (node->ymax - qy) * (node->ymax - qy);
471  }
472  /* below/right of xmax/ymin */
473  else if ( qx > node->xmin && qy < node->ymin )
474  {
475  d = (node->xmax - qx) * (node->xmax - qx) +
476  (node->ymin - qy) * (node->ymin - qy);
477  }
478  else
479  {
480  /*ERROR*/
481  elog(ERROR, "%s: reached unreachable code", __func__);
482  }
483  }
484 
485  return sqrt(d);
486 }
static bool box2df_overlaps(const BOX2DF *a, const BOX2DF *b)
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
Here is the call graph for this function:
Here is the caller graph for this function: