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