All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
clusttool.h File Reference
#include "host.h"
#include "cluster.h"
#include <stdio.h>

Go to the source code of this file.

Macros

#define ILLEGALSAMPLESIZE   5000
 
#define ILLEGALCIRCULARSPEC   5001
 
#define ILLEGALMINMAXSPEC   5002
 
#define ILLEGALSIGNIFICANCESPEC   5003
 
#define ILLEGALSTYLESPEC   5004
 
#define ILLEGALSAMPLECOUNT   5005
 
#define ILLEGALMEANSPEC   5006
 
#define ILLEGALVARIANCESPEC   5007
 
#define ILLEGALDISTRIBUTION   5008
 
#define ILLEGALFLOAT   5009
 
#define ILLEGALESSENTIALSPEC   5013
 

Functions

uinT16 ReadSampleSize (FILE *File)
 
PARAM_DESCReadParamDesc (FILE *File, uinT16 N)
 
PROTOTYPEReadPrototype (FILE *File, uinT16 N)
 
PROTOSTYLE ReadProtoStyle (FILE *File)
 
FLOAT32ReadNFloats (FILE *File, uinT16 N, FLOAT32 Buffer[])
 
void WriteParamDesc (FILE *File, uinT16 N, PARAM_DESC ParamDesc[])
 
void WritePrototype (FILE *File, uinT16 N, PROTOTYPE *Proto)
 
void WriteNFloats (FILE *File, uinT16 N, FLOAT32 Array[])
 
void WriteProtoStyle (FILE *File, PROTOSTYLE ProtoStyle)
 
void WriteProtoList (FILE *File, uinT16 N, PARAM_DESC ParamDesc[], LIST ProtoList, BOOL8 WriteSigProtos, BOOL8 WriteInsigProtos)
 

Macro Definition Documentation

#define ILLEGALCIRCULARSPEC   5001

Definition at line 58 of file clusttool.h.

#define ILLEGALDISTRIBUTION   5008

Definition at line 65 of file clusttool.h.

#define ILLEGALESSENTIALSPEC   5013

Definition at line 67 of file clusttool.h.

#define ILLEGALFLOAT   5009

Definition at line 66 of file clusttool.h.

#define ILLEGALMEANSPEC   5006

Definition at line 63 of file clusttool.h.

#define ILLEGALMINMAXSPEC   5002

Definition at line 59 of file clusttool.h.

#define ILLEGALSAMPLECOUNT   5005

Definition at line 62 of file clusttool.h.

#define ILLEGALSAMPLESIZE   5000

Definition at line 57 of file clusttool.h.

#define ILLEGALSIGNIFICANCESPEC   5003

Definition at line 60 of file clusttool.h.

#define ILLEGALSTYLESPEC   5004

Definition at line 61 of file clusttool.h.

#define ILLEGALVARIANCESPEC   5007

Definition at line 64 of file clusttool.h.

Function Documentation

FLOAT32* ReadNFloats ( FILE *  File,
uinT16  N,
FLOAT32  Buffer[] 
)

This routine reads N floats from the specified text file and places them into Buffer. If Buffer is NULL, a buffer is created and passed back to the caller. If EOF is encountered before any floats can be read, NULL is returned.

Parameters
Fileopen text file to read floats from
Nnumber of floats to read
Bufferpointer to buffer to place floats into
Returns
Pointer to buffer holding floats or NULL if EOF
Note
Globals: None
Exceptions: ILLEGALFLOAT
History: 6/6/89, DSJ, Created.

Definition at line 281 of file clusttool.cpp.

281  {
282  int i;
283  int NumFloatsRead;
284 
285  if (Buffer == NULL)
286  Buffer = reinterpret_cast<FLOAT32*>(Emalloc(N * sizeof(FLOAT32)));
287 
288  for (i = 0; i < N; i++) {
289  NumFloatsRead = tfscanf(File, "%f", &(Buffer[i]));
290  if (NumFloatsRead != 1) {
291  if ((NumFloatsRead == EOF) && (i == 0)) {
292  Efree(Buffer);
293  return NULL;
294  } else {
295  DoError(ILLEGALFLOAT, "Illegal float specification");
296  }
297  }
298  }
299  return Buffer;
300 }
float FLOAT32
Definition: host.h:111
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
void * Emalloc(int Size)
Definition: emalloc.cpp:47
#define ILLEGALFLOAT
Definition: clusttool.h:66
void Efree(void *ptr)
Definition: emalloc.cpp:79
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
#define NULL
Definition: host.h:144
PARAM_DESC* ReadParamDesc ( FILE *  File,
uinT16  N 
)

This routine reads textual descriptions of sets of parameters which describe the characteristics of feature dimensions.

Exceptions:

  • ILLEGALCIRCULARSPEC
  • ILLEGALESSENTIALSPEC
  • ILLEGALMINMAXSPEC
    Parameters
    Fileopen text file to read N parameter descriptions from
    Nnumber of parameter descriptions to read
    Returns
    Pointer to an array of parameter descriptors.
    Note
    Globals: None
    History: 6/6/89, DSJ, Created.

Definition at line 66 of file clusttool.cpp.

66  {
67  int i;
68  PARAM_DESC *ParamDesc;
69  char Token[TOKENSIZE];
70 
71  ParamDesc = (PARAM_DESC *) Emalloc (N * sizeof (PARAM_DESC));
72  for (i = 0; i < N; i++) {
73  if (tfscanf(File, "%s", Token) != 1)
75  "Illegal circular/linear specification");
76  if (Token[0] == 'c')
77  ParamDesc[i].Circular = TRUE;
78  else
79  ParamDesc[i].Circular = FALSE;
80 
81  if (tfscanf(File, "%s", Token) != 1)
83  "Illegal essential/non-essential spec");
84  if (Token[0] == 'e')
85  ParamDesc[i].NonEssential = FALSE;
86  else
87  ParamDesc[i].NonEssential = TRUE;
88  if (tfscanf(File, "%f%f", &(ParamDesc[i].Min), &(ParamDesc[i].Max)) != 2)
89  DoError (ILLEGALMINMAXSPEC, "Illegal min or max specification");
90  ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
91  ParamDesc[i].HalfRange = ParamDesc[i].Range / 2;
92  ParamDesc[i].MidRange = (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
93  }
94  return (ParamDesc);
95 }
FLOAT32 Min
Definition: ocrfeatures.h:49
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
FLOAT32 HalfRange
Definition: ocrfeatures.h:52
#define TOKENSIZE
Definition: clusttool.cpp:29
#define ILLEGALMINMAXSPEC
Definition: clusttool.h:59
FLOAT32 MidRange
Definition: ocrfeatures.h:53
#define ILLEGALESSENTIALSPEC
Definition: clusttool.h:67
void * Emalloc(int Size)
Definition: emalloc.cpp:47
inT8 NonEssential
Definition: ocrfeatures.h:48
inT8 Circular
Definition: ocrfeatures.h:47
#define FALSE
Definition: capi.h:29
FLOAT32 Range
Definition: ocrfeatures.h:51
#define TRUE
Definition: capi.h:28
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
FLOAT32 Max
Definition: ocrfeatures.h:50
#define ILLEGALCIRCULARSPEC
Definition: clusttool.h:58
PROTOSTYLE ReadProtoStyle ( FILE *  File)

This routine reads an single token from the specified text file and interprets it as a prototype specification.

Parameters
Fileopen text file to read prototype style from
Returns
Prototype style read from text file
Note
Globals: None
Exceptions: ILLEGALSTYLESPEC illegal prototype style specification
History: 6/8/89, DSJ, Created.

Definition at line 241 of file clusttool.cpp.

241  {
242  char Token[TOKENSIZE];
243  PROTOSTYLE Style;
244 
245  if (tfscanf(File, "%s", Token) != 1)
246  DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
247  switch (Token[0]) {
248  case 's':
249  Style = spherical;
250  break;
251  case 'e':
252  Style = elliptical;
253  break;
254  case 'm':
255  Style = mixed;
256  break;
257  case 'a':
258  Style = automatic;
259  break;
260  default:
261  Style = elliptical;
262  DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
263  }
264  return (Style);
265 }
#define ILLEGALSTYLESPEC
Definition: clusttool.h:61
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
#define TOKENSIZE
Definition: clusttool.cpp:29
Definition: cluster.h:45
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
PROTOSTYLE
Definition: cluster.h:44
PROTOTYPE* ReadPrototype ( FILE *  File,
uinT16  N 
)

This routine reads a textual description of a prototype from the specified file.

Exceptions:

  • ILLEGALSIGNIFICANCESPEC
  • ILLEGALSAMPLECOUNT
  • ILLEGALMEANSPEC
  • ILLEGALVARIANCESPEC
  • ILLEGALDISTRIBUTION
    Parameters
    Fileopen text file to read prototype from
    Nnumber of dimensions used in prototype
    Returns
    List of prototypes
    Note
    Globals: None
    History: 6/6/89, DSJ, Created.

Definition at line 113 of file clusttool.cpp.

113  {
114  char Token[TOKENSIZE];
115  int Status;
116  PROTOTYPE *Proto;
117  int SampleCount;
118  int i;
119 
120  if ((Status = tfscanf(File, "%s", Token)) == 1) {
121  Proto = (PROTOTYPE *) Emalloc (sizeof (PROTOTYPE));
122  Proto->Cluster = NULL;
123  if (Token[0] == 's')
124  Proto->Significant = TRUE;
125  else
126  Proto->Significant = FALSE;
127 
128  Proto->Style = ReadProtoStyle (File);
129 
130  if ((tfscanf(File, "%d", &SampleCount) != 1) || (SampleCount < 0))
131  DoError (ILLEGALSAMPLECOUNT, "Illegal sample count");
132  Proto->NumSamples = SampleCount;
133 
134  Proto->Mean = ReadNFloats (File, N, NULL);
135  if (Proto->Mean == NULL)
136  DoError (ILLEGALMEANSPEC, "Illegal prototype mean");
137 
138  switch (Proto->Style) {
139  case spherical:
140  if (ReadNFloats (File, 1, &(Proto->Variance.Spherical)) == NULL)
141  DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
142  Proto->Magnitude.Spherical =
143  1.0 / sqrt ((double) (2.0 * PI * Proto->Variance.Spherical));
144  Proto->TotalMagnitude =
145  pow (Proto->Magnitude.Spherical, (float) N);
146  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
147  Proto->Weight.Spherical = 1.0 / Proto->Variance.Spherical;
148  Proto->Distrib = NULL;
149  break;
150  case elliptical:
151  Proto->Variance.Elliptical = ReadNFloats (File, N, NULL);
152  if (Proto->Variance.Elliptical == NULL)
153  DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
154  Proto->Magnitude.Elliptical =
155  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
156  Proto->Weight.Elliptical =
157  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
158  Proto->TotalMagnitude = 1.0;
159  for (i = 0; i < N; i++) {
160  Proto->Magnitude.Elliptical[i] =
161  1.0 /
162  sqrt ((double) (2.0 * PI * Proto->Variance.Elliptical[i]));
163  Proto->Weight.Elliptical[i] =
164  1.0 / Proto->Variance.Elliptical[i];
165  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
166  }
167  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
168  Proto->Distrib = NULL;
169  break;
170  case mixed:
171  Proto->Distrib =
172  (DISTRIBUTION *) Emalloc (N * sizeof (DISTRIBUTION));
173  for (i = 0; i < N; i++) {
174  if (tfscanf(File, "%s", Token) != 1)
176  "Illegal prototype distribution");
177  switch (Token[0]) {
178  case 'n':
179  Proto->Distrib[i] = normal;
180  break;
181  case 'u':
182  Proto->Distrib[i] = uniform;
183  break;
184  case 'r':
185  Proto->Distrib[i] = D_random;
186  break;
187  default:
189  "Illegal prototype distribution");
190  }
191  }
192  Proto->Variance.Elliptical = ReadNFloats (File, N, NULL);
193  if (Proto->Variance.Elliptical == NULL)
194  DoError (ILLEGALVARIANCESPEC, "Illegal prototype variance");
195  Proto->Magnitude.Elliptical =
196  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
197  Proto->Weight.Elliptical =
198  (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
199  Proto->TotalMagnitude = 1.0;
200  for (i = 0; i < N; i++) {
201  switch (Proto->Distrib[i]) {
202  case normal:
203  Proto->Magnitude.Elliptical[i] = 1.0 /
204  sqrt ((double)
205  (2.0 * PI * Proto->Variance.Elliptical[i]));
206  Proto->Weight.Elliptical[i] =
207  1.0 / Proto->Variance.Elliptical[i];
208  break;
209  case uniform:
210  case D_random:
211  Proto->Magnitude.Elliptical[i] = 1.0 /
212  (2.0 * Proto->Variance.Elliptical[i]);
213  break;
214  case DISTRIBUTION_COUNT:
215  ASSERT_HOST(!"Distribution count not allowed!");
216  }
217  Proto->TotalMagnitude *= Proto->Magnitude.Elliptical[i];
218  }
219  Proto->LogMagnitude = log ((double) Proto->TotalMagnitude);
220  break;
221  }
222  return (Proto);
223  }
224  else if (Status == EOF)
225  return (NULL);
226  else {
227  DoError (ILLEGALSIGNIFICANCESPEC, "Illegal significance specification");
228  return (NULL);
229  }
230 }
float FLOAT32
Definition: host.h:111
DISTRIBUTION * Distrib
Definition: cluster.h:77
#define ILLEGALMEANSPEC
Definition: clusttool.h:63
#define ILLEGALSIGNIFICANCESPEC
Definition: clusttool.h:60
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
FLOAT32 * ReadNFloats(FILE *File, uinT16 N, FLOAT32 Buffer[])
Definition: clusttool.cpp:281
FLOAT32 Spherical
Definition: cluster.h:63
DISTRIBUTION
Definition: cluster.h:58
FLOAT32 LogMagnitude
Definition: cluster.h:80
FLOATUNION Variance
Definition: cluster.h:81
FLOAT32 * Mean
Definition: cluster.h:78
Definition: cluster.h:59
#define TOKENSIZE
Definition: clusttool.cpp:29
#define ASSERT_HOST(x)
Definition: errcode.h:84
unsigned Significant
Definition: cluster.h:68
FLOATUNION Weight
Definition: cluster.h:83
FLOAT32 TotalMagnitude
Definition: cluster.h:79
unsigned NumSamples
Definition: cluster.h:75
void * Emalloc(int Size)
Definition: emalloc.cpp:47
FLOATUNION Magnitude
Definition: cluster.h:82
FLOAT32 * Elliptical
Definition: cluster.h:64
CLUSTER * Cluster
Definition: cluster.h:76
#define ILLEGALVARIANCESPEC
Definition: clusttool.h:64
#define ILLEGALDISTRIBUTION
Definition: clusttool.h:65
#define ILLEGALSAMPLECOUNT
Definition: clusttool.h:62
Definition: cluster.h:45
#define FALSE
Definition: capi.h:29
#define PI
Definition: const.h:19
#define TRUE
Definition: capi.h:28
unsigned Style
Definition: cluster.h:74
PROTOSTYLE ReadProtoStyle(FILE *File)
Definition: clusttool.cpp:241
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
#define NULL
Definition: host.h:144
uinT16 ReadSampleSize ( FILE *  File)

This routine reads a single integer from the specified file and checks to ensure that it is between 0 and MAXSAMPLESIZE.

Parameters
Fileopen text file to read sample size from
Returns
Sample size
Note
Globals: None
Exceptions: ILLEGALSAMPLESIZE illegal format or range
History: 6/6/89, DSJ, Created.

Definition at line 43 of file clusttool.cpp.

43  {
44  int SampleSize;
45 
46  if ((tfscanf(File, "%d", &SampleSize) != 1) ||
47  (SampleSize < 0) || (SampleSize > MAXSAMPLESIZE))
48  DoError (ILLEGALSAMPLESIZE, "Illegal sample size");
49  return (SampleSize);
50 }
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
#define MAXSAMPLESIZE
Definition: clusttool.cpp:30
#define ILLEGALSAMPLESIZE
Definition: clusttool.h:57
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
void WriteNFloats ( FILE *  File,
uinT16  N,
FLOAT32  Array[] 
)

This routine writes a text representation of N floats from an array to a file. All of the floats are placed on one line.

Parameters
Fileopen text file to write N floats to
Nnumber of floats to write
Arrayarray of floats to write
Returns
None
Note
Globals: None
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 393 of file clusttool.cpp.

393  {
394  for (int i = 0; i < N; i++)
395  fprintf(File, " %9.6f", Array[i]);
396  fprintf(File, "\n");
397 }
void WriteParamDesc ( FILE *  File,
uinT16  N,
PARAM_DESC  ParamDesc[] 
)

This routine writes an array of dimension descriptors to the specified text file.

Parameters
Fileopen text file to write param descriptors to
Nnumber of param descriptors to write
ParamDescarray of param descriptors to write
Returns
None
Note
Globals: None
Exceptions: None
History: 6/6/89, DSJ, Created.

Definition at line 314 of file clusttool.cpp.

314  {
315  int i;
316 
317  for (i = 0; i < N; i++) {
318  if (ParamDesc[i].Circular)
319  fprintf (File, "circular ");
320  else
321  fprintf (File, "linear ");
322 
323  if (ParamDesc[i].NonEssential)
324  fprintf (File, "non-essential ");
325  else
326  fprintf (File, "essential ");
327 
328  fprintf (File, "%10.6f %10.6f\n", ParamDesc[i].Min, ParamDesc[i].Max);
329  }
330 }
void WriteProtoList ( FILE *  File,
uinT16  N,
PARAM_DESC  ParamDesc[],
LIST  ProtoList,
BOOL8  WriteSigProtos,
BOOL8  WriteInsigProtos 
)

This routine writes a textual description of each prototype in the prototype list to the specified file. It also writes a file header which includes the number of dimensions in feature space and the descriptions for each dimension.

Parameters
Fileopen text file to write prototypes to
Nnumber of dimensions in feature space
ParamDescdescriptions for each dimension
ProtoListlist of prototypes to be written
WriteSigProtosTRUE to write out significant prototypes
WriteInsigProtosTRUE to write out insignificants
Note
Globals: None
Returns
None
Note
Exceptions: None
History: 6/12/89, DSJ, Created.

Definition at line 444 of file clusttool.cpp.

451 {
452  PROTOTYPE *Proto;
453 
454  /* write file header */
455  fprintf(File,"%0d\n",N);
456  WriteParamDesc(File,N,ParamDesc);
457 
458  /* write prototypes */
459  iterate(ProtoList)
460  {
461  Proto = (PROTOTYPE *) first_node ( ProtoList );
462  if (( Proto->Significant && WriteSigProtos ) ||
463  ( ! Proto->Significant && WriteInsigProtos ) )
464  WritePrototype( File, N, Proto );
465  }
466 }
void WritePrototype(FILE *File, uinT16 N, PROTOTYPE *Proto)
Definition: clusttool.cpp:343
unsigned Significant
Definition: cluster.h:68
#define first_node(l)
Definition: oldlist.h:139
#define iterate(l)
Definition: oldlist.h:159
void WriteParamDesc(FILE *File, uinT16 N, PARAM_DESC ParamDesc[])
Definition: clusttool.cpp:314
void WriteProtoStyle ( FILE *  File,
PROTOSTYLE  ProtoStyle 
)

This routine writes to the specified text file a word which represents the ProtoStyle. It does not append a carriage return to the end.

Parameters
Fileopen text file to write prototype style to
ProtoStyleprototype style to write
Returns
None
Note
Globals: None
Exceptions: None
History: 6/8/89, DSJ, Created.

Definition at line 410 of file clusttool.cpp.

410  {
411  switch (ProtoStyle) {
412  case spherical:
413  fprintf (File, "spherical");
414  break;
415  case elliptical:
416  fprintf (File, "elliptical");
417  break;
418  case mixed:
419  fprintf (File, "mixed");
420  break;
421  case automatic:
422  fprintf (File, "automatic");
423  break;
424  }
425 }
Definition: cluster.h:45
void WritePrototype ( FILE *  File,
uinT16  N,
PROTOTYPE Proto 
)

This routine writes a textual description of a prototype to the specified text file.

Parameters
Fileopen text file to write prototype to
Nnumber of dimensions in feature space
Protoprototype to write out
Returns
None
Note
Globals: None
Exceptions: None
History: 6/12/89, DSJ, Created.

Definition at line 343 of file clusttool.cpp.

343  {
344  int i;
345 
346  if (Proto->Significant)
347  fprintf (File, "significant ");
348  else
349  fprintf (File, "insignificant ");
350  WriteProtoStyle (File, (PROTOSTYLE) Proto->Style);
351  fprintf (File, "%6d\n\t", Proto->NumSamples);
352  WriteNFloats (File, N, Proto->Mean);
353  fprintf (File, "\t");
354 
355  switch (Proto->Style) {
356  case spherical:
357  WriteNFloats (File, 1, &(Proto->Variance.Spherical));
358  break;
359  case elliptical:
360  WriteNFloats (File, N, Proto->Variance.Elliptical);
361  break;
362  case mixed:
363  for (i = 0; i < N; i++)
364  switch (Proto->Distrib[i]) {
365  case normal:
366  fprintf (File, " %9s", "normal");
367  break;
368  case uniform:
369  fprintf (File, " %9s", "uniform");
370  break;
371  case D_random:
372  fprintf (File, " %9s", "random");
373  break;
374  case DISTRIBUTION_COUNT:
375  ASSERT_HOST(!"Distribution count not allowed!");
376  }
377  fprintf (File, "\n\t");
378  WriteNFloats (File, N, Proto->Variance.Elliptical);
379  }
380 }
DISTRIBUTION * Distrib
Definition: cluster.h:77
FLOAT32 Spherical
Definition: cluster.h:63
FLOATUNION Variance
Definition: cluster.h:81
FLOAT32 * Mean
Definition: cluster.h:78
Definition: cluster.h:59
#define ASSERT_HOST(x)
Definition: errcode.h:84
unsigned Significant
Definition: cluster.h:68
unsigned NumSamples
Definition: cluster.h:75
FLOAT32 * Elliptical
Definition: cluster.h:64
void WriteNFloats(FILE *File, uinT16 N, FLOAT32 Array[])
Definition: clusttool.cpp:393
Definition: cluster.h:45
unsigned Style
Definition: cluster.h:74
PROTOSTYLE
Definition: cluster.h:44
void WriteProtoStyle(FILE *File, PROTOSTYLE ProtoStyle)
Definition: clusttool.cpp:410