dict/states.cpp

Go to the documentation of this file.
00001 
00020 /*----------------------------------------------------------------------
00021               I n c l u d e s
00022 ----------------------------------------------------------------------*/
00023 #include "states.h"
00024 #include "structures.h"
00025 #include "tordvars.h"
00026 #include "callcpp.h"
00027 
00028 /*-------------------------------------------------------------------------
00029             Variables
00030 --------------------------------------------------------------------------*/
00031 #define STATEBLOCK 100           /* Cells per block */
00032 
00033 
00036 makestructure (newstate, free_state, printstate, STATE,
00037 freestate, STATEBLOCK, "STATE", statecount);
00040 /*----------------------------------------------------------------------
00041               F u n c t i o n s
00042 ----------------------------------------------------------------------*/
00049 SEARCH_STATE bin_to_chunks(STATE *state, int num_joints) { 
00050   int x;
00051   unsigned int mask;
00052   int depth;
00053   int pieces = 0;
00054   SEARCH_STATE s;
00055 
00056   s = memalloc (sizeof (int) * (ones_in_state (state, num_joints) + 1));
00057 
00058   depth = 1;
00059   mask = 1 << (num_joints - 1 - 32);
00060   for (x = num_joints; x > 32; x--) {
00061     if (state->part1 & mask) {
00062       s[depth++] = pieces;
00063       pieces = 0;
00064     }
00065     else {
00066       pieces++;
00067     }
00068     mask >>= 1;
00069   }
00070 
00071   if (num_joints > 32)
00072     mask = 1 << 31;
00073   else
00074     mask = 1 << (num_joints - 1);
00075 
00076   while (x--) {
00077     if (state->part2 & mask) {
00078       s[depth++] = pieces;
00079       pieces = 0;
00080     }
00081     else {
00082       pieces++;
00083     }
00084     mask >>= 1;
00085   }
00086   s[0] = depth - 1;
00087 
00088   return (s);
00089 }
00090 
00091 
00098 void bin_to_pieces(STATE *state, int num_joints, PIECES_STATE pieces) { 
00099   int x;
00100   unsigned int mask;             /* Bit mask */
00101   INT16 num_pieces = 0;
00102   /* Preset mask */
00103   if (debug_8)
00104     print_state ("bin_to_pieces = ", state, num_joints);
00105 
00106   mask = ((num_joints > 32) ?
00107     (1 << (num_joints - 1 - 32)) : (1 << (num_joints - 1)));
00108 
00109   pieces[num_pieces] = 0;
00110 
00111   for (x = num_joints - 1; x >= 0; x--) {
00112                                  /* Iterate all bits */
00113     pieces[num_pieces]++;
00114 
00115     if ((x < 32) ?               /* Test for 1 bit */
00116       ((state->part2 & mask) ? TRUE : FALSE) :
00117     ((state->part1 & mask) ? TRUE : FALSE)) {
00118       pieces[++num_pieces] = 0;
00119       if (debug_8)
00120         cprintf ("[%d]=%d ", num_pieces - 1, pieces[num_pieces - 1]);
00121     }
00122     /* Next mask value */
00123     mask = ((mask == 1) ? (1 << 31) : (mask >> 1));
00124   }
00125   pieces[num_pieces]++;
00126   pieces[++num_pieces] = 0;
00127   ASSERT_HOST (num_pieces < MAX_NUM_CHUNKS + 2);
00128   if (debug_8)
00129     new_line(); 
00130 }
00131 
00132 
00136 void insert_new_chunk(register STATE *state,
00137                       register int index,
00138                       register int num_joints) {
00139   register unsigned int mask;
00140   register unsigned int result;
00141 
00142   index = (num_joints - index);
00143   if (index < 32) {
00144     mask = ~0;
00145     mask <<= index;
00146     result = (mask & state->part2) << 1;
00147     result |= (~mask & state->part2);
00148     state->part1 <<= 1;
00149     if (state->part2 & 0x80000000)
00150       state->part1 |= 1;
00151     state->part2 = result;
00152   }
00153   else {
00154     mask = ~0;
00155     mask <<= index - 32;
00156     result = (mask & state->part1) << 1;
00157     result |= (~mask & state->part1);
00158     state->part1 = result;
00159   }
00160 }
00161 
00162 
00167 STATE *new_state(STATE *oldstate) { 
00168   STATE *this_state;
00169 
00170   this_state = newstate ();
00171   this_state->part1 = oldstate->part1;
00172   this_state->part2 = oldstate->part2;
00173   return (this_state);
00174 }
00175 
00176 
00180 int ones_in_state(STATE *state, int num_joints) { 
00181   INT8 num_ones = 0;
00182   INT8 x;
00183   unsigned int mask;
00184 
00185   if (num_joints > 32)           /* Preset mask */
00186     mask = 1 << (num_joints - 1 - 32);
00187   else
00188     mask = 1 << (num_joints - 1);
00189 
00190   for (x = num_joints - 1; x >= 0; x--) {
00191                                  /* Iterate all bits */
00192 
00193     if (x < 32)
00194       num_ones += ((state->part2 & mask) ? 1 : 0);
00195     else
00196       num_ones += ((state->part1 & mask) ? 1 : 0);
00197 
00198     if (mask == 1)               /* Next mask value */
00199       mask = 1 << 31;
00200     else
00201       mask >>= 1;
00202   }
00203 
00204   return (num_ones);
00205 }
00206 
00207 
00211 void print_state(const char *label, STATE *state, int num_joints) { 
00212   int x;
00213   unsigned int mask;             /* Bit mask */
00214 
00215   if (num_joints > 32)           /* Preset mask */
00216     mask = 1 << (num_joints - 1 - 32);
00217   else
00218     mask = 1 << (num_joints - 1);
00219 
00220   cprintf ("%s ", label);
00221 
00222   for (x = num_joints - 1; x >= 0; x--) {
00223                                  /* Iterate all bits */
00224 
00225     if (x < 32)
00226       cprintf ("%d", ((state->part2 & mask) ? 1 : 0));
00227     else
00228       cprintf ("%d", ((state->part1 & mask) ? 1 : 0));
00229     if (x % 4 == 0)
00230       cprintf (" ");
00231 
00232     if (mask == 1)               /* Next mask value */
00233       mask = 1 << 31;
00234     else
00235       mask >>= 1;
00236   }
00237 
00238   new_line(); 
00239 }
00240 
00241 
00245 void set_n_ones(STATE *state, int n) { 
00246   if (n < 32) {
00247     state->part2 = ~0;
00248     state->part2 >>= 32 - n;
00249     state->part1 = 0;
00250   }
00251   else {
00252     state->part2 = ~0;
00253     state->part1 = ~0;
00254     state->part1 >>= 64 - n;
00255   }
00256 }
00257 
00258 
00274 int compare_states(STATE *true_state, STATE *this_state, int *blob_index) { 
00275   int blob_count;                //number found
00276   int true_index;                //index of true blob
00277   int index;                     //current
00278   int result = 0;                //return value
00279   UINT32 mask;
00280 
00281   if (true_state->part1 == this_state->part1
00282     && true_state->part2 == this_state->part2)
00283     return 2;
00284   if (*blob_index == 0) {
00285     if (bits_in_states > 32) {
00286       for (mask = 1 << bits_in_states - 33; mask != 0; mask >>= 1) {
00287         if (this_state->part1 & mask) {
00288           if (true_state->part1 & mask)
00289             return 2;
00290           else
00291             return 1;
00292         }
00293         else if (true_state->part1 & mask)
00294           return 4;
00295       }
00296       index = 31;
00297     }
00298     else
00299       index = bits_in_states - 1;
00300     for (mask = 1 << index; mask != 0; mask >>= 1) {
00301       if (this_state->part2 & mask) {
00302         if (true_state->part2 & mask)
00303           return 2;
00304         else
00305           return 1;
00306       }
00307       else if (true_state->part2 & mask)
00308         return 4;
00309     }
00310     return 2;
00311   }
00312   else {
00313     blob_count = 0;
00314     true_index = 0;
00315     if (bits_in_states > 32) {
00316       for (mask = 1 << bits_in_states - 33; mask != 0; mask >>= 1) {
00317         if (true_state->part1 & mask)
00318           true_index++;
00319         if (this_state->part1 & mask) {
00320           blob_count++;
00321           if (blob_count == *blob_index) {
00322             if ((true_state->part1 & mask) == 0)
00323               result = 1;
00324             break;
00325           }
00326         }
00327       }
00328       if (blob_count == *blob_index) {
00329         for (mask >>= 1; mask != 0; mask >>= 1) {
00330           if (this_state->part1 & mask) {
00331             if ((true_state->part1 & mask) && result == 0)
00332               return 2;
00333             else
00334               return result | 1;
00335           }
00336           else if (true_state->part1 & mask)
00337             result |= 4;
00338         }
00339       }
00340       index = 31;
00341     }
00342     else
00343       index = bits_in_states - 1;
00344     mask = 1 << index;
00345     if (blob_count < *blob_index) {
00346       for (; mask != 0; mask >>= 1) {
00347         if (true_state->part2 & mask)
00348           true_index++;
00349         if (this_state->part2 & mask) {
00350           blob_count++;
00351           if (blob_count == *blob_index) {
00352             if ((true_state->part2 & mask) == 0)
00353               result = 1;
00354             break;
00355           }
00356         }
00357       }
00358       if (blob_count != *blob_index)
00359         return 2;
00360       mask >>= 1;
00361     }
00362     *blob_index = true_index;
00363     for (; mask != 0; mask >>= 1) {
00364       if (this_state->part2 & mask) {
00365         if ((true_state->part2 & mask) && result == 0)
00366           return 2;
00367         else
00368           return result | 1;
00369       }
00370       else if (true_state->part2 & mask)
00371         result |= 4;
00372     }
00373     return result == 0 ? 2 : result;
00374   }
00375 }

Generated on Wed Feb 28 19:49:10 2007 for Tesseract by  doxygen 1.5.1