PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ _lwt_FindFaceContainingRing()

static LWT_ELEMID _lwt_FindFaceContainingRing ( LWT_TOPOLOGY topo,
LWT_EDGERING ring,
LWT_EDGERING_ARRAY shells 
)
static

Definition at line 6676 of file lwgeom_topo.c.

6678 {
6679  LWT_ELEMID foundInFace = -1;
6680  int i;
6681  const GBOX *minenv = NULL;
6682  POINT2D pt;
6683  const GBOX *testbox;
6684  GEOSGeometry *ghole;
6685 
6686  getPoint2d_p( ring->elems[0]->edge->geom->points, 0, &pt );
6687 
6688  testbox = _lwt_EdgeRingGetBbox(ring);
6689 
6690  /* Create a GEOS Point from a vertex of the hole ring */
6691  {
6692  LWPOINT *point = lwpoint_make2d(topo->srid, pt.x, pt.y);
6693  ghole = LWGEOM2GEOS( lwpoint_as_lwgeom(point), 1 );
6694  lwpoint_free(point);
6695  if ( ! ghole ) {
6696  lwerror("Could not convert edge geometry to GEOS: %s", lwgeom_geos_errmsg);
6697  return -1;
6698  }
6699  }
6700 
6701  /* Build STRtree of shell envelopes */
6702  if ( ! shells->tree )
6703  {
6704  static const int STRTREE_NODE_CAPACITY = 10;
6705  LWDEBUG(1, "Building STRtree");
6706  shells->tree = GEOSSTRtree_create(STRTREE_NODE_CAPACITY);
6707  if (shells->tree == NULL)
6708  {
6709  lwerror("Could not create GEOS STRTree: %s", lwgeom_geos_errmsg);
6710  return -1;
6711  }
6712  for (i=0; i<shells->size; ++i)
6713  {
6714  LWT_EDGERING *sring = shells->rings[i];
6715  const GBOX* shellbox = _lwt_EdgeRingGetBbox(sring);
6716  LWDEBUGF(2, "GBOX of shell %p for edge %d is %g %g,%g %g",
6717  sring, sring->elems[0]->edge->edge_id, shellbox->xmin,
6718  shellbox->ymin, shellbox->xmax, shellbox->ymax);
6719  POINTARRAY *pa = ptarray_construct(0, 0, 2);
6720  POINT4D pt;
6721  LWLINE *diag;
6722  pt.x = shellbox->xmin;
6723  pt.y = shellbox->ymin;
6724  ptarray_set_point4d(pa, 0, &pt);
6725  pt.x = shellbox->xmax;
6726  pt.y = shellbox->ymax;
6727  ptarray_set_point4d(pa, 1, &pt);
6728  diag = lwline_construct(topo->srid, NULL, pa);
6729  /* Record just envelope in ggeom */
6730  /* making valid, probably not needed */
6731  sring->genv = LWGEOM2GEOS( lwline_as_lwgeom(diag), 1 );
6732  lwline_free(diag);
6733  GEOSSTRtree_insert(shells->tree, sring->genv, sring);
6734  }
6735  LWDEBUG(1, "STRtree build completed");
6736  }
6737 
6738  LWT_EDGERING_ARRAY candidates;
6739  LWT_EDGERING_ARRAY_INIT(&candidates);
6740  GEOSSTRtree_query(shells->tree, ghole, &_lwt_AccumulateCanditates, &candidates);
6741  LWDEBUGF(1, "Found %d candidate shells containing first point of ring's originating edge %d",
6742  candidates.size, ring->elems[0]->edge->edge_id * ( ring->elems[0]->left ? 1 : -1 ) );
6743 
6744  /* TODO: sort candidates by bounding box size */
6745 
6746  for (i=0; i<candidates.size; ++i)
6747  {
6748  LWT_EDGERING *sring = candidates.rings[i];
6749  const GBOX* shellbox = _lwt_EdgeRingGetBbox(sring);
6750  int contains = 0;
6751 
6752  if ( sring->elems[0]->edge->edge_id == ring->elems[0]->edge->edge_id )
6753  {
6754  LWDEBUGF(1, "Shell %d is on other side of ring",
6755  _lwt_EdgeRingGetFace(sring));
6756  continue;
6757  }
6758 
6759  /* The hole envelope cannot equal the shell envelope */
6760  if ( gbox_same(shellbox, testbox) )
6761  {
6762  LWDEBUGF(1, "Bbox of shell %d equals that of hole ring",
6763  _lwt_EdgeRingGetFace(sring));
6764  continue;
6765  }
6766 
6767  /* Skip if ring box is not in shell box */
6768  if ( ! gbox_contains_2d(shellbox, testbox) )
6769  {
6770  LWDEBUGF(1, "Bbox of shell %d does not contain bbox of ring point",
6771  _lwt_EdgeRingGetFace(sring));
6772  continue;
6773  }
6774 
6775  /* Skip test if a containing shell was already found
6776  * and this shell's bbox is not contained in the other */
6777  if ( minenv && ! gbox_contains_2d(minenv, shellbox) )
6778  {
6779  LWDEBUGF(2, "Bbox of shell %d (face %d) not contained by bbox "
6780  "of last shell found to contain the point",
6781  i, _lwt_EdgeRingGetFace(sring));
6782  continue;
6783  }
6784 
6785  contains = _lwt_EdgeRingContainsPoint(sring, &pt);
6786  if ( contains )
6787  {
6788  /* Continue until all shells are tested, as we want to
6789  * use the one with the smallest bounding box */
6790  /* IDEA: sort shells by bbox size, stopping on first match */
6791  LWDEBUGF(1, "Shell %d contains hole of edge %d",
6792  _lwt_EdgeRingGetFace(sring),
6793  ring->elems[0]->edge->edge_id);
6794  minenv = shellbox;
6795  foundInFace = _lwt_EdgeRingGetFace(sring);
6796  }
6797  }
6798  if ( foundInFace == -1 ) foundInFace = 0;
6799 
6800  candidates.size = 0; /* Avoid destroying the actual shell rings */
6801  LWT_EDGERING_ARRAY_CLEAN(&candidates);
6802 
6803  GEOSGeom_destroy(ghole);
6804 
6805  return foundInFace;
6806 }
int gbox_same(const GBOX *g1, const GBOX *g2)
Check if 2 given Gbox are the same.
Definition: gbox.c:164
int gbox_contains_2d(const GBOX *g1, const GBOX *g2)
Return LW_TRUE if the first GBOX contains the second on the 2d plane, LW_FALSE otherwise.
Definition: gbox.c:339
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:343
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:51
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:327
void ptarray_set_point4d(POINTARRAY *pa, uint32_t n, const POINT4D *p4d)
Definition: lwgeom_api.c:370
void lwline_free(LWLINE *line)
Definition: lwline.c:67
LWT_INT64 LWT_ELEMID
Identifier of topology element.
static const int STRTREE_NODE_CAPACITY
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static int _lwt_EdgeRingContainsPoint(LWT_EDGERING *ring, POINT2D *p)
Definition: lwgeom_topo.c:6536
static LWT_ELEMID _lwt_EdgeRingGetFace(LWT_EDGERING *ring)
Definition: lwgeom_topo.c:6568
static GBOX * _lwt_EdgeRingGetBbox(LWT_EDGERING *ring)
Definition: lwgeom_topo.c:6547
#define LWT_EDGERING_ARRAY_INIT(a)
Definition: lwgeom_topo.c:6168
static void _lwt_AccumulateCanditates(void *item, void *userdata)
Definition: lwgeom_topo.c:6668
#define LWT_EDGERING_ARRAY_CLEAN(a)
Definition: lwgeom_topo.c:6177
Datum contains(PG_FUNCTION_ARGS)
double ymax
Definition: liblwgeom.h:371
double xmax
Definition: liblwgeom.h:369
double ymin
Definition: liblwgeom.h:370
double xmin
Definition: liblwgeom.h:368
POINTARRAY * points
Definition: liblwgeom.h:497
GEOSSTRtree * tree
Definition: lwgeom_topo.c:6165
LWT_EDGERING ** rings
Definition: lwgeom_topo.c:6162
LWT_ISO_EDGE * edge
Definition: lwgeom_topo.c:6109
GEOSGeometry * genv
Definition: lwgeom_topo.c:6126
LWT_EDGERING_ELEM ** elems
Definition: lwgeom_topo.c:6119
LWLINE * geom
LWT_ELEMID edge_id
double y
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:428
double y
Definition: liblwgeom.h:428

References _lwt_AccumulateCanditates(), _lwt_EdgeRingContainsPoint(), _lwt_EdgeRingGetBbox(), _lwt_EdgeRingGetFace(), contains(), LWT_EDGERING_ELEM_T::edge, LWT_ISO_EDGE::edge_id, LWT_EDGERING_T::elems, gbox_contains_2d(), gbox_same(), LWT_EDGERING_T::genv, LWT_ISO_EDGE::geom, getPoint2d_p(), LWT_EDGERING_ELEM_T::left, LWDEBUG, LWDEBUGF, lwerror(), LWGEOM2GEOS(), lwgeom_geos_errmsg, lwline_as_lwgeom(), lwline_construct(), lwline_free(), lwpoint_as_lwgeom(), lwpoint_free(), lwpoint_make2d(), LWT_EDGERING_ARRAY_CLEAN, LWT_EDGERING_ARRAY_INIT, LWLINE::points, ptarray_construct(), ptarray_set_point4d(), LWT_EDGERING_ARRAY_T::rings, LWT_EDGERING_ARRAY_T::size, LWT_TOPOLOGY_T::srid, STRTREE_NODE_CAPACITY, LWT_EDGERING_ARRAY_T::tree, POINT2D::x, POINT4D::x, GBOX::xmax, GBOX::xmin, POINT2D::y, POINT4D::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwt_Polygonize().

Here is the call graph for this function:
Here is the caller graph for this function: