11#include <QApplication>
12#include <QGraphicsScene>
14#include <QProgressDialog>
24 m_isGnuplot (isGnuplot)
29int 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)];
68int 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++) {
110void 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) {
145 seg =
new Segment(m_scene,
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;
167void SegmentFactory::loadBool (
const ColorFilter &filter,
172 for (
int y = 0; y < image.height(); y++) {
174 columnBool [y] =
false;
181void 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;
295void 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,
347void SegmentFactory::removeEmptySegments (QList<Segment*> &segments)
const
351 for (
int i = segments.count(); i > 0;) {
354 Segment *segment = segments.at (i);
362 segments.removeAt (i);
367void SegmentFactory::removeUnneededLines(
SegmentVector &lastSegment,
373 QList<Segment*> &segments)
377 Segment *segLast =
nullptr;
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);
422void 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++) {
#define ENGAUGE_ASSERT(cond)
Drop in replacement for Q_ASSERT.
#define ENGAUGE_CHECK_PTR(ptr)
Drop in replacement for Q_CHECK_PTR.
log4cpp::Category * mainCat
std::vector< Segment * > SegmentVector
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...
#define LOG4CPP_INFO_S(logger)
#define LOG4CPP_DEBUG_S(logger)