PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ do_median_test()

static void do_median_test ( char *  input,
char *  expected,
int  fail_if_not_converged,
int  iter_count 
)
static

Definition at line 1148 of file cu_algorithm.c.

1149 {
1152  LWPOINT* expected_result = NULL;
1153  POINT4D actual_pt;
1154  POINT4D expected_pt;
1155  const double tolerance = FP_TOLERANCE / 10.0;
1156 
1157  LWPOINT* result = lwgeom_median(g, tolerance, iter_count, fail_if_not_converged);
1158  int passed = LW_FALSE;
1159 
1160  if (expected != NULL)
1161  {
1162  expected_result = lwgeom_as_lwpoint(lwgeom_from_wkt(expected, LW_PARSER_CHECK_NONE));
1163  lwpoint_getPoint4d_p(expected_result, &expected_pt);
1164  }
1165  if (result != NULL)
1166  {
1167  lwpoint_getPoint4d_p(result, &actual_pt);
1168  }
1169 
1170  if (result != NULL && expected != NULL) /* got something, expecting something */
1171  {
1172  passed = LW_TRUE;
1173  passed = passed && lwgeom_is_empty((LWGEOM*) expected_result) == lwgeom_is_empty((LWGEOM*) result);
1174  passed = passed && (lwgeom_has_z((LWGEOM*) expected_result) == lwgeom_has_z((LWGEOM*) result));
1175 
1176  if (passed && !lwgeom_is_empty((LWGEOM*) result))
1177  {
1178  if (g->type == POINTTYPE)
1179  {
1180  passed &= fabs(actual_pt.x - expected_pt.x) < tolerance;
1181  passed &= fabs(actual_pt.y - expected_pt.y) < tolerance;
1182  passed &= (!lwgeom_has_z((LWGEOM*) expected_result) || fabs(actual_pt.z - expected_pt.z) < tolerance);
1183  passed &= (!lwgeom_has_m((LWGEOM*) expected_result) || fabs(actual_pt.m - expected_pt.m) < tolerance);
1184  }
1185  else
1186  {
1187  /* Check that the difference between the obtained geometric
1188  median and the expected point is within tolerance */
1189  uint32_t npoints = 1;
1190  int input_empty = LW_TRUE;
1191  POINT4D* points = lwmpoint_extract_points_4d(lwgeom_as_lwmpoint(g), &npoints, &input_empty);
1192  double distance_expected = test_weighted_distance(&expected_pt, points, npoints);
1193  double distance_result = test_weighted_distance(&actual_pt, points, npoints);
1194 
1195  passed = distance_result <= (1.0 + tolerance) * distance_expected;
1196  if (!passed)
1197  {
1198  printf("Diff: Got %.10f Expected %.10f\n", distance_result, distance_expected);
1199  }
1200  lwfree(points);
1201  }
1202  }
1203 
1204  if (!passed)
1205  {
1206  printf("median_test input %s (parsed %s) expected %s got %s\n",
1207  input, lwgeom_to_ewkt(g),
1208  lwgeom_to_ewkt((LWGEOM*) expected_result),
1209  lwgeom_to_ewkt((LWGEOM*) result));
1210  }
1211 
1212  }
1213  else if (result == NULL && expected == NULL) /* got nothing, expecting nothing */
1214  {
1215  passed = LW_TRUE;
1216  }
1217  else if (result != NULL && expected == NULL) /* got something, expecting nothing */
1218  {
1219  passed = LW_FALSE;
1220  printf("median_test input %s (parsed %s) expected NULL got %s\n", input, lwgeom_to_ewkt(g), lwgeom_to_ewkt((LWGEOM*) result));
1221  }
1222  else if (result == NULL && expected != NULL) /* got nothing, expecting something */
1223  {
1224  passed = LW_FALSE;
1225  printf("%s", cu_error_msg);
1226  printf("median_test input %s (parsed %s) expected %s got NULL\n", input, lwgeom_to_ewkt(g), lwgeom_to_ewkt((LWGEOM*) expected_result));
1227  }
1228 
1229  CU_ASSERT_TRUE(passed);
1230 
1231  lwgeom_free(g);
1232  lwpoint_free(expected_result);
1233  lwpoint_free(result);
1234 }
static double test_weighted_distance(const POINT4D *curr, const POINT4D *points, uint32_t npoints)
void cu_error_msg_reset()
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
#define LW_FALSE
Definition: liblwgeom.h:77
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:233
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2005
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:930
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:85
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
void lwfree(void *mem)
Definition: lwutil.c:244
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwgeom.c:1393
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
LWPOINT * lwgeom_median(const LWGEOM *g, double tol, uint32_t maxiter, char fail_if_not_converged)
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:937
POINT4D * lwmpoint_extract_points_4d(const LWMPOINT *g, uint32_t *npoints, int *input_empty)
#define FP_TOLERANCE
Floating point comparators.
uint8_t type
Definition: liblwgeom.h:399
double m
Definition: liblwgeom.h:355
double x
Definition: liblwgeom.h:355
double z
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355
unsigned int uint32_t
Definition: uthash.h:78

References cu_error_msg, cu_error_msg_reset(), FP_TOLERANCE, LW_FALSE, LW_PARSER_CHECK_NONE, LW_TRUE, lwfree(), lwgeom_as_lwmpoint(), lwgeom_as_lwpoint(), lwgeom_free(), lwgeom_from_wkt(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_is_empty(), lwgeom_median(), lwgeom_to_ewkt(), lwmpoint_extract_points_4d(), lwpoint_free(), lwpoint_getPoint4d_p(), POINT4D::m, POINTTYPE, test_weighted_distance(), LWGEOM::type, POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by test_median_robustness().

Here is the call graph for this function:
Here is the caller graph for this function: