forked from postgis/postgis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwcircstring.c
308 lines (254 loc) · 7.79 KB
/
lwcircstring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
*
* PostGIS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* PostGIS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*
* Copyright (C) 2001-2006 Refractions Research Inc.
*
**********************************************************************/
/* basic LWCIRCSTRING functions */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
void printLWCIRCSTRING(LWCIRCSTRING *curve);
void lwcircstring_release(LWCIRCSTRING *lwcirc);
char lwcircstring_same(const LWCIRCSTRING *me, const LWCIRCSTRING *you);
LWCIRCSTRING *lwcircstring_from_lwpointarray(int srid, uint32_t npoints, LWPOINT **points);
LWCIRCSTRING *lwcircstring_from_lwmpoint(int srid, LWMPOINT *mpoint);
LWCIRCSTRING *lwcircstring_addpoint(LWCIRCSTRING *curve, LWPOINT *point, uint32_t where);
LWCIRCSTRING *lwcircstring_removepoint(LWCIRCSTRING *curve, uint32_t index);
void lwcircstring_setPoint4d(LWCIRCSTRING *curve, uint32_t index, POINT4D *newpoint);
/*
* Construct a new LWCIRCSTRING. points will *NOT* be copied
* use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
*/
LWCIRCSTRING *
lwcircstring_construct(int srid, GBOX *bbox, POINTARRAY *points)
{
LWCIRCSTRING *result;
/*
* The first arc requires three points. Each additional
* arc requires two more points. Thus the minimum point count
* is three, and the count must be odd.
*/
if (points->npoints % 2 != 1 || points->npoints < 3)
{
lwnotice("lwcircstring_construct: invalid point count %d", points->npoints);
}
result = (LWCIRCSTRING*) lwalloc(sizeof(LWCIRCSTRING));
result->type = CIRCSTRINGTYPE;
result->flags = points->flags;
FLAGS_SET_BBOX(result->flags, bbox?1:0);
result->srid = srid;
result->points = points;
result->bbox = bbox;
return result;
}
LWCIRCSTRING *
lwcircstring_construct_empty(int srid, char hasz, char hasm)
{
LWCIRCSTRING *result = lwalloc(sizeof(LWCIRCSTRING));
result->type = CIRCSTRINGTYPE;
result->flags = gflags(hasz,hasm,0);
result->srid = srid;
result->points = ptarray_construct_empty(hasz, hasm, 1);
result->bbox = NULL;
return result;
}
void
lwcircstring_release(LWCIRCSTRING *lwcirc)
{
lwgeom_release(lwcircstring_as_lwgeom(lwcirc));
}
void lwcircstring_free(LWCIRCSTRING *curve)
{
if ( ! curve ) return;
if ( curve->bbox )
lwfree(curve->bbox);
if ( curve->points )
ptarray_free(curve->points);
lwfree(curve);
}
void printLWCIRCSTRING(LWCIRCSTRING *curve)
{
lwnotice("LWCIRCSTRING {");
lwnotice(" ndims = %i", (int)FLAGS_NDIMS(curve->flags));
lwnotice(" srid = %i", (int)curve->srid);
printPA(curve->points);
lwnotice("}");
}
/* @brief Clone LWCIRCSTRING object. Serialized point lists are not copied.
*
* @see ptarray_clone
*/
LWCIRCSTRING *
lwcircstring_clone(const LWCIRCSTRING *g)
{
return (LWCIRCSTRING *)lwline_clone((LWLINE *)g);
}
/* check coordinate equality */
char
lwcircstring_same(const LWCIRCSTRING *me, const LWCIRCSTRING *you)
{
return ptarray_same(me->points, you->points);
}
/*
* Construct a LWCIRCSTRING from an array of LWPOINTs
* LWCIRCSTRING dimensions are large enough to host all input dimensions.
*/
LWCIRCSTRING *
lwcircstring_from_lwpointarray(int srid, uint32_t npoints, LWPOINT **points)
{
int zmflag=0;
uint32_t i;
POINTARRAY *pa;
uint8_t *newpoints, *ptr;
size_t ptsize, size;
/*
* Find output dimensions, check integrity
*/
for (i = 0; i < npoints; i++)
{
if (points[i]->type != POINTTYPE)
{
lwerror("lwcurve_from_lwpointarray: invalid input type: %s",
lwtype_name(points[i]->type));
return NULL;
}
if (FLAGS_GET_Z(points[i]->flags)) zmflag |= 2;
if (FLAGS_GET_M(points[i]->flags)) zmflag |= 1;
if (zmflag == 3) break;
}
if (zmflag == 0) ptsize = 2 * sizeof(double);
else if (zmflag == 3) ptsize = 4 * sizeof(double);
else ptsize = 3 * sizeof(double);
/*
* Allocate output points array
*/
size = ptsize * npoints;
newpoints = lwalloc(size);
memset(newpoints, 0, size);
ptr = newpoints;
for (i = 0; i < npoints; i++)
{
size = ptarray_point_size(points[i]->point);
memcpy(ptr, getPoint_internal(points[i]->point, 0), size);
ptr += ptsize;
}
pa = ptarray_construct_reference_data(zmflag&2, zmflag&1, npoints, newpoints);
return lwcircstring_construct(srid, NULL, pa);
}
/*
* Construct a LWCIRCSTRING from a LWMPOINT
*/
LWCIRCSTRING *
lwcircstring_from_lwmpoint(int srid, LWMPOINT *mpoint)
{
uint32_t i;
POINTARRAY *pa;
char zmflag = FLAGS_GET_ZM(mpoint->flags);
size_t ptsize, size;
uint8_t *newpoints, *ptr;
if (zmflag == 0) ptsize = 2 * sizeof(double);
else if (zmflag == 3) ptsize = 4 * sizeof(double);
else ptsize = 3 * sizeof(double);
/* Allocate space for output points */
size = ptsize * mpoint->ngeoms;
newpoints = lwalloc(size);
memset(newpoints, 0, size);
ptr = newpoints;
for (i = 0; i < mpoint->ngeoms; i++)
{
memcpy(ptr,
getPoint_internal(mpoint->geoms[i]->point, 0),
ptsize);
ptr += ptsize;
}
pa = ptarray_construct_reference_data(zmflag&2, zmflag&1, mpoint->ngeoms, newpoints);
LWDEBUGF(3, "lwcurve_from_lwmpoint: constructed pointarray for %d points, %d zmflag", mpoint->ngeoms, zmflag);
return lwcircstring_construct(srid, NULL, pa);
}
LWCIRCSTRING *
lwcircstring_addpoint(LWCIRCSTRING *curve, LWPOINT *point, uint32_t where)
{
POINTARRAY *newpa;
LWCIRCSTRING *ret;
newpa = ptarray_addPoint(curve->points,
getPoint_internal(point->point, 0),
FLAGS_NDIMS(point->flags), where);
ret = lwcircstring_construct(curve->srid, NULL, newpa);
return ret;
}
LWCIRCSTRING *
lwcircstring_removepoint(LWCIRCSTRING *curve, uint32_t index)
{
POINTARRAY *newpa;
LWCIRCSTRING *ret;
newpa = ptarray_removePoint(curve->points, index);
ret = lwcircstring_construct(curve->srid, NULL, newpa);
return ret;
}
/*
* Note: input will be changed, make sure you have permissions for this.
* */
void
lwcircstring_setPoint4d(LWCIRCSTRING *curve, uint32_t index, POINT4D *newpoint)
{
ptarray_set_point4d(curve->points, index, newpoint);
}
int
lwcircstring_is_closed(const LWCIRCSTRING *curve)
{
if (FLAGS_GET_Z(curve->flags))
return ptarray_is_closed_3d(curve->points);
return ptarray_is_closed_2d(curve->points);
}
int lwcircstring_is_empty(const LWCIRCSTRING *circ)
{
if ( !circ->points || circ->points->npoints < 1 )
return LW_TRUE;
return LW_FALSE;
}
double lwcircstring_length(const LWCIRCSTRING *circ)
{
return lwcircstring_length_2d(circ);
}
double lwcircstring_length_2d(const LWCIRCSTRING *circ)
{
if ( lwcircstring_is_empty(circ) )
return 0.0;
return ptarray_arc_length_2d(circ->points);
}
/*
* Returns freshly allocated #LWPOINT that corresponds to the index where.
* Returns NULL if the geometry is empty or the index invalid.
*/
LWPOINT* lwcircstring_get_lwpoint(const LWCIRCSTRING *circ, uint32_t where) {
POINT4D pt;
LWPOINT *lwpoint;
POINTARRAY *pa;
if ( lwcircstring_is_empty(circ) || where >= circ->points->npoints )
return NULL;
pa = ptarray_construct_empty(FLAGS_GET_Z(circ->flags), FLAGS_GET_M(circ->flags), 1);
pt = getPoint4d(circ->points, where);
ptarray_append_point(pa, &pt, LW_TRUE);
lwpoint = lwpoint_construct(circ->srid, NULL, pa);
return lwpoint;
}