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