tesseract  4.00.00dev
kdtree.cpp File Reference
#include "kdtree.h"
#include "const.h"
#include "emalloc.h"
#include <stdio.h>
#include <math.h>

Go to the source code of this file.

Classes

class  MinK< Key, Value >
 
struct  MinK< Key, Value >::Element
 
class  KDTreeSearch
 

Macros

#define Magnitude(X)   ((X) < 0 ? -(X) : (X))
 
#define NodeFound(N, K, D)   (( (N)->Key == (K) ) && ( (N)->Data == (D) ))
 
#define MINSEARCH   -MAX_FLOAT32
 
#define MAXSEARCH   MAX_FLOAT32
 

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 ComputeDistance (int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
 
void Walk (KDTREE *tree, void_proc action, void *context, KDNODE *sub_tree, inT32 level)
 
void InsertNodes (KDTREE *tree, KDNODE *nodes)
 
void FreeSubTree (KDNODE *sub_tree)
 

Macro Definition Documentation

◆ Magnitude

#define Magnitude (   X)    ((X) < 0 ? -(X) : (X))

Definition at line 30 of file kdtree.cpp.

◆ MAXSEARCH

#define MAXSEARCH   MAX_FLOAT32

Definition at line 37 of file kdtree.cpp.

◆ MINSEARCH

#define MINSEARCH   -MAX_FLOAT32

Definition at line 36 of file kdtree.cpp.

◆ NodeFound

#define NodeFound (   N,
  K,
 
)    (( (N)->Key == (K) ) && ( (N)->Data == (D) ))

Definition at line 31 of file kdtree.cpp.

Function Documentation

◆ ComputeDistance()

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

Definition at line 467 of file kdtree.cpp.

467  {
468  return sqrt(DistanceSquared(k, dim, p1, p2));
469 }
FLOAT32 DistanceSquared(int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
Definition: kdtree.cpp:446

◆ 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 446 of file kdtree.cpp.

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

◆ FreeKDNode()

void FreeKDNode ( KDNODE Node)

Definition at line 389 of file kdtree.cpp.

389 { free(Node); }

◆ 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 348 of file kdtree.cpp.

348  {
349  FreeSubTree(Tree->Root.Left);
350  free(Tree);
351 } /* FreeKDTree */
struct KDNODE * Left
Definition: kdtree.h:45
KDNODE Root
Definition: kdtree.h:51
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:550

◆ FreeSubTree()

void FreeSubTree ( KDNODE sub_tree)

Free all of the nodes of a sub tree.

Definition at line 550 of file kdtree.cpp.

550  {
551  if (sub_tree != NULL) {
552  FreeSubTree(sub_tree->Left);
553  FreeSubTree(sub_tree->Right);
554  free(sub_tree);
555  }
556 }
struct KDNODE * Left
Definition: kdtree.h:45
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:550
struct KDNODE * Right
Definition: kdtree.h:46

◆ InsertNodes()

void InsertNodes ( KDTREE tree,
KDNODE nodes 
)

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

Definition at line 540 of file kdtree.cpp.

540  {
541  if (nodes == NULL)
542  return;
543 
544  KDStore(tree, nodes->Key, nodes->Data);
545  InsertNodes(tree, nodes->Left);
546  InsertNodes(tree, nodes->Right);
547 }
void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data)
Definition: kdtree.cpp:217
struct KDNODE * Left
Definition: kdtree.h:45
void InsertNodes(KDTREE *tree, KDNODE *nodes)
Definition: kdtree.cpp:540
void * Data
Definition: kdtree.h:41
FLOAT32 * Key
Definition: kdtree.h:40
struct KDNODE * Right
Definition: kdtree.h:46

◆ 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 263 of file kdtree.cpp.

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

◆ 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 319 of file kdtree.cpp.

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

◆ 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 217 of file kdtree.cpp.

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

◆ KDWalk()

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

Walk a given Tree with action.

Definition at line 329 of file kdtree.cpp.

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

◆ 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 371 of file kdtree.cpp.

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

◆ 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 181 of file kdtree.cpp.

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

◆ 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 530 of file kdtree.cpp.

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