tesseract  4.00.00dev
tesseract::IntFeatureSpace Class Reference

#include <intfeaturespace.h>

Public Member Functions

 IntFeatureSpace ()
 
void Init (uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets)
 
bool Serialize (FILE *fp) const
 
bool DeSerialize (bool swap, FILE *fp)
 
int Size () const
 
INT_FEATURE_STRUCT PositionFromIndex (int index) const
 
int Index (const INT_FEATURE_STRUCT &f) const
 
void IndexFeatures (const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *mapped_features) const
 
void IndexAndSortFeatures (const INT_FEATURE_STRUCT *features, int num_features, GenericVector< int > *sorted_features) const
 
int XYToFeatureIndex (int x, int y) const
 

Protected Member Functions

int XBucket (int x) const
 
int YBucket (int y) const
 
int ThetaBucket (int theta) const
 
INT_FEATURE_STRUCT PositionFromBuckets (int x, int y, int theta) const
 

Protected Attributes

uinT8 x_buckets_
 
uinT8 y_buckets_
 
uinT8 theta_buckets_
 

Detailed Description

Definition at line 38 of file intfeaturespace.h.

Constructor & Destructor Documentation

◆ IntFeatureSpace()

tesseract::IntFeatureSpace::IntFeatureSpace ( )

Member Function Documentation

◆ DeSerialize()

bool tesseract::IntFeatureSpace::DeSerialize ( bool  swap,
FILE *  fp 
)

Definition at line 50 of file intfeaturespace.cpp.

50  {
51  if (fread(&x_buckets_, sizeof(x_buckets_), 1, fp) != 1)
52  return false;
53  if (fread(&y_buckets_, sizeof(y_buckets_), 1, fp) != 1)
54  return false;
55  if (fread(&theta_buckets_, sizeof(theta_buckets_), 1, fp) != 1)
56  return false;
57  return true;
58 }

◆ Index()

int tesseract::IntFeatureSpace::Index ( const INT_FEATURE_STRUCT f) const
inline

Definition at line 65 of file intfeaturespace.h.

65  {
66  return (XBucket(f.X) * y_buckets_ + YBucket(f.Y)) * theta_buckets_ +
67  ThetaBucket(f.Theta);
68  }
int ThetaBucket(int theta) const

◆ IndexAndSortFeatures()

void tesseract::IntFeatureSpace::IndexAndSortFeatures ( const INT_FEATURE_STRUCT features,
int  num_features,
GenericVector< int > *  sorted_features 
) const

Definition at line 80 of file intfeaturespace.cpp.

82  {
83  sorted_features->truncate(0);
84  for (int f = 0; f < num_features; ++f)
85  sorted_features->push_back(Index(features[f]));
86  sorted_features->sort();
87 }
int push_back(T object)
int Index(const INT_FEATURE_STRUCT &f) const
void truncate(int size)

◆ IndexFeatures()

void tesseract::IntFeatureSpace::IndexFeatures ( const INT_FEATURE_STRUCT features,
int  num_features,
GenericVector< int > *  mapped_features 
) const

Definition at line 70 of file intfeaturespace.cpp.

72  {
73  mapped_features->truncate(0);
74  for (int f = 0; f < num_features; ++f)
75  mapped_features->push_back(Index(features[f]));
76 }
int push_back(T object)
int Index(const INT_FEATURE_STRUCT &f) const
void truncate(int size)

◆ Init()

void tesseract::IntFeatureSpace::Init ( uinT8  xbuckets,
uinT8  ybuckets,
uinT8  thetabuckets 
)

Definition at line 29 of file intfeaturespace.cpp.

29  {
30  x_buckets_ = xbuckets;
31  y_buckets_ = ybuckets;
32  theta_buckets_ = thetabuckets;
33 }

◆ PositionFromBuckets()

INT_FEATURE_STRUCT tesseract::IntFeatureSpace::PositionFromBuckets ( int  x,
int  y,
int  theta 
) const
protected

Definition at line 126 of file intfeaturespace.cpp.

128  {
129  INT_FEATURE_STRUCT pos(
133  return pos;
134 }
const int kIntFeatureExtent
int DivRounded(int a, int b)
Definition: helpers.h:173

◆ PositionFromIndex()

INT_FEATURE_STRUCT tesseract::IntFeatureSpace::PositionFromIndex ( int  index) const

Definition at line 62 of file intfeaturespace.cpp.

62  {
64  index / theta_buckets_ % y_buckets_,
65  index % theta_buckets_);
66 }
INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const

◆ Serialize()

bool tesseract::IntFeatureSpace::Serialize ( FILE *  fp) const

Definition at line 37 of file intfeaturespace.cpp.

37  {
38  if (fwrite(&x_buckets_, sizeof(x_buckets_), 1, fp) != 1)
39  return false;
40  if (fwrite(&y_buckets_, sizeof(y_buckets_), 1, fp) != 1)
41  return false;
42  if (fwrite(&theta_buckets_, sizeof(theta_buckets_), 1, fp) != 1)
43  return false;
44  return true;
45 }

◆ Size()

int tesseract::IntFeatureSpace::Size ( ) const
inline

Definition at line 56 of file intfeaturespace.h.

◆ ThetaBucket()

int tesseract::IntFeatureSpace::ThetaBucket ( int  theta) const
inlineprotected

Definition at line 94 of file intfeaturespace.h.

94  {
95  int bucket = DivRounded(theta * theta_buckets_, kIntFeatureExtent);
96  return Modulo(bucket, theta_buckets_);
97  }
const int kIntFeatureExtent
int DivRounded(int a, int b)
Definition: helpers.h:173
int Modulo(int a, int b)
Definition: helpers.h:164

◆ XBucket()

int tesseract::IntFeatureSpace::XBucket ( int  x) const
inlineprotected

Definition at line 84 of file intfeaturespace.h.

84  {
85  int bucket = x * x_buckets_ / kIntFeatureExtent;
86  return ClipToRange(bucket, 0, static_cast<int>(x_buckets_) - 1);
87  }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
const int kIntFeatureExtent

◆ XYToFeatureIndex()

int tesseract::IntFeatureSpace::XYToFeatureIndex ( int  x,
int  y 
) const

Definition at line 91 of file intfeaturespace.cpp.

91  {
92  // Round the x,y position to a feature. Search for a valid theta.
93  INT_FEATURE_STRUCT feature(x, y, 0);
94  int index = -1;
95  for (int theta = 0; theta <= MAX_UINT8 && index < 0; ++theta) {
96  feature.Theta = theta;
97  index = Index(feature);
98  }
99  if (index < 0) {
100  tprintf("(%d,%d) does not exist in feature space!\n", x, y);
101  return -1;
102  }
103  feature = PositionFromIndex(index);
104  tprintf("Click at (%d, %d) ->(%d, %d), ->(%d, %d)\n",
105  x, y, feature.X, feature.Y, x - feature.X, y - feature.Y);
106  // Get the relative position of x,y from the rounded feature.
107  x -= feature.X;
108  y -= feature.Y;
109  if (x != 0 || y != 0) {
110  double angle = atan2(static_cast<double>(y), static_cast<double>(x)) + PI;
111  angle *= kIntFeatureExtent / (2.0 * PI);
112  feature.Theta = static_cast<uinT8>(angle + 0.5);
113  index = Index(feature);
114  if (index < 0) {
115  tprintf("Feature failed to map to a valid index:");
116  feature.print();
117  return -1;
118  }
119  feature = PositionFromIndex(index);
120  }
121  feature.print();
122  return index;
123 }
INT_FEATURE_STRUCT PositionFromIndex(int index) const
#define MAX_UINT8
Definition: host.h:63
#define tprintf(...)
Definition: tprintf.h:31
int Index(const INT_FEATURE_STRUCT &f) const
#define PI
Definition: const.h:19
const int kIntFeatureExtent
uint8_t uinT8
Definition: host.h:35

◆ YBucket()

int tesseract::IntFeatureSpace::YBucket ( int  y) const
inlineprotected

Definition at line 88 of file intfeaturespace.h.

88  {
89  int bucket = y * y_buckets_ / kIntFeatureExtent;
90  return ClipToRange(bucket, 0, static_cast<int>(y_buckets_) - 1);
91  }
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:122
const int kIntFeatureExtent

Member Data Documentation

◆ theta_buckets_

uinT8 tesseract::IntFeatureSpace::theta_buckets_
protected

Definition at line 104 of file intfeaturespace.h.

◆ x_buckets_

uinT8 tesseract::IntFeatureSpace::x_buckets_
protected

Definition at line 102 of file intfeaturespace.h.

◆ y_buckets_

uinT8 tesseract::IntFeatureSpace::y_buckets_
protected

Definition at line 103 of file intfeaturespace.h.


The documentation for this class was generated from the following files: