tesseract v5.3.3.20231005
kdtree.h
Go to the documentation of this file.
1/******************************************************************************
2 ** Filename: kdtree.h
3 ** Purpose: Definition of K-D tree access routines.
4 ** Author: Dan Johnson
5 **
6 ** (c) Copyright Hewlett-Packard Company, 1988.
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 *****************************************************************************/
17
18#ifndef KDTREE_H
19#define KDTREE_H
20
21#include "ocrfeatures.h"
22
23namespace tesseract {
24
35struct ClusteringContext;
36struct CLUSTER;
37struct KDTREE;
38
39using kdwalk_proc = void (*)(ClusteringContext *context, CLUSTER *Cluster, int32_t Level);
40
41struct KDNODE {
50 KDNODE() = default;
51 KDNODE(KDTREE *tree, float key[], CLUSTER *data, int Index);
53 delete Left;
54 delete Right;
55 }
56
57 float *Key;
60 float LeftBranch;
64};
65
66struct KDTREE {
67 KDTREE(size_t n) : KeySize(n), KeyDesc(n) {
68 }
69
70 // The destructor frees all memory which is allocated to the
71 // specified KD-tree. This includes the data structure for
72 // the kd-tree itself plus the data structures for each node
73 // in the tree. It does not include the Key and Data items
74 // which are pointed to by the nodes. This memory is left
75 // untouched.
77 }
78
79 // TODO: KeySize might be replaced by KeyDesc.size().
80 int16_t KeySize = 0; // number of dimensions in the tree
81 KDNODE Root; // Root.Left points to actual root node
82 std::vector<PARAM_DESC> KeyDesc; // description of each dimension
83};
84
85inline KDNODE::KDNODE(KDTREE *tree, float key[], CLUSTER *data, int Index) {
86 Key = key;
87 Data = data;
88 BranchPoint = Key[Index];
89 LeftBranch = tree->KeyDesc[Index].Min;
90 RightBranch = tree->KeyDesc[Index].Max;
91 Left = nullptr;
92 Right = nullptr;
93}
94
95/*----------------------------------------------------------------------------
96 Macros
97-----------------------------------------------------------------------------*/
98#define RootOf(T) ((T)->Root.Left->Data)
99
100/*-----------------------------------------------------------------------------
101 Public Function Prototypes
102-----------------------------------------------------------------------------*/
103KDTREE *MakeKDTree(int16_t KeySize, const PARAM_DESC KeyDesc[]);
104
105void KDStore(KDTREE *Tree, float *Key, CLUSTER *Data);
106
107void KDDelete(KDTREE *Tree, float Key[], void *Data);
108
109void KDNearestNeighborSearch(KDTREE *Tree, float Query[], int QuerySize, float MaxDistance,
110 int *NumberOfResults, void **NBuffer, float DBuffer[]);
111
112void KDWalk(KDTREE *Tree, kdwalk_proc Action, ClusteringContext *context);
113
114/*-----------------------------------------------------------------------------
115 Private Function Prototypes
116-----------------------------------------------------------------------------*/
117
118float DistanceSquared(int k, PARAM_DESC *dim, float p1[], float p2[]);
119
121float ComputeDistance(int k, PARAM_DESC *dim, float p1[], float p2[]);
122
124
125void Walk(KDTREE *tree, kdwalk_proc action, ClusteringContext *context, KDNODE *SubTree, int32_t Level);
126
127void InsertNodes(KDTREE *tree, KDNODE *nodes);
128
129} // namespace tesseract
130
131#endif
void(*)(ClusteringContext *context, CLUSTER *Cluster, int32_t Level) kdwalk_proc
Definition: kdtree.h:39
void KDDelete(KDTREE *Tree, float Key[], void *Data)
Definition: kdtree.cpp:252
void KDWalk(KDTREE *Tree, kdwalk_proc action, ClusteringContext *context)
Definition: kdtree.cpp:313
float ComputeDistance(int k, PARAM_DESC *dim, float p1[], float p2[])
Definition: kdtree.cpp:400
void Walk(KDTREE *tree, kdwalk_proc action, ClusteringContext *context, KDNODE *sub_tree, int32_t level)
Definition: kdtree.cpp:466
float DistanceSquared(int k, PARAM_DESC *dim, float p1[], float p2[])
Definition: kdtree.cpp:378
void InsertNodes(KDTREE *tree, KDNODE *nodes)
Definition: kdtree.cpp:477
KDTREE * MakeKDTree(int16_t KeySize, const PARAM_DESC KeyDesc[])
Definition: kdtree.cpp:186
int QueryInSearch(KDTREE *tree)
void KDNearestNeighborSearch(KDTREE *Tree, float Query[], int QuerySize, float MaxDistance, int *NumberOfResults, void **NBuffer, float DBuffer[])
Definition: kdtree.cpp:305
void KDStore(KDTREE *Tree, float *Key, CLUSTER *Data)
Definition: kdtree.cpp:215
action
Definition: upload.py:408
float * Key
Definition: kdtree.h:57
KDNODE * Right
Definition: kdtree.h:63
float LeftBranch
Definition: kdtree.h:60
KDNODE * Left
Definition: kdtree.h:62
float RightBranch
Definition: kdtree.h:61
CLUSTER * Data
Definition: kdtree.h:58
float BranchPoint
Definition: kdtree.h:59
std::vector< PARAM_DESC > KeyDesc
Definition: kdtree.h:82
KDTREE(size_t n)
Definition: kdtree.h:67
int16_t KeySize
Definition: kdtree.h:80
KDNODE Root
Definition: kdtree.h:81
#define TESS_API
Definition: export.h:32