PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ test_kmeans()

static void test_kmeans ( void  )
static

Definition at line 1162 of file cu_algorithm.c.

References algorithms_suite_setup(), lwalloc(), lwfree(), lwgeom_cluster_2d_kmeans(), lwgeom_free(), lwpoint_as_lwgeom(), lwpoint_make2d(), r, and SRID_UNKNOWN.

Referenced by algorithms_suite_setup().

1163 {
1164  static int cluster_size = 100;
1165  static int num_clusters = 4;
1166  static double spread = 1.5;
1167  int N = cluster_size * num_clusters;
1168  LWGEOM **geoms;
1169  int i, j, k=0;
1170  int *r;
1171 
1172  geoms = lwalloc(sizeof(LWGEOM*) * N);
1173 
1174  for (j = 0; j < num_clusters; j++) {
1175  for (i = 0; i < cluster_size; i++)
1176  {
1177  double u1 = 1.0 * rand() / RAND_MAX;
1178  double u2 = 1.0 * rand() / RAND_MAX;
1179  double z1 = spread * j + sqrt(-2*log2(u1))*cos(2*M_PI*u2);
1180  double z2 = spread * j + sqrt(-2*log2(u1))*sin(2*M_PI*u2);
1181 
1182  LWPOINT *lwp = lwpoint_make2d(SRID_UNKNOWN, z1, z2);
1183  geoms[k++] = lwpoint_as_lwgeom(lwp);
1184  }
1185  }
1186 
1187  r = lwgeom_cluster_2d_kmeans((const LWGEOM **)geoms, N, num_clusters);
1188 
1189  // for (i = 0; i < k; i++)
1190  // {
1191  // printf("[%d] %s\n", r[i], lwgeom_to_ewkt(geoms[i]));
1192  // }
1193 
1194  /* Clean up */
1195  lwfree(r);
1196  for (i = 0; i < k; i++)
1197  lwgeom_free(geoms[i]);
1198  lwfree(geoms);
1199 
1200  return;
1201 }
char * r
Definition: cu_in_wkt.c:24
void lwfree(void *mem)
Definition: lwutil.c:244
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:163
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
int * lwgeom_cluster_2d_kmeans(const LWGEOM **geoms, int ngeoms, int k)
Take a list of LWGEOMs and a number of clusters and return an integer array indicating which cluster ...
Definition: lwkmeans.c:77
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:303
void * lwalloc(size_t size)
Definition: lwutil.c:229
Here is the call graph for this function:
Here is the caller graph for this function: