PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ improve_structure()

static uint32_t improve_structure ( POINT4D objs,
uint32_t *  clusters,
uint32_t  n,
POINT4D centers,
double *  radii,
uint32_t  k,
double  max_radius 
)
static

Definition at line 42 of file lwkmeans.c.

49 {
50  /* Input check: radius limit should be measurable */
51  if (max_radius <= 0)
52  return k;
53 
54  double max_radius_sq = max_radius * max_radius;
55 
56  /* Do we have the big clusters to split at all? */
57  uint32_t first_cluster_to_split = 0;
58  for (; first_cluster_to_split < k; first_cluster_to_split++)
59  if (radii[first_cluster_to_split] > max_radius_sq)
60  break;
61  if (first_cluster_to_split == k)
62  return k;
63 
64  POINT4D *temp_objs = lwalloc(sizeof(POINT4D) * n);
65  uint32_t *temp_clusters = lwalloc(sizeof(uint32_t) * n);
66  double *temp_radii = lwalloc(sizeof(double) * n);
67  POINT4D *temp_centers = lwalloc(sizeof(POINT4D) * n);
68 
69  uint32_t new_k = k;
70 
71  for (uint32_t cluster = first_cluster_to_split; cluster < k; cluster++)
72  {
73  if (radii[cluster] <= max_radius_sq)
74  continue;
75 
76  /* copy cluster alone */
77  uint32_t cluster_size = 0;
78  for (uint32_t i = 0; i < n; i++)
79  if (clusters[i] == cluster)
80  temp_objs[cluster_size++] = objs[i];
81  if (cluster_size <= 1)
82  continue;
83 
84  /* run 2-means on the cluster */
85  kmeans(temp_objs, temp_clusters, cluster_size, temp_centers, temp_radii, 2, 0);
86 
87  /* replace cluster with split */
88  uint32_t d = 0;
89  for (uint32_t i = 0; i < n; i++)
90  if (clusters[i] == cluster)
91  if (temp_clusters[d++])
92  clusters[i] = new_k;
93 
94  centers[cluster] = temp_centers[0];
95  centers[new_k] = temp_centers[1];
96  radii[cluster] = temp_radii[0];
97  radii[new_k] = temp_radii[1];
98  new_k++;
99  }
100  lwfree(temp_centers);
101  lwfree(temp_radii);
102  lwfree(temp_clusters);
103  lwfree(temp_objs);
104  return new_k;
105 }
void lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
static uint32_t kmeans(POINT4D *objs, uint32_t *clusters, uint32_t n, POINT4D *centers, double *radii, uint32_t min_k, double max_radius)
Definition: lwkmeans.c:276

References kmeans(), lwalloc(), and lwfree().

Referenced by kmeans().

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