Find the time of closest point of approach.
1145{
1147 int i;
1149 double tmin, tmax;
1150 double *mvals;
1151 int nmvals = 0;
1152 double mintime;
1153 double mindist2 = FLT_MAX;
1154
1156 {
1157 lwerror(
"Both input geometries must have a measure dimension");
1158 return -1;
1159 }
1160
1163
1164 if (!l1 || !l2)
1165 {
1166 lwerror(
"Both input geometries must be linestrings");
1167 return -1;
1168 }
1169
1171 {
1172 lwerror(
"Both input lines must have at least 2 points");
1173 return -1;
1174 }
1175
1176
1177
1178
1181
1182
1183
1184
1185
1186
1189
1190 if (tmax < tmin)
1191 {
1192 LWDEBUG(1,
"Inputs never exist at the same time");
1193 return -2;
1194 }
1195
1196
1197
1198
1199
1200
1201
1203
1204
1207
1208
1210
1211
1212 nmvals =
uniq(mvals, nmvals);
1213
1214 if (nmvals < 2)
1215 {
1216 {
1217
1218 double t0 = mvals[0];
1220 LWDEBUGF(1,
"Inputs only exist both at a single time (%g)", t0);
1221 if (mindist)
1222 {
1224 {
1226 lwerror(
"Could not find point with M=%g on first geom", t0);
1227 return -1;
1228 }
1230 {
1232 lwerror(
"Could not find point with M=%g on second geom", t0);
1233 return -1;
1234 }
1236 }
1238 return t0;
1239 }
1240 }
1241
1242
1243
1244
1245
1246 mintime = tmin;
1247 for (i = 1; i < nmvals; ++i)
1248 {
1249 double t0 = mvals[i - 1];
1250 double t1 = mvals[i];
1251 double t;
1253 int seg;
1254 double dist2;
1255
1256
1257
1259 if (-1 == seg)
1260 continue;
1261
1262
1264 if (-1 == seg)
1265 continue;
1266
1267
1269 if (-1 == seg)
1270 continue;
1271
1272
1274 if (-1 == seg)
1275 continue;
1276
1277
1279
1280
1281
1282
1283
1284
1285
1286 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);
1287 if (dist2 < mindist2)
1288 {
1289 mindist2 = dist2;
1290 mintime = t;
1291
1292 }
1293 }
1294
1295
1296
1297
1298
1300
1301 if (mindist)
1302 {
1303 *mindist = sqrt(mindist2);
1304 }
1305
1306
1307 return mintime;
1308}
void * lwalloc(size_t size)
double distance3d_pt_pt(const POINT3D *p1, const POINT3D *p2)
int lwgeom_calculate_gbox(const LWGEOM *lwgeom, GBOX *gbox)
Calculate bounding box of a geometry, automatically taking into account whether it is cartesian or ge...
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
#define LWDEBUG(level, msg)
#define LWDEBUGF(level, msg,...)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
static int ptarray_collect_mvals(const POINTARRAY *pa, double tmin, double tmax, double *mvals)
static int compare_double(const void *pa, const void *pb)
static double segments_tcpa(POINT4D *p0, const POINT4D *p1, POINT4D *q0, const POINT4D *q1, double t0, double t1)
static int ptarray_locate_along_linear(const POINTARRAY *pa, double m, POINT4D *p, uint32_t from)
static int uniq(double *vals, int nvals)