PostGIS  3.7.0dev-r@@SVN_REVISION@@
lwt_edgeend.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2024 Sandro Santilli <strk@kbt.io>
22  *
23  **********************************************************************/
24 #include "../postgis_config.h"
25 
26 //#define POSTGIS_DEBUG_LEVEL 1
27 #include "lwgeom_log.h"
28 
29 #include "liblwgeom_internal.h"
31 
32 #include "lwt_edgeend.h"
33 
35 {
36  lwfree(end);
37 }
38 
39 static int
40 _lwt_DistinctVertexes2D(const POINTARRAY* pa, int from, int dir, POINT2D *p0, POINT2D *p1)
41 {
42  int i, toofar, inc;
43 
44  if ( dir > 0 ) {
45  toofar = pa->npoints;
46  inc = 1;
47  } else {
48  toofar = -1;
49  inc = -1;
50  }
51 
52  LWDEBUGF(1, "first point is index %d", from);
53  getPoint2d_p(pa, from, p0);
54  for ( i = from+inc; i != toofar; i += inc )
55  {
56  LWDEBUGF(1, "testing point %d", i);
57  getPoint2d_p(pa, i, p1); /* pick next point */
58  if ( P2D_SAME_STRICT(p0, p1) ) continue; /* equal to startpoint */
59  /* this is a good one, neither same of start nor of end point */
60  return 1; /* found */
61  }
62 
63  /* no distinct vertices found */
64  return 0;
65 }
66 
68 lwt_edgeEnd_fromEdge( const LWT_ISO_EDGE *edge, int outgoing )
69 {
70  LWT_EDGEEND *ee = lwalloc(sizeof(LWT_EDGEEND));
71  ee->edge = edge;
72  ee->outgoing = outgoing;
73  const POINTARRAY *pa = edge->geom->points;
74 
75  int ret = _lwt_DistinctVertexes2D(
76  edge->geom->points,
77  outgoing ? 0 : pa->npoints-1, // from,
78  outgoing ? 1 : -1, // dir,
79  &(ee->p0),
80  &(ee->p1)
81  );
82  if (!ret)
83  {
84  lwerror("No distinct vertices found in edge %" LWTFMT_ELEMID, edge->edge_id);
85  return NULL;
86  }
87 
88  if ( ! azimuth_pt_pt(&(ee->p0), &(ee->p1), &(ee->azimuth)) ) {
89  lwerror("error computing azimuth of endpoint [%.15g %.15g,%.15g %.15g]",
90  ee->p0.x, ee->p0.y,
91  ee->p1.x, ee->p1.y
92  );
93  return NULL;
94  }
95 
96  LWDEBUGF(1, "Azimuth of segment [%.15g %.15g,%.15g %.15g] = %.15g",
97  ee->p0.x, ee->p0.y,
98  ee->p1.x, ee->p1.y,
99  ee->azimuth
100  );
101 
102  return ee;
103 }
104 
int azimuth_pt_pt(const POINT2D *p1, const POINT2D *p2, double *ret)
Compute the azimuth of segment AB in radians.
Definition: measures.c:2509
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:342
void lwfree(void *mem)
Definition: lwutil.c:248
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define P2D_SAME_STRICT(a, b)
#define LWTFMT_ELEMID
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:106
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
LWT_EDGEEND * lwt_edgeEnd_fromEdge(const LWT_ISO_EDGE *edge, int outgoing)
Definition: lwt_edgeend.c:68
static int _lwt_DistinctVertexes2D(const POINTARRAY *pa, int from, int dir, POINT2D *p0, POINT2D *p1)
Definition: lwt_edgeend.c:40
void lwt_edgeEnd_release(LWT_EDGEEND *end)
Definition: lwt_edgeend.c:34
POINTARRAY * points
Definition: liblwgeom.h:483
double azimuth
Definition: lwt_edgeend.h:37
POINT2D p0
Definition: lwt_edgeend.h:34
POINT2D p1
Definition: lwt_edgeend.h:35
const LWT_ISO_EDGE * edge
Definition: lwt_edgeend.h:33
LWLINE * geom
LWT_ELEMID edge_id
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427