7 #include "ColorFilter.h" 8 #include "DocumentModelSegments.h" 9 #include "EngaugeAssert.h" 11 #include <QApplication> 12 #include <QGraphicsScene> 13 #include <QProgressDialog> 15 #include "SegmentFactory.h" 22 m_isGnuplot (isGnuplot)
24 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::SegmentFactory";
27 int SegmentFactory::adjacentRuns(
bool *columnBool,
34 for (
int y = yStart - 1; y <= yStop + 1; y++) {
35 if ((0 <= y) && (y < height)) {
36 if (!inRun && columnBool [y]) {
39 }
else if (inRun && !columnBool [y]) {
48 Segment *SegmentFactory::adjacentSegment(SegmentVector &lastSegment,
53 for (
int y = yStart - 1; y <= yStop + 1; y++) {
54 if ((0 <= y) && (y < height)) {
56 ENGAUGE_ASSERT (y < height);
57 if (lastSegment [y]) {
58 return lastSegment [y];
66 int SegmentFactory::adjacentSegments(SegmentVector &lastSegment,
71 int adjacentSegments = 0;
73 bool inSegment =
false;
74 for (
int y = yStart - 1; y <= yStop + 1; y++) {
75 if ((0 <= y) && (y < height)) {
77 ENGAUGE_ASSERT (y < height);
78 if (!inSegment && lastSegment [y]) {
82 }
else if (inSegment && !lastSegment [y]) {
88 return adjacentSegments;
92 QList<Segment*> segments)
94 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::fillPoints";
97 QList<Segment*>::iterator itr;
98 for (itr = segments.begin (); itr != segments.end(); itr++) {
101 ENGAUGE_CHECK_PTR(segment);
108 void SegmentFactory::finishRun(
bool *lastBool,
110 SegmentVector &lastSegment,
111 SegmentVector &currSegment,
119 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::finishRun" 121 <<
" rows=" << yStart <<
"-" << yStop
122 <<
" runsOnLeft=" << adjacentRuns (nextBool, yStart, yStop, height)
123 <<
" runsOnRight=" << adjacentSegments (lastSegment, yStart, yStop, height);
130 if (adjacentRuns(lastBool, yStart, yStop, height) > 1) {
135 if (adjacentRuns(nextBool, yStart, yStop, height) > 1) {
140 if (adjacentSegments(lastSegment, yStart, yStop, height) == 0) {
144 (
int) (0.5 + (yStart + yStop) / 2.0),
146 ENGAUGE_CHECK_PTR (seg);
151 seg = adjacentSegment(lastSegment, yStart, yStop, height);
154 ENGAUGE_CHECK_PTR(seg);
155 seg->
appendColumn(x, (
int) (0.5 + (yStart + yStop) / 2.0), modelSegments);
158 for (
int y = yStart; y <= yStop; y++) {
160 ENGAUGE_ASSERT (y < height);
161 currSegment [y] = seg;
165 void SegmentFactory::loadBool (
const ColorFilter &filter,
170 for (
int y = 0; y < image.height(); y++) {
172 columnBool [y] =
false;
179 void SegmentFactory::loadSegment (SegmentVector &columnSegment,
182 for (
int y = 0; y < height; y++) {
183 columnSegment [y] = 0;
189 QList<Segment*> &segments,
192 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::makeSegments";
211 int width = imageFiltered.width();
212 int height = imageFiltered.height();
214 QProgressDialog* dlg = 0;
218 dlg =
new QProgressDialog(
"Scanning segments in image",
"Cancel", 0, width);
219 ENGAUGE_CHECK_PTR (dlg);
223 bool* lastBool =
new bool [height];
224 ENGAUGE_CHECK_PTR(lastBool);
225 bool* currBool =
new bool [height];
226 ENGAUGE_CHECK_PTR(currBool);
227 bool* nextBool =
new bool [height];
228 ENGAUGE_CHECK_PTR(nextBool);
229 SegmentVector lastSegment (height);
230 SegmentVector currSegment (height);
233 loadBool(filter, lastBool, imageFiltered, -1);
234 loadBool(filter, currBool, imageFiltered, 0);
235 loadBool(filter, nextBool, imageFiltered, 1);
236 loadSegment(lastSegment, height);
238 for (
int x = 0; x < width; x++) {
244 qApp->processEvents();
246 if (dlg->wasCanceled()) {
253 matchRunsToSegments(x,
267 scrollBool(lastBool, currBool, height);
268 scrollBool(currBool, nextBool, height);
270 loadBool(filter, nextBool, imageFiltered, x + 1);
272 scrollSegment(lastSegment, currSegment, height);
277 dlg->setValue(width);
281 removeEmptySegments (segments);
283 LOG4CPP_INFO_S ((*mainCat)) <<
"SegmentFactory::makeSegments" 284 <<
" linesCreated=" << madeLines
285 <<
" linesTooShortSoRemoved=" << shortLines
286 <<
" linesFoldedTogether=" << foldedLines;
293 void SegmentFactory::matchRunsToSegments(
int x,
296 SegmentVector &lastSegment,
298 SegmentVector &currSegment,
304 QList<Segment*> &segments)
306 loadSegment(currSegment,
311 for (
int y = 0; y < height; y++) {
313 ENGAUGE_ASSERT (y < height);
314 if (!inRun && currBool [y]) {
319 if ((y + 1 >= height) || !currBool [y + 1]) {
336 removeUnneededLines(lastSegment,
345 void SegmentFactory::removeEmptySegments (QList<Segment*> &segments)
const 347 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::removeUnneededLines";
349 for (
int i = segments.count(); i > 0;) {
352 Segment *segment = segments.at (i);
360 segments.removeAt (i);
365 void SegmentFactory::removeUnneededLines(SegmentVector &lastSegment,
366 SegmentVector &currSegment,
371 QList<Segment*> &segments)
373 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::removeUnneededLines";
376 for (
int yLast = 0; yLast < height; yLast++) {
378 ENGAUGE_ASSERT (yLast < height);
379 if (lastSegment [yLast] && (lastSegment [yLast] != segLast)) {
381 segLast = lastSegment [yLast];
385 for (
int yCur = 0; yCur < height; yCur++) {
387 ENGAUGE_ASSERT (yCur < height);
388 if (segLast == currSegment [yCur]) {
396 ENGAUGE_CHECK_PTR(segLast);
404 lastSegment [yLast] = 0;
412 segments.push_back (segLast);
420 void SegmentFactory::scrollBool(
bool *left,
424 for (
int y = 0; y < height; y++) {
425 left [y] = right [y];
429 void SegmentFactory::scrollSegment(SegmentVector &left,
430 SegmentVector &right,
433 for (
int y = 0; y < height; y++) {
434 left [y] = right [y];
440 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::clearSegments";
442 QList<Segment*>::iterator itr;
443 for (itr = segments.begin(); itr != segments.end(); itr++) {
double pointSeparation() const
Get method for point separation.
void removeUnneededLines(int *foldedLines)
Try to compress a segment that was just completed, by folding together line from point i to point i+1...
bool pixelFilteredIsOn(const QImage &image, int x, int y) const
Return true if specified filtered pixel is on.
SegmentFactory(QGraphicsScene &scene, bool isGnuplot)
Single constructor.
double minLength() const
Get method for min length.
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments, QList< Segment *> segments)
Return segment fill points for all segments, for previewing.
Class for filtering image to remove unimportant information.
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.
void appendColumn(int x, int y, const DocumentModelSegments &modelSegments)
Add some more pixels in a new column to an active segment.
void clearSegments(QList< Segment *> &segments)
Remove the segments created by makeSegments.
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.
int lineCount() const
Get method for number of lines.
Model for DlgSettingsSegments and CmdSettingsSegments.
double length() const
Get method for length in pixels.