tesseract  5.0.0-alpha-619-ge9db
tesseract::IntFeatureSpace Class Reference

#include <intfeaturespace.h>

Public Member Functions

 IntFeatureSpace ()
 
void Init (uint8_t xbuckets, uint8_t ybuckets, uint8_t thetabuckets)
 
bool Serialize (FILE *fp) const
 
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_t x_buckets_
 
uint8_t y_buckets_
 
uint8_t theta_buckets_
 

Detailed Description

Definition at line 38 of file intfeaturespace.h.

Constructor & Destructor Documentation

◆ IntFeatureSpace()

tesseract::IntFeatureSpace::IntFeatureSpace ( )

Definition at line 26 of file intfeaturespace.cpp.

27  : x_buckets_(0), y_buckets_(0), theta_buckets_(0) {
28 }

Member Function Documentation

◆ Index()

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

Definition at line 60 of file intfeaturespace.h.

60  {
61  return (XBucket(f.X) * y_buckets_ + YBucket(f.Y)) * theta_buckets_ +
62  ThetaBucket(f.Theta);
63  }

◆ IndexAndSortFeatures()

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

Definition at line 68 of file intfeaturespace.cpp.

70  {
71  sorted_features->truncate(0);
72  for (int f = 0; f < num_features; ++f)
73  sorted_features->push_back(Index(features[f]));
74  sorted_features->sort();
75 }

◆ IndexFeatures()

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

Definition at line 58 of file intfeaturespace.cpp.

60  {
61  mapped_features->truncate(0);
62  for (int f = 0; f < num_features; ++f)
63  mapped_features->push_back(Index(features[f]));
64 }

◆ Init()

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

Definition at line 30 of file intfeaturespace.cpp.

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

◆ PositionFromBuckets()

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

Definition at line 114 of file intfeaturespace.cpp.

116  {
117  INT_FEATURE_STRUCT pos(
121  return pos;
122 }

◆ PositionFromIndex()

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

Definition at line 50 of file intfeaturespace.cpp.

50  {
52  index / theta_buckets_ % y_buckets_,
53  index % theta_buckets_);
54 }

◆ Serialize()

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

Definition at line 38 of file intfeaturespace.cpp.

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

◆ Size()

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

Definition at line 51 of file intfeaturespace.h.

51  {
52  return static_cast<int>(x_buckets_) * y_buckets_ * theta_buckets_;
53  }

◆ ThetaBucket()

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

Definition at line 89 of file intfeaturespace.h.

89  {
90  int bucket = DivRounded(theta * theta_buckets_, kIntFeatureExtent);
91  return Modulo(bucket, theta_buckets_);
92  }

◆ XBucket()

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

Definition at line 79 of file intfeaturespace.h.

79  {
80  int bucket = x * x_buckets_ / kIntFeatureExtent;
81  return ClipToRange(bucket, 0, static_cast<int>(x_buckets_) - 1);
82  }

◆ XYToFeatureIndex()

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

Definition at line 79 of file intfeaturespace.cpp.

79  {
80  // Round the x,y position to a feature. Search for a valid theta.
81  INT_FEATURE_STRUCT feature(x, y, 0);
82  int index = -1;
83  for (int theta = 0; theta <= UINT8_MAX && index < 0; ++theta) {
84  feature.Theta = theta;
85  index = Index(feature);
86  }
87  if (index < 0) {
88  tprintf("(%d,%d) does not exist in feature space!\n", x, y);
89  return -1;
90  }
91  feature = PositionFromIndex(index);
92  tprintf("Click at (%d, %d) ->(%d, %d), ->(%d, %d)\n",
93  x, y, feature.X, feature.Y, x - feature.X, y - feature.Y);
94  // Get the relative position of x,y from the rounded feature.
95  x -= feature.X;
96  y -= feature.Y;
97  if (x != 0 || y != 0) {
98  double angle = atan2(static_cast<double>(y), static_cast<double>(x)) + M_PI;
99  angle *= kIntFeatureExtent / (2.0 * M_PI);
100  feature.Theta = static_cast<uint8_t>(angle + 0.5);
101  index = Index(feature);
102  if (index < 0) {
103  tprintf("Feature failed to map to a valid index:");
104  feature.print();
105  return -1;
106  }
107  feature = PositionFromIndex(index);
108  }
109  feature.print();
110  return index;
111 }

◆ YBucket()

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

Definition at line 83 of file intfeaturespace.h.

83  {
84  int bucket = y * y_buckets_ / kIntFeatureExtent;
85  return ClipToRange(bucket, 0, static_cast<int>(y_buckets_) - 1);
86  }

Member Data Documentation

◆ theta_buckets_

uint8_t tesseract::IntFeatureSpace::theta_buckets_
protected

Definition at line 99 of file intfeaturespace.h.

◆ x_buckets_

uint8_t tesseract::IntFeatureSpace::x_buckets_
protected

Definition at line 97 of file intfeaturespace.h.

◆ y_buckets_

uint8_t tesseract::IntFeatureSpace::y_buckets_
protected

Definition at line 98 of file intfeaturespace.h.


The documentation for this class was generated from the following files:
ClipToRange
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:106
tesseract::IntFeatureSpace::x_buckets_
uint8_t x_buckets_
Definition: intfeaturespace.h:97
tesseract::IntFeatureSpace::XBucket
int XBucket(int x) const
Definition: intfeaturespace.h:79
tesseract::IntFeatureSpace::ThetaBucket
int ThetaBucket(int theta) const
Definition: intfeaturespace.h:89
INT_FEATURE_STRUCT::Theta
uint8_t Theta
Definition: intproto.h:141
tesseract::IntFeatureSpace::y_buckets_
uint8_t y_buckets_
Definition: intfeaturespace.h:98
tesseract::IntFeatureSpace::YBucket
int YBucket(int y) const
Definition: intfeaturespace.h:83
tesseract::IntFeatureSpace::PositionFromBuckets
INT_FEATURE_STRUCT PositionFromBuckets(int x, int y, int theta) const
Definition: intfeaturespace.cpp:114
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
INT_FEATURE_STRUCT::Y
uint8_t Y
Definition: intproto.h:140
DivRounded
int DivRounded(int a, int b)
Definition: helpers.h:165
tesseract::IntFeatureSpace::theta_buckets_
uint8_t theta_buckets_
Definition: intfeaturespace.h:99
tesseract::IntFeatureSpace::PositionFromIndex
INT_FEATURE_STRUCT PositionFromIndex(int index) const
Definition: intfeaturespace.cpp:50
INT_FEATURE_STRUCT
Definition: intproto.h:131
GenericVector::truncate
void truncate(int size)
Definition: genericvector.h:132
Modulo
int Modulo(int a, int b)
Definition: helpers.h:156
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
GenericVector::sort
void sort()
Definition: genericvector.h:1102
INT_FEATURE_STRUCT::X
uint8_t X
Definition: intproto.h:139
kIntFeatureExtent
const int kIntFeatureExtent
Definition: intfeaturespace.h:27
tesseract::IntFeatureSpace::Index
int Index(const INT_FEATURE_STRUCT &f) const
Definition: intfeaturespace.h:60