tesseract  4.00.00dev
matrix.h
Go to the documentation of this file.
1 /* -*-C-*-
2  ******************************************************************************
3  * File: matrix.h (Formerly matrix.h)
4  * Description: Generic 2-d array/matrix and banded triangular matrix class.
5  * Author: Ray Smith
6  * TODO(rays) Separate from ratings matrix, which it also contains:
7  *
8  * Descrition: Ratings matrix class (specialization of banded matrix).
9  * Segmentation search matrix of lists of BLOB_CHOICE.
10  * Author: Mark Seaman, OCR Technology
11  * Created: Wed May 16 13:22:06 1990
12  * Modified: Tue Mar 19 16:00:20 1991 (Mark Seaman) marks@hpgrlt
13  * Language: C
14  * Package: N/A
15  * Status: Experimental (Do Not Distribute)
16  *
17  * (c) Copyright 1990, Hewlett-Packard Company.
18  ** Licensed under the Apache License, Version 2.0 (the "License");
19  ** you may not use this file except in compliance with the License.
20  ** You may obtain a copy of the License at
21  ** http://www.apache.org/licenses/LICENSE-2.0
22  ** Unless required by applicable law or agreed to in writing, software
23  ** distributed under the License is distributed on an "AS IS" BASIS,
24  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  ** See the License for the specific language governing permissions and
26  ** limitations under the License.
27  *
28  *********************************************************************************/
29 #ifndef TESSERACT_CCSTRUCT_MATRIX_H_
30 #define TESSERACT_CCSTRUCT_MATRIX_H_
31 
32 #include <math.h>
33 #include "kdpair.h"
34 #include "points.h"
35 #include "serialis.h"
36 #include "unicharset.h"
37 
38 class BLOB_CHOICE;
39 class BLOB_CHOICE_LIST;
40 
41 #define NOT_CLASSIFIED static_cast<BLOB_CHOICE_LIST*>(0)
42 
43 // A generic class to hold a 2-D matrix with entries of type T, but can also
44 // act as a base class for other implementations, such as a triangular or
45 // banded matrix.
46 template <class T>
48  public:
49  // Initializes the array size, and empty element, but cannot allocate memory
50  // for the subclasses or initialize because calls to the num_elements
51  // member will be routed to the base class implementation. Subclasses can
52  // either pass the memory in, or allocate after by calling Resize().
53  GENERIC_2D_ARRAY(int dim1, int dim2, const T& empty, T* array)
54  : empty_(empty), dim1_(dim1), dim2_(dim2), array_(array) {
55  size_allocated_ = dim1 * dim2;
56  }
57  // Original constructor for a full rectangular matrix DOES allocate memory
58  // and initialize it to empty.
59  GENERIC_2D_ARRAY(int dim1, int dim2, const T& empty)
60  : empty_(empty), dim1_(dim1), dim2_(dim2) {
61  int new_size = dim1 * dim2;
62  array_ = new T[new_size];
63  size_allocated_ = new_size;
64  for (int i = 0; i < size_allocated_; ++i)
65  array_[i] = empty_;
66  }
67  // Default constructor for array allocation. Use Resize to set the size.
69  : array_(NULL), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
70  size_allocated_(0) {
71  }
73  : array_(NULL), empty_(static_cast<T>(0)), dim1_(0), dim2_(0),
74  size_allocated_(0) {
75  *this = src;
76  }
77  virtual ~GENERIC_2D_ARRAY() { delete[] array_; }
78 
79  void operator=(const GENERIC_2D_ARRAY<T>& src) {
80  ResizeNoInit(src.dim1(), src.dim2());
81  memcpy(array_, src.array_, num_elements() * sizeof(array_[0]));
82  }
83 
84  // Reallocates the array to the given size. Does not keep old data, but does
85  // not initialize the array either.
86  // The allocated memory is expanded on the end by pad, allowing deliberate
87  // access beyond the bounds of the array.
88  void ResizeNoInit(int size1, int size2, int pad = 0) {
89  int new_size = size1 * size2 + pad;
90  if (new_size > size_allocated_) {
91  delete [] array_;
92  array_ = new T[new_size];
93  size_allocated_ = new_size;
94  }
95  dim1_ = size1;
96  dim2_ = size2;
97  // Fill the padding data so it isn't uninitialized.
98  for (int i = size1 * size2; i < new_size; ++i) array_[i] = empty_;
99  }
100 
101  // Reallocate the array to the given size. Does not keep old data.
102  void Resize(int size1, int size2, const T& empty) {
103  empty_ = empty;
104  ResizeNoInit(size1, size2);
105  Clear();
106  }
107 
108  // Reallocate the array to the given size, keeping old data.
109  void ResizeWithCopy(int size1, int size2) {
110  if (size1 != dim1_ || size2 != dim2_) {
111  int new_size = size1 * size2;
112  T* new_array = new T[new_size];
113  for (int col = 0; col < size1; ++col) {
114  for (int row = 0; row < size2; ++row) {
115  int old_index = col * dim2() + row;
116  int new_index = col * size2 + row;
117  if (col < dim1_ && row < dim2_) {
118  new_array[new_index] = array_[old_index];
119  } else {
120  new_array[new_index] = empty_;
121  }
122  }
123  }
124  delete[] array_;
125  array_ = new_array;
126  dim1_ = size1;
127  dim2_ = size2;
128  size_allocated_ = new_size;
129  }
130  }
131 
132  // Sets all the elements of the array to the empty value.
133  void Clear() {
134  int total_size = num_elements();
135  for (int i = 0; i < total_size; ++i)
136  array_[i] = empty_;
137  }
138 
139  // Writes to the given file. Returns false in case of error.
140  // Only works with bitwise-serializeable types!
141  bool Serialize(FILE* fp) const {
142  if (!SerializeSize(fp)) return false;
143  if (fwrite(&empty_, sizeof(empty_), 1, fp) != 1) return false;
144  int size = num_elements();
145  if (fwrite(array_, sizeof(*array_), size, fp) != size) return false;
146  return true;
147  }
148  bool Serialize(tesseract::TFile* fp) const {
149  if (!SerializeSize(fp)) return false;
150  if (fp->FWrite(&empty_, sizeof(empty_), 1) != 1) return false;
151  int size = num_elements();
152  if (fp->FWrite(array_, sizeof(*array_), size) != size) return false;
153  return true;
154  }
155 
156  // Reads from the given file. Returns false in case of error.
157  // Only works with bitwise-serializeable types!
158  // If swap is true, assumes a big/little-endian swap is needed.
159  bool DeSerialize(bool swap, FILE* fp) {
160  if (!DeSerializeSize(swap, fp)) return false;
161  if (fread(&empty_, sizeof(empty_), 1, fp) != 1) return false;
162  if (swap) ReverseN(&empty_, sizeof(empty_));
163  int size = num_elements();
164  if (fread(array_, sizeof(*array_), size, fp) != size) return false;
165  if (swap) {
166  for (int i = 0; i < size; ++i)
167  ReverseN(&array_[i], sizeof(array_[i]));
168  }
169  return true;
170  }
172  if (!DeSerializeSize(fp)) return false;
173  if (fp->FReadEndian(&empty_, sizeof(empty_), 1) != 1) return false;
174  int size = num_elements();
175  if (fp->FReadEndian(array_, sizeof(*array_), size) != size) return false;
176  return true;
177  }
178 
179  // Writes to the given file. Returns false in case of error.
180  // Assumes a T::Serialize(FILE*) const function.
181  bool SerializeClasses(FILE* fp) const {
182  if (!SerializeSize(fp)) return false;
183  if (!empty_.Serialize(fp)) return false;
184  int size = num_elements();
185  for (int i = 0; i < size; ++i) {
186  if (!array_[i].Serialize(fp)) return false;
187  }
188  return true;
189  }
190 
191  // Reads from the given file. Returns false in case of error.
192  // Assumes a T::DeSerialize(bool swap, FILE*) function.
193  // If swap is true, assumes a big/little-endian swap is needed.
194  bool DeSerializeClasses(bool swap, FILE* fp) {
195  if (!DeSerializeSize(swap, fp)) return false;
196  if (!empty_.DeSerialize(swap, fp)) return false;
197  int size = num_elements();
198  for (int i = 0; i < size; ++i) {
199  if (!array_[i].DeSerialize(swap, fp)) return false;
200  }
201  return true;
202  }
203 
204  // Provide the dimensions of this rectangular matrix.
205  int dim1() const { return dim1_; }
206  int dim2() const { return dim2_; }
207  // Returns the number of elements in the array.
208  // Banded/triangular matrices may override.
209  virtual int num_elements() const { return dim1_ * dim2_; }
210 
211  // Expression to select a specific location in the matrix. The matrix is
212  // stored COLUMN-major, so the left-most index is the most significant.
213  // This allows [][] access to use indices in the same order as (,).
214  virtual int index(int column, int row) const {
215  return (column * dim2_ + row);
216  }
217 
218  // Put a list element into the matrix at a specific location.
219  void put(ICOORD pos, const T& thing) {
220  array_[this->index(pos.x(), pos.y())] = thing;
221  }
222  void put(int column, int row, const T& thing) {
223  array_[this->index(column, row)] = thing;
224  }
225 
226  // Get the item at a specified location from the matrix.
227  T get(ICOORD pos) const {
228  return array_[this->index(pos.x(), pos.y())];
229  }
230  T get(int column, int row) const {
231  return array_[this->index(column, row)];
232  }
233  // Return a reference to the element at the specified location.
234  const T& operator()(int column, int row) const {
235  return array_[this->index(column, row)];
236  }
237  T& operator()(int column, int row) {
238  return array_[this->index(column, row)];
239  }
240  // Allow access using array[column][row]. NOTE that the indices are
241  // in the same left-to-right order as the () indexing.
242  T* operator[](int column) {
243  return &array_[this->index(column, 0)];
244  }
245  const T* operator[](int column) const {
246  return &array_[this->index(column, 0)];
247  }
248 
249  // Adds addend to *this, element-by-element.
250  void operator+=(const GENERIC_2D_ARRAY<T>& addend) {
251  if (dim2_ == addend.dim2_) {
252  // Faster if equal size in the major dimension.
253  int size = MIN(num_elements(), addend.num_elements());
254  for (int i = 0; i < size; ++i) {
255  array_[i] += addend.array_[i];
256  }
257  } else {
258  for (int x = 0; x < dim1_; x++) {
259  for (int y = 0; y < dim2_; y++) {
260  (*this)(x, y) += addend(x, y);
261  }
262  }
263  }
264  }
265  // Subtracts minuend from *this, element-by-element.
266  void operator-=(const GENERIC_2D_ARRAY<T>& minuend) {
267  if (dim2_ == minuend.dim2_) {
268  // Faster if equal size in the major dimension.
269  int size = MIN(num_elements(), minuend.num_elements());
270  for (int i = 0; i < size; ++i) {
271  array_[i] -= minuend.array_[i];
272  }
273  } else {
274  for (int x = 0; x < dim1_; x++) {
275  for (int y = 0; y < dim2_; y++) {
276  (*this)(x, y) -= minuend(x, y);
277  }
278  }
279  }
280  }
281  // Adds addend to all elements.
282  void operator+=(const T& addend) {
283  int size = num_elements();
284  for (int i = 0; i < size; ++i) {
285  array_[i] += addend;
286  }
287  }
288  // Multiplies *this by factor, element-by-element.
289  void operator*=(const T& factor) {
290  int size = num_elements();
291  for (int i = 0; i < size; ++i) {
292  array_[i] *= factor;
293  }
294  }
295  // Clips *this to the given range.
296  void Clip(const T& rangemin, const T& rangemax) {
297  int size = num_elements();
298  for (int i = 0; i < size; ++i) {
299  array_[i] = ClipToRange(array_[i], rangemin, rangemax);
300  }
301  }
302  // Returns true if all elements of *this are within the given range.
303  // Only uses operator<
304  bool WithinBounds(const T& rangemin, const T& rangemax) const {
305  int size = num_elements();
306  for (int i = 0; i < size; ++i) {
307  const T& value = array_[i];
308  if (value < rangemin || rangemax < value)
309  return false;
310  }
311  return true;
312  }
313  // Normalize the whole array.
314  double Normalize() {
315  int size = num_elements();
316  if (size <= 0) return 0.0;
317  // Compute the mean.
318  double mean = 0.0;
319  for (int i = 0; i < size; ++i) {
320  mean += array_[i];
321  }
322  mean /= size;
323  // Subtract the mean and compute the standard deviation.
324  double sd = 0.0;
325  for (int i = 0; i < size; ++i) {
326  double normed = array_[i] - mean;
327  array_[i] = normed;
328  sd += normed * normed;
329  }
330  sd = sqrt(sd / size);
331  if (sd > 0.0) {
332  // Divide by the sd.
333  for (int i = 0; i < size; ++i) {
334  array_[i] /= sd;
335  }
336  }
337  return sd;
338  }
339 
340  // Returns the maximum value of the array.
341  T Max() const {
342  int size = num_elements();
343  if (size <= 0) return empty_;
344  // Compute the max.
345  T max_value = array_[0];
346  for (int i = 1; i < size; ++i) {
347  const T& value = array_[i];
348  if (value > max_value) max_value = value;
349  }
350  return max_value;
351  }
352 
353  // Returns the maximum absolute value of the array.
354  T MaxAbs() const {
355  int size = num_elements();
356  if (size <= 0) return empty_;
357  // Compute the max.
358  T max_abs = static_cast<T>(0);
359  for (int i = 0; i < size; ++i) {
360  T value = static_cast<T>(fabs(array_[i]));
361  if (value > max_abs) max_abs = value;
362  }
363  return max_abs;
364  }
365 
366  // Accumulates the element-wise sums of squares of src into *this.
367  void SumSquares(const GENERIC_2D_ARRAY<T>& src, T decay_factor) {
368  T update_factor = 1.0 - decay_factor;
369  int size = num_elements();
370  for (int i = 0; i < size; ++i) {
371  array_[i] = array_[i] * decay_factor +
372  update_factor * src.array_[i] * src.array_[i];
373  }
374  }
375 
376  // Scales each element using the adam algorithm, ie array_[i] by
377  // sqrt(sqsum[i] + epsilon)).
379  const GENERIC_2D_ARRAY<T>& sqsum, T epsilon) {
380  int size = num_elements();
381  for (int i = 0; i < size; ++i) {
382  array_[i] += sum.array_[i] / (sqrt(sqsum.array_[i]) + epsilon);
383  }
384  }
385 
386  void AssertFinite() const {
387  int size = num_elements();
388  for (int i = 0; i < size; ++i) {
389  ASSERT_HOST(isfinite(array_[i]));
390  }
391  }
392 
393  // REGARDLESS OF THE CURRENT DIMENSIONS, treats the data as a
394  // num_dims-dimensional array/tensor with dimensions given by dims, (ordered
395  // from most significant to least significant, the same as standard C arrays)
396  // and moves src_dim to dest_dim, with the initial dest_dim and any dimensions
397  // in between shifted towards the hole left by src_dim. Example:
398  // Current data content: array_=[0, 1, 2, ....119]
399  // perhaps *this may be of dim[40, 3], with values [[0, 1, 2][3, 4, 5]...
400  // but the current dimensions are irrelevant.
401  // num_dims = 4, dims=[5, 4, 3, 2]
402  // src_dim=3, dest_dim=1
403  // tensor=[[[[0, 1][2, 3][4, 5]]
404  // [[6, 7][8, 9][10, 11]]
405  // [[12, 13][14, 15][16, 17]]
406  // [[18, 19][20, 21][22, 23]]]
407  // [[[24, 25]...
408  // output dims =[5, 2, 4, 3]
409  // output tensor=[[[[0, 2, 4][6, 8, 10][12, 14, 16][18, 20, 22]]
410  // [[1, 3, 5][7, 9, 11][13, 15, 17][19, 21, 23]]]
411  // [[[24, 26, 28]...
412  // which is stored in the array_ as:
413  // [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 1, 3, 5, 7, 9, 11, 13...]
414  // NOTE: the 2 stored matrix dimensions are simply copied from *this. To
415  // change the dimensions after the transpose, use ResizeNoInit.
416  // Higher dimensions above 2 are strictly the responsibility of the caller.
417  void RotatingTranspose(const int* dims, int num_dims, int src_dim,
418  int dest_dim, GENERIC_2D_ARRAY<T>* result) const {
419  int max_d = MAX(src_dim, dest_dim);
420  int min_d = MIN(src_dim, dest_dim);
421  // In a tensor of shape [d0, d1... min_d, ... max_d, ... dn-2, dn-1], the
422  // ends outside of min_d and max_d are unaffected, with [max_d +1, dn-1]
423  // being contiguous blocks of data that will move together, and
424  // [d0, min_d -1] being replicas of the transpose operation.
425  // num_replicas represents the large dimensions unchanged by the operation.
426  // move_size represents the small dimensions unchanged by the operation.
427  // src_step represents the stride in the src between each adjacent group
428  // in the destination.
429  int num_replicas = 1, move_size = 1, src_step = 1;
430  for (int d = 0; d < min_d; ++d) num_replicas *= dims[d];
431  for (int d = max_d + 1; d < num_dims; ++d) move_size *= dims[d];
432  for (int d = src_dim + 1; d < num_dims; ++d) src_step *= dims[d];
433  if (src_dim > dest_dim) src_step *= dims[src_dim];
434  // wrap_size is the size of a single replica, being the amount that is
435  // handled num_replicas times.
436  int wrap_size = move_size;
437  for (int d = min_d; d <= max_d; ++d) wrap_size *= dims[d];
438  result->ResizeNoInit(dim1_, dim2_);
439  result->empty_ = empty_;
440  const T* src = array_;
441  T* dest = result->array_;
442  for (int replica = 0; replica < num_replicas; ++replica) {
443  for (int start = 0; start < src_step; start += move_size) {
444  for (int pos = start; pos < wrap_size; pos += src_step) {
445  memcpy(dest, src + pos, sizeof(*dest) * move_size);
446  dest += move_size;
447  }
448  }
449  src += wrap_size;
450  }
451  }
452 
453  // Delete objects pointed to by array_[i].
455  int size = num_elements();
456  for (int i = 0; i < size; ++i) {
457  T matrix_cell = array_[i];
458  if (matrix_cell != empty_)
459  delete matrix_cell;
460  }
461  }
462 
463  protected:
464  // Factored helper to serialize the size.
465  bool SerializeSize(FILE* fp) const {
466  inT32 size = dim1_;
467  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
468  size = dim2_;
469  if (fwrite(&size, sizeof(size), 1, fp) != 1) return false;
470  return true;
471  }
472  bool SerializeSize(tesseract::TFile* fp) const {
473  inT32 size = dim1_;
474  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
475  size = dim2_;
476  if (fp->FWrite(&size, sizeof(size), 1) != 1) return false;
477  return true;
478  }
479  // Factored helper to deserialize the size.
480  // If swap is true, assumes a big/little-endian swap is needed.
481  bool DeSerializeSize(bool swap, FILE* fp) {
482  inT32 size1, size2;
483  if (fread(&size1, sizeof(size1), 1, fp) != 1) return false;
484  if (fread(&size2, sizeof(size2), 1, fp) != 1) return false;
485  if (swap) {
486  ReverseN(&size1, sizeof(size1));
487  ReverseN(&size2, sizeof(size2));
488  }
489  Resize(size1, size2, empty_);
490  return true;
491  }
493  inT32 size1, size2;
494  if (fp->FReadEndian(&size1, sizeof(size1), 1) != 1) return false;
495  if (fp->FReadEndian(&size2, sizeof(size2), 1) != 1) return false;
496  Resize(size1, size2, empty_);
497  return true;
498  }
499 
500  T* array_;
501  T empty_; // The unused cell.
502  int dim1_; // Size of the 1st dimension in indexing functions.
503  int dim2_; // Size of the 2nd dimension in indexing functions.
504  // The total size to which the array can be expanded before a realloc is
505  // needed. If Resize is used, memory is retained so it can be re-expanded
506  // without a further alloc, and this stores the allocated size.
508 };
509 
510 // A generic class to store a banded triangular matrix with entries of type T.
511 // In this array, the nominally square matrix is dim1_ x dim1_, and dim2_ is
512 // the number of bands, INCLUDING the diagonal. The storage is thus of size
513 // dim1_ * dim2_ and index(col, row) = col * dim2_ + row - col, and an
514 // assert will fail if row < col or row - col >= dim2.
515 template <class T>
516 class BandTriMatrix : public GENERIC_2D_ARRAY<T> {
517  public:
518  // Allocate a piece of memory to hold a 2d-array of the given dimension.
519  // Initialize all the elements of the array to empty instead of assuming
520  // that a default constructor can be used.
521  BandTriMatrix(int dim1, int dim2, const T& empty)
522  : GENERIC_2D_ARRAY<T>(dim1, dim2, empty) {
523  }
524  // The default destructor will do.
525 
526  // Provide the dimensions of this matrix.
527  // dimension is the size of the nominally square matrix.
528  int dimension() const { return this->dim1_; }
529  // bandwidth is the number of bands in the matrix, INCLUDING the diagonal.
530  int bandwidth() const { return this->dim2_; }
531 
532  // Expression to select a specific location in the matrix. The matrix is
533  // stored COLUMN-major, so the left-most index is the most significant.
534  // This allows [][] access to use indices in the same order as (,).
535  virtual int index(int column, int row) const {
536  ASSERT_HOST(row >= column);
537  ASSERT_HOST(row - column < this->dim2_);
538  return column * this->dim2_ + row - column;
539  }
540 
541  // Appends array2 corner-to-corner to *this, making an array of dimension
542  // equal to the sum of the individual dimensions.
543  // array2 is not destroyed, but is left empty, as all elements are moved
544  // to *this.
546  int new_dim1 = this->dim1_ + array2->dim1_;
547  int new_dim2 = MAX(this->dim2_, array2->dim2_);
548  T* new_array = new T[new_dim1 * new_dim2];
549  for (int col = 0; col < new_dim1; ++col) {
550  for (int j = 0; j < new_dim2; ++j) {
551  int new_index = col * new_dim2 + j;
552  if (col < this->dim1_ && j < this->dim2_) {
553  new_array[new_index] = this->get(col, col + j);
554  } else if (col >= this->dim1_ && j < array2->dim2_) {
555  new_array[new_index] = array2->get(col - this->dim1_,
556  col - this->dim1_ + j);
557  array2->put(col - this->dim1_, col - this->dim1_ + j, NULL);
558  } else {
559  new_array[new_index] = this->empty_;
560  }
561  }
562  }
563  delete[] this->array_;
564  this->array_ = new_array;
565  this->dim1_ = new_dim1;
566  this->dim2_ = new_dim2;
567  }
568 };
569 
570 class MATRIX : public BandTriMatrix<BLOB_CHOICE_LIST *> {
571  public:
572  MATRIX(int dimension, int bandwidth)
573  : BandTriMatrix<BLOB_CHOICE_LIST *>(dimension, bandwidth, NOT_CLASSIFIED) {}
574 
575  // Returns true if there are any real classification results.
576  bool Classified(int col, int row, int wildcard_id) const;
577 
578  // Expands the existing matrix in-place to make the band wider, without
579  // losing any existing data.
580  void IncreaseBandSize(int bandwidth);
581 
582  // Returns a bigger MATRIX with a new column and row in the matrix in order
583  // to split the blob at the given (ind,ind) diagonal location.
584  // Entries are relocated to the new MATRIX using the transformation defined
585  // by MATRIX_COORD::MapForSplit.
586  // Transfers the pointer data to the new MATRIX and deletes *this.
587  MATRIX* ConsumeAndMakeBigger(int ind);
588 
589  // Makes and returns a deep copy of *this, including all the BLOB_CHOICEs
590  // on the lists, but not any LanguageModelState that may be attached to the
591  // BLOB_CHOICEs.
592  MATRIX* DeepCopy() const;
593 
594  // Print a shortened version of the contents of the matrix.
595  void print(const UNICHARSET &unicharset) const;
596 };
597 
598 struct MATRIX_COORD {
599  static void Delete(void *arg) {
600  MATRIX_COORD *c = static_cast<MATRIX_COORD *>(arg);
601  delete c;
602  }
603  // Default constructor required by GenericHeap.
604  MATRIX_COORD() : col(0), row(0) {}
605  MATRIX_COORD(int c, int r): col(c), row(r) {}
607 
608  bool Valid(const MATRIX &m) const {
609  return 0 <= col && col < m.dimension() &&
610  col <= row && row < col + m.bandwidth() && row < m.dimension();
611  }
612 
613  // Remaps the col,row pair to split the blob at the given (ind,ind) diagonal
614  // location.
615  // Entries at (i,j) for i in [0,ind] and j in [ind,dim) move to (i,j+1),
616  // making a new row at ind.
617  // Entries at (i,j) for i in [ind+1,dim) and j in [i,dim) move to (i+i,j+1),
618  // making a new column at ind+1.
619  void MapForSplit(int ind) {
620  ASSERT_HOST(row >= col);
621  if (col > ind) ++col;
622  if (row >= ind) ++row;
623  ASSERT_HOST(row >= col);
624  }
625 
626  int col;
627  int row;
628 };
629 
630 // The MatrixCoordPair contains a MATRIX_COORD and its priority.
632 
633 #endif // TESSERACT_CCSTRUCT_MATRIX_H_
#define MIN(x, y)
Definition: ndminx.h:28
void MapForSplit(int ind)
Definition: matrix.h:619
int FWrite(const void *buffer, int size, int count)
Definition: serialis.cpp:148
tesseract::KDPairInc< float, MATRIX_COORD > MatrixCoordPair
Definition: matrix.h:631
void put(ICOORD pos, const T &thing)
Definition: matrix.h:219
void put(int column, int row, const T &thing)
Definition: matrix.h:222
void ResizeNoInit(int size1, int size2, int pad=0)
Definition: matrix.h:88
void operator+=(const GENERIC_2D_ARRAY< T > &addend)
Definition: matrix.h:250
void operator-=(const GENERIC_2D_ARRAY< T > &minuend)
Definition: matrix.h:266
const T & operator()(int column, int row) const
Definition: matrix.h:234
bool SerializeSize(FILE *fp) const
Definition: matrix.h:465
#define MAX(x, y)
Definition: ndminx.h:24
virtual int index(int column, int row) const
Definition: matrix.h:535
void Resize(int size1, int size2, const T &empty)
Definition: matrix.h:102
GENERIC_2D_ARRAY(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:72
int dimension() const
Definition: matrix.h:528
GENERIC_2D_ARRAY()
Definition: matrix.h:68
inT16 x() const
access function
Definition: points.h:52
void AdamUpdate(const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, T epsilon)
Definition: matrix.h:378
bool Serialize(FILE *fp) const
Definition: matrix.h:141
void Clear()
Definition: matrix.h:133
bool WithinBounds(const T &rangemin, const T &rangemax) const
Definition: matrix.h:304
void SumSquares(const GENERIC_2D_ARRAY< T > &src, T decay_factor)
Definition: matrix.h:367
void operator=(const GENERIC_2D_ARRAY< T > &src)
Definition: matrix.h:79
virtual int num_elements() const
Definition: matrix.h:209
void Clip(const T &rangemin, const T &rangemax)
Definition: matrix.h:296
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
virtual ~GENERIC_2D_ARRAY()
Definition: matrix.h:77
int size_allocated_
Definition: matrix.h:507
~MATRIX_COORD()
Definition: matrix.h:606
bool Serialize(tesseract::TFile *fp) const
Definition: matrix.h:148
virtual int index(int column, int row) const
Definition: matrix.h:214
int dim2() const
Definition: matrix.h:206
void RotatingTranspose(const int *dims, int num_dims, int src_dim, int dest_dim, GENERIC_2D_ARRAY< T > *result) const
Definition: matrix.h:417
MATRIX_COORD(int c, int r)
Definition: matrix.h:605
void operator+=(const T &addend)
Definition: matrix.h:282
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty, T *array)
Definition: matrix.h:53
T get(ICOORD pos) const
Definition: matrix.h:227
static void Delete(void *arg)
Definition: matrix.h:599
const T * operator[](int column) const
Definition: matrix.h:245
double Normalize()
Definition: matrix.h:314
bool DeSerialize(bool swap, FILE *fp)
Definition: matrix.h:159
bool DeSerializeSize(tesseract::TFile *fp)
Definition: matrix.h:492
void operator*=(const T &factor)
Definition: matrix.h:289
bool DeSerializeClasses(bool swap, FILE *fp)
Definition: matrix.h:194
T Max() const
Definition: matrix.h:341
int32_t inT32
Definition: host.h:38
bool Valid(const MATRIX &m) const
Definition: matrix.h:608
bool SerializeSize(tesseract::TFile *fp) const
Definition: matrix.h:472
Definition: matrix.h:570
#define ASSERT_HOST(x)
Definition: errcode.h:84
int bandwidth() const
Definition: matrix.h:530
bool SerializeClasses(FILE *fp) const
Definition: matrix.h:181
BandTriMatrix(int dim1, int dim2, const T &empty)
Definition: matrix.h:521
T MaxAbs() const
Definition: matrix.h:354
T & operator()(int column, int row)
Definition: matrix.h:237
int FReadEndian(void *buffer, int size, int count)
Definition: serialis.cpp:97
void ReverseN(void *ptr, int num_bytes)
Definition: helpers.h:184
bool DeSerialize(tesseract::TFile *fp)
Definition: matrix.h:171
inT16 y() const
access_function
Definition: points.h:56
#define NOT_CLASSIFIED
Definition: matrix.h:41
void ResizeWithCopy(int size1, int size2)
Definition: matrix.h:109
void AttachOnCorner(BandTriMatrix< T > *array2)
Definition: matrix.h:545
void delete_matrix_pointers()
Definition: matrix.h:454
MATRIX_COORD()
Definition: matrix.h:604
T * operator[](int column)
Definition: matrix.h:242
integer coordinate
Definition: points.h:30
bool DeSerializeSize(bool swap, FILE *fp)
Definition: matrix.h:481
void AssertFinite() const
Definition: matrix.h:386
MATRIX(int dimension, int bandwidth)
Definition: matrix.h:572
GENERIC_2D_ARRAY(int dim1, int dim2, const T &empty)
Definition: matrix.h:59
int dim1() const
Definition: matrix.h:205