All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 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

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

Definition at line 58 of file kdtree.h.

Function Documentation

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

Definition at line 473 of file kdtree.cpp.

473  {
474  return sqrt(DistanceSquared(k, dim, p1, p2));
475 }
FLOAT32 DistanceSquared(int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
Definition: kdtree.cpp:452
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 452 of file kdtree.cpp.

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

Definition at line 392 of file kdtree.cpp.

392  {
393  memfree ((char *)Node);
394 }
void memfree(void *element)
Definition: freelist.cpp:30
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 351 of file kdtree.cpp.

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

Free all of the nodes of a sub tree.

Definition at line 556 of file kdtree.cpp.

556  {
557  if (sub_tree != NULL) {
558  FreeSubTree(sub_tree->Left);
559  FreeSubTree(sub_tree->Right);
560  memfree(sub_tree);
561  }
562 }
void memfree(void *element)
Definition: freelist.cpp:30
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:556
struct KDNODE * Left
Definition: kdtree.h:45
struct KDNODE * Right
Definition: kdtree.h:46
#define NULL
Definition: host.h:144
void InsertNodes ( KDTREE tree,
KDNODE nodes 
)

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

Definition at line 546 of file kdtree.cpp.

546  {
547  if (nodes == NULL)
548  return;
549 
550  KDStore(tree, nodes->Key, nodes->Data);
551  InsertNodes(tree, nodes->Left);
552  InsertNodes(tree, nodes->Right);
553 }
struct KDNODE * Left
Definition: kdtree.h:45
void InsertNodes(KDTREE *tree, KDNODE *nodes)
Definition: kdtree.cpp:546
struct KDNODE * Right
Definition: kdtree.h:46
void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data)
Definition: kdtree.cpp:218
#define NULL
Definition: host.h:144
void * Data
Definition: kdtree.h:41
FLOAT32 * Key
Definition: kdtree.h:40
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 */
FLOAT32 RightBranch
Definition: kdtree.h:44
FLOAT32 Min
Definition: ocrfeatures.h:49
#define NodeFound(N, K, D)
Definition: kdtree.cpp:32
KDNODE Root
Definition: kdtree.h:51
PARAM_DESC KeyDesc[1]
Definition: kdtree.h:52
void FreeSubTree(KDNODE *sub_tree)
Definition: kdtree.cpp:556
struct KDNODE * Left
Definition: kdtree.h:45
void InsertNodes(KDTREE *tree, KDNODE *nodes)
Definition: kdtree.cpp:546
struct KDNODE * Right
Definition: kdtree.h:46
Definition: kdtree.h:39
FLOAT32 LeftBranch
Definition: kdtree.h:43
#define NULL
Definition: host.h:144
FLOAT32 BranchPoint
Definition: kdtree.h:42
FLOAT32 Max
Definition: ocrfeatures.h:50
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 322 of file kdtree.cpp.

324  {
325  KDTreeSearch search(Tree, Query, QuerySize);
326  search.Search(NumberOfResults, DBuffer, NBuffer);
327 }
LIST search(LIST list, void *key, int_compare is_equal)
Definition: oldlist.cpp:413
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 218 of file kdtree.cpp.

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

Walk a given Tree with action.

Definition at line 332 of file kdtree.cpp.

332  {
333  if (Tree->Root.Left != NULL)
334  Walk(Tree, action, context, Tree->Root.Left, NextLevel(Tree, -1));
335 }
void Walk(KDTREE *tree, void_proc action, void *context, KDNODE *sub_tree, inT32 level)
Definition: kdtree.cpp:535
KDNODE Root
Definition: kdtree.h:51
struct KDNODE * Left
Definition: kdtree.h:45
#define NULL
Definition: host.h:144
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 374 of file kdtree.cpp.

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

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