1 /****************************************************************************** 2 ** Filename: kdtree.h 3 ** Purpose: Definition of K-D tree access routines. 4 ** Author: Dan Johnson 5 ** History: 3/11/89, DSJ, Created. 6 ** 5/23/89, DSJ, Added circular feature capability. 7 ** 8 ** (c) Copyright Hewlett-Packard Company, 1988. 9 ** Licensed under the Apache License, Version 2.0 (the "License"); 10 ** you may not use this file except in compliance with the License. 11 ** You may obtain a copy of the License at 12 ** http://www.apache.org/licenses/LICENSE-2.0 13 ** Unless required by applicable law or agreed to in writing, software 14 ** distributed under the License is distributed on an "AS IS" BASIS, 15 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 ** See the License for the specific language governing permissions and 17 ** limitations under the License. 18 ******************************************************************************/ 19 #ifndef KDTREE_H 20 #define KDTREE_H 21 22 /**---------------------------------------------------------------------------- 23 Include Files and Type Defines 24 ----------------------------------------------------------------------------**/ 25 #include "general.h" 26 #include "cutil.h" 27 #include "ocrfeatures.h" 28 29 /* 30 NOTE: All circular parameters of all keys must be in the range 31 32 Min <= Param < Max 33 34 where Min and Max are specified in the KeyDesc parameter passed to 35 MakeKDTree. All KD routines assume that this is true and will not operate 36 correctly if circular parameters outside the specified range are used. 37 */ 38 39 typedef struct kdnode 40 { 41 FLOAT32 *Key; /* search key */ 42 char *Data; /* data that corresponds to key */ 43 FLOAT32 BranchPoint; /* needed to make deletes work efficiently */ 44 FLOAT32 LeftBranch; /* used to optimize search pruning */ 45 FLOAT32 RightBranch; /* used to optimize search pruning */ 46 struct kdnode *Left; /* ptrs for KD tree structure */ 47 struct kdnode *Right; 48 } 49 50 51 KDNODE; 52 53 typedef struct 54 { 55 inT16 KeySize; /* number of dimensions in the tree */ 56 KDNODE Root; /* Root.Left points to actual root node */ 57 PARAM_DESC KeyDesc[1]; /* description of each dimension */ 58 } 59 60 61 KDTREE; 62 63 typedef enum { /* used for walking thru KD trees */ 64 preorder, postorder, endorder, leaf 65 } 66 67 68 VISIT; 69 70 /*---------------------------------------------------------------------------- 71 Macros 72 -----------------------------------------------------------------------------*/ 73 #define RootOf(T) ((T)->Root.Left->Data) 74 75 /**---------------------------------------------------------------------------- 76 Public Function Prototypes 77 ----------------------------------------------------------------------------**/ 78 KDTREE *MakeKDTree (inT16 KeySize, PARAM_DESC KeyDesc[]); 79 80 void KDStore(KDTREE *Tree, FLOAT32 *Key, void *Data); 81 82 void KDDelete (KDTREE * Tree, FLOAT32 Key[], void *Data); 83 84 int KDNearestNeighborSearch (KDTREE * Tree, 85 FLOAT32 Query[], 86 int QuerySize, 87 FLOAT32 MaxDistance, 88 void *NBuffer, FLOAT32 DBuffer[]); 89 90 void KDWalk(KDTREE *Tree, void_proc Action); 91 92 void FreeKDTree(KDTREE *Tree); 93 94 /**---------------------------------------------------------------------------- 95 Private Function Prototypes 96 ----------------------------------------------------------------------------**/ 97 int Equal (FLOAT32 Key1[], FLOAT32 Key2[]); 98 99 KDNODE *MakeKDNode (FLOAT32 Key[], char *Data, int Index); 100 101 void FreeKDNode(KDNODE *Node); 102 103 void Search(int Level, KDNODE *SubTree); 104 105 FLOAT32 ComputeDistance (register int N, 106 register PARAM_DESC Dim[], 107 register FLOAT32 p1[], register FLOAT32 p2[]); 108 109 void FindMaxDistance(); 110 111 int QueryIntersectsSearch(); 112 113 int QueryInSearch(); 114 115 void Walk(KDNODE *SubTree, inT32 Level); 116 117 void FreeSubTree(KDNODE *SubTree); 118 #endif 119