All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
protos.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: protos.c (Formerly protos.c)
5  * Description:
6  * Author: Mark Seaman, OCR Technology
7  * Created: Fri Oct 16 14:37:00 1987
8  * Modified: Mon Mar 4 14:51:24 1991 (Dan Johnson) danj@hpgrlj
9  * Language: C
10  * Package: N/A
11  * Status: Reusable Software Component
12  *
13  * (c) Copyright 1987, Hewlett-Packard Company.
14  ** Licensed under the Apache License, Version 2.0 (the "License");
15  ** you may not use this file except in compliance with the License.
16  ** You may obtain a copy of the License at
17  ** http://www.apache.org/licenses/LICENSE-2.0
18  ** Unless required by applicable law or agreed to in writing, software
19  ** distributed under the License is distributed on an "AS IS" BASIS,
20  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  ** See the License for the specific language governing permissions and
22  ** limitations under the License.
23  *
24  *********************************************************************************/
25 /*----------------------------------------------------------------------
26  I n c l u d e s
27 ----------------------------------------------------------------------*/
28 #include "protos.h"
29 #include "const.h"
30 #include "emalloc.h"
31 #include "freelist.h"
32 #include "callcpp.h"
33 #include "tprintf.h"
34 #include "scanutils.h"
35 #include "globals.h"
36 #include "classify.h"
37 #include "params.h"
38 
39 #include <stdio.h>
40 #include <math.h>
41 
42 #define PROTO_INCREMENT 32
43 #define CONFIG_INCREMENT 16
44 
45 /*----------------------------------------------------------------------
46  V a r i a b l e s
47 ----------------------------------------------------------------------*/
49 
50 STRING_VAR(classify_training_file, "MicroFeatures", "Training file");
51 
52 /*----------------------------------------------------------------------
53  F u n c t i o n s
54 ----------------------------------------------------------------------*/
64  int NewNumConfigs;
65  int NewConfig;
66  int MaxNumProtos;
68 
69  MaxNumProtos = Class->MaxNumProtos;
70 
71  if (Class->NumConfigs >= Class->MaxNumConfigs) {
72  /* add configs in CONFIG_INCREMENT chunks at a time */
73  NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
75 
76  Class->Configurations =
78  sizeof (BIT_VECTOR) * NewNumConfigs);
79 
80  Class->MaxNumConfigs = NewNumConfigs;
81  }
82  NewConfig = Class->NumConfigs++;
83  Config = NewBitVector (MaxNumProtos);
84  Class->Configurations[NewConfig] = Config;
85  zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos));
86 
87  return (NewConfig);
88 }
89 
90 
100  int i;
101  int Bit;
102  int NewNumProtos;
103  int NewProto;
105 
106  if (Class->NumProtos >= Class->MaxNumProtos) {
107  /* add protos in PROTO_INCREMENT chunks at a time */
108  NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
110 
111  Class->Prototypes = (PROTO) Erealloc (Class->Prototypes,
112  sizeof (PROTO_STRUCT) *
113  NewNumProtos);
114 
115  Class->MaxNumProtos = NewNumProtos;
116 
117  for (i = 0; i < Class->NumConfigs; i++) {
118  Config = Class->Configurations[i];
119  Class->Configurations[i] = ExpandBitVector (Config, NewNumProtos);
120 
121  for (Bit = Class->NumProtos; Bit < NewNumProtos; Bit++)
122  reset_bit(Config, Bit);
123  }
124  }
125  NewProto = Class->NumProtos++;
126  if (Class->NumProtos > MAX_NUM_PROTOS) {
127  tprintf("Ouch! number of protos = %d, vs max of %d!",
128  Class->NumProtos, MAX_NUM_PROTOS);
129  }
130  return (NewProto);
131 }
132 
133 
143  inT16 Pid;
144  FLOAT32 TotalLength = 0;
145 
146  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
147  if (test_bit (Config, Pid)) {
148 
149  TotalLength += (ProtoIn (Class, Pid))->Length;
150  }
151  }
152  return (TotalLength);
153 }
154 
155 
164  inT16 Pid;
165  FLOAT32 TotalLength = 0;
166 
167  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
168  TotalLength += (ProtoIn (Class, Pid))->Length;
169  }
170  return (TotalLength);
171 }
172 
173 
182 void CopyProto(PROTO Src, PROTO Dest) {
183  Dest->X = Src->X;
184  Dest->Y = Src->Y;
185  Dest->Length = Src->Length;
186  Dest->Angle = Src->Angle;
187  Dest->A = Src->A;
188  Dest->B = Src->B;
189  Dest->C = Src->C;
190 }
191 
192 
193 /**********************************************************************
194  * FillABC
195  *
196  * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
197  **********************************************************************/
198 void FillABC(PROTO Proto) {
199  FLOAT32 Slope, Intercept, Normalizer;
200 
201  Slope = tan (Proto->Angle * 2.0 * PI);
202  Intercept = Proto->Y - Slope * Proto->X;
203  Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
204  Proto->A = Slope * Normalizer;
205  Proto->B = -Normalizer;
206  Proto->C = Intercept * Normalizer;
207 }
208 
209 
210 /**********************************************************************
211  * FreeClass
212  *
213  * Deallocate the memory consumed by the specified class.
214  **********************************************************************/
215 void FreeClass(CLASS_TYPE Class) {
216  if (Class) {
217  FreeClassFields(Class);
218  delete Class;
219  }
220 }
221 
222 
223 /**********************************************************************
224  * FreeClassFields
225  *
226  * Deallocate the memory consumed by subfields of the specified class.
227  **********************************************************************/
229  int i;
230 
231  if (Class) {
232  if (Class->MaxNumProtos > 0)
233  memfree (Class->Prototypes);
234  if (Class->MaxNumConfigs > 0) {
235  for (i = 0; i < Class->NumConfigs; i++)
236  FreeBitVector (Class->Configurations[i]);
237  memfree (Class->Configurations);
238  }
239  }
240 }
241 
242 /**********************************************************************
243  * NewClass
244  *
245  * Allocate a new class with enough memory to hold the specified number
246  * of prototypes and configurations.
247  **********************************************************************/
248 CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
249  CLASS_TYPE Class;
250 
251  Class = new CLASS_STRUCT;
252 
253  if (NumProtos > 0)
254  Class->Prototypes = (PROTO) Emalloc (NumProtos * sizeof (PROTO_STRUCT));
255 
256  if (NumConfigs > 0)
257  Class->Configurations = (CONFIGS) Emalloc (NumConfigs *
258  sizeof (BIT_VECTOR));
259  Class->MaxNumProtos = NumProtos;
260  Class->MaxNumConfigs = NumConfigs;
261  Class->NumProtos = 0;
262  Class->NumConfigs = 0;
263  return (Class);
264 
265 }
266 
267 
268 /**********************************************************************
269  * PrintProtos
270  *
271  * Print the list of prototypes in this class type.
272  **********************************************************************/
273 void PrintProtos(CLASS_TYPE Class) {
274  inT16 Pid;
275 
276  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
277  cprintf ("Proto %d:\t", Pid);
278  PrintProto (ProtoIn (Class, Pid));
279  cprintf ("\t");
280  PrintProtoLine (ProtoIn (Class, Pid));
281  new_line();
282  }
283 }
#define CONFIG_INCREMENT
Definition: protos.cpp:43
#define zero_all_bits(array, length)
Definition: bitvec.h:33
#define PrintProtoLine(Proto)
Definition: protos.h:148
void memfree(void *element)
Definition: freelist.cpp:30
BIT_VECTOR * CONFIGS
Definition: protos.h:40
PROTO_STRUCT * PROTO
Definition: protos.h:52
int AddConfigToClass(CLASS_TYPE Class)
Definition: protos.cpp:63
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:55
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
float FLOAT32
Definition: host.h:111
void * Erealloc(void *ptr, int size)
Definition: emalloc.cpp:64
#define new_line()
Definition: cutil.h:83
#define ProtoIn(Class, Pid)
Definition: protos.h:123
#define NUMBER_OF_CLASSES
Definition: protos.h:73
#define reset_bit(array, bit)
Definition: bitvec.h:59
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
#define tprintf(...)
Definition: tprintf.h:31
inT16 NumConfigs
Definition: protos.h:62
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:90
CLASS_STRUCT TrainingData[NUMBER_OF_CLASSES]
Definition: protos.cpp:48
#define test_bit(array, bit)
Definition: bitvec.h:61
inT16 NumProtos
Definition: protos.h:59
BIT_VECTOR ExpandBitVector(BIT_VECTOR Vector, int NewNumBits)
Definition: bitvec.cpp:48
FLOAT32 ClassConfigLength(CLASS_TYPE Class, BIT_VECTOR Config)
Definition: protos.cpp:142
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
Definition: protos.cpp:248
#define STRING_VAR(name, val, comment)
Definition: params.h:283
FLOAT32 X
Definition: protos.h:47
FLOAT32 Angle
Definition: protos.h:49
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:99
void * Emalloc(int Size)
Definition: emalloc.cpp:47
void PrintProtos(CLASS_TYPE Class)
Definition: protos.cpp:273
inT16 MaxNumConfigs
Definition: protos.h:63
FLOAT32 B
Definition: protos.h:45
void FreeClass(CLASS_TYPE Class)
Definition: protos.cpp:215
CLUSTERCONFIG Config
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:228
#define PrintProto(Proto)
Definition: protos.h:133
void FillABC(PROTO Proto)
Definition: protos.cpp:198
FLOAT32 Length
Definition: protos.h:50
#define PROTO_INCREMENT
Definition: protos.cpp:42
#define PI
Definition: const.h:19
FLOAT32 C
Definition: protos.h:46
void cprintf(const char *format,...)
Definition: callcpp.cpp:40
char * classify_training_file
Definition: protos.cpp:50
#define MAX_NUM_PROTOS
Definition: intproto.h:47
FLOAT32 ClassProtoLength(CLASS_TYPE Class)
Definition: protos.cpp:163
void CopyProto(PROTO Src, PROTO Dest)
Definition: protos.cpp:182
FLOAT32 A
Definition: protos.h:44
inT16 MaxNumProtos
Definition: protos.h:60
PROTO Prototypes
Definition: protos.h:61
CONFIGS Configurations
Definition: protos.h:64
short inT16
Definition: host.h:100
FLOAT32 Y
Definition: protos.h:48