tesseract  3.05.02
kdtree.h File Reference
#include "host.h"
#include "cutil.h"
#include "ocrfeatures.h"

Go to the source code of this file.

Classes

struct  KDNODE
 
struct  KDTREE
 

Macros

#define RootOf(T)   ((T)->Root.Left->Data)
 

Functions

KDTREEMakeKDTree (inT16 KeySize, const PARAM_DESC KeyDesc[])
 
void KDStore (KDTREE *Tree, FLOAT32 *Key, void *Data)
 
void KDDelete (KDTREE *Tree, FLOAT32 Key[], void *Data)
 
void KDNearestNeighborSearch (KDTREE *Tree, FLOAT32 Query[], int QuerySize, FLOAT32 MaxDistance, int *NumberOfResults, void **NBuffer, FLOAT32 DBuffer[])
 
void KDWalk (KDTREE *Tree, void_proc Action, void *context)
 
void FreeKDTree (KDTREE *Tree)
 
KDNODEMakeKDNode (KDTREE *tree, FLOAT32 Key[], void *Data, int Index)
 
void FreeKDNode (KDNODE *Node)
 
FLOAT32 DistanceSquared (int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
 
FLOAT32 TESS_API ComputeDistance (int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
 
int QueryInSearch (KDTREE *tree)
 
void Walk (KDTREE *tree, void_proc action, void *context, KDNODE *SubTree, inT32 Level)
 
void InsertNodes (KDTREE *tree, KDNODE *nodes)
 
void FreeSubTree (KDNODE *SubTree)
 

Macro Definition Documentation

◆ RootOf

#define RootOf (   T)    ((T)->Root.Left->Data)

Definition at line 58 of file kdtree.h.

Function Documentation

◆ ComputeDistance()

FLOAT32 TESS_API ComputeDistance ( int  k,
PARAM_DESC dim,
FLOAT32  p1[],
FLOAT32  p2[] 
)

Definition at line 472 of file kdtree.cpp.

472  {
473  return sqrt(DistanceSquared(k, dim, p1, p2));
474 }
FLOAT32 DistanceSquared(int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
Definition: kdtree.cpp:451

◆ DistanceSquared()

FLOAT32 DistanceSquared ( int  k,
PARAM_DESC dim,
FLOAT32  p1[],
FLOAT32  p2[] 
)

Returns the Euclidean distance squared between p1 and p2 for all essential dimensions.

Parameters
kkeys are in k-space
dimdimension descriptions (essential, circular, etc)
p1,p2two different points in K-D space

Definition at line 451 of file kdtree.cpp.

451  {
452  FLOAT32 total_distance = 0;
453 
454  for (; k > 0; k--, p1++, p2++, dim++) {
455  if (dim->NonEssential)
456  continue;
457 
458  FLOAT32 dimension_distance = *p1 - *p2;
459 
460  /* if this dimension is circular - check wraparound distance */
461  if (dim->Circular) {
462  dimension_distance = Magnitude(dimension_distance);
463  FLOAT32 wrap_distance = dim->Max - dim->Min - dimension_distance;
464  dimension_distance = MIN(dimension_distance, wrap_distance);
465  }
466 
467  total_distance += dimension_distance * dimension_distance;
468  }
469  return total_distance;
470 }
#define Magnitude(X)
Definition: kdtree.cpp:31
#define MIN(x, y)
Definition: ndminx.h:28
float FLOAT32
Definition: host.h:44
inT8 Circular
Definition: ocrfeatures.h:47
inT8 NonEssential
Definition: ocrfeatures.h:48
FLOAT32 Min
Definition: ocrfeatures.h:49
FLOAT32 Max
Definition: ocrfeatures.h:50

◆ FreeKDNode()

void FreeKDNode ( KDNODE Node)

Definition at line 391 of file kdtree.cpp.

391  {
392  memfree ((char *)Node);
393 }
void memfree(void *element)
Definition: freelist.cpp:30

◆ FreeKDTree()

void FreeKDTree ( KDTREE Tree)

This routine frees all memory which is allocated to the specified KD-tree. This includes the data structure for the kd-tree itself plus the data structures for each node in the tree. It does not include the Key and Data items which are pointed to by the nodes. This memory is left untouched.

Parameters
Treetree data structure to be released
Returns
none
Note
Exceptions: none
History: 5/26/89, DSJ, Created.

Definition at line 350 of file kdtree.cpp.

350  {
351  FreeSubTree(Tree->Root.Left);
352  memfree(Tree);
353 } /* FreeKDTree */
void memfree(void *element)
Definition: freelist.cpp:30
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:555
struct KDNODE * Left
Definition: kdtree.h:45
KDNODE Root
Definition: kdtree.h:51

◆ FreeSubTree()

void FreeSubTree ( KDNODE sub_tree)

Free all of the nodes of a sub tree.

Definition at line 555 of file kdtree.cpp.

555  {
556  if (sub_tree != NULL) {
557  FreeSubTree(sub_tree->Left);
558  FreeSubTree(sub_tree->Right);
559  memfree(sub_tree);
560  }
561 }
struct KDNODE * Right
Definition: kdtree.h:46
void memfree(void *element)
Definition: freelist.cpp:30
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:555
struct KDNODE * Left
Definition: kdtree.h:45

◆ InsertNodes()

void InsertNodes ( KDTREE tree,
KDNODE nodes 
)

Given a subtree nodes, insert all of its elements into tree.

Definition at line 545 of file kdtree.cpp.

545  {
546  if (nodes == NULL)
547  return;
548 
549  KDStore(tree, nodes->Key, nodes->Data);
550  InsertNodes(tree, nodes->Left);
551  InsertNodes(tree, nodes->Right);
552 }
struct KDNODE * Right
Definition: kdtree.h:46
void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data)
Definition: kdtree.cpp:219
void InsertNodes(KDTREE *tree, KDNODE *nodes)
Definition: kdtree.cpp:545
FLOAT32 * Key
Definition: kdtree.h:40
struct KDNODE * Left
Definition: kdtree.h:45
void * Data
Definition: kdtree.h:41

◆ KDDelete()

void KDDelete ( KDTREE Tree,
FLOAT32  Key[],
void *  Data 
)

This routine deletes a node from Tree. The node to be deleted is specified by the Key for the node and the Data contents of the node. These two pointers must be identical to the pointers that were used for the node when it was originally stored in the tree. A node will be deleted from the tree only if its key and data pointers are identical to Key and Data respectively. The tree is re-formed by removing the affected subtree and inserting all elements but the root.

Parameters
TreeK-D tree to delete node from
Keykey of node to be deleted
Datadata contents of node to be deleted
Note
Exceptions: none
History: 3/13/89, DSJ, Created. 7/13/89, DSJ, Specify node indirectly by key and data.

Definition at line 265 of file kdtree.cpp.

265  {
266  int Level;
267  KDNODE *Current;
268  KDNODE *Father;
269 
270  /* initialize search at root of tree */
271  Father = &(Tree->Root);
272  Current = Father->Left;
273  Level = NextLevel(Tree, -1);
274 
275  /* search tree for node to be deleted */
276  while ((Current != NULL) && (!NodeFound (Current, Key, Data))) {
277  Father = Current;
278  if (Key[Level] < Current->BranchPoint)
279  Current = Current->Left;
280  else
281  Current = Current->Right;
282 
283  Level = NextLevel(Tree, Level);
284  }
285 
286  if (Current != NULL) { /* if node to be deleted was found */
287  if (Current == Father->Left) {
288  Father->Left = NULL;
289  Father->LeftBranch = Tree->KeyDesc[Level].Min;
290  } else {
291  Father->Right = NULL;
292  Father->RightBranch = Tree->KeyDesc[Level].Max;
293  }
294 
295  InsertNodes(Tree, Current->Left);
296  InsertNodes(Tree, Current->Right);
297  FreeSubTree(Current);
298  }
299 } /* KDDelete */
struct KDNODE * Right
Definition: kdtree.h:46
FLOAT32 RightBranch
Definition: kdtree.h:44
#define NodeFound(N, K, D)
Definition: kdtree.cpp:32
FLOAT32 LeftBranch
Definition: kdtree.h:43
FLOAT32 BranchPoint
Definition: kdtree.h:42
void InsertNodes(KDTREE *tree, KDNODE *nodes)
Definition: kdtree.cpp:545
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:555
PARAM_DESC KeyDesc[1]
Definition: kdtree.h:52
Definition: kdtree.h:39
FLOAT32 Min
Definition: ocrfeatures.h:49
struct KDNODE * Left
Definition: kdtree.h:45
FLOAT32 Max
Definition: ocrfeatures.h:50
KDNODE Root
Definition: kdtree.h:51

◆ KDNearestNeighborSearch()

void KDNearestNeighborSearch ( KDTREE Tree,
FLOAT32  Query[],
int  QuerySize,
FLOAT32  MaxDistance,
int *  NumberOfResults,
void **  NBuffer,
FLOAT32  DBuffer[] 
)

This routine searches the K-D tree specified by Tree and finds the QuerySize nearest neighbors of Query. All neighbors must be within MaxDistance of Query. The data contents of the nearest neighbors are placed in NBuffer and their distances from Query are placed in DBuffer.

Parameters
Treeptr to K-D tree to be searched
Queryptr to query key (point in D-space)
QuerySizenumber of nearest neighbors to be found
MaxDistanceall neighbors must be within this distance
NBufferptr to QuerySize buffer to hold nearest neighbors
DBufferptr to QuerySize buffer to hold distances from nearest neighbor to query point
NumberOfResults[out] Number of nearest neighbors actually found
Note
Exceptions: none
History:
  • 3/10/89, DSJ, Created.
  • 7/13/89, DSJ, Return contents of node instead of node itself.

Definition at line 321 of file kdtree.cpp.

323  {
324  KDTreeSearch search(Tree, Query, QuerySize);
325  search.Search(NumberOfResults, DBuffer, NBuffer);
326 }
LIST search(LIST list, void *key, int_compare is_equal)
Definition: oldlist.cpp:406

◆ KDStore()

void KDStore ( KDTREE Tree,
FLOAT32 Key,
void *  Data 
)

This routine stores Data in the K-D tree specified by Tree using Key as an access key.

Parameters
TreeK-D tree in which data is to be stored
Keyptr to key by which data can be retrieved
Dataptr to data to be stored in the tree
Note
Exceptions: none
History: 3/10/89, DSJ, Created. 7/13/89, DSJ, Changed return to void.

Definition at line 219 of file kdtree.cpp.

219  {
220  int Level;
221  KDNODE *Node;
222  KDNODE **PtrToNode;
223 
224  PtrToNode = &(Tree->Root.Left);
225  Node = *PtrToNode;
226  Level = NextLevel(Tree, -1);
227  while (Node != NULL) {
228  if (Key[Level] < Node->BranchPoint) {
229  PtrToNode = &(Node->Left);
230  if (Key[Level] > Node->LeftBranch)
231  Node->LeftBranch = Key[Level];
232  }
233  else {
234  PtrToNode = &(Node->Right);
235  if (Key[Level] < Node->RightBranch)
236  Node->RightBranch = Key[Level];
237  }
238  Level = NextLevel(Tree, Level);
239  Node = *PtrToNode;
240  }
241 
242  *PtrToNode = MakeKDNode(Tree, Key, (void *) Data, Level);
243 } /* KDStore */
struct KDNODE * Right
Definition: kdtree.h:46
FLOAT32 RightBranch
Definition: kdtree.h:44
FLOAT32 LeftBranch
Definition: kdtree.h:43
FLOAT32 BranchPoint
Definition: kdtree.h:42
Definition: kdtree.h:39
struct KDNODE * Left
Definition: kdtree.h:45
KDNODE Root
Definition: kdtree.h:51
KDNODE * MakeKDNode(KDTREE *tree, FLOAT32 Key[], void *Data, int Index)
Definition: kdtree.cpp:373

◆ KDWalk()

void KDWalk ( KDTREE Tree,
void_proc  action,
void *  context 
)

Walk a given Tree with action.

Definition at line 331 of file kdtree.cpp.

331  {
332  if (Tree->Root.Left != NULL)
333  Walk(Tree, action, context, Tree->Root.Left, NextLevel(Tree, -1));
334 }
void Walk(KDTREE *tree, void_proc action, void *context, KDNODE *sub_tree, inT32 level)
Definition: kdtree.cpp:535
struct KDNODE * Left
Definition: kdtree.h:45
KDNODE Root
Definition: kdtree.h:51

◆ MakeKDNode()

KDNODE* MakeKDNode ( KDTREE tree,
FLOAT32  Key[],
void *  Data,
int  Index 
)

This routine allocates memory for a new K-D tree node and places the specified Key and Data into it. The left and right subtree pointers for the node are initialized to empty subtrees.

Parameters
treeThe tree to create the node for
KeyAccess key for new node in KD tree
Dataptr to data to be stored in new node
Indexindex of Key to branch on
Returns
pointer to new K-D tree node
Note
Exceptions: None
History: 3/11/89, DSJ, Created.

Definition at line 373 of file kdtree.cpp.

373  {
374  KDNODE *NewNode;
375 
376  NewNode = (KDNODE *) Emalloc (sizeof (KDNODE));
377 
378  NewNode->Key = Key;
379  NewNode->Data = Data;
380  NewNode->BranchPoint = Key[Index];
381  NewNode->LeftBranch = tree->KeyDesc[Index].Min;
382  NewNode->RightBranch = tree->KeyDesc[Index].Max;
383  NewNode->Left = NULL;
384  NewNode->Right = NULL;
385 
386  return NewNode;
387 } /* MakeKDNode */
struct KDNODE * Right
Definition: kdtree.h:46
FLOAT32 RightBranch
Definition: kdtree.h:44
FLOAT32 LeftBranch
Definition: kdtree.h:43
FLOAT32 BranchPoint
Definition: kdtree.h:42
FLOAT32 * Key
Definition: kdtree.h:40
PARAM_DESC KeyDesc[1]
Definition: kdtree.h:52
Definition: kdtree.h:39
FLOAT32 Min
Definition: ocrfeatures.h:49
void * Emalloc(int Size)
Definition: emalloc.cpp:47
struct KDNODE * Left
Definition: kdtree.h:45
FLOAT32 Max
Definition: ocrfeatures.h:50
void * Data
Definition: kdtree.h:41

◆ MakeKDTree()

KDTREE* MakeKDTree ( inT16  KeySize,
const PARAM_DESC  KeyDesc[] 
)
Returns
a new KDTREE based on the specified parameters.
Parameters
KeySize# of dimensions in the K-D tree
KeyDescarray of params to describe key dimensions

Definition at line 183 of file kdtree.cpp.

183  {
184  KDTREE *KDTree = (KDTREE *) Emalloc(
185  sizeof(KDTREE) + (KeySize - 1) * sizeof(PARAM_DESC));
186  for (int i = 0; i < KeySize; i++) {
187  KDTree->KeyDesc[i].NonEssential = KeyDesc[i].NonEssential;
188  KDTree->KeyDesc[i].Circular = KeyDesc[i].Circular;
189  if (KeyDesc[i].Circular) {
190  KDTree->KeyDesc[i].Min = KeyDesc[i].Min;
191  KDTree->KeyDesc[i].Max = KeyDesc[i].Max;
192  KDTree->KeyDesc[i].Range = KeyDesc[i].Max - KeyDesc[i].Min;
193  KDTree->KeyDesc[i].HalfRange = KDTree->KeyDesc[i].Range / 2;
194  KDTree->KeyDesc[i].MidRange = (KeyDesc[i].Max + KeyDesc[i].Min) / 2;
195  } else {
196  KDTree->KeyDesc[i].Min = MINSEARCH;
197  KDTree->KeyDesc[i].Max = MAXSEARCH;
198  }
199  }
200  KDTree->KeySize = KeySize;
201  KDTree->Root.Left = NULL;
202  KDTree->Root.Right = NULL;
203  return KDTree;
204 }
struct KDNODE * Right
Definition: kdtree.h:46
inT16 KeySize
Definition: kdtree.h:50
#define MINSEARCH
Definition: kdtree.cpp:37
FLOAT32 Range
Definition: ocrfeatures.h:51
Definition: kdtree.h:49
FLOAT32 HalfRange
Definition: ocrfeatures.h:52
#define MAXSEARCH
Definition: kdtree.cpp:38
PARAM_DESC KeyDesc[1]
Definition: kdtree.h:52
FLOAT32 MidRange
Definition: ocrfeatures.h:53
inT8 Circular
Definition: ocrfeatures.h:47
inT8 NonEssential
Definition: ocrfeatures.h:48
FLOAT32 Min
Definition: ocrfeatures.h:49
void * Emalloc(int Size)
Definition: emalloc.cpp:47
struct KDNODE * Left
Definition: kdtree.h:45
FLOAT32 Max
Definition: ocrfeatures.h:50
KDNODE Root
Definition: kdtree.h:51

◆ QueryInSearch()

int QueryInSearch ( KDTREE tree)

◆ Walk()

void Walk ( KDTREE tree,
void_proc  action,
void *  context,
KDNODE sub_tree,
inT32  level 
)

Walk a tree, calling action once on each node.

Operation: This routine walks through the specified sub_tree and invokes action action at each node as follows: action(context, data, level) data the data contents of the node being visited, level is the level of the node in the tree with the root being level 0.

Parameters
treeroot of the tree being walked.
actionaction to be performed at every node
contextaction's context
sub_treeptr to root of subtree to be walked
levelcurrent level in the tree for this node

Definition at line 535 of file kdtree.cpp.

536  {
537  (*action)(context, sub_tree->Data, level);
538  if (sub_tree->Left != NULL)
539  Walk(tree, action, context, sub_tree->Left, NextLevel(tree, level));
540  if (sub_tree->Right != NULL)
541  Walk(tree, action, context, sub_tree->Right, NextLevel(tree, level));
542 }
void Walk(KDTREE *tree, void_proc action, void *context, KDNODE *sub_tree, inT32 level)
Definition: kdtree.cpp:535
struct KDNODE * Right
Definition: kdtree.h:46
struct KDNODE * Left
Definition: kdtree.h:45
void * Data
Definition: kdtree.h:41