Coverage Report

Created: 2026-12-01 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgis/liblwgeom/lwline.c
Line
Count
Source
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) 2026 Darafei Praliaskouski <me@komzpa.net>
22
 * Copyright (C) 2012 Sandro Santilli <strk@kbt.io>
23
 * Copyright (C) 2001-2006 Refractions Research Inc.
24
 *
25
 **********************************************************************/
26
27
/* basic LWLINE functions */
28
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
#include "liblwgeom_internal.h"
33
#include "lwgeom_log.h"
34
35
/*
36
 * Construct a new LWLINE.  points will *NOT* be copied
37
 * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
38
 */
39
LWLINE *
40
lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
41
5.74k
{
42
5.74k
  LWLINE *result = (LWLINE *)lwalloc(sizeof(LWLINE));
43
5.74k
  result->type = LINETYPE;
44
5.74k
  result->flags = points->flags;
45
5.74k
  FLAGS_SET_BBOX(result->flags, bbox ? 1 : 0);
46
5.74k
  result->srid = srid;
47
5.74k
  result->points = points;
48
5.74k
  result->bbox = bbox;
49
5.74k
  return result;
50
5.74k
}
51
52
LWLINE *
53
lwline_construct_empty(int32_t srid, char hasz, char hasm)
54
102k
{
55
102k
  LWLINE *result = lwalloc(sizeof(LWLINE));
56
102k
  result->type = LINETYPE;
57
102k
  result->flags = lwflags(hasz, hasm, 0);
58
102k
  result->srid = srid;
59
102k
  result->points = ptarray_construct_empty(hasz, hasm, 1);
60
102k
  result->bbox = NULL;
61
102k
  return result;
62
102k
}
63
64
void
65
lwline_free(LWLINE *line)
66
108k
{
67
108k
  if (!line)
68
0
    return;
69
70
108k
  if (line->bbox)
71
0
    lwfree(line->bbox);
72
108k
  if (line->points)
73
108k
    ptarray_free(line->points);
74
108k
  lwfree(line);
75
108k
}
76
77
void
78
printLWLINE(LWLINE *line)
79
0
{
80
0
  lwnotice("LWLINE {");
81
0
  lwnotice("    ndims = %i", (int)FLAGS_NDIMS(line->flags));
82
0
  lwnotice("    srid = %i", (int)line->srid);
83
0
  printPA(line->points);
84
0
  lwnotice("}");
85
0
}
86
87
/* @brief Clone LWLINE object. Serialized point lists are not copied.
88
 *
89
 * @see ptarray_clone
90
 */
91
LWLINE *
92
lwline_clone(const LWLINE *g)
93
0
{
94
0
  LWLINE *ret = lwalloc(sizeof(LWLINE));
95
96
0
  LWDEBUGF(2, "lwline_clone called with %p", g);
97
98
0
  memcpy(ret, g, sizeof(LWLINE));
99
100
0
  ret->points = ptarray_clone(g->points);
101
102
0
  if (g->bbox)
103
0
    ret->bbox = gbox_copy(g->bbox);
104
0
  return ret;
105
0
}
106
107
/* Deep clone LWLINE object. POINTARRAY *is* copied. */
108
LWLINE *
109
lwline_clone_deep(const LWLINE *g)
110
0
{
111
0
  LWLINE *ret = lwalloc(sizeof(LWLINE));
112
113
0
  LWDEBUGF(2, "lwline_clone_deep called with %p", g);
114
0
  memcpy(ret, g, sizeof(LWLINE));
115
116
0
  if (g->bbox)
117
0
    ret->bbox = gbox_copy(g->bbox);
118
0
  if (g->points)
119
0
    ret->points = ptarray_clone_deep(g->points);
120
0
  FLAGS_SET_READONLY(ret->flags, 0);
121
122
0
  return ret;
123
0
}
124
125
void
126
lwline_release(LWLINE *lwline)
127
0
{
128
0
  lwgeom_release(lwline_as_lwgeom(lwline));
129
0
}
130
131
LWLINE *
132
lwline_segmentize2d(const LWLINE *line, double dist)
133
0
{
134
0
  POINTARRAY *segmentized = ptarray_segmentize2d(line->points, dist);
135
0
  if (!segmentized)
136
0
    return NULL;
137
0
  return lwline_construct(line->srid, NULL, segmentized);
138
0
}
139
140
/* check coordinate equality  */
141
char
142
lwline_same(const LWLINE *l1, const LWLINE *l2)
143
0
{
144
0
  return ptarray_same(l1->points, l2->points);
145
0
}
146
147
/*
148
 * Construct a LWLINE from an array of point and line geometries
149
 * LWLINE dimensions are large enough to host all input dimensions.
150
 */
151
LWLINE *
152
lwline_from_lwgeom_array(int32_t srid, uint32_t ngeoms, LWGEOM **geoms)
153
0
{
154
0
  uint32_t i;
155
0
  int hasz = LW_FALSE;
156
0
  int hasm = LW_FALSE;
157
0
  POINTARRAY *pa;
158
0
  LWLINE *line;
159
0
  POINT4D pt;
160
0
  LWPOINTITERATOR *it;
161
162
  /*
163
   * Find output dimensions, check integrity
164
   */
165
0
  for (i = 0; i < ngeoms; i++)
166
0
  {
167
0
    if (FLAGS_GET_Z(geoms[i]->flags))
168
0
      hasz = LW_TRUE;
169
0
    if (FLAGS_GET_M(geoms[i]->flags))
170
0
      hasm = LW_TRUE;
171
0
    if (hasz && hasm)
172
0
      break; /* Nothing more to learn! */
173
0
  }
174
175
  /*
176
   * ngeoms should be a guess about how many points we have in input.
177
   * It's an underestimate for lines and multipoints */
178
0
  pa = ptarray_construct_empty(hasz, hasm, ngeoms);
179
180
0
  for (i = 0; i < ngeoms; i++)
181
0
  {
182
0
    LWGEOM *g = geoms[i];
183
184
0
    if (lwgeom_is_empty(g))
185
0
      continue;
186
187
0
    if (g->type == POINTTYPE)
188
0
    {
189
0
      lwpoint_getPoint4d_p((LWPOINT *)g, &pt);
190
0
      ptarray_append_point(pa, &pt, LW_TRUE);
191
0
    }
192
0
    else if (g->type == LINETYPE)
193
0
    {
194
      /*
195
       * Append the new line points, de-duplicating against the previous points.
196
       * Duplicated points internal to the linestring are untouched.
197
       */
198
0
      ptarray_append_ptarray(pa, ((LWLINE *)g)->points, -1);
199
0
    }
200
0
    else if (g->type == MULTILINETYPE)
201
0
    {
202
0
      LWMLINE *mline = lwgeom_as_lwmline(g);
203
0
      for (uint32_t j = 0; j < mline->ngeoms; j++)
204
0
      {
205
0
        LWLINE *line = mline->geoms[j];
206
0
        if (lwline_is_empty(line))
207
0
          continue;
208
0
        ptarray_append_ptarray(pa, line->points, -1);
209
0
      }
210
0
    }
211
0
    else if (g->type == MULTIPOINTTYPE)
212
0
    {
213
0
      it = lwpointiterator_create(g);
214
0
      while (lwpointiterator_next(it, &pt))
215
0
      {
216
0
        ptarray_append_point(pa, &pt, LW_TRUE);
217
0
      }
218
0
      lwpointiterator_destroy(it);
219
0
    }
220
0
    else
221
0
    {
222
0
      ptarray_free(pa);
223
0
      lwerror("lwline_from_ptarray: invalid input type: %s", lwtype_name(g->type));
224
0
      return NULL;
225
0
    }
226
0
  }
227
228
0
  if (pa->npoints > 0)
229
0
    line = lwline_construct(srid, NULL, pa);
230
0
  else
231
0
  {
232
    /* Is this really any different from the above ? */
233
0
    ptarray_free(pa);
234
0
    line = lwline_construct_empty(srid, hasz, hasm);
235
0
  }
236
237
0
  return line;
238
0
}
239
240
/*
241
 * Construct a LWLINE from an array of LWPOINTs
242
 * LWLINE dimensions are large enough to host all input dimensions.
243
 */
244
LWLINE *
245
lwline_from_ptarray(int32_t srid, uint32_t npoints, LWPOINT **points)
246
0
{
247
0
  uint32_t i;
248
0
  int hasz = LW_FALSE;
249
0
  int hasm = LW_FALSE;
250
0
  POINTARRAY *pa;
251
0
  LWLINE *line;
252
0
  POINT4D pt;
253
254
  /*
255
   * Find output dimensions, check integrity
256
   */
257
0
  for (i = 0; i < npoints; i++)
258
0
  {
259
0
    if (points[i]->type != POINTTYPE)
260
0
    {
261
0
      lwerror("lwline_from_ptarray: invalid input type: %s", lwtype_name(points[i]->type));
262
0
      return NULL;
263
0
    }
264
0
    if (FLAGS_GET_Z(points[i]->flags))
265
0
      hasz = LW_TRUE;
266
0
    if (FLAGS_GET_M(points[i]->flags))
267
0
      hasm = LW_TRUE;
268
0
    if (hasz && hasm)
269
0
      break; /* Nothing more to learn! */
270
0
  }
271
272
0
  pa = ptarray_construct_empty(hasz, hasm, npoints);
273
274
0
  for (i = 0; i < npoints; i++)
275
0
  {
276
0
    if (!lwpoint_is_empty(points[i]))
277
0
    {
278
0
      lwpoint_getPoint4d_p(points[i], &pt);
279
0
      ptarray_append_point(pa, &pt, LW_TRUE);
280
0
    }
281
0
  }
282
283
0
  if (pa->npoints > 0)
284
0
    line = lwline_construct(srid, NULL, pa);
285
0
  else
286
0
    line = lwline_construct_empty(srid, hasz, hasm);
287
288
0
  return line;
289
0
}
290
291
/*
292
 * Construct a LWLINE from a LWMPOINT
293
 */
294
LWLINE *
295
lwline_from_lwmpoint(int32_t srid, const LWMPOINT *mpoint)
296
0
{
297
0
  uint32_t i;
298
0
  POINTARRAY *pa = NULL;
299
0
  LWGEOM *lwgeom = (LWGEOM *)mpoint;
300
0
  POINT4D pt;
301
302
0
  char hasz = lwgeom_has_z(lwgeom);
303
0
  char hasm = lwgeom_has_m(lwgeom);
304
0
  uint32_t npoints = mpoint->ngeoms;
305
306
0
  if (lwgeom_is_empty(lwgeom))
307
0
  {
308
0
    return lwline_construct_empty(srid, hasz, hasm);
309
0
  }
310
311
0
  pa = ptarray_construct(hasz, hasm, npoints);
312
313
0
  for (i = 0; i < npoints; i++)
314
0
  {
315
0
    getPoint4d_p(mpoint->geoms[i]->point, 0, &pt);
316
0
    ptarray_set_point4d(pa, i, &pt);
317
0
  }
318
319
0
  LWDEBUGF(3, "lwline_from_lwmpoint: constructed pointarray for %d points", mpoint->ngeoms);
320
321
0
  return lwline_construct(srid, NULL, pa);
322
0
}
323
324
/**
325
 * Returns freshly allocated #LWPOINT that corresponds to the index where.
326
 * Returns NULL if the geometry is empty or the index invalid.
327
 */
328
LWPOINT *
329
lwline_get_lwpoint(const LWLINE *line, uint32_t where)
330
0
{
331
0
  POINT4D pt;
332
0
  LWPOINT *lwpoint;
333
0
  POINTARRAY *pa;
334
335
0
  if (lwline_is_empty(line) || where >= line->points->npoints)
336
0
    return NULL;
337
338
0
  pa = ptarray_construct_empty(FLAGS_GET_Z(line->flags), FLAGS_GET_M(line->flags), 1);
339
0
  pt = getPoint4d(line->points, where);
340
0
  ptarray_append_point(pa, &pt, LW_TRUE);
341
0
  lwpoint = lwpoint_construct(line->srid, NULL, pa);
342
0
  return lwpoint;
343
0
}
344
345
int
346
lwline_add_lwpoint(LWLINE *line, LWPOINT *point, uint32_t where)
347
0
{
348
0
  POINT4D pt;
349
0
  getPoint4d_p(point->point, 0, &pt);
350
351
0
  if (ptarray_insert_point(line->points, &pt, where) != LW_SUCCESS)
352
0
    return LW_FAILURE;
353
354
  /* Update the bounding box */
355
0
  if (line->bbox)
356
0
  {
357
0
    lwgeom_refresh_bbox((LWGEOM *)line);
358
0
  }
359
360
0
  return LW_SUCCESS;
361
0
}
362
363
LWLINE *
364
lwline_removepoint(LWLINE *line, uint32_t index)
365
0
{
366
0
  POINTARRAY *newpa;
367
0
  LWLINE *ret;
368
369
0
  newpa = ptarray_removePoint(line->points, index);
370
371
0
  ret = lwline_construct(line->srid, NULL, newpa);
372
0
  lwgeom_add_bbox((LWGEOM *)ret);
373
374
0
  return ret;
375
0
}
376
377
/*
378
 * Note: input will be changed, make sure you have permissions for this.
379
 */
380
void
381
lwline_setPoint4d(LWLINE *line, uint32_t index, POINT4D *newpoint)
382
0
{
383
0
  ptarray_set_point4d(line->points, index, newpoint);
384
  /* Update the box, if there is one to update */
385
0
  if (line->bbox)
386
0
  {
387
0
    lwgeom_refresh_bbox((LWGEOM *)line);
388
0
  }
389
0
}
390
391
/**
392
 * Re-write the measure coordinate (or add one, if it isn't already there) interpolating
393
 * the measure between the supplied start and end values.
394
 */
395
LWLINE *
396
lwline_measured_from_lwline(const LWLINE *lwline, double m_start, double m_end)
397
0
{
398
0
  int i = 0;
399
0
  int hasm = 0, hasz = 0;
400
0
  int npoints = 0;
401
0
  double length = 0.0;
402
0
  double length_so_far = 0.0;
403
0
  double m_range = m_end - m_start;
404
0
  double m;
405
0
  POINTARRAY *pa = NULL;
406
0
  POINT3DZ p1, p2;
407
408
0
  if (lwline->type != LINETYPE)
409
0
  {
410
0
    lwerror("lwline_construct_from_lwline: only line types supported");
411
0
    return NULL;
412
0
  }
413
414
0
  hasz = FLAGS_GET_Z(lwline->flags);
415
0
  hasm = 1;
416
417
  /* Null points or npoints == 0 will result in empty return geometry */
418
0
  if (lwline->points)
419
0
  {
420
0
    npoints = lwline->points->npoints;
421
0
    length = ptarray_length_2d(lwline->points);
422
0
    getPoint3dz_p(lwline->points, 0, &p1);
423
0
  }
424
425
0
  pa = ptarray_construct(hasz, hasm, npoints);
426
427
0
  for (i = 0; i < npoints; i++)
428
0
  {
429
0
    POINT4D q;
430
0
    POINT2D a, b;
431
0
    getPoint3dz_p(lwline->points, i, &p2);
432
0
    a.x = p1.x;
433
0
    a.y = p1.y;
434
0
    b.x = p2.x;
435
0
    b.y = p2.y;
436
0
    length_so_far += distance2d_pt_pt(&a, &b);
437
0
    if (length > 0.0)
438
0
      m = m_start + m_range * length_so_far / length;
439
    /* #3172, support (valid) zero-length inputs */
440
0
    else if (length == 0.0 && npoints > 1)
441
0
      m = m_start + m_range * i / (npoints - 1);
442
0
    else
443
0
      m = 0.0;
444
0
    q.x = p2.x;
445
0
    q.y = p2.y;
446
0
    q.z = p2.z;
447
0
    q.m = m;
448
0
    ptarray_set_point4d(pa, i, &q);
449
0
    p1 = p2;
450
0
  }
451
452
0
  return lwline_construct(lwline->srid, NULL, pa);
453
0
}
454
455
LWGEOM *
456
lwline_remove_repeated_points(const LWLINE *lwline, double tolerance)
457
0
{
458
0
  return lwgeom_remove_repeated_points((LWGEOM *)lwline, tolerance);
459
0
}
460
461
int
462
lwline_is_closed(const LWLINE *line)
463
0
{
464
0
  if (FLAGS_GET_Z(line->flags))
465
0
    return ptarray_is_closed_3d(line->points);
466
467
0
  return ptarray_is_closed_2d(line->points);
468
0
}
469
470
int
471
lwline_is_trajectory(const LWLINE *line)
472
0
{
473
0
  if (!FLAGS_GET_M(line->flags))
474
0
  {
475
0
    lwnotice("Line does not have M dimension");
476
0
    return LW_FALSE;
477
0
  }
478
479
0
  uint32_t n = line->points->npoints;
480
481
0
  if (n < 2)
482
0
    return LW_TRUE; /* empty or single-point are "good" */
483
484
0
  double m = -1 * FLT_MAX;
485
0
  for (uint32_t i = 0; i < n; ++i)
486
0
  {
487
0
    POINT3DM p;
488
0
    if (!getPoint3dm_p(line->points, i, &p))
489
0
      return LW_FALSE;
490
0
    if (p.m <= m)
491
0
    {
492
0
      lwnotice(
493
0
          "Measure of vertex %d (%g) not bigger than measure of vertex %d (%g)", i, p.m, i - 1, m);
494
0
      return LW_FALSE;
495
0
    }
496
0
    m = p.m;
497
0
  }
498
499
0
  return LW_TRUE;
500
0
}
501
502
LWLINE *
503
lwline_force_dims(const LWLINE *line, int hasz, int hasm, double zval, double mval)
504
0
{
505
0
  POINTARRAY *pdims = NULL;
506
0
  LWLINE *lineout;
507
508
  /* Return 2D empty */
509
0
  if (lwline_is_empty(line))
510
0
  {
511
0
    lineout = lwline_construct_empty(line->srid, hasz, hasm);
512
0
  }
513
0
  else
514
0
  {
515
0
    pdims = ptarray_force_dims(line->points, hasz, hasm, zval, mval);
516
0
    lineout = lwline_construct(line->srid, NULL, pdims);
517
0
  }
518
0
  lineout->type = line->type;
519
0
  return lineout;
520
0
}
521
522
uint32_t
523
lwline_count_vertices(const LWLINE *line)
524
0
{
525
0
  assert(line);
526
0
  if (!line->points)
527
0
    return 0;
528
0
  return line->points->npoints;
529
0
}
530
531
double
532
lwline_length(const LWLINE *line)
533
0
{
534
0
  if (lwline_is_empty(line))
535
0
    return 0.0;
536
0
  return ptarray_length(line->points);
537
0
}
538
539
double
540
lwline_length_2d(const LWLINE *line)
541
0
{
542
0
  if (lwline_is_empty(line))
543
0
    return 0.0;
544
0
  return ptarray_length_2d(line->points);
545
0
}
546
547
POINTARRAY *
548
lwline_interpolate_points(const LWLINE *line, double length_fraction, char repeat)
549
0
{
550
0
  POINT4D pt;
551
0
  uint32_t points_to_interpolate;
552
0
  uint32_t points_found = 0;
553
0
  double length;
554
0
  double length_fraction_increment = length_fraction;
555
0
  double length_fraction_consumed = 0;
556
0
  char has_z = (char)lwgeom_has_z(lwline_as_lwgeom(line));
557
0
  char has_m = (char)lwgeom_has_m(lwline_as_lwgeom(line));
558
0
  const POINTARRAY *ipa = line->points;
559
0
  POINTARRAY *opa;
560
561
  /* Empty.InterpolatePoint == Point Empty */
562
0
  if (lwline_is_empty(line))
563
0
  {
564
0
    return ptarray_construct_empty(has_z, has_m, 0);
565
0
  }
566
567
  /* If distance is one of the two extremes, return the point on that
568
   * end rather than doing any computations
569
   */
570
0
  if (length_fraction == 0.0 || length_fraction == 1.0)
571
0
  {
572
0
    if (length_fraction == 0.0)
573
0
      getPoint4d_p(ipa, 0, &pt);
574
0
    else
575
0
      getPoint4d_p(ipa, ipa->npoints - 1, &pt);
576
577
0
    opa = ptarray_construct(has_z, has_m, 1);
578
0
    ptarray_set_point4d(opa, 0, &pt);
579
580
0
    return opa;
581
0
  }
582
583
  /* Interpolate points along the line */
584
0
  length = ptarray_length_2d(ipa);
585
0
  points_to_interpolate = repeat ? (uint32_t)floor(1 / length_fraction) : 1;
586
0
  opa = ptarray_construct(has_z, has_m, points_to_interpolate);
587
588
0
  const POINT2D *p1 = getPoint2d_cp(ipa, 0);
589
0
  for (uint32_t i = 0; length > 0.0 && i < ipa->npoints - 1 && points_found < points_to_interpolate; i++)
590
0
  {
591
0
    const POINT2D *p2 = getPoint2d_cp(ipa, i + 1);
592
0
    double segment_length_frac = distance2d_pt_pt(p1, p2) / length;
593
594
    /* If our target distance is before the total length we've seen
595
     * so far. create a new point some distance down the current
596
     * segment.
597
     */
598
0
    while (length_fraction < length_fraction_consumed + segment_length_frac &&
599
0
           points_found < points_to_interpolate)
600
0
    {
601
0
      POINT4D p1_4d = getPoint4d(ipa, i);
602
0
      POINT4D p2_4d = getPoint4d(ipa, i + 1);
603
604
0
      double segment_fraction = (length_fraction - length_fraction_consumed) / segment_length_frac;
605
0
      interpolate_point4d(&p1_4d, &p2_4d, &pt, segment_fraction);
606
0
      ptarray_set_point4d(opa, points_found++, &pt);
607
0
      length_fraction += length_fraction_increment;
608
0
    }
609
610
0
    length_fraction_consumed += segment_length_frac;
611
612
0
    p1 = p2;
613
0
  }
614
615
  /* A zero-length line or rounding can leave points to fill at the endpoint. */
616
0
  if (points_found < points_to_interpolate)
617
0
  {
618
0
    getPoint4d_p(ipa, ipa->npoints - 1, &pt);
619
0
    while (points_found < points_to_interpolate)
620
0
      ptarray_set_point4d(opa, points_found++, &pt);
621
0
  }
622
623
0
  return opa;
624
0
}
625
626
extern LWPOINT *
627
lwline_interpolate_point_3d(const LWLINE *line, double distance)
628
0
{
629
0
  double length, slength, tlength;
630
0
  POINTARRAY *ipa;
631
0
  POINT4D pt;
632
0
  int nsegs, i;
633
0
  LWGEOM *geom = lwline_as_lwgeom(line);
634
0
  int has_z = lwgeom_has_z(geom);
635
0
  int has_m = lwgeom_has_m(geom);
636
0
  ipa = line->points;
637
638
  /* Empty.InterpolatePoint == Point Empty */
639
0
  if (lwline_is_empty(line))
640
0
  {
641
0
    return lwpoint_construct_empty(line->srid, has_z, has_m);
642
0
  }
643
644
  /* If distance is one of the two extremes, return the point on that
645
   * end rather than doing any expensive computations
646
   */
647
0
  if (distance == 0.0 || distance == 1.0)
648
0
  {
649
0
    if (distance == 0.0)
650
0
      getPoint4d_p(ipa, 0, &pt);
651
0
    else
652
0
      getPoint4d_p(ipa, ipa->npoints - 1, &pt);
653
654
0
    return lwpoint_make(line->srid, has_z, has_m, &pt);
655
0
  }
656
657
  /* Interpolate a point on the line */
658
0
  nsegs = ipa->npoints - 1;
659
0
  length = ptarray_length(ipa);
660
0
  tlength = 0;
661
0
  for (i = 0; i < nsegs; i++)
662
0
  {
663
0
    POINT4D p1, p2;
664
0
    POINT4D *p1ptr = &p1, *p2ptr = &p2; /* don't break
665
                 * strict-aliasing rules
666
                 */
667
668
0
    getPoint4d_p(ipa, i, &p1);
669
0
    getPoint4d_p(ipa, i + 1, &p2);
670
671
    /* Find the relative length of this segment */
672
0
    slength = distance3d_pt_pt((POINT3D *)p1ptr, (POINT3D *)p2ptr) / length;
673
674
    /* If our target distance is before the total length we've seen
675
     * so far. create a new point some distance down the current
676
     * segment.
677
     */
678
0
    if (distance < tlength + slength)
679
0
    {
680
0
      double dseg = (distance - tlength) / slength;
681
0
      interpolate_point4d(&p1, &p2, &pt, dseg);
682
0
      return lwpoint_make(line->srid, has_z, has_m, &pt);
683
0
    }
684
0
    tlength += slength;
685
0
  }
686
687
  /* Return the last point on the line. This shouldn't happen, but
688
   * could if there's some floating point rounding errors. */
689
0
  getPoint4d_p(ipa, ipa->npoints - 1, &pt);
690
0
  return lwpoint_make(line->srid, has_z, has_m, &pt);
691
0
}
692
693
extern LWLINE *
694
lwline_extend(const LWLINE *line, double distance_forward, double distance_backward)
695
0
{
696
0
  POINTARRAY *pa, *opa;
697
0
  POINT4D p00, p01, p10, p11;
698
0
  POINT4D p_start, p_end;
699
0
  uint32_t i;
700
0
  bool forward = false, backward = false;
701
702
0
  if (distance_forward < 0 || distance_backward < 0)
703
0
    lwerror("%s: distances must be non-negative", __func__);
704
705
0
  if (!line || lwline_is_empty(line) || lwline_count_vertices(line) < 2)
706
0
  {
707
0
    lwerror("%s: line must have at least two points", __func__);
708
0
  }
709
710
0
  pa = line->points;
711
0
  if (distance_backward > 0.0)
712
0
  {
713
0
    i = 0;
714
    /* Get two distinct points at start of pointarray */
715
0
    getPoint4d_p(pa, i++, &p00);
716
0
    getPoint4d_p(pa, i, &p01);
717
0
    while (p4d_same(&p00, &p01))
718
0
    {
719
0
      if (i == pa->npoints - 1)
720
0
      {
721
0
        lwerror("%s: line must have at least two distinct points", __func__);
722
0
      }
723
0
      i++;
724
0
      getPoint4d_p(pa, i, &p01);
725
0
    }
726
0
    project_pt_pt(&p01, &p00, distance_backward, &p_start);
727
0
    backward = true;
728
0
  }
729
730
0
  if (distance_forward > 0.0)
731
0
  {
732
0
    i = pa->npoints - 1;
733
    /* Get two distinct points at end of pointarray */
734
0
    getPoint4d_p(pa, i--, &p10);
735
0
    getPoint4d_p(pa, i, &p11);
736
0
    while (p4d_same(&p10, &p11))
737
0
    {
738
0
      if (i == 0)
739
0
      {
740
0
        lwerror("%s: line must have at least two distinct points", __func__);
741
0
      }
742
0
      i--;
743
0
      getPoint4d_p(pa, i, &p11);
744
0
    }
745
0
    project_pt_pt(&p11, &p10, distance_forward, &p_end);
746
0
    forward = true;
747
0
  }
748
749
0
  opa = ptarray_construct_empty(ptarray_has_z(pa), ptarray_has_m(pa), pa->npoints + 2);
750
751
0
  if (backward)
752
0
  {
753
0
    ptarray_append_point(opa, &p_start, true);
754
0
  }
755
0
  ptarray_append_ptarray(opa, pa, -1.0);
756
0
  if (forward)
757
0
  {
758
0
    ptarray_append_point(opa, &p_end, true);
759
0
  }
760
  return lwline_construct(line->srid, NULL, opa);
761
0
}