1 #include "ColorFilter.h"
2 #include "DocumentModelSegments.h"
3 #include "EngaugeAssert.h"
5 #include <QApplication>
6 #include <QGraphicsScene>
7 #include <QProgressDialog>
9 #include "SegmentFactory.h"
16 m_isGnuplot (isGnuplot)
18 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::SegmentFactory";
21 int SegmentFactory::adjacentRuns(
bool *columnBool,
28 for (
int y = yStart - 1; y <= yStop + 1; y++) {
29 if ((0 <= y) && (y < height)) {
30 if (!inRun && columnBool [y]) {
33 }
else if (inRun && !columnBool [y]) {
42 Segment *SegmentFactory::adjacentSegment(SegmentVector &lastSegment,
47 for (
int y = yStart - 1; y <= yStop + 1; y++) {
48 if ((0 <= y) && (y < height)) {
50 ENGAUGE_ASSERT (y < height);
51 if (lastSegment [y]) {
52 return lastSegment [y];
60 int SegmentFactory::adjacentSegments(SegmentVector &lastSegment,
65 int adjacentSegments = 0;
67 bool inSegment =
false;
68 for (
int y = yStart - 1; y <= yStop + 1; y++) {
69 if ((0 <= y) && (y < height)) {
71 ENGAUGE_ASSERT (y < height);
72 if (!inSegment && lastSegment [y]) {
76 }
else if (inSegment && !lastSegment [y]) {
82 return adjacentSegments;
86 QList<Segment*> segments)
88 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::fillPoints";
91 QList<Segment*>::iterator itr;
92 for (itr = segments.begin (); itr != segments.end(); itr++) {
95 ENGAUGE_CHECK_PTR(segment);
102 void SegmentFactory::finishRun(
bool *lastBool,
104 SegmentVector &lastSegment,
105 SegmentVector &currSegment,
113 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::finishRun"
115 <<
" rows=" << yStart <<
"-" << yStop
116 <<
" runsOnLeft=" << adjacentRuns (nextBool, yStart, yStop, height)
117 <<
" runsOnRight=" << adjacentSegments (lastSegment, yStart, yStop, height);
124 if (adjacentRuns(lastBool, yStart, yStop, height) > 1) {
129 if (adjacentRuns(nextBool, yStart, yStop, height) > 1) {
134 if (adjacentSegments(lastSegment, yStart, yStop, height) == 0) {
138 (
int) (0.5 + (yStart + yStop) / 2.0),
140 ENGAUGE_CHECK_PTR (seg);
145 seg = adjacentSegment(lastSegment, yStart, yStop, height);
148 ENGAUGE_CHECK_PTR(seg);
149 seg->
appendColumn(x, (
int) (0.5 + (yStart + yStop) / 2.0), modelSegments);
152 for (
int y = yStart; y <= yStop; y++) {
154 ENGAUGE_ASSERT (y < height);
155 currSegment [y] = seg;
159 void SegmentFactory::loadBool (
const ColorFilter &filter,
164 for (
int y = 0; y < image.height(); y++) {
166 columnBool [y] =
false;
173 void SegmentFactory::loadSegment (SegmentVector &columnSegment,
176 for (
int y = 0; y < height; y++) {
177 columnSegment [y] = 0;
183 QList<Segment*> &segments)
185 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::makeSegments";
193 const bool useDlg =
true;
207 int width = imageFiltered.width();
208 int height = imageFiltered.height();
210 QProgressDialog* dlg;
214 dlg =
new QProgressDialog(
"Scanning segments in image",
"Cancel", 0, width);
215 ENGAUGE_CHECK_PTR (dlg);
219 bool* lastBool =
new bool [height];
220 ENGAUGE_CHECK_PTR(lastBool);
221 bool* currBool =
new bool [height];
222 ENGAUGE_CHECK_PTR(currBool);
223 bool* nextBool =
new bool [height];
224 ENGAUGE_CHECK_PTR(nextBool);
225 SegmentVector lastSegment (height);
226 SegmentVector currSegment (height);
229 loadBool(filter, lastBool, imageFiltered, -1);
230 loadBool(filter, currBool, imageFiltered, 0);
231 loadBool(filter, nextBool, imageFiltered, 1);
232 loadSegment(lastSegment, height);
234 for (
int x = 0; x < width; x++) {
240 qApp->processEvents();
242 if (dlg->wasCanceled()) {
249 matchRunsToSegments(x,
263 scrollBool(lastBool, currBool, height);
264 scrollBool(currBool, nextBool, height);
266 loadBool(filter, nextBool, imageFiltered, x + 1);
268 scrollSegment(lastSegment, currSegment, height);
273 dlg->setValue(width);
277 removeEmptySegments (segments);
279 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::makeSegments"
280 <<
" linesCreated=" << madeLines
281 <<
" linesTooShortSoRemoved=" << shortLines
282 <<
" linesFoldedTogether=" << foldedLines;
289 void SegmentFactory::matchRunsToSegments(
int x,
292 SegmentVector &lastSegment,
294 SegmentVector &currSegment,
300 QList<Segment*> &segments)
302 loadSegment(currSegment,
307 for (
int y = 0; y < height; y++) {
309 ENGAUGE_ASSERT (y < height);
310 if (!inRun && currBool [y]) {
315 if ((y + 1 >= height) || !currBool [y + 1]) {
332 removeUnneededLines(lastSegment,
341 void SegmentFactory::removeEmptySegments (QList<Segment*> &segments)
const
343 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::removeUnneededLines";
345 for (
int i = segments.count(); i > 0;) {
348 Segment *segment = segments.at (i);
355 segments.removeAt (i);
360 void SegmentFactory::removeUnneededLines(SegmentVector &lastSegment,
361 SegmentVector &currSegment,
366 QList<Segment*> &segments)
368 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::removeUnneededLines";
371 for (
int yLast = 0; yLast < height; yLast++) {
373 ENGAUGE_ASSERT (yLast < height);
374 if (lastSegment [yLast] && (lastSegment [yLast] != segLast)) {
376 segLast = lastSegment [yLast];
380 for (
int yCur = 0; yCur < height; yCur++) {
382 ENGAUGE_ASSERT (yCur < height);
383 if (segLast == currSegment [yCur]) {
391 ENGAUGE_CHECK_PTR(segLast);
399 lastSegment [yLast] = 0;
407 segments.push_back (segLast);
415 void SegmentFactory::scrollBool(
bool *left,
419 for (
int y = 0; y < height; y++) {
420 left [y] = right [y];
424 void SegmentFactory::scrollSegment(SegmentVector &left,
425 SegmentVector &right,
428 for (
int y = 0; y < height; y++) {
429 left [y] = right [y];
435 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::clearSegments";
437 QList<Segment*>::iterator itr;
438 for (itr = segments.begin(); itr != segments.end(); itr++) {
void makeSegments(const QImage &imageFiltered, const DocumentModelSegments &modelSegments, QList< Segment * > &segments)
Main entry point for creating all Segments for the filtered image.
int lineCount() const
Get method for number of lines.
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments, QList< Segment * > segments)
Return segment fill points for all segments, for previewing.
void removeUnneededLines(int *foldedLines)
Try to compress a segment that was just completed, by folding together line from point i to point i+1...
SegmentFactory(QGraphicsScene &scene, bool isGnuplot)
Single constructor.
Class for filtering image to remove unimportant information.
void clearSegments(QList< Segment * > &segments)
Remove the segments created by makeSegments.
double pointSeparation() const
Get method for point separation.
void appendColumn(int x, int y, const DocumentModelSegments &modelSegments)
Add some more pixels in a new column to an active segment.
double length() const
Get method for length in pixels.
Selectable piecewise-defined line that follows a filtered line in the image.
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments)
Create evenly spaced points along the segment.
bool pixelFilteredIsOn(const QImage &image, int x, int y) const
Return true if specified filtered pixel is on.
double minLength() const
Get method for min length.
Model for DlgSettingsSegments and CmdSettingsSegments.