tesseract  3.04.00
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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[] 
)

ReadNFloats ************************************************************* Parameters: File open text file to read floats from N number of floats to read Buffer pointer to buffer to place floats into Globals: None Operation: 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. Return: Pointer to buffer holding floats or NULL if EOF Exceptions: ILLEGALFLOAT History: 6/6/89, DSJ, Created.

Definition at line 284 of file clusttool.cpp.

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

ReadParamDesc ************************************************************* Parameters: File open text file to read N parameter descriptions from N number of parameter descriptions to read Globals: None Operation: This routine reads textual descriptions of sets of parameters which describe the characteristics of feature dimensions. Return: Pointer to an array of parameter descriptors. Exceptions: ILLEGALCIRCULARSPEC ILLEGALESSENTIALSPEC ILLEGALMINMAXSPEC History: 6/6/89, DSJ, Created.

Definition at line 68 of file clusttool.cpp.

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

Definition at line 243 of file clusttool.cpp.

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

ReadPrototype ************************************************************* Parameters: File open text file to read prototype from N number of dimensions used in prototype Globals: None Operation: This routine reads a textual description of a prototype from the specified file. Return: List of prototypes Exceptions: ILLEGALSIGNIFICANCESPEC ILLEGALSAMPLECOUNT ILLEGALMEANSPEC ILLEGALVARIANCESPEC ILLEGALDISTRIBUTION History: 6/6/89, DSJ, Created.

Definition at line 114 of file clusttool.cpp.

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

ReadSampleSize *********************************************************** Parameters: File open text file to read sample size from Globals: None Operation: This routine reads a single integer from the specified file and checks to ensure that it is between 0 and MAXSAMPLESIZE. Return: Sample size Exceptions: ILLEGALSAMPLESIZE illegal format or range History: 6/6/89, DSJ, Created.

Definition at line 46 of file clusttool.cpp.

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

WriteNFloats *********************************************************** Parameters: File open text file to write N floats to N number of floats to write Array array of floats to write Globals: None Operation: This routine writes a text representation of N floats from an array to a file. All of the floats are placed on one line. Return: None Exceptions: None History: 6/6/89, DSJ, Created.

Definition at line 399 of file clusttool.cpp.

399  {
400  for (int i = 0; i < N; i++)
401  fprintf(File, " %9.6f", Array[i]);
402  fprintf(File, "\n");
403 } // WriteNFloats
void WriteParamDesc ( FILE *  File,
uinT16  N,
PARAM_DESC  ParamDesc[] 
)

WriteParamDesc ************************************************************ Parameters: File open text file to write param descriptors to N number of param descriptors to write ParamDesc array of param descriptors to write Globals: None Operation: This routine writes an array of dimension descriptors to the specified text file. Return: None Exceptions: None History: 6/6/89, DSJ, Created.

Definition at line 318 of file clusttool.cpp.

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

Definition at line 435 of file clusttool.cpp.

466 {
467  PROTOTYPE *Proto;
468 
469  /* write file header */
470  fprintf(File,"%0d\n",N);
471  WriteParamDesc(File,N,ParamDesc);
472 
473  /* write prototypes */
474  iterate(ProtoList)
475  {
476  Proto = (PROTOTYPE *) first_node ( ProtoList );
477  if (( Proto->Significant && WriteSigProtos ) ||
478  ( ! Proto->Significant && WriteInsigProtos ) )
479  WritePrototype( File, N, Proto );
480  }
481 } /* WriteProtoList */
void WritePrototype(FILE *File, uinT16 N, PROTOTYPE *Proto)
Definition: clusttool.cpp:348
#define first_node(l)
Definition: oldlist.h:139
#define iterate(l)
Definition: oldlist.h:159
unsigned Significant
Definition: cluster.h:68
void WriteParamDesc(FILE *File, uinT16 N, PARAM_DESC ParamDesc[])
Definition: clusttool.cpp:318
void WriteProtoStyle ( FILE *  File,
PROTOSTYLE  ProtoStyle 
)

WriteProtoSyle ********************************************************** Parameters: File open text file to write prototype style to ProtoStyle prototype style to write Globals: None Operation: This routine writes to the specified text file a word which represents the ProtoStyle. It does not append a carriage return to the end. Return: None Exceptions: None History: 6/8/89, DSJ, Created.

Definition at line 417 of file clusttool.cpp.

417  {
418  switch (ProtoStyle) {
419  case spherical:
420  fprintf (File, "spherical");
421  break;
422  case elliptical:
423  fprintf (File, "elliptical");
424  break;
425  case mixed:
426  fprintf (File, "mixed");
427  break;
428  case automatic:
429  fprintf (File, "automatic");
430  break;
431  }
432 } // WriteProtoStyle
Definition: cluster.h:45
void WritePrototype ( FILE *  File,
uinT16  N,
PROTOTYPE Proto 
)

WritePrototype ************************************************************ Parameters: File open text file to write prototype to N number of dimensions in feature space Proto prototype to write out Globals: None Operation: This routine writes a textual description of a prototype to the specified text file. Return: None Exceptions: None History: 6/12/89, DSJ, Created.

Definition at line 348 of file clusttool.cpp.

348  {
349  int i;
350 
351  if (Proto->Significant)
352  fprintf (File, "significant ");
353  else
354  fprintf (File, "insignificant ");
355  WriteProtoStyle (File, (PROTOSTYLE) Proto->Style);
356  fprintf (File, "%6d\n\t", Proto->NumSamples);
357  WriteNFloats (File, N, Proto->Mean);
358  fprintf (File, "\t");
359 
360  switch (Proto->Style) {
361  case spherical:
362  WriteNFloats (File, 1, &(Proto->Variance.Spherical));
363  break;
364  case elliptical:
365  WriteNFloats (File, N, Proto->Variance.Elliptical);
366  break;
367  case mixed:
368  for (i = 0; i < N; i++)
369  switch (Proto->Distrib[i]) {
370  case normal:
371  fprintf (File, " %9s", "normal");
372  break;
373  case uniform:
374  fprintf (File, " %9s", "uniform");
375  break;
376  case D_random:
377  fprintf (File, " %9s", "random");
378  break;
379  case DISTRIBUTION_COUNT:
380  ASSERT_HOST(!"Distribution count not allowed!");
381  }
382  fprintf (File, "\n\t");
383  WriteNFloats (File, N, Proto->Variance.Elliptical);
384  }
385 } // WritePrototype
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:399
Definition: cluster.h:45
unsigned Style
Definition: cluster.h:74
PROTOSTYLE
Definition: cluster.h:44
void WriteProtoStyle(FILE *File, PROTOSTYLE ProtoStyle)
Definition: clusttool.cpp:417