tesseract  4.0.0-1-g2a2b
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 25 of file intfeaturespace.cpp.

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  }
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 67 of file intfeaturespace.cpp.

69  {
70  sorted_features->truncate(0);
71  for (int f = 0; f < num_features; ++f)
72  sorted_features->push_back(Index(features[f]));
73  sorted_features->sort();
74 }
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 57 of file intfeaturespace.cpp.

59  {
60  mapped_features->truncate(0);
61  for (int f = 0; f < num_features; ++f)
62  mapped_features->push_back(Index(features[f]));
63 }
int push_back(T object)
int Index(const INT_FEATURE_STRUCT &f) const
void truncate(int size)

◆ Init()

void tesseract::IntFeatureSpace::Init ( uint8_t  xbuckets,
uint8_t  ybuckets,
uint8_t  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 113 of file intfeaturespace.cpp.

115  {
116  INT_FEATURE_STRUCT pos(
120  return pos;
121 }
const int kIntFeatureExtent
int DivRounded(int a, int b)
Definition: helpers.h:162

◆ PositionFromIndex()

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

Definition at line 49 of file intfeaturespace.cpp.

49  {
51  index / theta_buckets_ % y_buckets_,
52  index % theta_buckets_);
53 }
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 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  }
const int kIntFeatureExtent
int Modulo(int a, int b)
Definition: helpers.h:153
int DivRounded(int a, int b)
Definition: helpers.h:162

◆ 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  }
const int kIntFeatureExtent
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

◆ XYToFeatureIndex()

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

Definition at line 78 of file intfeaturespace.cpp.

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

◆ 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  }
const int kIntFeatureExtent
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
Definition: helpers.h:111

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: