11 #include <QApplication>
12 #include <QGraphicsScene>
14 #include <QProgressDialog>
24 m_isGnuplot (isGnuplot)
29 int SegmentFactory::adjacentRuns(
bool *columnBool,
36 for (
int y = yStart - 1; y <= yStop + 1; y++) {
37 if ((0 <= y) && (y < height)) {
38 if (!inRun && columnBool [y]) {
41 }
else if (inRun && !columnBool [y]) {
55 for (
int y = yStart - 1; y <= yStop + 1; y++) {
56 if ((0 <= y) && (y < height)) {
59 if (lastSegment [
unsigned (y)]) {
60 return lastSegment [unsigned (y)];
68 int SegmentFactory::adjacentSegments(
SegmentVector &lastSegment,
73 int adjacentSegments = 0;
75 bool inSegment =
false;
76 for (
int y = yStart - 1; y <= yStop + 1; y++) {
77 if ((0 <= y) && (y < height)) {
80 if (!inSegment && lastSegment [
unsigned (y)]) {
84 }
else if (inSegment && !lastSegment [
unsigned (y)]) {
90 return adjacentSegments;
94 QList<Segment*> segments)
99 QList<Segment*>::iterator itr;
100 for (itr = segments.begin (); itr != segments.end(); itr++) {
110 void SegmentFactory::finishRun(
bool *lastBool,
123 <<
" rows=" << yStart <<
"-" << yStop
124 <<
" runsOnLeft=" << adjacentRuns (nextBool, yStart, yStop, height)
125 <<
" runsOnRight=" << adjacentSegments (lastSegment, yStart, yStop, height);
132 if (adjacentRuns(lastBool, yStart, yStop, height) > 1) {
137 if (adjacentRuns(nextBool, yStart, yStop, height) > 1) {
142 if (adjacentSegments(lastSegment, yStart, yStop, height) == 0) {
146 qFloor (0.5 + (yStart + yStop) / 2.0),
153 seg = adjacentSegment(lastSegment, yStart, yStop, height);
157 seg->
appendColumn(x, qFloor (0.5 + (yStart + yStop) / 2.0), modelSegments);
160 for (
int y = yStart; y <= yStop; y++) {
163 currSegment [unsigned (y)] = seg;
167 void SegmentFactory::loadBool (
const ColorFilter &filter,
172 for (
int y = 0; y < image.height(); y++) {
174 columnBool [y] =
false;
181 void SegmentFactory::loadSegment (
SegmentVector &columnSegment,
184 for (
int y = 0; y < height; y++) {
185 columnSegment [unsigned (y)] =
nullptr;
191 QList<Segment*> &segments,
213 int width = imageFiltered.width();
214 int height = imageFiltered.height();
216 QProgressDialog* dlg =
nullptr;
220 dlg =
new QProgressDialog(
"Scanning segments in image",
"Cancel", 0, width);
225 bool* lastBool =
new bool [unsigned (height)];
227 bool* currBool =
new bool [unsigned (height)];
229 bool* nextBool =
new bool [unsigned (height)];
231 SegmentVector lastSegment (static_cast<unsigned long> (height));
232 SegmentVector currSegment (static_cast<unsigned long> (height));
235 loadBool(filter, lastBool, imageFiltered, -1);
236 loadBool(filter, currBool, imageFiltered, 0);
237 loadBool(filter, nextBool, imageFiltered, 1);
238 loadSegment(lastSegment, height);
240 for (
int x = 0; x < width; x++) {
246 qApp->processEvents();
248 if (dlg->wasCanceled()) {
255 matchRunsToSegments(x,
269 scrollBool(lastBool, currBool, height);
270 scrollBool(currBool, nextBool, height);
272 loadBool(filter, nextBool, imageFiltered, x + 1);
274 scrollSegment(lastSegment, currSegment, height);
279 dlg->setValue(width);
283 removeEmptySegments (segments);
286 <<
" linesCreated=" << madeLines
287 <<
" linesTooShortSoRemoved=" << shortLines
288 <<
" linesFoldedTogether=" << foldedLines;
295 void SegmentFactory::matchRunsToSegments(
int x,
306 QList<Segment*> &segments)
308 loadSegment(currSegment,
313 for (
int y = 0; y < height; y++) {
316 if (!inRun && currBool [y]) {
321 if ((y + 1 >= height) || !currBool [y + 1]) {
338 removeUnneededLines(lastSegment,
347 void SegmentFactory::removeEmptySegments (QList<Segment*> &segments)
const
351 for (
int i = segments.count(); i > 0;) {
354 Segment *segment = segments.at (i);
362 segments.removeAt (i);
367 void SegmentFactory::removeUnneededLines(
SegmentVector &lastSegment,
373 QList<Segment*> &segments)
378 for (
int yLast = 0; yLast < height; yLast++) {
381 if (lastSegment [
unsigned (yLast)] && (lastSegment [
unsigned (yLast)] != segLast)) {
383 segLast = lastSegment [unsigned (yLast)];
387 for (
int yCur = 0; yCur < height; yCur++) {
390 if (segLast == currSegment [
unsigned (yCur)]) {
406 lastSegment [unsigned (yLast)] =
nullptr;
414 segments.push_back (segLast);
422 void SegmentFactory::scrollBool(
bool *left,
426 for (
int y = 0; y < height; y++) {
427 left [y] = right [y];
435 for (
int y = 0; y < height; y++) {
436 left [static_cast<unsigned long> (y)] = right [static_cast<unsigned long> (y)];
444 QList<Segment*>::iterator itr;
445 for (itr = segments.begin(); itr != segments.end(); itr++) {