tesseract  4.1.1
seam.h
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: seam.h
5  * Author: Mark Seaman, SW Productivity
6  *
7  * (c) Copyright 1987, Hewlett-Packard Company.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  *********************************************************************************/
19 #ifndef SEAM_H
20 #define SEAM_H
21 
22 // Include automatically generated configuration file if running autoconf.
23 #ifdef HAVE_CONFIG_H
24 #include "config_auto.h"
25 #endif
26 
27 /*----------------------------------------------------------------------
28  I n c l u d e s
29 ----------------------------------------------------------------------*/
30 #include "blobs.h"
31 #include "split.h"
32 
33 /*----------------------------------------------------------------------
34  T y p e s
35 ----------------------------------------------------------------------*/
36 using PRIORITY = float; /* PRIORITY */
37 
38 class SEAM {
39  public:
40  // A seam with no splits
41  SEAM(float priority, const TPOINT& location)
42  : priority_(priority),
43  location_(location),
44  widthp_(0),
45  widthn_(0),
46  num_splits_(0) {}
47  // A seam with a single split point.
48  SEAM(float priority, const TPOINT& location, const SPLIT& split)
49  : priority_(priority),
50  location_(location),
51  widthp_(0),
52  widthn_(0),
53  num_splits_(1) {
54  splits_[0] = split;
55  }
56  // Default copy constructor, operator= and destructor are OK!
57 
58  // Accessors.
59  float priority() const { return priority_; }
60  void set_priority(float priority) { priority_ = priority; }
61  bool HasAnySplits() const { return num_splits_ > 0; }
62 
63  // Returns the bounding box of all the points in the seam.
64  TBOX bounding_box() const;
65 
66  // Returns true if other can be combined into *this.
67  bool CombineableWith(const SEAM& other, int max_x_dist,
68  float max_total_priority) const;
69  // Combines other into *this. Only works if CombinableWith returned true.
70  void CombineWith(const SEAM& other);
71 
72  // Returns true if the given blob contains all splits of *this SEAM.
73  bool ContainedByBlob(const TBLOB& blob) const {
74  for (int s = 0; s < num_splits_; ++s) {
75  if (!splits_[s].ContainedByBlob(blob)) return false;
76  }
77  return true;
78  }
79 
80  // Returns true if the given EDGEPT is used by this SEAM, checking only
81  // the EDGEPT pointer, not the coordinates.
82  bool UsesPoint(const EDGEPT* point) const {
83  for (int s = 0; s < num_splits_; ++s) {
84  if (splits_[s].UsesPoint(point)) return true;
85  }
86  return false;
87  }
88  // Returns true if *this and other share any common point, by coordinates.
89  bool SharesPosition(const SEAM& other) const {
90  for (int s = 0; s < num_splits_; ++s) {
91  for (int t = 0; t < other.num_splits_; ++t)
92  if (splits_[s].SharesPosition(other.splits_[t])) return true;
93  }
94  return false;
95  }
96  // Returns true if *this and other have any vertically overlapping splits.
97  bool OverlappingSplits(const SEAM& other) const {
98  for (int s = 0; s < num_splits_; ++s) {
99  TBOX split1_box = splits_[s].bounding_box();
100  for (int t = 0; t < other.num_splits_; ++t) {
101  TBOX split2_box = other.splits_[t].bounding_box();
102  if (split1_box.y_overlap(split2_box)) return true;
103  }
104  }
105  return false;
106  }
107 
108  // Marks the edgepts used by the seam so the segments made by the cut
109  // never get split further by another seam in the future.
110  void Finalize() {
111  for (int s = 0; s < num_splits_; ++s) {
112  splits_[s].point1->MarkChop();
113  splits_[s].point2->MarkChop();
114  }
115  }
116 
117  // Returns true if the splits in *this SEAM appear OK in the sense that they
118  // do not cross any outlines and do not chop off any ridiculously small
119  // pieces.
120  bool IsHealthy(const TBLOB& blob, int min_points, int min_area) const;
121 
122  // Computes the widthp_/widthn_ range for all existing SEAMs and for *this
123  // seam, which is about to be inserted at insert_index. Returns false if
124  // any of the computations fails, as this indicates an invalid chop.
125  // widthn_/widthp_ are only changed if modify is true.
126  bool PrepareToInsertSeam(const GenericVector<SEAM*>& seams,
127  const GenericVector<TBLOB*>& blobs, int insert_index,
128  bool modify);
129  // Computes the widthp_/widthn_ range. Returns false if not all the splits
130  // are accounted for. widthn_/widthp_ are only changed if modify is true.
131  bool FindBlobWidth(const GenericVector<TBLOB*>& blobs, int index,
132  bool modify);
133 
134  // Splits this blob into two blobs by applying the splits included in
135  // *this SEAM
136  void ApplySeam(bool italic_blob, TBLOB* blob, TBLOB* other_blob) const;
137  // Undoes ApplySeam by removing the seam between these two blobs.
138  // Produces one blob as a result, and deletes other_blob.
139  void UndoSeam(TBLOB* blob, TBLOB* other_blob) const;
140 
141  // Prints everything in *this SEAM.
142  void Print(const char* label) const;
143  // Prints a collection of SEAMs.
144  static void PrintSeams(const char* label, const GenericVector<SEAM*>& seams);
145 #ifndef GRAPHICS_DISABLED
146  // Draws the seam in the given window.
147  void Mark(ScrollView* window) const;
148 #endif
149 
150  // Break up the blobs in this chain so that they are all independent.
151  // This operation should undo the affect of join_pieces.
152  static void BreakPieces(const GenericVector<SEAM*>& seams,
153  const GenericVector<TBLOB*>& blobs, int first,
154  int last);
155  // Join a group of base level pieces into a single blob that can then
156  // be classified.
157  static void JoinPieces(const GenericVector<SEAM*>& seams,
158  const GenericVector<TBLOB*>& blobs, int first,
159  int last);
160 
161  // Hides the seam so the outlines appear not to be cut by it.
162  void Hide() const;
163  // Undoes hide, so the outlines are cut by the seam.
164  void Reveal() const;
165 
166  // Computes and returns, but does not set, the full priority of *this SEAM.
167  // The arguments here are config parameters defined in Wordrec. Add chop_
168  // to the beginning of the name.
169  float FullPriority(int xmin, int xmax, double overlap_knob,
170  int centered_maxwidth, double center_knob,
171  double width_change_knob) const;
172 
173  private:
174  // Maximum number of splits that a SEAM can hold.
175  static const uint8_t kMaxNumSplits = 3;
176  // Priority of this split. Lower is better.
177  float priority_;
178  // Position of the middle of the seam.
179  TPOINT location_;
180  // A range such that all splits in *this SEAM are contained within blobs in
181  // the range [index - widthn_,index + widthp_] where index is the index of
182  // this SEAM in the seams vector.
183  int8_t widthp_;
184  int8_t widthn_;
185  // Number of splits_ that are used.
186  uint8_t num_splits_;
187  // Set of pairs of points that are the ends of each split in the SEAM.
188  SPLIT splits_[kMaxNumSplits];
189 };
190 
191 /*----------------------------------------------------------------------
192  F u n c t i o n s
193 ----------------------------------------------------------------------*/
194 
195 void start_seam_list(TWERD* word, GenericVector<SEAM*>* seam_array);
196 
197 #endif
float PRIORITY
Definition: seam.h:36
void start_seam_list(TWERD *word, GenericVector< SEAM * > *seam_array)
Definition: seam.cpp:263
LIST last(LIST var_list)
Definition: oldlist.cpp:190
Definition: blobs.h:51
Definition: blobs.h:99
void MarkChop()
Definition: blobs.h:179
Definition: blobs.h:284
Definition: blobs.h:418
Definition: rect.h:34
bool y_overlap(const TBOX &box) const
Definition: rect.h:428
Definition: seam.h:38
float FullPriority(int xmin, int xmax, double overlap_knob, int centered_maxwidth, double center_knob, double width_change_knob) const
Definition: seam.cpp:239
TBOX bounding_box() const
Definition: seam.cpp:31
static void BreakPieces(const GenericVector< SEAM * > &seams, const GenericVector< TBLOB * > &blobs, int first, int last)
Definition: seam.cpp:188
float priority() const
Definition: seam.h:59
bool HasAnySplits() const
Definition: seam.h:61
void set_priority(float priority)
Definition: seam.h:60
bool SharesPosition(const SEAM &other) const
Definition: seam.h:89
void UndoSeam(TBLOB *blob, TBLOB *other_blob) const
Definition: seam.cpp:134
SEAM(float priority, const TPOINT &location, const SPLIT &split)
Definition: seam.h:48
bool UsesPoint(const EDGEPT *point) const
Definition: seam.h:82
void Mark(ScrollView *window) const
Definition: seam.cpp:180
void Hide() const
Definition: seam.cpp:225
bool PrepareToInsertSeam(const GenericVector< SEAM * > &seams, const GenericVector< TBLOB * > &blobs, int insert_index, bool modify)
Definition: seam.cpp:76
bool FindBlobWidth(const GenericVector< TBLOB * > &blobs, int index, bool modify)
Definition: seam.cpp:91
bool IsHealthy(const TBLOB &blob, int min_points, int min_area) const
Definition: seam.cpp:66
void Reveal() const
Definition: seam.cpp:232
bool OverlappingSplits(const SEAM &other) const
Definition: seam.h:97
void ApplySeam(bool italic_blob, TBLOB *blob, TBLOB *other_blob) const
Definition: seam.cpp:118
bool ContainedByBlob(const TBLOB &blob) const
Definition: seam.h:73
void Finalize()
Definition: seam.h:110
static void JoinPieces(const GenericVector< SEAM * > &seams, const GenericVector< TBLOB * > &blobs, int first, int last)
Definition: seam.cpp:210
SEAM(float priority, const TPOINT &location)
Definition: seam.h:41
static void PrintSeams(const char *label, const GenericVector< SEAM * > &seams)
Definition: seam.cpp:167
bool CombineableWith(const SEAM &other, int max_x_dist, float max_total_priority) const
Definition: seam.cpp:40
void Print(const char *label) const
Definition: seam.cpp:154
void CombineWith(const SEAM &other)
Definition: seam.cpp:54
Definition: split.h:37
EDGEPT * point1
Definition: split.h:103
EDGEPT * point2
Definition: split.h:104
TBOX bounding_box() const
Definition: split.cpp:44