PostGIS  2.5.0dev-r@@SVN_REVISION@@
static int _lwt_FindAdjacentEdges ( LWT_TOPOLOGY topo,
LWT_ELEMID  node,
edgeend data,
edgeend other,
int  myedge_id 
)
static

Definition at line 1519 of file lwgeom_topo.c.

References _lwt_FirstDistinctVertex2D(), _lwt_release_edges(), azimuth_pt_pt(), LWT_TOPOLOGY_T::be_iface, edgeend_t::ccwFace, edgeend_t::cwFace, 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, edgeend_t::nextCCW, edgeend_t::nextCW, POINTARRAY::npoints, LWLINE::points, LWT_ISO_EDGE::start_node, POINT2D::x, and POINT2D::y.

Referenced by _lwt_AddEdge(), and lwt_ChangeEdgeGeom().

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

Here is the call graph for this function:

Here is the caller graph for this function: