tesseract  5.0.0
intfeaturedist.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 // Author: rays@google.com (Ray Smith)
4 // File: intfeaturedist.h
5 // Description: Fast set-difference-based feature distance calculator.
6 //
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 //
18 
19 #ifndef TESSERACT_CLASSIFY_INTFEATUREDIST_H_
20 #define TESSERACT_CLASSIFY_INTFEATUREDIST_H_
21 
22 #include <vector>
23 
24 namespace tesseract {
25 
26 class IntFeatureMap;
27 
28 // Feature distance calculator designed to provide a fast distance calculation
29 // based on set difference between a given feature set and many other feature
30 // sets in turn.
31 // Representation of a feature set as an array of bools that are sparsely
32 // true, and companion arrays that allow fast feature set distance
33 // calculations with allowance of offsets in position.
34 // Init is expensive, so for greatest efficiency, to re-initialize for a new
35 // feature set, use Set(..., false) on the SAME feature set as was used to
36 // setup with Set(..., true), to return to its initialized state before
37 // reuse with Set(..., true) on a new feature set.
39 public:
42 
43  // Initialize the bool array to the given size of feature space.
44  // The feature_map is just borrowed, and must exist for the entire
45  // lifetime of the IntFeatureDist.
46  void Init(const IntFeatureMap *feature_map);
47 
48  // Setup the map for the given indexed_features that have been indexed by
49  // feature_map. After use, use Set(..., false) to reset to the initial state
50  // as this is faster than calling Init for sparse spaces.
51  void Set(const std::vector<int> &indexed_features, int canonical_count, bool value);
52 
53  // Compute the distance between the given feature vector and the last
54  // Set feature vector.
55  double FeatureDistance(const std::vector<int> &features) const;
56  double DebugFeatureDistance(const std::vector<int> &features) const;
57 
58 private:
59  // Clear all data.
60  void Clear();
61 
62  // Size of the indexed feature space.
63  int size_;
64  // Total weight of features currently stored in the maps.
65  double total_feature_weight_;
66  // Pointer to IntFeatureMap given at Init to find offset features.
67  const IntFeatureMap *feature_map_;
68  // Array of bools indicating presence of a feature.
69  bool *features_;
70  // Array indicating the presence of a feature offset by one unit.
71  bool *features_delta_one_;
72  // Array indicating the presence of a feature offset by two units.
73  bool *features_delta_two_;
74 };
75 
76 } // namespace tesseract
77 
78 #endif // TESSERACT_CLASSIFY_INTFEATUREDIST_H_
void Set(const std::vector< int > &indexed_features, int canonical_count, bool value)
double DebugFeatureDistance(const std::vector< int > &features) const
void Init(const IntFeatureMap *feature_map)
double FeatureDistance(const std::vector< int > &features) const