PostGIS  3.2.2dev-r@@SVN_REVISION@@

◆ _lwt_FindAdjacentEdges()

static int _lwt_FindAdjacentEdges ( LWT_TOPOLOGY topo,
LWT_ELEMID  node,
edgeend data,
edgeend other,
int  myedge_id 
)
static

Definition at line 1530 of file lwgeom_topo.c.

1532 {
1533  LWT_ISO_EDGE *edges;
1534  uint64_t numedges = 1;
1535  uint64_t i;
1536  double minaz, maxaz;
1537  double az, azdif;
1538 
1539  data->nextCW = data->nextCCW = 0;
1540  data->cwFace = data->ccwFace = -1;
1541 
1542  if ( other ) {
1543  azdif = other->myaz - data->myaz;
1544  if ( azdif < 0 ) azdif += 2 * M_PI;
1545  minaz = maxaz = azdif;
1546  /* TODO: set nextCW/nextCCW/cwFace/ccwFace to other->something ? */
1547  LWDEBUGF(1, "Other edge end has cwFace=%d and ccwFace=%d",
1548  other->cwFace, other->ccwFace);
1549  } else {
1550  minaz = maxaz = -1;
1551  }
1552 
1553  LWDEBUGF(1, "Looking for edges incident to node %" LWTFMT_ELEMID
1554  " and adjacent to azimuth %g", node, data->myaz);
1555 
1556  /* Get incident edges */
1557  edges = lwt_be_getEdgeByNode( topo, &node, &numedges, LWT_COL_EDGE_ALL );
1558  if (numedges == UINT64_MAX)
1559  {
1560  lwerror("Backend error: %s", lwt_be_lastErrorMessage(topo->be_iface));
1561  return 0;
1562  }
1563 
1564  LWDEBUGF(1, "getEdgeByNode returned %d edges, minaz=%g, maxaz=%g",
1565  numedges, minaz, maxaz);
1566 
1567  /* For each incident edge-end (1 or 2): */
1568  for ( i = 0; i < numedges; ++i )
1569  {
1570  LWT_ISO_EDGE *edge;
1571  LWGEOM *g;
1572  LWGEOM *cleangeom;
1573  POINT2D p1, p2;
1574  POINTARRAY *pa;
1575 
1576  edge = &(edges[i]);
1577 
1578  if ( edge->edge_id == myedge_id ) continue;
1579 
1580  g = lwline_as_lwgeom(edge->geom);
1581  /* NOTE: remove_repeated_points call could be replaced by
1582  * some other mean to pick two distinct points for endpoints */
1583  cleangeom = lwgeom_remove_repeated_points( g, 0 );
1584  pa = lwgeom_as_lwline(cleangeom)->points;
1585 
1586  if ( pa->npoints < 2 ) {{
1587  LWT_ELEMID id = edge->edge_id;
1588  _lwt_release_edges(edges, numedges);
1589  lwgeom_free(cleangeom);
1590  lwerror("corrupted topology: edge %" LWTFMT_ELEMID
1591  " does not have two distinct points", id);
1592  return -1;
1593  }}
1594 
1595  if ( edge->start_node == node ) {
1596  getPoint2d_p(pa, 0, &p1);
1597  if ( ! _lwt_FirstDistinctVertex2D(pa, &p1, 0, 1, &p2) )
1598  {
1599  lwerror("Edge %d has no distinct vertices: [%.15g %.15g,%.15g %.15g]: ",
1600  edge->edge_id, p1.x, p1.y, p2.x, p2.y);
1601  return -1;
1602  }
1603  LWDEBUGF(1, "edge %" LWTFMT_ELEMID
1604  " starts on node %" LWTFMT_ELEMID
1605  ", edgeend is %g,%g-%g,%g",
1606  edge->edge_id, node, p1.x, p1.y, p2.x, p2.y);
1607  if ( ! azimuth_pt_pt(&p1, &p2, &az) ) {{
1608  LWT_ELEMID id = edge->edge_id;
1609  _lwt_release_edges(edges, numedges);
1610  lwgeom_free(cleangeom);
1611  lwerror("error computing azimuth of edge %d first edgeend [%.15g %.15g,%.15g %.15g]",
1612  id, p1.x, p1.y, p2.x, p2.y);
1613  return -1;
1614  }}
1615  azdif = az - data->myaz;
1616  LWDEBUGF(1, "azimuth of edge %" LWTFMT_ELEMID
1617  ": %g (diff: %g)", edge->edge_id, az, azdif);
1618 
1619  if ( azdif < 0 ) azdif += 2 * M_PI;
1620  if ( minaz == -1 ) {
1621  minaz = maxaz = azdif;
1622  data->nextCW = data->nextCCW = edge->edge_id; /* outgoing */
1623  data->cwFace = edge->face_left;
1624  data->ccwFace = edge->face_right;
1625  LWDEBUGF(1, "new nextCW and nextCCW edge is %" LWTFMT_ELEMID
1626  ", outgoing, "
1627  "with face_left %" LWTFMT_ELEMID " and face_right %" LWTFMT_ELEMID
1628  " (face_right is new ccwFace, face_left is new cwFace)",
1629  edge->edge_id, edge->face_left,
1630  edge->face_right);
1631  } else {
1632  if ( azdif < minaz ) {
1633  data->nextCW = edge->edge_id; /* outgoing */
1634  data->cwFace = edge->face_left;
1635  LWDEBUGF(1, "new nextCW edge is %" LWTFMT_ELEMID
1636  ", outgoing, "
1637  "with face_left %" LWTFMT_ELEMID " and face_right %" LWTFMT_ELEMID
1638  " (previous had minaz=%g, face_left is new cwFace)",
1639  edge->edge_id, edge->face_left,
1640  edge->face_right, minaz);
1641  minaz = azdif;
1642  }
1643  else if ( azdif > maxaz ) {
1644  data->nextCCW = edge->edge_id; /* outgoing */
1645  data->ccwFace = edge->face_right;
1646  LWDEBUGF(1, "new nextCCW edge is %" LWTFMT_ELEMID
1647  ", outgoing, "
1648  "with face_left %" LWTFMT_ELEMID " and face_right %" LWTFMT_ELEMID
1649  " (previous had maxaz=%g, face_right is new ccwFace)",
1650  edge->edge_id, edge->face_left,
1651  edge->face_right, maxaz);
1652  maxaz = azdif;
1653  }
1654  }
1655  }
1656 
1657  if ( edge->end_node == node ) {
1658  getPoint2d_p(pa, pa->npoints-1, &p1);
1659  if ( ! _lwt_FirstDistinctVertex2D(pa, &p1, pa->npoints-1, -1, &p2) )
1660  {
1661  lwerror("Edge %d has no distinct vertices: [%.15g %.15g,%.15g %.15g]: ",
1662  edge->edge_id, p1.x, p1.y, p2.x, p2.y);
1663  return -1;
1664  }
1665  LWDEBUGF(1, "edge %" LWTFMT_ELEMID " ends on node %" LWTFMT_ELEMID
1666  ", edgeend is %g,%g-%g,%g",
1667  edge->edge_id, node, p1.x, p1.y, p2.x, p2.y);
1668  if ( ! azimuth_pt_pt(&p1, &p2, &az) ) {{
1669  LWT_ELEMID id = edge->edge_id;
1670  _lwt_release_edges(edges, numedges);
1671  lwgeom_free(cleangeom);
1672  lwerror("error computing azimuth of edge %d last edgeend [%.15g %.15g,%.15g %.15g]",
1673  id, p1.x, p1.y, p2.x, p2.y);
1674  return -1;
1675  }}
1676  azdif = az - data->myaz;
1677  LWDEBUGF(1, "azimuth of edge %" LWTFMT_ELEMID
1678  ": %g (diff: %g)", edge->edge_id, az, azdif);
1679  if ( azdif < 0 ) azdif += 2 * M_PI;
1680  if ( minaz == -1 ) {
1681  minaz = maxaz = azdif;
1682  data->nextCW = data->nextCCW = -edge->edge_id; /* incoming */
1683  data->cwFace = edge->face_right;
1684  data->ccwFace = edge->face_left;
1685  LWDEBUGF(1, "new nextCW and nextCCW edge is %" LWTFMT_ELEMID
1686  ", incoming, "
1687  "with face_left %" LWTFMT_ELEMID " and face_right %" LWTFMT_ELEMID
1688  " (face_right is new cwFace, face_left is new ccwFace)",
1689  edge->edge_id, edge->face_left,
1690  edge->face_right);
1691  } else {
1692  if ( azdif < minaz ) {
1693  data->nextCW = -edge->edge_id; /* incoming */
1694  data->cwFace = edge->face_right;
1695  LWDEBUGF(1, "new nextCW edge is %" LWTFMT_ELEMID
1696  ", incoming, "
1697  "with face_left %" LWTFMT_ELEMID " and face_right %" LWTFMT_ELEMID
1698  " (previous had minaz=%g, face_right is new cwFace)",
1699  edge->edge_id, edge->face_left,
1700  edge->face_right, minaz);
1701  minaz = azdif;
1702  }
1703  else if ( azdif > maxaz ) {
1704  data->nextCCW = -edge->edge_id; /* incoming */
1705  data->ccwFace = edge->face_left;
1706  LWDEBUGF(1, "new nextCCW edge is %" LWTFMT_ELEMID
1707  ", outgoing, from start point, "
1708  "with face_left %" LWTFMT_ELEMID " and face_right %" LWTFMT_ELEMID
1709  " (previous had maxaz=%g, face_left is new ccwFace)",
1710  edge->edge_id, edge->face_left,
1711  edge->face_right, maxaz);
1712  maxaz = azdif;
1713  }
1714  }
1715  }
1716 
1717  lwgeom_free(cleangeom);
1718  }
1719  if ( numedges ) _lwt_release_edges(edges, numedges);
1720 
1721  LWDEBUGF(1, "edges adjacent to azimuth %g"
1722  " (incident to node %" LWTFMT_ELEMID ")"
1723  ": CW:%" LWTFMT_ELEMID "(%g) CCW:%" LWTFMT_ELEMID "(%g)",
1724  data->myaz, node, data->nextCW, minaz,
1725  data->nextCCW, maxaz);
1726 
1727  if ( myedge_id < 1 && numedges && data->cwFace != data->ccwFace )
1728  {
1729  if ( data->cwFace != -1 && data->ccwFace != -1 ) {
1730  lwerror("Corrupted topology: adjacent edges %" LWTFMT_ELEMID " and %" LWTFMT_ELEMID
1731  " bind different face (%" LWTFMT_ELEMID " and %" LWTFMT_ELEMID ")",
1732  data->nextCW, data->nextCCW,
1733  data->cwFace, data->ccwFace);
1734  return -1;
1735  }
1736  }
1737 
1738  /* Return number of incident edges found */
1739  return numedges;
1740 }
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:162
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:322
int azimuth_pt_pt(const POINT2D *p1, const POINT2D *p2, double *ret)
Compute the azimuth of segment AB in radians.
Definition: measures.c:2461
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:343
LWGEOM * lwgeom_remove_repeated_points(const LWGEOM *in, double tolerance)
Definition: lwgeom.c:1454
LWT_INT64 LWT_ELEMID
Identifier of topology element.
#define LWT_COL_EDGE_ALL
#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
const char * lwt_be_lastErrorMessage(const LWT_BE_IFACE *be)
Definition: lwgeom_topo.c:119
static LWT_ISO_EDGE * lwt_be_getEdgeByNode(LWT_TOPOLOGY *topo, const LWT_ELEMID *ids, uint64_t *numelems, int fields)
Definition: lwgeom_topo.c:232
static int _lwt_FirstDistinctVertex2D(const POINTARRAY *pa, POINT2D *ref, int from, int dir, POINT2D *op)
Definition: lwgeom_topo.c:1433
#define LWTFMT_ELEMID
Definition: lwgeom_topo.c:43
static void _lwt_release_edges(LWT_ISO_EDGE *edges, int num_edges)
Definition: lwgeom_topo.c:459
data
Definition: ovdump.py:104
POINTARRAY * points
Definition: liblwgeom.h:497
LWT_ELEMID face_right
LWT_ELEMID end_node
LWT_ELEMID face_left
LWLINE * geom
LWT_ELEMID edge_id
LWT_ELEMID start_node
const LWT_BE_IFACE * be_iface
double y
Definition: liblwgeom.h:404
double x
Definition: liblwgeom.h:404
uint32_t npoints
Definition: liblwgeom.h:441
double myaz
Definition: lwgeom_topo.c:1420
LWT_ELEMID ccwFace
Definition: lwgeom_topo.c:1418
LWT_ELEMID cwFace
Definition: lwgeom_topo.c:1414

References _lwt_FirstDistinctVertex2D(), _lwt_release_edges(), azimuth_pt_pt(), LWT_TOPOLOGY_T::be_iface, edgeend_t::ccwFace, edgeend_t::cwFace, ovdump::data, LWT_ISO_EDGE::edge_id, LWT_ISO_EDGE::end_node, LWT_ISO_EDGE::face_left, LWT_ISO_EDGE::face_right, LWT_ISO_EDGE::geom, getPoint2d_p(), LWDEBUGF, lwerror(), lwgeom_as_lwline(), lwgeom_free(), lwgeom_remove_repeated_points(), lwline_as_lwgeom(), lwt_be_getEdgeByNode(), lwt_be_lastErrorMessage(), LWT_COL_EDGE_ALL, LWTFMT_ELEMID, edgeend_t::myaz, POINTARRAY::npoints, LWLINE::points, LWT_ISO_EDGE::start_node, POINT2D::x, and POINT2D::y.

Referenced by _lwt_AddEdge(), lwt_ChangeEdgeGeom(), and lwt_GetFaceContainingPoint().

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