PostGIS  2.5.1dev-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 1131 of file cu_algorithm.c.

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().

1132 {
1135  LWPOINT* expected_result = NULL;
1136  POINT4D actual_pt;
1137  POINT4D expected_pt;
1138  const double tolerance = FP_TOLERANCE / 10.0;
1139 
1140  LWPOINT* result = lwgeom_median(g, tolerance, iter_count, fail_if_not_converged);
1141  int passed = LW_FALSE;
1142 
1143  if (expected != NULL)
1144  {
1145  expected_result = lwgeom_as_lwpoint(lwgeom_from_wkt(expected, LW_PARSER_CHECK_NONE));
1146  lwpoint_getPoint4d_p(expected_result, &expected_pt);
1147  }
1148  if (result != NULL)
1149  {
1150  lwpoint_getPoint4d_p(result, &actual_pt);
1151  }
1152 
1153  if (result != NULL && expected != NULL) /* got something, expecting something */
1154  {
1155  passed = LW_TRUE;
1156  passed = passed && lwgeom_is_empty((LWGEOM*) expected_result) == lwgeom_is_empty((LWGEOM*) result);
1157  passed = passed && (lwgeom_has_z((LWGEOM*) expected_result) == lwgeom_has_z((LWGEOM*) result));
1158 
1159  if (passed && !lwgeom_is_empty((LWGEOM*) result))
1160  {
1161  if (g->type == POINTTYPE)
1162  {
1163  passed &= fabs(actual_pt.x - expected_pt.x) < tolerance;
1164  passed &= fabs(actual_pt.y - expected_pt.y) < tolerance;
1165  passed &= (!lwgeom_has_z((LWGEOM*) expected_result) || fabs(actual_pt.z - expected_pt.z) < tolerance);
1166  passed &= (!lwgeom_has_m((LWGEOM*) expected_result) || fabs(actual_pt.m - expected_pt.m) < tolerance);
1167  }
1168  else
1169  {
1170  /* Check that the difference between the obtained geometric
1171  median and the expected point is within tolerance */
1172  uint32_t npoints = 1;
1173  int input_empty = LW_TRUE;
1174  POINT4D* points = lwmpoint_extract_points_4d(lwgeom_as_lwmpoint(g), &npoints, &input_empty);
1175  double distance_expected = test_weighted_distance(&expected_pt, points, npoints);
1176  double distance_result = test_weighted_distance(&actual_pt, points, npoints);
1177 
1178  passed = distance_result <= (1.0 + tolerance) * distance_expected;
1179  if (!passed)
1180  {
1181  printf("Diff: Got %.10f Expected %.10f\n", distance_result, distance_expected);
1182  }
1183  lwfree(points);
1184  }
1185  }
1186 
1187  if (!passed)
1188  {
1189  printf("median_test input %s (parsed %s) expected %s got %s\n",
1190  input, lwgeom_to_ewkt(g),
1191  lwgeom_to_ewkt((LWGEOM*) expected_result),
1192  lwgeom_to_ewkt((LWGEOM*) result));
1193  }
1194 
1195  }
1196  else if (result == NULL && expected == NULL) /* got nothing, expecting nothing */
1197  {
1198  passed = LW_TRUE;
1199  }
1200  else if (result != NULL && expected == NULL) /* got something, expecting nothing */
1201  {
1202  passed = LW_FALSE;
1203  printf("median_test input %s (parsed %s) expected NULL got %s\n", input, lwgeom_to_ewkt(g), lwgeom_to_ewkt((LWGEOM*) result));
1204  }
1205  else if (result == NULL && expected != NULL) /* got nothing, expecting something */
1206  {
1207  passed = LW_FALSE;
1208  printf("%s", cu_error_msg);
1209  printf("median_test input %s (parsed %s) expected %s got NULL\n", input, lwgeom_to_ewkt(g), lwgeom_to_ewkt((LWGEOM*) expected_result));
1210  }
1211 
1212  CU_ASSERT_TRUE(passed);
1213 
1214  lwgeom_free(g);
1215  lwpoint_free(expected_result);
1216  lwpoint_free(result);
1217 }
double x
Definition: liblwgeom.h:354
double m
Definition: liblwgeom.h:354
void lwfree(void *mem)
Definition: lwutil.c:244
char * lwgeom_to_ewkt(const LWGEOM *lwgeom)
Return an alloced string.
Definition: lwgeom.c:556
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:904
LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:161
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:930
POINT4D * lwmpoint_extract_points_4d(const LWMPOINT *g, uint32_t *npoints, int *input_empty)
unsigned int uint32_t
Definition: uthash.h:78
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2004
#define LW_FALSE
Definition: liblwgeom.h:76
void cu_error_msg_reset()
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
#define FP_TOLERANCE
Floating point comparators.
double z
Definition: liblwgeom.h:354
static double test_weighted_distance(const POINT4D *curr, const POINT4D *points, uint32_t npoints)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:233
uint8_t type
Definition: liblwgeom.h:398
LWPOINT * lwgeom_median(const LWGEOM *g, double tol, uint32_t maxiter, char fail_if_not_converged)
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
double y
Definition: liblwgeom.h:354
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:937
Here is the call graph for this function:
Here is the caller graph for this function: