Engauge Digitizer  2
Public Member Functions | List of all members
SegmentFactory Class Reference

Factory class for Segment objects. More...

#include <SegmentFactory.h>

Collaboration diagram for SegmentFactory:
Collaboration graph

Public Member Functions

 SegmentFactory (QGraphicsScene &scene, bool isGnuplot)
 Single constructor. More...
 
void clearSegments (QList< Segment * > &segments)
 Remove the segments created by makeSegments. More...
 
QList< QPoint > fillPoints (const DocumentModelSegments &modelSegments, QList< Segment * > segments)
 Return segment fill points for all segments, for previewing. More...
 
void makeSegments (const QImage &imageFiltered, const DocumentModelSegments &modelSegments, QList< Segment * > &segments, bool useDlg=true)
 Main entry point for creating all Segments for the filtered image. More...
 

Detailed Description

Factory class for Segment objects.

The input is the filtered image.

The strategy is to fill out the segments output array as each segment finishes. This makes it easy to keep too-short Segments out of the output array, versus adding every new Segment to the output array as soon as it is created

Definition at line 27 of file SegmentFactory.h.

Constructor & Destructor Documentation

◆ SegmentFactory()

SegmentFactory::SegmentFactory ( QGraphicsScene &  scene,
bool  isGnuplot 
)

Single constructor.

Definition at line 21 of file SegmentFactory.cpp.

22  :
23  m_scene (scene),
24  m_isGnuplot (isGnuplot)
25 {
26  LOG4CPP_INFO_S ((*mainCat)) << "SegmentFactory::SegmentFactory";
27 }
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
log4cpp::Category * mainCat
Definition: Logger.cpp:14

Member Function Documentation

◆ clearSegments()

void SegmentFactory::clearSegments ( QList< Segment * > &  segments)

Remove the segments created by makeSegments.

Definition at line 440 of file SegmentFactory.cpp.

441 {
442  LOG4CPP_DEBUG_S ((*mainCat)) << "SegmentFactory::clearSegments";
443 
444  QList<Segment*>::iterator itr;
445  for (itr = segments.begin(); itr != segments.end(); itr++) {
446 
447  Segment *segment = *itr;
448 
449  delete segment;
450  }
451 
452  segments.clear ();
453 }
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:21
log4cpp::Category * mainCat
Definition: Logger.cpp:14
#define LOG4CPP_DEBUG_S(logger)
Definition: convenience.h:20

◆ fillPoints()

QList< QPoint > SegmentFactory::fillPoints ( const DocumentModelSegments modelSegments,
QList< Segment * >  segments 
)

Return segment fill points for all segments, for previewing.

Definition at line 93 of file SegmentFactory.cpp.

95 {
96  LOG4CPP_INFO_S ((*mainCat)) << "SegmentFactory::fillPoints";
97 
98  QList<QPoint> list;
99  QList<Segment*>::iterator itr;
100  for (itr = segments.begin (); itr != segments.end(); itr++) {
101 
102  Segment *segment = *itr;
103  ENGAUGE_CHECK_PTR(segment);
104  list += segment->fillPoints(modelSegments);
105  }
106 
107  return list;
108 }
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
#define ENGAUGE_CHECK_PTR(ptr)
#endif
Definition: EngaugeAssert.h:27
Selectable piecewise-defined line that follows a filtered line in the image.
Definition: Segment.h:21
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments)
Create evenly spaced points along the segment.
Definition: Segment.cpp:205
log4cpp::Category * mainCat
Definition: Logger.cpp:14

◆ makeSegments()

void SegmentFactory::makeSegments ( const QImage &  imageFiltered,
const DocumentModelSegments modelSegments,
QList< Segment * > &  segments,
bool  useDlg = true 
)

Main entry point for creating all Segments for the filtered image.

Definition at line 189 of file SegmentFactory.cpp.

193 {
194  LOG4CPP_INFO_S ((*mainCat)) << "SegmentFactory::makeSegments";
195 
196  // Statistics that show up in debug spew
197  int madeLines = 0;
198  int shortLines = 0; // Lines rejected since their segments are too short
199  int foldedLines = 0; // Lines rejected since they could be into other lines
200 
201  // For each new column of pixels, loop through the runs. a run is defined as
202  // one or more colored pixels that are all touching, with one uncolored pixel or the
203  // image boundary at each end of the set. for each set in the current column, count
204  // the number of runs it touches in the adjacent (left and right) columns. here is
205  // the pseudocode:
206  // if ((L > 1) || (R > 1))
207  // "this run is at a branch point so ignore the set"
208  // else
209  // if (L == 0)
210  // "this run is the start of a new segment"
211  // else
212  // "this run is appended to the segment on the left
213  int width = imageFiltered.width();
214  int height = imageFiltered.height();
215 
216  QProgressDialog* dlg = nullptr;
217  if (useDlg)
218  {
219 
220  dlg = new QProgressDialog("Scanning segments in image", "Cancel", 0, width);
221  ENGAUGE_CHECK_PTR (dlg);
222  dlg->show();
223  }
224 
225  bool* lastBool = new bool [unsigned (height)];
226  ENGAUGE_CHECK_PTR(lastBool);
227  bool* currBool = new bool [unsigned (height)];
228  ENGAUGE_CHECK_PTR(currBool);
229  bool* nextBool = new bool [unsigned (height)];
230  ENGAUGE_CHECK_PTR(nextBool);
231  SegmentVector lastSegment (static_cast<unsigned long> (height));
232  SegmentVector currSegment (static_cast<unsigned long> (height));
233 
234  ColorFilter filter;
235  loadBool(filter, lastBool, imageFiltered, -1);
236  loadBool(filter, currBool, imageFiltered, 0);
237  loadBool(filter, nextBool, imageFiltered, 1);
238  loadSegment(lastSegment, height);
239 
240  for (int x = 0; x < width; x++) {
241 
242  if (useDlg) {
243 
244  // Update progress bar
245  dlg->setValue(x);
246  qApp->processEvents();
247 
248  if (dlg->wasCanceled()) {
249 
250  // Quit scanning. only existing segments will be available
251  break;
252  }
253  }
254 
255  matchRunsToSegments(x,
256  height,
257  lastBool,
258  lastSegment,
259  currBool,
260  currSegment,
261  nextBool,
262  modelSegments,
263  &madeLines,
264  &foldedLines,
265  &shortLines,
266  segments);
267 
268  // Get ready for next column
269  scrollBool(lastBool, currBool, height);
270  scrollBool(currBool, nextBool, height);
271  if (x + 1 < width) {
272  loadBool(filter, nextBool, imageFiltered, x + 1);
273  }
274  scrollSegment(lastSegment, currSegment, height);
275  }
276 
277  if (useDlg) {
278 
279  dlg->setValue(width);
280  delete dlg;
281  }
282 
283  removeEmptySegments (segments);
284 
285  LOG4CPP_INFO_S ((*mainCat)) << "SegmentFactory::makeSegments"
286  << " linesCreated=" << madeLines
287  << " linesTooShortSoRemoved=" << shortLines
288  << " linesFoldedTogether=" << foldedLines;
289 
290  delete[] lastBool;
291  delete[] currBool;
292  delete[] nextBool;
293 }
std::vector< Segment * > SegmentVector
#define LOG4CPP_INFO_S(logger)
Definition: convenience.h:18
Class for filtering image to remove unimportant information.
Definition: ColorFilter.h:20
#define ENGAUGE_CHECK_PTR(ptr)
#endif
Definition: EngaugeAssert.h:27
log4cpp::Category * mainCat
Definition: Logger.cpp:14

The documentation for this class was generated from the following files: