PostGIS  3.0.6dev-r@@SVN_REVISION@@
lwinline.h
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 2018 Darafei Praliaskouski <me@komzpa.net>
22  * Copyright 2017-2018 Daniel Baston <dbaston@gmail.com>
23  * Copyright 2011 Sandro Santilli <strk@kbt.io>
24  * Copyright 2011 Paul Ramsey <pramsey@cleverelephant.ca>
25  * Copyright 2007-2008 Mark Cave-Ayland
26  * Copyright 2001-2006 Refractions Research Inc.
27  *
28  **********************************************************************/
29 
30 #if PARANOIA_LEVEL > 0
31 #include <assert.h>
32 #endif
33 
34 inline static double
35 distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
36 {
37  double hside = p2->x - p1->x;
38  double vside = p2->y - p1->y;
39 
40  return hside * hside + vside * vside;
41 }
42 
43 /*
44  * Size of point represeneted in the POINTARRAY
45  * 16 for 2d, 24 for 3d, 32 for 4d
46  */
47 static inline size_t
49 {
50  return sizeof(double) * FLAGS_NDIMS(pa->flags);
51 }
52 
53 /*
54  * Get a pointer to Nth point of a POINTARRAY
55  * You'll need to cast it to appropriate dimensioned point.
56  * Note that if you cast to a higher dimensional point you'll
57  * possibly corrupt the POINTARRAY.
58  *
59  * Casting to returned pointer to POINT2D* should be safe,
60  * as gserialized format always keeps the POINTARRAY pointer
61  * aligned to double boundary.
62  *
63  * WARNING: Don't cast this to a POINT!
64  * it would not be reliable due to memory alignment constraints
65  */
66 static inline uint8_t *
67 getPoint_internal(const POINTARRAY *pa, uint32_t n)
68 {
69  size_t size;
70  uint8_t *ptr;
71 
72 #if PARANOIA_LEVEL > 0
73  assert(pa);
74  assert(n <= pa->npoints);
75  assert(n <= pa->maxpoints);
76 #endif
77 
78  size = ptarray_point_size(pa);
79  ptr = pa->serialized_pointlist + size * n;
80 
81  return ptr;
82 }
83 
90 static inline const POINT2D *
91 getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
92 {
93  return (const POINT2D *)getPoint_internal(pa, n);
94 }
95 
102 static inline const POINT3D *
103 getPoint3d_cp(const POINTARRAY *pa, uint32_t n)
104 {
105  return (const POINT3D *)getPoint_internal(pa, n);
106 }
107 
114 static inline const POINT4D *
115 getPoint4d_cp(const POINTARRAY *pa, uint32_t n)
116 {
117  return (const POINT4D *)getPoint_internal(pa, n);
118 }
119 
120 static inline LWPOINT *
121 lwgeom_as_lwpoint(const LWGEOM *lwgeom)
122 {
123  if (!lwgeom)
124  return NULL;
125  if (lwgeom->type == POINTTYPE)
126  return (LWPOINT *)lwgeom;
127  else
128  return NULL;
129 }
130 
134 static inline uint32_t
136 {
137  if (!geom)
138  return 0;
139  return geom->type;
140 }
141 
142 static inline int
144 {
145  return !point->point || point->point->npoints < 1;
146 }
147 
148 static inline int
150 {
151  return !line->points || line->points->npoints < 1;
152 }
153 
154 static inline int
156 {
157  return !circ->points || circ->points->npoints < 1;
158 }
159 
160 static inline int
162 {
163  return poly->nrings < 1 || !poly->rings || !poly->rings[0] || poly->rings[0]->npoints < 1;
164 }
165 
166 static inline int
168 {
169  return !triangle->points || triangle->points->npoints < 1;
170 }
171 
172 static inline int lwgeom_is_empty(const LWGEOM *geom);
173 
174 static inline int
176 {
177  uint32_t i;
178  if (col->ngeoms == 0 || !col->geoms)
179  return LW_TRUE;
180  for (i = 0; i < col->ngeoms; i++)
181  {
182  if (!lwgeom_is_empty(col->geoms[i]))
183  return LW_FALSE;
184  }
185  return LW_TRUE;
186 }
187 
192 static inline int
194 {
195  switch (geom->type)
196  {
197  case POINTTYPE:
198  return lwpoint_is_empty((LWPOINT *)geom);
199  break;
200  case LINETYPE:
201  return lwline_is_empty((LWLINE *)geom);
202  break;
203  case CIRCSTRINGTYPE:
204  return lwcircstring_is_empty((LWCIRCSTRING *)geom);
205  break;
206  case POLYGONTYPE:
207  return lwpoly_is_empty((LWPOLY *)geom);
208  break;
209  case TRIANGLETYPE:
210  return lwtriangle_is_empty((LWTRIANGLE *)geom);
211  break;
212  case MULTIPOINTTYPE:
213  case MULTILINETYPE:
214  case MULTIPOLYGONTYPE:
215  case COMPOUNDTYPE:
216  case CURVEPOLYTYPE:
217  case MULTICURVETYPE:
218  case MULTISURFACETYPE:
220  case TINTYPE:
221  case COLLECTIONTYPE:
222  return lwcollection_is_empty((LWCOLLECTION *)geom);
223  break;
224  default:
225  return LW_FALSE;
226  break;
227  }
228 }
229 
230 /*
231  * This macro is based on PG_FREE_IF_COPY, except that it accepts two pointers.
232  * See PG_FREE_IF_COPY comment in src/include/fmgr.h in postgres source code
233  * for more details.
234  */
235 #define POSTGIS_FREE_IF_COPY_P(ptrsrc, ptrori) \
236  do \
237  { \
238  if ((Pointer)(ptrsrc) != (Pointer)(ptrori)) \
239  pfree(ptrsrc); \
240  } while (0)
#define LW_FALSE
Definition: liblwgeom.h:108
#define COLLECTIONTYPE
Definition: liblwgeom.h:122
#define COMPOUNDTYPE
Definition: liblwgeom.h:124
#define CURVEPOLYTYPE
Definition: liblwgeom.h:125
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define MULTISURFACETYPE
Definition: liblwgeom.h:127
#define LINETYPE
Definition: liblwgeom.h:117
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
#define TINTYPE
Definition: liblwgeom.h:130
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:193
#define POLYGONTYPE
Definition: liblwgeom.h:118
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:128
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:123
#define MULTICURVETYPE
Definition: liblwgeom.h:126
#define TRIANGLETYPE
Definition: liblwgeom.h:129
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:107
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:91
static const POINT3D * getPoint3d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:103
static const POINT4D * getPoint4d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:115
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
Definition: lwinline.h:35
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwinline.h:135
static int lwcircstring_is_empty(const LWCIRCSTRING *circ)
Definition: lwinline.h:155
static int lwpoly_is_empty(const LWPOLY *poly)
Definition: lwinline.h:161
static size_t ptarray_point_size(const POINTARRAY *pa)
Definition: lwinline.h:48
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: lwinline.h:67
static int lwcollection_is_empty(const LWCOLLECTION *col)
Definition: lwinline.h:175
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:193
static int lwline_is_empty(const LWLINE *line)
Definition: lwinline.h:149
static int lwtriangle_is_empty(const LWTRIANGLE *triangle)
Definition: lwinline.h:167
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:121
static int lwpoint_is_empty(const LWPOINT *point)
Definition: lwinline.h:143
POINTARRAY * points
Definition: liblwgeom.h:493
uint32_t ngeoms
Definition: liblwgeom.h:566
LWGEOM ** geoms
Definition: liblwgeom.h:561
uint8_t type
Definition: liblwgeom.h:448
POINTARRAY * points
Definition: liblwgeom.h:469
POINTARRAY * point
Definition: liblwgeom.h:457
POINTARRAY ** rings
Definition: liblwgeom.h:505
uint32_t nrings
Definition: liblwgeom.h:510
POINTARRAY * points
Definition: liblwgeom.h:481
double y
Definition: liblwgeom.h:376
double x
Definition: liblwgeom.h:376
lwflags_t flags
Definition: liblwgeom.h:417
uint32_t npoints
Definition: liblwgeom.h:413
uint8_t * serialized_pointlist
Definition: liblwgeom.h:420