All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cluster.h File Reference
#include "kdtree.h"
#include "oldlist.h"

Go to the source code of this file.

Classes

struct  sample
 
struct  CLUSTERCONFIG
 
union  FLOATUNION
 
struct  PROTOTYPE
 
struct  CLUSTERER
 
struct  SAMPLELIST
 

Macros

#define MINBUCKETS   5
 
#define MAXBUCKETS   39
 
#define InitSampleSearch(S, C)   (((C)==NULL)?(S=NIL_LIST):(S=push(NIL_LIST,(C))))
 
#define ALREADYCLUSTERED   4000
 

Typedefs

typedef struct sample CLUSTER
 
typedef CLUSTER SAMPLE
 

Enumerations

enum  PROTOSTYLE { spherical, elliptical, mixed, automatic }
 
enum  DISTRIBUTION { normal, uniform, D_random, DISTRIBUTION_COUNT }
 

Functions

CLUSTERERMakeClusterer (inT16 SampleSize, const PARAM_DESC ParamDesc[])
 
SAMPLEMakeSample (CLUSTERER *Clusterer, const FLOAT32 *Feature, inT32 CharID)
 
LIST ClusterSamples (CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
 
void FreeClusterer (CLUSTERER *Clusterer)
 
void FreeProtoList (LIST *ProtoList)
 
void FreePrototype (void *arg)
 
CLUSTERNextSample (LIST *SearchState)
 
FLOAT32 Mean (PROTOTYPE *Proto, uinT16 Dimension)
 
FLOAT32 StandardDeviation (PROTOTYPE *Proto, uinT16 Dimension)
 
inT32 MergeClusters (inT16 N, PARAM_DESC ParamDesc[], inT32 n1, inT32 n2, FLOAT32 m[], FLOAT32 m1[], FLOAT32 m2[])
 

Macro Definition Documentation

#define ALREADYCLUSTERED   4000

Definition at line 133 of file cluster.h.

#define InitSampleSearch (   S,
 
)    (((C)==NULL)?(S=NIL_LIST):(S=push(NIL_LIST,(C))))

Definition at line 105 of file cluster.h.

#define MAXBUCKETS   39

Definition at line 27 of file cluster.h.

#define MINBUCKETS   5

Definition at line 26 of file cluster.h.

Typedef Documentation

typedef struct sample CLUSTER
typedef CLUSTER SAMPLE

Definition at line 42 of file cluster.h.

Enumeration Type Documentation

Enumerator
normal 
uniform 
D_random 
DISTRIBUTION_COUNT 

Definition at line 58 of file cluster.h.

enum PROTOSTYLE
Enumerator
spherical 
elliptical 
mixed 
automatic 

Definition at line 44 of file cluster.h.

44  {
46 } PROTOSTYLE;
Definition: cluster.h:45
PROTOSTYLE
Definition: cluster.h:44

Function Documentation

LIST ClusterSamples ( CLUSTERER Clusterer,
CLUSTERCONFIG Config 
)

This routine first checks to see if the samples in this clusterer have already been clustered before; if so, it does not bother to recreate the cluster tree. It simply recomputes the prototypes based on the new Config info.

If the samples have not been clustered before, the samples in the KD tree are formed into a cluster tree and then the prototypes are computed from the cluster tree.

In either case this routine returns a pointer to a list of prototypes that best represent the samples given the constraints specified in Config.

Parameters
Clustererdata struct containing samples to be clustered
Configparameters which control clustering process
Returns
Pointer to a list of prototypes
Note
Exceptions: None
History: 5/29/89, DSJ, Created.

Definition at line 515 of file cluster.cpp.

515  {
516  //only create cluster tree if samples have never been clustered before
517  if (Clusterer->Root == NULL)
518  CreateClusterTree(Clusterer);
519 
520  //deallocate the old prototype list if one exists
521  FreeProtoList (&Clusterer->ProtoList);
522  Clusterer->ProtoList = NIL_LIST;
523 
524  //compute prototypes starting at the root node in the tree
525  ComputePrototypes(Clusterer, Config);
526  return (Clusterer->ProtoList);
527 } // ClusterSamples
#define NIL_LIST
Definition: oldlist.h:126
void FreeProtoList(LIST *ProtoList)
Definition: cluster.cpp:571
CLUSTER * Root
Definition: cluster.h:91
LIST ProtoList
Definition: cluster.h:92
void ComputePrototypes(CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
Definition: cluster.cpp:936
void CreateClusterTree(CLUSTERER *Clusterer)
Definition: cluster.cpp:705
#define NULL
Definition: host.h:144
void FreeClusterer ( CLUSTERER Clusterer)

This routine frees all of the memory allocated to the specified data structure. It will not, however, free the memory used by the prototype list. The pointers to the clusters for each prototype in the list will be set to NULL to indicate that the cluster data structures no longer exist. Any sample lists that have been obtained via calls to GetSamples are no longer valid.

Parameters
Clustererpointer to data structure to be freed
Returns
None
Note
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 543 of file cluster.cpp.

543  {
544  if (Clusterer != NULL) {
545  memfree (Clusterer->ParamDesc);
546  if (Clusterer->KDTree != NULL)
547  FreeKDTree (Clusterer->KDTree);
548  if (Clusterer->Root != NULL)
549  FreeCluster (Clusterer->Root);
550  // Free up all used buckets structures.
551  for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
552  for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
553  if (Clusterer->bucket_cache[d][c] != NULL)
554  FreeBuckets(Clusterer->bucket_cache[d][c]);
555  }
556 
557  memfree(Clusterer);
558  }
559 } // FreeClusterer
void memfree(void *element)
Definition: freelist.cpp:30
void FreeBuckets(BUCKETS *Buckets)
Definition: cluster.cpp:2201
#define MINBUCKETS
Definition: cluster.h:26
void FreeCluster(CLUSTER *Cluster)
Definition: cluster.cpp:2220
BUCKETS * bucket_cache[DISTRIBUTION_COUNT][MAXBUCKETS+1-MINBUCKETS]
Definition: cluster.h:95
KDTREE * KDTree
Definition: cluster.h:90
void FreeKDTree(KDTREE *Tree)
Definition: kdtree.cpp:351
CLUSTER * Root
Definition: cluster.h:91
PARAM_DESC * ParamDesc
Definition: cluster.h:88
#define MAXBUCKETS
Definition: cluster.h:27
#define NULL
Definition: host.h:144
void FreeProtoList ( LIST ProtoList)

This routine frees all of the memory allocated to the specified list of prototypes. The clusters which are pointed to by the prototypes are not freed.

Parameters
ProtoListpointer to list of prototypes to be freed
Returns
None
Note
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 571 of file cluster.cpp.

571  {
572  destroy_nodes(*ProtoList, FreePrototype);
573 } // FreeProtoList
void destroy_nodes(LIST list, void_dest destructor)
Definition: oldlist.cpp:204
void FreePrototype(void *arg)
Definition: cluster.cpp:586
void FreePrototype ( void *  arg)

This routine deallocates the memory consumed by the specified prototype and modifies the corresponding cluster so that it is no longer marked as a prototype. The cluster is NOT deallocated by this routine.

Parameters
argprototype data structure to be deallocated
Returns
None
Note
Exceptions: None
History: 5/30/89, DSJ, Created.

Definition at line 586 of file cluster.cpp.

586  { //PROTOTYPE *Prototype)
587  PROTOTYPE *Prototype = (PROTOTYPE *) arg;
588 
589  // unmark the corresponding cluster (if there is one
590  if (Prototype->Cluster != NULL)
591  Prototype->Cluster->Prototype = FALSE;
592 
593  // deallocate the prototype statistics and then the prototype itself
594  if (Prototype->Distrib != NULL)
595  memfree (Prototype->Distrib);
596  if (Prototype->Mean != NULL)
597  memfree (Prototype->Mean);
598  if (Prototype->Style != spherical) {
599  if (Prototype->Variance.Elliptical != NULL)
600  memfree (Prototype->Variance.Elliptical);
601  if (Prototype->Magnitude.Elliptical != NULL)
602  memfree (Prototype->Magnitude.Elliptical);
603  if (Prototype->Weight.Elliptical != NULL)
604  memfree (Prototype->Weight.Elliptical);
605  }
606  memfree(Prototype);
607 } // FreePrototype
void memfree(void *element)
Definition: freelist.cpp:30
DISTRIBUTION * Distrib
Definition: cluster.h:77
FLOATUNION Variance
Definition: cluster.h:81
FLOAT32 * Mean
Definition: cluster.h:78
FLOATUNION Weight
Definition: cluster.h:83
FLOATUNION Magnitude
Definition: cluster.h:82
FLOAT32 * Elliptical
Definition: cluster.h:64
CLUSTER * Cluster
Definition: cluster.h:76
unsigned Prototype
Definition: cluster.h:34
#define FALSE
Definition: capi.h:29
unsigned Style
Definition: cluster.h:74
#define NULL
Definition: host.h:144
CLUSTERER* MakeClusterer ( inT16  SampleSize,
const PARAM_DESC  ParamDesc[] 
)

This routine creates a new clusterer data structure, initializes it, and returns a pointer to it.

Parameters
SampleSizenumber of dimensions in feature space
ParamDescdescription of each dimension
Returns
pointer to the new clusterer data structure
Note
Exceptions: None
History: 5/29/89, DSJ, Created.

Definition at line 400 of file cluster.cpp.

400  {
401  CLUSTERER *Clusterer;
402  int i;
403 
404  // allocate main clusterer data structure and init simple fields
405  Clusterer = (CLUSTERER *) Emalloc (sizeof (CLUSTERER));
406  Clusterer->SampleSize = SampleSize;
407  Clusterer->NumberOfSamples = 0;
408  Clusterer->NumChar = 0;
409 
410  // init fields which will not be used initially
411  Clusterer->Root = NULL;
412  Clusterer->ProtoList = NIL_LIST;
413 
414  // maintain a copy of param descriptors in the clusterer data structure
415  Clusterer->ParamDesc =
416  (PARAM_DESC *) Emalloc (SampleSize * sizeof (PARAM_DESC));
417  for (i = 0; i < SampleSize; i++) {
418  Clusterer->ParamDesc[i].Circular = ParamDesc[i].Circular;
419  Clusterer->ParamDesc[i].NonEssential = ParamDesc[i].NonEssential;
420  Clusterer->ParamDesc[i].Min = ParamDesc[i].Min;
421  Clusterer->ParamDesc[i].Max = ParamDesc[i].Max;
422  Clusterer->ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
423  Clusterer->ParamDesc[i].HalfRange = Clusterer->ParamDesc[i].Range / 2;
424  Clusterer->ParamDesc[i].MidRange =
425  (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
426  }
427 
428  // allocate a kd tree to hold the samples
429  Clusterer->KDTree = MakeKDTree (SampleSize, ParamDesc);
430 
431  // Initialize cache of histogram buckets to minimize recomputing them.
432  for (int d = 0; d < DISTRIBUTION_COUNT; ++d) {
433  for (int c = 0; c < MAXBUCKETS + 1 - MINBUCKETS; ++c)
434  Clusterer->bucket_cache[d][c] = NULL;
435  }
436 
437  return Clusterer;
438 } // MakeClusterer
FLOAT32 Min
Definition: ocrfeatures.h:49
#define MINBUCKETS
Definition: cluster.h:26
#define NIL_LIST
Definition: oldlist.h:126
BUCKETS * bucket_cache[DISTRIBUTION_COUNT][MAXBUCKETS+1-MINBUCKETS]
Definition: cluster.h:95
KDTREE * KDTree
Definition: cluster.h:90
FLOAT32 HalfRange
Definition: ocrfeatures.h:52
inT32 NumberOfSamples
Definition: cluster.h:89
FLOAT32 MidRange
Definition: ocrfeatures.h:53
CLUSTER * Root
Definition: cluster.h:91
void * Emalloc(int Size)
Definition: emalloc.cpp:47
inT8 NonEssential
Definition: ocrfeatures.h:48
inT32 NumChar
Definition: cluster.h:93
inT8 Circular
Definition: ocrfeatures.h:47
LIST ProtoList
Definition: cluster.h:92
PARAM_DESC * ParamDesc
Definition: cluster.h:88
FLOAT32 Range
Definition: ocrfeatures.h:51
#define MAXBUCKETS
Definition: cluster.h:27
#define NULL
Definition: host.h:144
KDTREE * MakeKDTree(inT16 KeySize, const PARAM_DESC KeyDesc[])
Definition: kdtree.cpp:182
inT16 SampleSize
Definition: cluster.h:87
FLOAT32 Max
Definition: ocrfeatures.h:50
SAMPLE* MakeSample ( CLUSTERER Clusterer,
const FLOAT32 Feature,
inT32  CharID 
)

This routine creates a new sample data structure to hold the specified feature. This sample is added to the clusterer data structure (so that it knows which samples are to be clustered later), and a pointer to the sample is returned to the caller.

Parameters
Clustererclusterer data structure to add sample to
Featurefeature to be added to clusterer
CharIDunique ident. of char that sample came from
Returns
Pointer to the new sample data structure
Note
Exceptions: ALREADYCLUSTERED MakeSample can't be called after ClusterSamples has been called
History: 5/29/89, DSJ, Created.

Definition at line 457 of file cluster.cpp.

458  {
459  SAMPLE *Sample;
460  int i;
461 
462  // see if the samples have already been clustered - if so trap an error
463  if (Clusterer->Root != NULL)
465  "Can't add samples after they have been clustered");
466 
467  // allocate the new sample and initialize it
468  Sample = (SAMPLE *) Emalloc (sizeof (SAMPLE) +
469  (Clusterer->SampleSize -
470  1) * sizeof (FLOAT32));
471  Sample->Clustered = FALSE;
472  Sample->Prototype = FALSE;
473  Sample->SampleCount = 1;
474  Sample->Left = NULL;
475  Sample->Right = NULL;
476  Sample->CharID = CharID;
477 
478  for (i = 0; i < Clusterer->SampleSize; i++)
479  Sample->Mean[i] = Feature[i];
480 
481  // add the sample to the KD tree - keep track of the total # of samples
482  Clusterer->NumberOfSamples++;
483  KDStore (Clusterer->KDTree, Sample->Mean, (char *) Sample);
484  if (CharID >= Clusterer->NumChar)
485  Clusterer->NumChar = CharID + 1;
486 
487  // execute hook for monitoring clustering operation
488  // (*SampleCreationHook)( Sample );
489 
490  return (Sample);
491 } // MakeSample
struct sample * Left
Definition: cluster.h:36
float FLOAT32
Definition: host.h:111
unsigned Clustered
Definition: cluster.h:33
unsigned SampleCount
Definition: cluster.h:35
struct sample * Right
Definition: cluster.h:37
KDTREE * KDTree
Definition: cluster.h:90
inT32 NumberOfSamples
Definition: cluster.h:89
FLOAT32 Mean[1]
Definition: cluster.h:39
CLUSTER * Root
Definition: cluster.h:91
void * Emalloc(int Size)
Definition: emalloc.cpp:47
#define ALREADYCLUSTERED
Definition: cluster.h:133
inT32 NumChar
Definition: cluster.h:93
void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data)
Definition: kdtree.cpp:218
unsigned Prototype
Definition: cluster.h:34
#define FALSE
Definition: capi.h:29
Definition: cluster.h:32
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
inT32 CharID
Definition: cluster.h:38
#define NULL
Definition: host.h:144
inT16 SampleSize
Definition: cluster.h:87
FLOAT32 Mean ( PROTOTYPE Proto,
uinT16  Dimension 
)

This routine returns the mean of the specified prototype in the indicated dimension.

Parameters
Protoprototype to return mean of
Dimensiondimension whose mean is to be returned
Returns
Mean of Prototype in Dimension
Note
Exceptions: none
History: 7/6/89, DSJ, Created.

Definition at line 650 of file cluster.cpp.

650  {
651  return (Proto->Mean[Dimension]);
652 } // Mean
FLOAT32 * Mean
Definition: cluster.h:78
inT32 MergeClusters ( inT16  N,
PARAM_DESC  ParamDesc[],
inT32  n1,
inT32  n2,
FLOAT32  m[],
FLOAT32  m1[],
FLOAT32  m2[] 
)

This routine merges two clusters into one larger cluster. To do this it computes the number of samples in the new cluster and the mean of the new cluster. The ParamDesc information is used to ensure that circular dimensions are handled correctly.

Parameters
N# of dimensions (size of arrays)
ParamDescarray of dimension descriptions
n1,n2number of samples in each old cluster
marray to hold mean of new cluster
m1,m2arrays containing means of old clusters
Returns
The number of samples in the new cluster.
Note
Exceptions: None
History: 5/31/89, DSJ, Created.

Definition at line 891 of file cluster.cpp.

896  {
897  inT32 i, n;
898 
899  n = n1 + n2;
900  for (i = N; i > 0; i--, ParamDesc++, m++, m1++, m2++) {
901  if (ParamDesc->Circular) {
902  // if distance between means is greater than allowed
903  // reduce upper point by one "rotation" to compute mean
904  // then normalize the mean back into the accepted range
905  if ((*m2 - *m1) > ParamDesc->HalfRange) {
906  *m = (n1 * *m1 + n2 * (*m2 - ParamDesc->Range)) / n;
907  if (*m < ParamDesc->Min)
908  *m += ParamDesc->Range;
909  }
910  else if ((*m1 - *m2) > ParamDesc->HalfRange) {
911  *m = (n1 * (*m1 - ParamDesc->Range) + n2 * *m2) / n;
912  if (*m < ParamDesc->Min)
913  *m += ParamDesc->Range;
914  }
915  else
916  *m = (n1 * *m1 + n2 * *m2) / n;
917  }
918  else
919  *m = (n1 * *m1 + n2 * *m2) / n;
920  }
921  return n;
922 } // MergeClusters
int inT32
Definition: host.h:102
CLUSTER* NextSample ( LIST SearchState)

This routine is used to find all of the samples which belong to a cluster. It starts by removing the top cluster on the cluster list (SearchState). If this cluster is a leaf it is returned. Otherwise, the right subcluster is pushed on the list and we continue the search in the left subcluster. This continues until a leaf is found. If all samples have been found, NULL is returned. InitSampleSearch() must be called before NextSample() to initialize the search.

Parameters
SearchStateptr to list containing clusters to be searched
Returns
Pointer to the next leaf cluster (sample) or NULL.
Note
Exceptions: None
History: 6/16/89, DSJ, Created.

Definition at line 625 of file cluster.cpp.

625  {
626  CLUSTER *Cluster;
627 
628  if (*SearchState == NIL_LIST)
629  return (NULL);
630  Cluster = (CLUSTER *) first_node (*SearchState);
631  *SearchState = pop (*SearchState);
632  while (TRUE) {
633  if (Cluster->Left == NULL)
634  return (Cluster);
635  *SearchState = push (*SearchState, Cluster->Right);
636  Cluster = Cluster->Left;
637  }
638 } // NextSample
struct sample * Left
Definition: cluster.h:36
#define NIL_LIST
Definition: oldlist.h:126
struct sample * Right
Definition: cluster.h:37
#define first_node(l)
Definition: oldlist.h:139
LIST pop(LIST list)
Definition: oldlist.cpp:305
Definition: cluster.h:32
#define TRUE
Definition: capi.h:28
#define NULL
Definition: host.h:144
LIST push(LIST list, void *element)
Definition: oldlist.cpp:323
FLOAT32 StandardDeviation ( PROTOTYPE Proto,
uinT16  Dimension 
)

This routine returns the standard deviation of the prototype in the indicated dimension.

Parameters
Protoprototype to return standard deviation of
Dimensiondimension whose stddev is to be returned
Returns
Standard deviation of Prototype in Dimension
Note
Exceptions: none
History: 7/6/89, DSJ, Created.

Definition at line 664 of file cluster.cpp.

664  {
665  switch (Proto->Style) {
666  case spherical:
667  return ((FLOAT32) sqrt ((double) Proto->Variance.Spherical));
668  case elliptical:
669  return ((FLOAT32)
670  sqrt ((double) Proto->Variance.Elliptical[Dimension]));
671  case mixed:
672  switch (Proto->Distrib[Dimension]) {
673  case normal:
674  return ((FLOAT32)
675  sqrt ((double) Proto->Variance.Elliptical[Dimension]));
676  case uniform:
677  case D_random:
678  return (Proto->Variance.Elliptical[Dimension]);
679  case DISTRIBUTION_COUNT:
680  ASSERT_HOST(!"Distribution count not allowed!");
681  }
682  }
683  return 0.0f;
684 } // StandardDeviation
float FLOAT32
Definition: host.h:111
DISTRIBUTION * Distrib
Definition: cluster.h:77
FLOAT32 Spherical
Definition: cluster.h:63
FLOATUNION Variance
Definition: cluster.h:81
Definition: cluster.h:59
#define ASSERT_HOST(x)
Definition: errcode.h:84
FLOAT32 * Elliptical
Definition: cluster.h:64
Definition: cluster.h:45
unsigned Style
Definition: cluster.h:74