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";
27int 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]) {
48Segment *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];
66int 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);
108void 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;
165void SegmentFactory::loadBool (
const ColorFilter &filter,
170 for (
int y = 0; y < image.height(); y++) {
172 columnBool [y] =
false;
179void 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;
293void 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,
345void 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);
359 segments.removeAt (i);
364void SegmentFactory::removeUnneededLines(SegmentVector &lastSegment,
365 SegmentVector &currSegment,
370 QList<Segment*> &segments)
372 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::removeUnneededLines";
375 for (
int yLast = 0; yLast < height; yLast++) {
377 ENGAUGE_ASSERT (yLast < height);
378 if (lastSegment [yLast] && (lastSegment [yLast] != segLast)) {
380 segLast = lastSegment [yLast];
384 for (
int yCur = 0; yCur < height; yCur++) {
386 ENGAUGE_ASSERT (yCur < height);
387 if (segLast == currSegment [yCur]) {
395 ENGAUGE_CHECK_PTR(segLast);
403 lastSegment [yLast] = 0;
411 segments.push_back (segLast);
419void SegmentFactory::scrollBool(
bool *left,
423 for (
int y = 0; y < height; y++) {
424 left [y] = right [y];
428void SegmentFactory::scrollSegment(SegmentVector &left,
429 SegmentVector &right,
432 for (
int y = 0; y < height; y++) {
433 left [y] = right [y];
439 LOG4CPP_DEBUG_S ((*mainCat)) <<
"SegmentFactory::clearSegments";
441 QList<Segment*>::iterator itr;
442 for (itr = segments.begin(); itr != segments.end(); itr++) {
Class for filtering image to remove unimportant information.
bool pixelFilteredIsOn(const QImage &image, int x, int y) const
Return true if specified filtered pixel is on.
Model for DlgSettingsSegments and CmdSettingsSegments.
double minLength() const
Get method for min length.
double pointSeparation() const
Get method for point separation.
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments, QList< Segment * > segments)
Return segment fill points for all segments, for previewing.
void clearSegments(QList< Segment * > &segments)
Remove the segments created by makeSegments.
SegmentFactory(QGraphicsScene &scene, bool isGnuplot)
Single constructor.
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.
Selectable piecewise-defined line that follows a filtered line in the image.
double length() const
Get method for length in pixels.
int lineCount() const
Get method for number of lines.
QList< QPoint > fillPoints(const DocumentModelSegments &modelSegments)
Create evenly spaced points along the segment.
void appendColumn(int x, int y, const DocumentModelSegments &modelSegments)
Add some more pixels in a new column to an active segment.
void removeUnneededLines(int *foldedLines)
Try to compress a segment that was just completed, by folding together line from point i to point i+1...