File: rtpoint.c

package info (click to toggle)
librttopo 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,732 kB
  • ctags: 1,648
  • sloc: ansic: 31,501; sh: 77; makefile: 54
file content (288 lines) | stat: -rw-r--r-- 7,497 bytes parent folder | download
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
/**********************************************************************
 *
 * rttopo - topology library
 * http://git.osgeo.org/gogs/rttopo/librttopo
 *
 * rttopo 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.
 *
 * rttopo 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 rttopo.  If not, see <http://www.gnu.org/licenses/>.
 *
 **********************************************************************
 *
 * Copyright (C) 2001-2006 Refractions Research Inc.
 *
 **********************************************************************/



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rttopo_config.h"
/*#define RTGEOM_DEBUG_LEVEL 4*/
#include "librttopo_geom_internal.h"
#include "rtgeom_log.h"


/*
 * Convenience functions to hide the RTPOINTARRAY
 * TODO: obsolete this
 */
int
rtpoint_getPoint2d_p(const RTCTX *ctx, const RTPOINT *point, RTPOINT2D *out)
{
  return rt_getPoint2d_p(ctx, point->point, 0, out);
}

/* convenience functions to hide the RTPOINTARRAY */
int
rtpoint_getPoint3dz_p(const RTCTX *ctx, const RTPOINT *point, RTPOINT3DZ *out)
{
  return rt_getPoint3dz_p(ctx, point->point,0,out);
}
int
rtpoint_getPoint3dm_p(const RTCTX *ctx, const RTPOINT *point, RTPOINT3DM *out)
{
  return rt_getPoint3dm_p(ctx, point->point,0,out);
}
int
rtpoint_getPoint4d_p(const RTCTX *ctx, const RTPOINT *point, RTPOINT4D *out)
{
  return rt_getPoint4d_p(ctx, point->point,0,out);
}

double
rtpoint_get_x(const RTCTX *ctx, const RTPOINT *point)
{
  RTPOINT4D pt;
  if ( rtpoint_is_empty(ctx, point) )
    rterror(ctx, "rtpoint_get_x called with empty geometry");
  rt_getPoint4d_p(ctx, point->point, 0, &pt);
  return pt.x;
}

double
rtpoint_get_y(const RTCTX *ctx, const RTPOINT *point)
{
  RTPOINT4D pt;
  if ( rtpoint_is_empty(ctx, point) )
    rterror(ctx, "rtpoint_get_y called with empty geometry");
  rt_getPoint4d_p(ctx, point->point, 0, &pt);
  return pt.y;
}

double
rtpoint_get_z(const RTCTX *ctx, const RTPOINT *point)
{
  RTPOINT4D pt;
  if ( rtpoint_is_empty(ctx, point) )
    rterror(ctx, "rtpoint_get_z called with empty geometry");
  if ( ! RTFLAGS_GET_Z(point->flags) )
    rterror(ctx, "rtpoint_get_z called without z dimension");
  rt_getPoint4d_p(ctx, point->point, 0, &pt);
  return pt.z;
}

double
rtpoint_get_m(const RTCTX *ctx, const RTPOINT *point)
{
  RTPOINT4D pt;
  if ( rtpoint_is_empty(ctx, point) )
    rterror(ctx, "rtpoint_get_m called with empty geometry");
  if ( ! RTFLAGS_GET_M(point->flags) )
    rterror(ctx, "rtpoint_get_m called without m dimension");
  rt_getPoint4d_p(ctx, point->point, 0, &pt);
  return pt.m;
}

/*
 * Construct a new point.  point will not be copied
 * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
 */
RTPOINT *
rtpoint_construct(const RTCTX *ctx, int srid, RTGBOX *bbox, RTPOINTARRAY *point)
{
  RTPOINT *result;
  uint8_t flags = 0;

  if (point == NULL)
    return NULL; /* error */

  result = rtalloc(ctx, sizeof(RTPOINT));
  result->type = RTPOINTTYPE;
  RTFLAGS_SET_Z(flags, RTFLAGS_GET_Z(point->flags));
  RTFLAGS_SET_M(flags, RTFLAGS_GET_M(point->flags));
  RTFLAGS_SET_BBOX(flags, bbox?1:0);
  result->flags = flags;
  result->srid = srid;
  result->point = point;
  result->bbox = bbox;

  return result;
}

RTPOINT *
rtpoint_construct_empty(const RTCTX *ctx, int srid, char hasz, char hasm)
{
  RTPOINT *result = rtalloc(ctx, sizeof(RTPOINT));
  result->type = RTPOINTTYPE;
  result->flags = gflags(ctx, hasz, hasm, 0);
  result->srid = srid;
  result->point = ptarray_construct(ctx, hasz, hasm, 0);
  result->bbox = NULL;
  return result;
}

RTPOINT *
rtpoint_make2d(const RTCTX *ctx, int srid, double x, double y)
{
  RTPOINT4D p = {x, y, 0.0, 0.0};
  RTPOINTARRAY *pa = ptarray_construct_empty(ctx, 0, 0, 1);

  ptarray_append_point(ctx, pa, &p, RT_TRUE);
  return rtpoint_construct(ctx, srid, NULL, pa);
}

RTPOINT *
rtpoint_make3dz(const RTCTX *ctx, int srid, double x, double y, double z)
{
  RTPOINT4D p = {x, y, z, 0.0};
  RTPOINTARRAY *pa = ptarray_construct_empty(ctx, 1, 0, 1);

  ptarray_append_point(ctx, pa, &p, RT_TRUE);

  return rtpoint_construct(ctx, srid, NULL, pa);
}

RTPOINT *
rtpoint_make3dm(const RTCTX *ctx, int srid, double x, double y, double m)
{
  RTPOINT4D p = {x, y, 0.0, m};
  RTPOINTARRAY *pa = ptarray_construct_empty(ctx, 0, 1, 1);

  ptarray_append_point(ctx, pa, &p, RT_TRUE);

  return rtpoint_construct(ctx, srid, NULL, pa);
}

RTPOINT *
rtpoint_make4d(const RTCTX *ctx, int srid, double x, double y, double z, double m)
{
  RTPOINT4D p = {x, y, z, m};
  RTPOINTARRAY *pa = ptarray_construct_empty(ctx, 1, 1, 1);

  ptarray_append_point(ctx, pa, &p, RT_TRUE);

  return rtpoint_construct(ctx, srid, NULL, pa);
}

RTPOINT *
rtpoint_make(const RTCTX *ctx, int srid, int hasz, int hasm, const RTPOINT4D *p)
{
  RTPOINTARRAY *pa = ptarray_construct_empty(ctx, hasz, hasm, 1);
  ptarray_append_point(ctx, pa, p, RT_TRUE);
  return rtpoint_construct(ctx, srid, NULL, pa);
}

void rtpoint_free(const RTCTX *ctx, RTPOINT *pt)
{
  if ( ! pt ) return;

  if ( pt->bbox )
    rtfree(ctx, pt->bbox);
  if ( pt->point )
    ptarray_free(ctx, pt->point);
  rtfree(ctx, pt);
}

void printRTPOINT(const RTCTX *ctx, RTPOINT *point)
{
  rtnotice(ctx, "RTPOINT {");
  rtnotice(ctx, "    ndims = %i", (int)RTFLAGS_NDIMS(point->flags));
  rtnotice(ctx, "    BBOX = %i", RTFLAGS_GET_BBOX(point->flags) ? 1 : 0 );
  rtnotice(ctx, "    SRID = %i", (int)point->srid);
  printPA(ctx, point->point);
  rtnotice(ctx, "}");
}

/* @brief Clone RTPOINT object. Serialized point lists are not copied.
 *
 * @see ptarray_clone
 */
RTPOINT *
rtpoint_clone(const RTCTX *ctx, const RTPOINT *g)
{
  RTPOINT *ret = rtalloc(ctx, sizeof(RTPOINT));

  RTDEBUG(2, "rtpoint_clone called");

  memcpy(ret, g, sizeof(RTPOINT));

  ret->point = ptarray_clone(ctx, g->point);

  if ( g->bbox ) ret->bbox = gbox_copy(ctx, g->bbox);
  return ret;
}



void
rtpoint_release(const RTCTX *ctx, RTPOINT *rtpoint)
{
  rtgeom_release(ctx, rtpoint_as_rtgeom(ctx, rtpoint));
}


/* check coordinate equality  */
char
rtpoint_same(const RTCTX *ctx, const RTPOINT *p1, const RTPOINT *p2)
{
  return ptarray_same(ctx, p1->point, p2->point);
}


RTPOINT*
rtpoint_force_dims(const RTCTX *ctx, const RTPOINT *point, int hasz, int hasm)
{
  RTPOINTARRAY *pdims = NULL;
  RTPOINT *pointout;

  /* Return 2D empty */
  if( rtpoint_is_empty(ctx, point) )
  {
    pointout = rtpoint_construct_empty(ctx, point->srid, hasz, hasm);
  }
  else
  {
    /* Artays we duplicate the ptarray and return */
    pdims = ptarray_force_dims(ctx, point->point, hasz, hasm);
    pointout = rtpoint_construct(ctx, point->srid, NULL, pdims);
  }
  pointout->type = point->type;
  return pointout;
}

int rtpoint_is_empty(const RTCTX *ctx, const RTPOINT *point)
{
  if ( ! point->point || point->point->npoints < 1 )
    return RT_TRUE;
  return RT_FALSE;
}


RTPOINT *
rtpoint_grid(const RTCTX *ctx, const RTPOINT *point, const gridspec *grid)
{
  RTPOINTARRAY *opa = ptarray_grid(ctx, point->point, grid);
  return rtpoint_construct(ctx, point->srid, NULL, opa);
}