tesseract v5.3.3.20231005
tesseract::DPPoint Class Reference

#include <dppoint.h>

Public Types

using CostFunc = int64_t(DPPoint::*)(const DPPoint *)
 

Public Member Functions

 DPPoint ()
 
int64_t CostWithVariance (const DPPoint *prev)
 
int total_cost () const
 
int Pathlength () const
 
const DPPointbest_prev () const
 
void AddLocalCost (int new_cost)
 

Static Public Member Functions

static DPPointSolve (int min_step, int max_step, bool debug, CostFunc cost_func, int size, DPPoint *points)
 

Detailed Description

Definition at line 44 of file dppoint.h.

Member Typedef Documentation

◆ CostFunc

using tesseract::DPPoint::CostFunc = int64_t (DPPoint::*)(const DPPoint *)

Definition at line 49 of file dppoint.h.

Constructor & Destructor Documentation

◆ DPPoint()

tesseract::DPPoint::DPPoint ( )
inline

Definition at line 51 of file dppoint.h.

52 : local_cost_(0)
53 , total_cost_(INT32_MAX)
54 , total_steps_(1)
55 , best_prev_(nullptr)
56 , n_(0)
57 , sig_x_(0)
58 , sig_xsq_(0) {}

Member Function Documentation

◆ AddLocalCost()

void tesseract::DPPoint::AddLocalCost ( int  new_cost)
inline

Definition at line 81 of file dppoint.h.

81 {
82 local_cost_ += new_cost;
83 }

◆ best_prev()

const DPPoint * tesseract::DPPoint::best_prev ( ) const
inline

Definition at line 78 of file dppoint.h.

78 {
79 return best_prev_;
80 }

◆ CostWithVariance()

int64_t tesseract::DPPoint::CostWithVariance ( const DPPoint prev)

Definition at line 70 of file dppoint.cpp.

70 {
71 if (prev == nullptr || prev == this) {
72 UpdateIfBetter(0, 1, nullptr, 0, 0, 0);
73 return 0;
74 }
75
76 int delta = this - prev;
77 int32_t n = prev->n_ + 1;
78 int32_t sig_x = prev->sig_x_ + delta;
79 int64_t sig_xsq = prev->sig_xsq_ + static_cast<int64_t>(delta) * delta;
80 int64_t cost = (sig_xsq - sig_x * sig_x / n) / n;
81 cost += prev->total_cost_;
82 UpdateIfBetter(cost, prev->total_steps_ + 1, prev, n, sig_x, sig_xsq);
83 return cost;
84}

◆ Pathlength()

int tesseract::DPPoint::Pathlength ( ) const
inline

Definition at line 75 of file dppoint.h.

75 {
76 return total_steps_;
77 }

◆ Solve()

DPPoint * tesseract::DPPoint::Solve ( int  min_step,
int  max_step,
bool  debug,
CostFunc  cost_func,
int  size,
DPPoint points 
)
static

Definition at line 31 of file dppoint.cpp.

32 {
33 if (size <= 0 || max_step < min_step || min_step >= size) {
34 return nullptr; // Degenerate, but not necessarily an error.
35 }
36 ASSERT_HOST(min_step > 0); // Infinite loop possible if this is not true.
37 if (debug) {
38 tprintf("min = %d, max=%d\n", min_step, max_step);
39 }
40 // Evaluate the total cost at each point.
41 for (int i = 0; i < size; ++i) {
42 for (int offset = min_step; offset <= max_step; ++offset) {
43 DPPoint *prev = offset <= i ? points + i - offset : nullptr;
44 int64_t new_cost = (points[i].*cost_func)(prev);
45 if (points[i].best_prev_ != nullptr && offset > min_step * 2 &&
46 new_cost > points[i].total_cost_) {
47 break; // Find only the first minimum if going over twice the min.
48 }
49 }
50 points[i].total_cost_ += points[i].local_cost_;
51 if (debug) {
52 tprintf("At point %d, local cost=%d, total_cost=%d, steps=%d\n", i, points[i].local_cost_,
53 points[i].total_cost_, points[i].total_steps_);
54 }
55 }
56 // Now find the end of the best path and return it.
57 int best_cost = points[size - 1].total_cost_;
58 int best_end = size - 1;
59 for (int end = best_end - 1; end >= size - min_step; --end) {
60 int cost = points[end].total_cost_;
61 if (cost < best_cost) {
62 best_cost = cost;
63 best_end = end;
64 }
65 }
66 return points + best_end;
67}
#define ASSERT_HOST(x)
Definition: errcode.h:54
void tprintf(const char *format,...)
Definition: tprintf.cpp:41

◆ total_cost()

int tesseract::DPPoint::total_cost ( ) const
inline

Definition at line 72 of file dppoint.h.

72 {
73 return total_cost_;
74 }

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