File: lwlinearreferencing.c

package info (click to toggle)
postgis 3.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 70,052 kB
  • sloc: ansic: 162,204; sql: 93,950; xml: 53,121; cpp: 12,646; perl: 5,658; sh: 5,369; makefile: 3,434; python: 1,205; yacc: 447; lex: 151; pascal: 58
file content (1460 lines) | stat: -rw-r--r-- 38,739 bytes parent folder | download | duplicates (3)
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
/**********************************************************************
 *
 * 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) 2015 Sandro Santilli <strk@kbt.io>
 * Copyright (C) 2011 Paul Ramsey
 *
 **********************************************************************/

#include "liblwgeom_internal.h"
#include "lwgeom_log.h"
#include "measures3d.h"

static int
segment_locate_along(const POINT4D *p1, const POINT4D *p2, double m, double offset, POINT4D *pn)
{
	double m1 = p1->m;
	double m2 = p2->m;
	double mprop;

	/* M is out of range, no new point generated. */
	if ((m < FP_MIN(m1, m2)) || (m > FP_MAX(m1, m2)))
	{
		return LW_FALSE;
	}

	if (m1 == m2)
	{
		/* Degenerate case: same M on both points.
		   If they are the same point we just return one of them. */
		if (p4d_same(p1, p2))
		{
			*pn = *p1;
			return LW_TRUE;
		}
		/* If the points are different we split the difference */
		mprop = 0.5;
	}
	else
	{
		mprop = (m - m1) / (m2 - m1);
	}

	/* M is in range, new point to be generated. */
	pn->x = p1->x + (p2->x - p1->x) * mprop;
	pn->y = p1->y + (p2->y - p1->y) * mprop;
	pn->z = p1->z + (p2->z - p1->z) * mprop;
	pn->m = m;

	/* Offset to the left or right, if necessary. */
	if (offset != 0.0)
	{
		double theta = atan2(p2->y - p1->y, p2->x - p1->x);
		pn->x -= sin(theta) * offset;
		pn->y += cos(theta) * offset;
	}

	return LW_TRUE;
}

static POINTARRAY *
ptarray_locate_along(const POINTARRAY *pa, double m, double offset)
{
	uint32_t i;
	POINT4D p1, p2, pn;
	POINTARRAY *dpa = NULL;

	/* Can't do anything with degenerate point arrays */
	if (!pa || pa->npoints < 2)
		return NULL;

	/* Walk through each segment in the point array */
	for (i = 1; i < pa->npoints; i++)
	{
		getPoint4d_p(pa, i - 1, &p1);
		getPoint4d_p(pa, i, &p2);

		/* No derived point? Move to next segment. */
		if (segment_locate_along(&p1, &p2, m, offset, &pn) == LW_FALSE)
			continue;

		/* No pointarray, make a fresh one */
		if (dpa == NULL)
			dpa = ptarray_construct_empty(ptarray_has_z(pa), ptarray_has_m(pa), 8);

		/* Add our new point to the array */
		ptarray_append_point(dpa, &pn, 0);
	}

	return dpa;
}

static LWMPOINT *
lwline_locate_along(const LWLINE *lwline, double m, double offset)
{
	POINTARRAY *opa = NULL;
	LWMPOINT *mp = NULL;
	LWGEOM *lwg = lwline_as_lwgeom(lwline);
	int hasz, hasm, srid;

	/* Return degenerates upwards */
	if (!lwline)
		return NULL;

	/* Create empty return shell */
	srid = lwgeom_get_srid(lwg);
	hasz = lwgeom_has_z(lwg);
	hasm = lwgeom_has_m(lwg);

	if (hasm)
	{
		/* Find points along */
		opa = ptarray_locate_along(lwline->points, m, offset);
	}
	else
	{
		LWLINE *lwline_measured = lwline_measured_from_lwline(lwline, 0.0, 1.0);
		opa = ptarray_locate_along(lwline_measured->points, m, offset);
		lwline_free(lwline_measured);
	}

	/* Return NULL as EMPTY */
	if (!opa)
		return lwmpoint_construct_empty(srid, hasz, hasm);

	/* Convert pointarray into a multipoint */
	mp = lwmpoint_construct(srid, opa);
	ptarray_free(opa);
	return mp;
}

static LWMPOINT *
lwmline_locate_along(const LWMLINE *lwmline, double m, double offset)
{
	LWMPOINT *lwmpoint = NULL;
	LWGEOM *lwg = lwmline_as_lwgeom(lwmline);
	uint32_t i, j;

	/* Return degenerates upwards */
	if ((!lwmline) || (lwmline->ngeoms < 1))
		return NULL;

	/* Construct return */
	lwmpoint = lwmpoint_construct_empty(lwgeom_get_srid(lwg), lwgeom_has_z(lwg), lwgeom_has_m(lwg));

	/* Locate along each sub-line */
	for (i = 0; i < lwmline->ngeoms; i++)
	{
		LWMPOINT *along = lwline_locate_along(lwmline->geoms[i], m, offset);
		if (along)
		{
			if (!lwgeom_is_empty((LWGEOM *)along))
			{
				for (j = 0; j < along->ngeoms; j++)
				{
					lwmpoint_add_lwpoint(lwmpoint, along->geoms[j]);
				}
			}
			/* Free the containing geometry, but leave the sub-geometries around */
			along->ngeoms = 0;
			lwmpoint_free(along);
		}
	}
	return lwmpoint;
}

static LWMPOINT *
lwpoint_locate_along(const LWPOINT *lwpoint, double m, __attribute__((__unused__)) double offset)
{
	double point_m = lwpoint_get_m(lwpoint);
	LWGEOM *lwg = lwpoint_as_lwgeom(lwpoint);
	LWMPOINT *r = lwmpoint_construct_empty(lwgeom_get_srid(lwg), lwgeom_has_z(lwg), lwgeom_has_m(lwg));
	if (FP_EQUALS(m, point_m))
	{
		lwmpoint_add_lwpoint(r, lwpoint_clone(lwpoint));
	}
	return r;
}

static LWMPOINT *
lwmpoint_locate_along(const LWMPOINT *lwin, double m, __attribute__((__unused__)) double offset)
{
	LWGEOM *lwg = lwmpoint_as_lwgeom(lwin);
	LWMPOINT *lwout = NULL;
	uint32_t i;

	/* Construct return */
	lwout = lwmpoint_construct_empty(lwgeom_get_srid(lwg), lwgeom_has_z(lwg), lwgeom_has_m(lwg));

	for (i = 0; i < lwin->ngeoms; i++)
	{
		double point_m = lwpoint_get_m(lwin->geoms[i]);
		if (FP_EQUALS(m, point_m))
		{
			lwmpoint_add_lwpoint(lwout, lwpoint_clone(lwin->geoms[i]));
		}
	}

	return lwout;
}

LWGEOM *
lwgeom_locate_along(const LWGEOM *lwin, double m, double offset)
{
	if (!lwin)
		return NULL;

	if (!lwgeom_has_m(lwin))
		lwerror("Input geometry does not have a measure dimension");

	switch (lwin->type)
	{
	case POINTTYPE:
		return (LWGEOM *)lwpoint_locate_along((LWPOINT *)lwin, m, offset);
	case MULTIPOINTTYPE:
		return (LWGEOM *)lwmpoint_locate_along((LWMPOINT *)lwin, m, offset);
	case LINETYPE:
		return (LWGEOM *)lwline_locate_along((LWLINE *)lwin, m, offset);
	case MULTILINETYPE:
		return (LWGEOM *)lwmline_locate_along((LWMLINE *)lwin, m, offset);
	/* Only line types supported right now */
	/* TO DO: CurveString, CompoundCurve, MultiCurve */
	/* TO DO: Point, MultiPoint */
	default:
		lwerror("Only linear geometries are supported, %s provided.", lwtype_name(lwin->type));
		return NULL;
	}
	return NULL;
}

/**
 * Given a POINT4D and an ordinate number, return
 * the value of the ordinate.
 * @param p input point
 * @param ordinate number (1=x, 2=y, 3=z, 4=m)
 * @return d value at that ordinate
 */
inline double
lwpoint_get_ordinate(const POINT4D *p, char ordinate)
{
	if (!p)
	{
		lwerror("Null input geometry.");
		return 0.0;
	}

	switch (ordinate)
	{
	case 'X':
		return p->x;
	case 'Y':
		return p->y;
	case 'Z':
		return p->z;
	case 'M':
		return p->m;
	}
	lwerror("Cannot extract %c ordinate.", ordinate);
	return 0.0;
}

/**
 * Given a point, ordinate number and value, set that ordinate on the
 * point.
 */
inline void
lwpoint_set_ordinate(POINT4D *p, char ordinate, double value)
{
	if (!p)
	{
		lwerror("Null input geometry.");
		return;
	}

	switch (ordinate)
	{
	case 'X':
		p->x = value;
		return;
	case 'Y':
		p->y = value;
		return;
	case 'Z':
		p->z = value;
		return;
	case 'M':
		p->m = value;
		return;
	}
	lwerror("Cannot set %c ordinate.", ordinate);
	return;
}

/**
 * Given two points, a dimensionality, an ordinate, and an interpolation value
 * generate a new point that is proportionally between the input points,
 * using the values in the provided dimension as the scaling factors.
 */
inline int
point_interpolate(const POINT4D *p1,
		  const POINT4D *p2,
		  POINT4D *p,
		  int hasz,
		  int hasm,
		  char ordinate,
		  double interpolation_value)
{
	static char *dims = "XYZM";
	double p1_value = lwpoint_get_ordinate(p1, ordinate);
	double p2_value = lwpoint_get_ordinate(p2, ordinate);
	double proportion;
	int i = 0;

	assert(ordinate == 'X' || ordinate == 'Y' ||
	       ordinate == 'Z' || ordinate == 'M');
	assert(FP_MIN(p1_value, p2_value) <= interpolation_value &&
	       FP_MAX(p1_value, p2_value) >= interpolation_value);

	proportion = (interpolation_value - p1_value) / (p2_value - p1_value);

	for (i = 0; i < 4; i++)
	{
		if (dims[i] == 'Z' && !hasz)
			continue;
		if (dims[i] == 'M' && !hasm)
			continue;
		if (dims[i] == ordinate)
			lwpoint_set_ordinate(p, dims[i], interpolation_value);
		else
		{
			double newordinate = 0.0;
			p1_value = lwpoint_get_ordinate(p1, dims[i]);
			p2_value = lwpoint_get_ordinate(p2, dims[i]);
			newordinate = p1_value + proportion * (p2_value - p1_value);
			lwpoint_set_ordinate(p, dims[i], newordinate);
		}
	}

	return LW_SUCCESS;
}

/**
 * Clip an input POINT between two values, on any ordinate input.
 */
static inline LWCOLLECTION *
lwpoint_clip_to_ordinate_range(const LWPOINT *point, char ordinate, double from, double to)
{
	LWCOLLECTION *lwgeom_out = NULL;
	char hasz, hasm;
	POINT4D p4d;
	double ordinate_value;

	/* Read Z/M info */
	hasz = lwgeom_has_z(lwpoint_as_lwgeom(point));
	hasm = lwgeom_has_m(lwpoint_as_lwgeom(point));

	/* Prepare return object */
	lwgeom_out = lwcollection_construct_empty(MULTIPOINTTYPE, point->srid, hasz, hasm);

	/* Test if ordinate is in range */
	lwpoint_getPoint4d_p(point, &p4d);
	ordinate_value = lwpoint_get_ordinate(&p4d, ordinate);
	if (from <= ordinate_value && to >= ordinate_value)
	{
		LWPOINT *lwp = lwpoint_clone(point);
		lwcollection_add_lwgeom(lwgeom_out, lwpoint_as_lwgeom(lwp));
	}

	return lwgeom_out;
}

/**
 * Clip an input MULTIPOINT between two values, on any ordinate input.
 */
static inline LWCOLLECTION *
lwmpoint_clip_to_ordinate_range(const LWMPOINT *mpoint, char ordinate, double from, double to)
{
	LWCOLLECTION *lwgeom_out = NULL;
	char hasz, hasm;
	uint32_t i;

	/* Read Z/M info */
	hasz = lwgeom_has_z(lwmpoint_as_lwgeom(mpoint));
	hasm = lwgeom_has_m(lwmpoint_as_lwgeom(mpoint));

	/* Prepare return object */
	lwgeom_out = lwcollection_construct_empty(MULTIPOINTTYPE, mpoint->srid, hasz, hasm);

	/* For each point, is its ordinate value between from and to? */
	for (i = 0; i < mpoint->ngeoms; i++)
	{
		POINT4D p4d;
		double ordinate_value;

		lwpoint_getPoint4d_p(mpoint->geoms[i], &p4d);
		ordinate_value = lwpoint_get_ordinate(&p4d, ordinate);

		if (from <= ordinate_value && to >= ordinate_value)
		{
			LWPOINT *lwp = lwpoint_clone(mpoint->geoms[i]);
			lwcollection_add_lwgeom(lwgeom_out, lwpoint_as_lwgeom(lwp));
		}
	}

	/* Set the bbox, if necessary */
	if (mpoint->bbox)
		lwgeom_refresh_bbox((LWGEOM *)lwgeom_out);

	return lwgeom_out;
}

static inline POINTARRAY *
ptarray_clamp_to_ordinate_range(const POINTARRAY *ipa, char ordinate, double from, double to, uint8_t is_closed)
{
	POINT4D p1, p2;
	POINTARRAY *opa;
	double ovp1, ovp2;
	POINT4D *t;
	int8_t p1out, p2out; /* -1 - smaller than from, 0 - in range, 1 - larger than to */
	uint32_t i;
	uint8_t hasz = FLAGS_GET_Z(ipa->flags);
	uint8_t hasm = FLAGS_GET_M(ipa->flags);

	assert(from <= to);

	t = lwalloc(sizeof(POINT4D));

	/* Initial storage */
	opa = ptarray_construct_empty(hasz, hasm, ipa->npoints);

	/* Add first point */
	getPoint4d_p(ipa, 0, &p1);
	ovp1 = lwpoint_get_ordinate(&p1, ordinate);

	p1out = (ovp1 < from) ? -1 : ((ovp1 > to) ? 1 : 0);

	if (from <= ovp1 && ovp1 <= to)
		ptarray_append_point(opa, &p1, LW_FALSE);

	/* Loop on all other input points */
	for (i = 1; i < ipa->npoints; i++)
	{
		getPoint4d_p(ipa, i, &p2);
		ovp2 = lwpoint_get_ordinate(&p2, ordinate);
		p2out = (ovp2 < from) ? -1 : ((ovp2 > to) ? 1 : 0);

		if (p1out == 0 && p2out == 0) /* both visible */
		{
			ptarray_append_point(opa, &p2, LW_FALSE);
		}
		else if (p1out == p2out && p1out != 0) /* both invisible on the same side */
		{
			/* skip */
		}
		else if (p1out == -1 && p2out == 0)
		{
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, from);
			ptarray_append_point(opa, t, LW_FALSE);
			ptarray_append_point(opa, &p2, LW_FALSE);
		}
		else if (p1out == -1 && p2out == 1)
		{
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, from);
			ptarray_append_point(opa, t, LW_FALSE);
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, to);
			ptarray_append_point(opa, t, LW_FALSE);
		}
		else if (p1out == 0 && p2out == -1)
		{
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, from);
			ptarray_append_point(opa, t, LW_FALSE);
		}
		else if (p1out == 0 && p2out == 1)
		{
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, to);
			ptarray_append_point(opa, t, LW_FALSE);
		}
		else if (p1out == 1 && p2out == -1)
		{
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, to);
			ptarray_append_point(opa, t, LW_FALSE);
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, from);
			ptarray_append_point(opa, t, LW_FALSE);
		}
		else if (p1out == 1 && p2out == 0)
		{
			point_interpolate(&p1, &p2, t, hasz, hasm, ordinate, to);
			ptarray_append_point(opa, t, LW_FALSE);
			ptarray_append_point(opa, &p2, LW_FALSE);
		}

		p1 = p2;
		p1out = p2out;
		LW_ON_INTERRUPT(ptarray_free(opa); return NULL);
	}

	if (is_closed && opa->npoints > 2)
	{
		getPoint4d_p(opa, 0, &p1);
		ptarray_append_point(opa, &p1, LW_FALSE);
	}
	lwfree(t);

	return opa;
}

/**
 * Take in a LINESTRING and return a MULTILINESTRING of those portions of the
 * LINESTRING between the from/to range for the specified ordinate (XYZM)
 */
static inline LWCOLLECTION *
lwline_clip_to_ordinate_range(const LWLINE *line, char ordinate, double from, double to)
{
	POINTARRAY *pa_in = NULL;
	LWCOLLECTION *lwgeom_out = NULL;
	POINTARRAY *dp = NULL;
	uint32_t i;
	int added_last_point = 0;
	POINT4D *p = NULL, *q = NULL, *r = NULL;
	double ordinate_value_p = 0.0, ordinate_value_q = 0.0;
	char hasz, hasm;
	char dims;

	/* Null input, nothing we can do. */
	assert(line);
	hasz = lwgeom_has_z(lwline_as_lwgeom(line));
	hasm = lwgeom_has_m(lwline_as_lwgeom(line));
	dims = FLAGS_NDIMS(line->flags);

	/* Asking for an ordinate we don't have. Error. */
	if ((ordinate == 'Z' && !hasz) || (ordinate == 'M' && !hasm))
	{
		lwerror("Cannot clip on ordinate %d in a %d-d geometry.", ordinate, dims);
		return NULL;
	}

	/* Prepare our working point objects. */
	p = lwalloc(sizeof(POINT4D));
	q = lwalloc(sizeof(POINT4D));
	r = lwalloc(sizeof(POINT4D));

	/* Construct a collection to hold our outputs. */
	lwgeom_out = lwcollection_construct_empty(MULTILINETYPE, line->srid, hasz, hasm);

	/* Get our input point array */
	pa_in = line->points;

	for (i = 0; i < pa_in->npoints; i++)
	{
		if (i > 0)
		{
			*q = *p;
			ordinate_value_q = ordinate_value_p;
		}
		getPoint4d_p(pa_in, i, p);
		ordinate_value_p = lwpoint_get_ordinate(p, ordinate);

		/* Is this point inside the ordinate range? Yes. */
		if (ordinate_value_p >= from && ordinate_value_p <= to)
		{

			if (!added_last_point)
			{
				/* We didn't add the previous point, so this is a new segment.
				 *  Make a new point array. */
				dp = ptarray_construct_empty(hasz, hasm, 32);

				/* We're transiting into the range so add an interpolated
				 *  point at the range boundary.
				 *  If we're on a boundary and crossing from the far side,
				 *  we also need an interpolated point. */
				if (i > 0 &&
				    (/* Don't try to interpolate if this is the first point */
				     (ordinate_value_p > from && ordinate_value_p < to) ||  /* Inside */
				     (ordinate_value_p == from && ordinate_value_q > to) || /* Hopping from above */
				     (ordinate_value_p == to && ordinate_value_q < from)))  /* Hopping from below */
				{
					double interpolation_value;
					(ordinate_value_q > to) ? (interpolation_value = to)
								: (interpolation_value = from);
					point_interpolate(q, p, r, hasz, hasm, ordinate, interpolation_value);
					ptarray_append_point(dp, r, LW_FALSE);
				}
			}
			/* Add the current vertex to the point array. */
			ptarray_append_point(dp, p, LW_FALSE);
			if (ordinate_value_p == from || ordinate_value_p == to)
				added_last_point = 2; /* Added on boundary. */
			else
				added_last_point = 1; /* Added inside range. */
		}
		/* Is this point inside the ordinate range? No. */
		else
		{
			if (added_last_point == 1)
			{
				/* We're transiting out of the range, so add an interpolated point
				 *  to the point array at the range boundary. */
				double interpolation_value;
				(ordinate_value_p > to) ? (interpolation_value = to) : (interpolation_value = from);
				point_interpolate(q, p, r, hasz, hasm, ordinate, interpolation_value);
				ptarray_append_point(dp, r, LW_FALSE);
			}
			else if (added_last_point == 2)
			{
				/* We're out and the last point was on the boundary.
				 *  If the last point was the near boundary, nothing to do.
				 *  If it was the far boundary, we need an interpolated point. */
				if (from != to && ((ordinate_value_q == from && ordinate_value_p > from) ||
						   (ordinate_value_q == to && ordinate_value_p < to)))
				{
					double interpolation_value;
					(ordinate_value_p > to) ? (interpolation_value = to)
								: (interpolation_value = from);
					point_interpolate(q, p, r, hasz, hasm, ordinate, interpolation_value);
					ptarray_append_point(dp, r, LW_FALSE);
				}
			}
			else if (i && ordinate_value_q < from && ordinate_value_p > to)
			{
				/* We just hopped over the whole range, from bottom to top,
				 *  so we need to add *two* interpolated points! */
				dp = ptarray_construct(hasz, hasm, 2);
				/* Interpolate lower point. */
				point_interpolate(p, q, r, hasz, hasm, ordinate, from);
				ptarray_set_point4d(dp, 0, r);
				/* Interpolate upper point. */
				point_interpolate(p, q, r, hasz, hasm, ordinate, to);
				ptarray_set_point4d(dp, 1, r);
			}
			else if (i && ordinate_value_q > to && ordinate_value_p < from)
			{
				/* We just hopped over the whole range, from top to bottom,
				 *  so we need to add *two* interpolated points! */
				dp = ptarray_construct(hasz, hasm, 2);
				/* Interpolate upper point. */
				point_interpolate(p, q, r, hasz, hasm, ordinate, to);
				ptarray_set_point4d(dp, 0, r);
				/* Interpolate lower point. */
				point_interpolate(p, q, r, hasz, hasm, ordinate, from);
				ptarray_set_point4d(dp, 1, r);
			}
			/* We have an extant point-array, save it out to a multi-line. */
			if (dp)
			{
				/* Only one point, so we have to make an lwpoint to hold this
				 *  and set the overall output type to a generic collection. */
				if (dp->npoints == 1)
				{
					LWPOINT *opoint = lwpoint_construct(line->srid, NULL, dp);
					lwgeom_out->type = COLLECTIONTYPE;
					lwgeom_out = lwcollection_add_lwgeom(lwgeom_out, lwpoint_as_lwgeom(opoint));
				}
				else
				{
					LWLINE *oline = lwline_construct(line->srid, NULL, dp);
					lwgeom_out = lwcollection_add_lwgeom(lwgeom_out, lwline_as_lwgeom(oline));
				}

				/* Pointarray is now owned by lwgeom_out, so drop reference to it */
				dp = NULL;
			}
			added_last_point = 0;
		}
	}

	/* Still some points left to be saved out. */
	if (dp)
	{
		if (dp->npoints == 1)
		{
			LWPOINT *opoint = lwpoint_construct(line->srid, NULL, dp);
			lwgeom_out->type = COLLECTIONTYPE;
			lwgeom_out = lwcollection_add_lwgeom(lwgeom_out, lwpoint_as_lwgeom(opoint));
		}
		else if (dp->npoints > 1)
		{
			LWLINE *oline = lwline_construct(line->srid, NULL, dp);
			lwgeom_out = lwcollection_add_lwgeom(lwgeom_out, lwline_as_lwgeom(oline));
		}
		else
			ptarray_free(dp);
	}

	lwfree(p);
	lwfree(q);
	lwfree(r);

	if (line->bbox && lwgeom_out->ngeoms > 0)
		lwgeom_refresh_bbox((LWGEOM *)lwgeom_out);

	return lwgeom_out;
}

/**
 * Clip an input LWPOLY between two values, on any ordinate input.
 */
static inline LWCOLLECTION *
lwpoly_clip_to_ordinate_range(const LWPOLY *poly, char ordinate, double from, double to)
{
	assert(poly);
	char hasz = FLAGS_GET_Z(poly->flags), hasm = FLAGS_GET_M(poly->flags);
	LWPOLY *poly_res = lwpoly_construct_empty(poly->srid, hasz, hasm);
	LWCOLLECTION *lwgeom_out = lwcollection_construct_empty(MULTIPOLYGONTYPE, poly->srid, hasz, hasm);

	for (uint32_t i = 0; i < poly->nrings; i++)
	{
		/* Ret number of points */
		POINTARRAY *pa = ptarray_clamp_to_ordinate_range(poly->rings[i], ordinate, from, to, LW_TRUE);

		if (!pa)
			return NULL;

		if (pa->npoints >= 4)
			lwpoly_add_ring(poly_res, pa);
		else
		{
			ptarray_free(pa);
			if (i == 0)
				break;
		}
	}
	if (poly_res->nrings > 0)
		lwgeom_out = lwcollection_add_lwgeom(lwgeom_out, (LWGEOM *)poly_res);
	else
		lwpoly_free(poly_res);

	return lwgeom_out;
}

/**
 * Clip an input LWTRIANGLE between two values, on any ordinate input.
 */
static inline LWCOLLECTION *
lwtriangle_clip_to_ordinate_range(const LWTRIANGLE *tri, char ordinate, double from, double to)
{
	assert(tri);
	char hasz = FLAGS_GET_Z(tri->flags), hasm = FLAGS_GET_M(tri->flags);
	LWCOLLECTION *lwgeom_out = lwcollection_construct_empty(TINTYPE, tri->srid, hasz, hasm);
	POINTARRAY *pa = ptarray_clamp_to_ordinate_range(tri->points, ordinate, from, to, LW_TRUE);

	if (!pa)
		return NULL;

	if (pa->npoints >= 4)
	{
		POINT4D first = getPoint4d(pa, 0);
		for (uint32_t i = 1; i < pa->npoints - 2; i++)
		{
			POINT4D p;
			POINTARRAY *tpa = ptarray_construct_empty(hasz, hasm, 4);
			ptarray_append_point(tpa, &first, LW_TRUE);
			getPoint4d_p(pa, i, &p);
			ptarray_append_point(tpa, &p, LW_TRUE);
			getPoint4d_p(pa, i + 1, &p);
			ptarray_append_point(tpa, &p, LW_TRUE);
			ptarray_append_point(tpa, &first, LW_TRUE);
			LWTRIANGLE *otri = lwtriangle_construct(tri->srid, NULL, tpa);
			lwgeom_out = lwcollection_add_lwgeom(lwgeom_out, (LWGEOM *)otri);
		}
	}
	ptarray_free(pa);
	return lwgeom_out;
}

/**
 * Clip an input COLLECTION between two values, on any ordinate input.
 */
static inline LWCOLLECTION *
lwcollection_clip_to_ordinate_range(const LWCOLLECTION *icol, char ordinate, double from, double to)
{
	LWCOLLECTION *lwgeom_out;

	assert(icol);
	if (icol->ngeoms == 1)
		lwgeom_out = lwgeom_clip_to_ordinate_range(icol->geoms[0], ordinate, from, to, 0);
	else
	{
		LWCOLLECTION *col;
		char hasz = lwgeom_has_z(lwcollection_as_lwgeom(icol));
		char hasm = lwgeom_has_m(lwcollection_as_lwgeom(icol));
		uint32_t i;
		lwgeom_out = lwcollection_construct_empty(icol->type, icol->srid, hasz, hasm);
		FLAGS_SET_Z(lwgeom_out->flags, hasz);
		FLAGS_SET_M(lwgeom_out->flags, hasm);
		for (i = 0; i < icol->ngeoms; i++)
		{
			col = lwgeom_clip_to_ordinate_range(icol->geoms[i], ordinate, from, to, 0);
			if (col)
			{
				if (col->type != icol->type)
					lwgeom_out->type = COLLECTIONTYPE;
				lwgeom_out = lwcollection_concat_in_place(lwgeom_out, col);
				lwfree(col->geoms);
				lwcollection_release(col);
			}
		}
	}

	if (icol->bbox)
		lwgeom_refresh_bbox((LWGEOM *)lwgeom_out);

	return lwgeom_out;
}

LWCOLLECTION *
lwgeom_clip_to_ordinate_range(const LWGEOM *lwin, char ordinate, double from, double to, double offset)
{
	LWCOLLECTION *out_col;
	LWCOLLECTION *out_offset;
	uint32_t i;

	/* Ensure 'from' is less than 'to'. */
	if (to < from)
	{
		double t = from;
		from = to;
		to = t;
	}

	if (!lwin)
		lwerror("lwgeom_clip_to_ordinate_range: null input geometry!");

	switch (lwin->type)
	{
	case LINETYPE:
		out_col = lwline_clip_to_ordinate_range((LWLINE *)lwin, ordinate, from, to);
		break;
	case MULTIPOINTTYPE:
		out_col = lwmpoint_clip_to_ordinate_range((LWMPOINT *)lwin, ordinate, from, to);
		break;
	case POINTTYPE:
		out_col = lwpoint_clip_to_ordinate_range((LWPOINT *)lwin, ordinate, from, to);
		break;
	case POLYGONTYPE:
		out_col = lwpoly_clip_to_ordinate_range((LWPOLY *)lwin, ordinate, from, to);
		break;
	case TRIANGLETYPE:
		out_col = lwtriangle_clip_to_ordinate_range((LWTRIANGLE *)lwin, ordinate, from, to);
		break;
	case TINTYPE:
	case MULTILINETYPE:
	case MULTIPOLYGONTYPE:
	case COLLECTIONTYPE:
	case POLYHEDRALSURFACETYPE:
		out_col = lwcollection_clip_to_ordinate_range((LWCOLLECTION *)lwin, ordinate, from, to);
		break;
	default:
		lwerror("This function does not accept %s geometries.", lwtype_name(lwin->type));
		return NULL;
	}

	/* Stop if result is NULL */
	if (!out_col)
		lwerror("lwgeom_clip_to_ordinate_range clipping routine returned NULL");

	/* Return if we aren't going to offset the result */
	if (FP_IS_ZERO(offset) || lwgeom_is_empty(lwcollection_as_lwgeom(out_col)))
		return out_col;

	/* Construct a collection to hold our outputs. */
	/* Things get ugly: GEOS offset drops Z's and M's so we have to drop ours */
	out_offset = lwcollection_construct_empty(MULTILINETYPE, lwin->srid, 0, 0);

	/* Try and offset the linear portions of the return value */
	for (i = 0; i < out_col->ngeoms; i++)
	{
		int type = out_col->geoms[i]->type;
		if (type == POINTTYPE)
		{
			lwnotice("lwgeom_clip_to_ordinate_range cannot offset a clipped point");
			continue;
		}
		else if (type == LINETYPE)
		{
			/* lwgeom_offsetcurve(line, offset, quadsegs, joinstyle (round), mitrelimit) */
			LWGEOM *lwoff = lwgeom_offsetcurve(out_col->geoms[i], offset, 8, 1, 5.0);
			if (!lwoff)
			{
				lwerror("lwgeom_offsetcurve returned null");
			}
			lwcollection_add_lwgeom(out_offset, lwoff);
		}
		else
		{
			lwerror("lwgeom_clip_to_ordinate_range found an unexpected type (%s) in the offset routine",
				lwtype_name(type));
		}
	}

	return out_offset;
}

LWCOLLECTION *
lwgeom_locate_between(const LWGEOM *lwin, double from, double to, double offset)
{
	if (!lwgeom_has_m(lwin))
		lwerror("Input geometry does not have a measure dimension");

	return lwgeom_clip_to_ordinate_range(lwin, 'M', from, to, offset);
}

double
lwgeom_interpolate_point(const LWGEOM *lwin, const LWPOINT *lwpt)
{
	POINT4D p, p_proj;
	double ret = 0.0;

	if (!lwin)
		lwerror("lwgeom_interpolate_point: null input geometry!");

	if (!lwgeom_has_m(lwin))
		lwerror("Input geometry does not have a measure dimension");

	if (lwgeom_is_empty(lwin) || lwpoint_is_empty(lwpt))
		lwerror("Input geometry is empty");

	switch (lwin->type)
	{
	case LINETYPE:
	{
		LWLINE *lwline = lwgeom_as_lwline(lwin);
		lwpoint_getPoint4d_p(lwpt, &p);
		ret = ptarray_locate_point(lwline->points, &p, NULL, &p_proj);
		ret = p_proj.m;
		break;
	}
	default:
		lwerror("This function does not accept %s geometries.", lwtype_name(lwin->type));
	}
	return ret;
}

/*
 * Time of closest point of approach
 *
 * Given two vectors (p1-p2 and q1-q2) and
 * a time range (t1-t2) return the time in which
 * a point p is closest to a point q on their
 * respective vectors, and the actual points
 *
 * Here we use algorithm from softsurfer.com
 * that can be found here
 * http://softsurfer.com/Archive/algorithm_0106/algorithm_0106.htm
 *
 * @param p0 start of first segment, will be set to actual
 *           closest point of approach on segment.
 * @param p1 end of first segment
 * @param q0 start of second segment, will be set to actual
 *           closest point of approach on segment.
 * @param q1 end of second segment
 * @param t0 start of travel time
 * @param t1 end of travel time
 *
 * @return time of closest point of approach
 *
 */
static double
segments_tcpa(POINT4D *p0, const POINT4D *p1, POINT4D *q0, const POINT4D *q1, double t0, double t1)
{
	POINT3DZ pv; /* velocity of p, aka u */
	POINT3DZ qv; /* velocity of q, aka v */
	POINT3DZ dv; /* velocity difference */
	POINT3DZ w0; /* vector between first points */

	/*
	  lwnotice("FROM %g,%g,%g,%g -- %g,%g,%g,%g",
	    p0->x, p0->y, p0->z, p0->m,
	    p1->x, p1->y, p1->z, p1->m);
	  lwnotice("  TO %g,%g,%g,%g -- %g,%g,%g,%g",
	    q0->x, q0->y, q0->z, q0->m,
	    q1->x, q1->y, q1->z, q1->m);
	*/

	/* PV aka U */
	pv.x = (p1->x - p0->x);
	pv.y = (p1->y - p0->y);
	pv.z = (p1->z - p0->z);
	/*lwnotice("PV:  %g, %g, %g", pv.x, pv.y, pv.z);*/

	/* QV aka V */
	qv.x = (q1->x - q0->x);
	qv.y = (q1->y - q0->y);
	qv.z = (q1->z - q0->z);
	/*lwnotice("QV:  %g, %g, %g", qv.x, qv.y, qv.z);*/

	dv.x = pv.x - qv.x;
	dv.y = pv.y - qv.y;
	dv.z = pv.z - qv.z;
	/*lwnotice("DV:  %g, %g, %g", dv.x, dv.y, dv.z);*/

	double dv2 = DOT(dv, dv);
	/*lwnotice("DOT: %g", dv2);*/

	if (dv2 == 0.0)
	{
		/* Distance is the same at any time, we pick the earliest */
		return t0;
	}

	/* Distance at any given time, with t0 */
	w0.x = (p0->x - q0->x);
	w0.y = (p0->y - q0->y);
	w0.z = (p0->z - q0->z);

	/*lwnotice("W0:  %g, %g, %g", w0.x, w0.y, w0.z);*/

	/* Check that at distance dt w0 is distance */

	/* This is the fraction of measure difference */
	double t = -DOT(w0, dv) / dv2;
	/*lwnotice("CLOSEST TIME (fraction): %g", t);*/

	if (t > 1.0)
	{
		/* Getting closer as we move to the end */
		/*lwnotice("Converging");*/
		t = 1;
	}
	else if (t < 0.0)
	{
		/*lwnotice("Diverging");*/
		t = 0;
	}

	/* Interpolate the actual points now */

	p0->x += pv.x * t;
	p0->y += pv.y * t;
	p0->z += pv.z * t;

	q0->x += qv.x * t;
	q0->y += qv.y * t;
	q0->z += qv.z * t;

	t = t0 + (t1 - t0) * t;
	/*lwnotice("CLOSEST TIME (real): %g", t);*/

	return t;
}

static int
ptarray_collect_mvals(const POINTARRAY *pa, double tmin, double tmax, double *mvals)
{
	POINT4D pbuf;
	uint32_t i, n = 0;
	for (i = 0; i < pa->npoints; ++i)
	{
		getPoint4d_p(pa, i, &pbuf); /* could be optimized */
		if (pbuf.m >= tmin && pbuf.m <= tmax)
			mvals[n++] = pbuf.m;
	}
	return n;
}

static int
compare_double(const void *pa, const void *pb)
{
	double a = *((double *)pa);
	double b = *((double *)pb);
	if (a < b)
		return -1;
	else if (a > b)
		return 1;
	else
		return 0;
}

/* Return number of elements in unique array */
static int
uniq(double *vals, int nvals)
{
	int i, last = 0;
	for (i = 1; i < nvals; ++i)
	{
		// lwnotice("(I%d):%g", i, vals[i]);
		if (vals[i] != vals[last])
		{
			vals[++last] = vals[i];
			// lwnotice("(O%d):%g", last, vals[last]);
		}
	}
	return last + 1;
}

/*
 * Find point at a given measure
 *
 * The function assumes measures are linear so that always a single point
 * is returned for a single measure.
 *
 * @param pa the point array to perform search on
 * @param m the measure to search for
 * @param p the point to write result into
 * @param from the segment number to start from
 *
 * @return the segment number the point was found into
 *         or -1 if given measure was out of the known range.
 */
static int
ptarray_locate_along_linear(const POINTARRAY *pa, double m, POINT4D *p, uint32_t from)
{
	uint32_t i = from;
	POINT4D p1, p2;

	/* Walk through each segment in the point array */
	getPoint4d_p(pa, i, &p1);
	for (i = from + 1; i < pa->npoints; i++)
	{
		getPoint4d_p(pa, i, &p2);

		if (segment_locate_along(&p1, &p2, m, 0, p) == LW_TRUE)
			return i - 1; /* found */

		p1 = p2;
	}

	return -1; /* not found */
}

double
lwgeom_tcpa(const LWGEOM *g1, const LWGEOM *g2, double *mindist)
{
	LWLINE *l1, *l2;
	int i;
	GBOX gbox1, gbox2;
	double tmin, tmax;
	double *mvals;
	int nmvals = 0;
	double mintime;
	double mindist2 = FLT_MAX; /* minimum distance, squared */

	if (!lwgeom_has_m(g1) || !lwgeom_has_m(g2))
	{
		lwerror("Both input geometries must have a measure dimension");
		return -1;
	}

	l1 = lwgeom_as_lwline(g1);
	l2 = lwgeom_as_lwline(g2);

	if (!l1 || !l2)
	{
		lwerror("Both input geometries must be linestrings");
		return -1;
	}

	if (l1->points->npoints < 2 || l2->points->npoints < 2)
	{
		lwerror("Both input lines must have at least 2 points");
		return -1;
	}

	/* We use lwgeom_calculate_gbox() instead of lwgeom_get_gbox() */
	/* because we cannot afford the float rounding inaccuracy when */
	/* we compare the ranges for overlap below */
	lwgeom_calculate_gbox(g1, &gbox1);
	lwgeom_calculate_gbox(g2, &gbox2);

	/*
	 * Find overlapping M range
	 * WARNING: may be larger than the real one
	 */

	tmin = FP_MAX(gbox1.mmin, gbox2.mmin);
	tmax = FP_MIN(gbox1.mmax, gbox2.mmax);

	if (tmax < tmin)
	{
		LWDEBUG(1, "Inputs never exist at the same time");
		return -2;
	}

	// lwnotice("Min:%g, Max:%g", tmin, tmax);

	/*
	 * Collect M values in common time range from inputs
	 */

	mvals = lwalloc(sizeof(double) * (l1->points->npoints + l2->points->npoints));

	/* TODO: also clip the lines ? */
	nmvals = ptarray_collect_mvals(l1->points, tmin, tmax, mvals);
	nmvals += ptarray_collect_mvals(l2->points, tmin, tmax, mvals + nmvals);

	/* Sort values in ascending order */
	qsort(mvals, nmvals, sizeof(double), compare_double);

	/* Remove duplicated values */
	nmvals = uniq(mvals, nmvals);

	if (nmvals < 2)
	{
		{
			/* there's a single time, must be that one... */
			double t0 = mvals[0];
			POINT4D p0, p1;
			LWDEBUGF(1, "Inputs only exist both at a single time (%g)", t0);
			if (mindist)
			{
				if (-1 == ptarray_locate_along_linear(l1->points, t0, &p0, 0))
				{
					lwfree(mvals);
					lwerror("Could not find point with M=%g on first geom", t0);
					return -1;
				}
				if (-1 == ptarray_locate_along_linear(l2->points, t0, &p1, 0))
				{
					lwfree(mvals);
					lwerror("Could not find point with M=%g on second geom", t0);
					return -1;
				}
				*mindist = distance3d_pt_pt((POINT3D *)&p0, (POINT3D *)&p1);
			}
			lwfree(mvals);
			return t0;
		}
	}

	/*
	 * For each consecutive pair of measures, compute time of closest point
	 * approach and actual distance between points at that time
	 */
	mintime = tmin;
	for (i = 1; i < nmvals; ++i)
	{
		double t0 = mvals[i - 1];
		double t1 = mvals[i];
		double t;
		POINT4D p0, p1, q0, q1;
		int seg;
		double dist2;

		// lwnotice("T %g-%g", t0, t1);

		seg = ptarray_locate_along_linear(l1->points, t0, &p0, 0);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 1: %g, %g, %g", t0, seg, p0.x, p0.y, p0.z);

		seg = ptarray_locate_along_linear(l1->points, t1, &p1, seg);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 1: %g, %g, %g", t1, seg, p1.x, p1.y, p1.z);

		seg = ptarray_locate_along_linear(l2->points, t0, &q0, 0);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 2: %g, %g, %g", t0, seg, q0.x, q0.y, q0.z);

		seg = ptarray_locate_along_linear(l2->points, t1, &q1, seg);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 2: %g, %g, %g", t1, seg, q1.x, q1.y, q1.z);

		t = segments_tcpa(&p0, &p1, &q0, &q1, t0, t1);

		/*
		lwnotice("Closest points: %g,%g,%g and %g,%g,%g at time %g",
		p0.x, p0.y, p0.z,
		q0.x, q0.y, q0.z, t);
		*/

		dist2 = (q0.x - p0.x) * (q0.x - p0.x) + (q0.y - p0.y) * (q0.y - p0.y) + (q0.z - p0.z) * (q0.z - p0.z);
		if (dist2 < mindist2)
		{
			mindist2 = dist2;
			mintime = t;
			// lwnotice("MINTIME: %g", mintime);
		}
	}

	/*
	 * Release memory
	 */

	lwfree(mvals);

	if (mindist)
	{
		*mindist = sqrt(mindist2);
	}
	/*lwnotice("MINDIST: %g", sqrt(mindist2));*/

	return mintime;
}

int
lwgeom_cpa_within(const LWGEOM *g1, const LWGEOM *g2, double maxdist)
{
	LWLINE *l1, *l2;
	int i;
	GBOX gbox1, gbox2;
	double tmin, tmax;
	double *mvals;
	int nmvals = 0;
	double maxdist2 = maxdist * maxdist;
	int within = LW_FALSE;

	if (!lwgeom_has_m(g1) || !lwgeom_has_m(g2))
	{
		lwerror("Both input geometries must have a measure dimension");
		return LW_FALSE;
	}

	l1 = lwgeom_as_lwline(g1);
	l2 = lwgeom_as_lwline(g2);

	if (!l1 || !l2)
	{
		lwerror("Both input geometries must be linestrings");
		return LW_FALSE;
	}

	if (l1->points->npoints < 2 || l2->points->npoints < 2)
	{
		/* TODO: return distance between these two points */
		lwerror("Both input lines must have at least 2 points");
		return LW_FALSE;
	}

	/* We use lwgeom_calculate_gbox() instead of lwgeom_get_gbox() */
	/* because we cannot afford the float rounding inaccuracy when */
	/* we compare the ranges for overlap below */
	lwgeom_calculate_gbox(g1, &gbox1);
	lwgeom_calculate_gbox(g2, &gbox2);

	/*
	 * Find overlapping M range
	 * WARNING: may be larger than the real one
	 */

	tmin = FP_MAX(gbox1.mmin, gbox2.mmin);
	tmax = FP_MIN(gbox1.mmax, gbox2.mmax);

	if (tmax < tmin)
	{
		LWDEBUG(1, "Inputs never exist at the same time");
		return LW_FALSE;
	}

	/*
	 * Collect M values in common time range from inputs
	 */

	mvals = lwalloc(sizeof(double) * (l1->points->npoints + l2->points->npoints));

	/* TODO: also clip the lines ? */
	nmvals = ptarray_collect_mvals(l1->points, tmin, tmax, mvals);
	nmvals += ptarray_collect_mvals(l2->points, tmin, tmax, mvals + nmvals);

	/* Sort values in ascending order */
	qsort(mvals, nmvals, sizeof(double), compare_double);

	/* Remove duplicated values */
	nmvals = uniq(mvals, nmvals);

	if (nmvals < 2)
	{
		/* there's a single time, must be that one... */
		double t0 = mvals[0];
		POINT4D p0, p1;
		LWDEBUGF(1, "Inputs only exist both at a single time (%g)", t0);
		if (-1 == ptarray_locate_along_linear(l1->points, t0, &p0, 0))
		{
			lwnotice("Could not find point with M=%g on first geom", t0);
			return LW_FALSE;
		}
		if (-1 == ptarray_locate_along_linear(l2->points, t0, &p1, 0))
		{
			lwnotice("Could not find point with M=%g on second geom", t0);
			return LW_FALSE;
		}
		if (distance3d_pt_pt((POINT3D *)&p0, (POINT3D *)&p1) <= maxdist)
			within = LW_TRUE;
		lwfree(mvals);
		return within;
	}

	/*
	 * For each consecutive pair of measures, compute time of closest point
	 * approach and actual distance between points at that time
	 */
	for (i = 1; i < nmvals; ++i)
	{
		double t0 = mvals[i - 1];
		double t1 = mvals[i];
#if POSTGIS_DEBUG_LEVEL >= 1
		double t;
#endif
		POINT4D p0, p1, q0, q1;
		int seg;
		double dist2;

		// lwnotice("T %g-%g", t0, t1);

		seg = ptarray_locate_along_linear(l1->points, t0, &p0, 0);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 1: %g, %g, %g", t0, seg, p0.x, p0.y, p0.z);

		seg = ptarray_locate_along_linear(l1->points, t1, &p1, seg);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 1: %g, %g, %g", t1, seg, p1.x, p1.y, p1.z);

		seg = ptarray_locate_along_linear(l2->points, t0, &q0, 0);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
		// lwnotice("Measure %g on segment %d of line 2: %g, %g, %g", t0, seg, q0.x, q0.y, q0.z);

		seg = ptarray_locate_along_linear(l2->points, t1, &q1, seg);
		if (-1 == seg)
			continue; /* possible, if GBOX is approximated */
			// lwnotice("Measure %g on segment %d of line 2: %g, %g, %g", t1, seg, q1.x, q1.y, q1.z);

#if POSTGIS_DEBUG_LEVEL >= 1
		t =
#endif
		    segments_tcpa(&p0, &p1, &q0, &q1, t0, t1);

		/*
		lwnotice("Closest points: %g,%g,%g and %g,%g,%g at time %g",
		p0.x, p0.y, p0.z,
		q0.x, q0.y, q0.z, t);
		*/

		dist2 = (q0.x - p0.x) * (q0.x - p0.x) + (q0.y - p0.y) * (q0.y - p0.y) + (q0.z - p0.z) * (q0.z - p0.z);
		if (dist2 <= maxdist2)
		{
			LWDEBUGF(1, "Within distance %g at time %g, breaking", sqrt(dist2), t);
			within = LW_TRUE;
			break;
		}
	}

	/*
	 * Release memory
	 */

	lwfree(mvals);

	return within;
}