Tesseract  3.02
 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 286 of file clusttool.cpp.

286  {
287  int i;
288  int NumFloatsRead;
289 
290  if (Buffer == NULL)
291  Buffer = (FLOAT32 *) Emalloc (N * sizeof (FLOAT32));
292 
293  for (i = 0; i < N; i++) {
294  NumFloatsRead = fscanf (File, "%f", &(Buffer[i]));
295  if (NumFloatsRead != 1) {
296  if ((NumFloatsRead == EOF) && (i == 0))
297  return (NULL);
298  else
299  DoError (ILLEGALFLOAT, "Illegal float specification");
300  }
301  }
302  return (Buffer);
303 } // ReadNFloats
#define NULL
Definition: host.h:144
void DoError(int Error, const char *Message)
Definition: danerror.cpp:32
float FLOAT32
Definition: host.h:111
#define ILLEGALFLOAT
Definition: clusttool.h:66
void * Emalloc(size_t Size)
Definition: emalloc.cpp:35
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 (fscanf (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 (fscanf (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 (fscanf (File, "%f%f", &(ParamDesc[i].Min), &(ParamDesc[i].Max)) !=
91  2)
92  DoError (ILLEGALMINMAXSPEC, "Illegal min or max specification");
93  ParamDesc[i].Range = ParamDesc[i].Max - ParamDesc[i].Min;
94  ParamDesc[i].HalfRange = ParamDesc[i].Range / 2;
95  ParamDesc[i].MidRange = (ParamDesc[i].Max + ParamDesc[i].Min) / 2;
96  }
97  return (ParamDesc);
98 } // ReadParamDesc
FLOAT32 MidRange
Definition: ocrfeatures.h:52
inT8 Circular
Definition: ocrfeatures.h:46
FLOAT32 HalfRange
Definition: ocrfeatures.h:51
void DoError(int Error, const char *Message)
Definition: danerror.cpp:32
#define FALSE
Definition: capi.h:28
FLOAT32 Range
Definition: ocrfeatures.h:50
#define ILLEGALCIRCULARSPEC
Definition: clusttool.h:58
FLOAT32 Min
Definition: ocrfeatures.h:48
FLOAT32 Max
Definition: ocrfeatures.h:49
#define ILLEGALMINMAXSPEC
Definition: clusttool.h:59
void * Emalloc(size_t Size)
Definition: emalloc.cpp:35
inT8 NonEssential
Definition: ocrfeatures.h:47
#define ILLEGALESSENTIALSPEC
Definition: clusttool.h:67
#define TOKENSIZE
Definition: clusttool.cpp:29
#define TRUE
Definition: capi.h:27
PROTOSTYLE ReadProtoStyle ( FILE *  File)

Definition at line 244 of file clusttool.cpp.

244  {
245  char Token[TOKENSIZE];
246  PROTOSTYLE Style;
247 
248  if (fscanf (File, "%s", Token) != 1)
249  DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
250  switch (Token[0]) {
251  case 's':
252  Style = spherical;
253  break;
254  case 'e':
255  Style = elliptical;
256  break;
257  case 'm':
258  Style = mixed;
259  break;
260  case 'a':
261  Style = automatic;
262  break;
263  default:
264  Style = elliptical;
265  DoError (ILLEGALSTYLESPEC, "Illegal prototype style specification");
266  }
267  return (Style);
268 } // ReadProtoStyle
void DoError(int Error, const char *Message)
Definition: danerror.cpp:32
Definition: cluster.h:45
#define ILLEGALSTYLESPEC
Definition: clusttool.h:61
PROTOSTYLE
Definition: cluster.h:44
#define TOKENSIZE
Definition: clusttool.cpp:29
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 115 of file clusttool.cpp.

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