[TASK] Switch to other bresenham implementation for now.

This commit is contained in:
Jan Philipp Timme 2016-01-08 13:10:59 +01:00
parent 4ce9e0e6c2
commit 85f2dd2824
2 changed files with 154 additions and 99 deletions

View File

@ -119,67 +119,67 @@ class HoughMachine {
};
void drawLineBresenham(int start_x, int start_y, int end_x, int end_y) {
this->bresenham_orientation_sector = this->getOrientationSector(end_x - start_x, end_y - start_y);
// Now adapt input
// Output will be adapted in setPixel()
switch(this->bresenham_orientation_sector) {
case 0:
// Going right, nothing to do here
this->bresenham(start_x, start_y, end_x, end_y);
break;
case 1:
// Going right, but more up
this->bresenham(start_y, start_x, end_y, end_x);
break;
case 2:
// Going left, but more up
this->bresenham(start_y, end_x, end_y, start_x);
break;
case 3:
// Going left
this->bresenham(end_x, start_y, start_x, end_y);
break;
case 4:
// Going left
this->bresenham(end_x, end_y, start_x, start_y);
break;
case 5:
// Going left, but more down
this->bresenham(end_y, end_x, start_y, start_x);
break;
case 6:
// Going right, but more down
this->bresenham(end_y, start_x, start_y, end_x);
break;
case 7:
// Going right
this->bresenham(start_x, end_y, end_x, start_y);
break;
}
void setPixelDebug(int x, int y, int color=0) {
int our_color = QColor::fromRgb(0, 255, 0).rgb();
if(color == 0) color = our_color;
std::cout << " --> SET (" << x << ", " << y << ")" << std::endl;
this->working_copy->getImage()->setPixel(x, y, color);
};
void bresenham(int start_x, int start_y, int end_x, int end_y) {
int x, y;
int delta_x, delta_y;
int d;
int delta_ne, delta_e;
x = start_x;
y = start_y;
delta_x = end_x - start_x;
delta_y = end_y - start_y;
delta_ne = 2 * (delta_y - delta_x);
delta_e = 2 * delta_y;
d = 2 * delta_y - delta_x;
this->setPixel(start_x, start_y, x - start_x, y - start_y);
while(x < end_x) {
if(d >= 0) {
d += delta_ne; x++; y++;
} else {
d += delta_e; x++;
void otherBresenham(int x, int x2, int y, int y2) {
int width = x2 - x;
int height = y2 - y;
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
dx1 = (width < 0) ? -1 : 1;
dy1 = (height < 0) ? -1 : 1;
dx2 = (width < 0) ? -1 : 1;
int longest = abs(width);
int shortest = abs(height);
if(longest <= shortest) {
longest = abs(height);
shortest = abs(width);
dy2 = (height < 0) ? -1 : 1;
dx2 = 0;
}
int numerator = longest / 2;
for(int i=0; i<=longest; i++) {
this->setPixelDebug(x, y);
numerator += shortest;
if(numerator >= longest) {
numerator -= longest;
x += dx1;
y += dy1;
} else {
x += dx2;
y += dy2;
}
}
};
void debugShow() {
std::cout << "Dimensions: " << this->n_ang << " x " << this->n_rad << " | " << std::endl;
int max_value = 0;
for(int i=0; i<this->n_ang*this->n_rad; i++) {
int current_value = this->hough_arr[i];
if(current_value > max_value) max_value = current_value;
}
for(int x=0; x<this->n_ang; x++) {
for(int y=0; y<this->n_rad; y++) {
int value = qRound(255.0*this->hough_arr[x*this->n_ang+y] / max_value);
if(value < 0) {
std::cout << value << std::endl;
value = 0;
}
if(value > 255) {
std::cout << value << std::endl;
value = 255;
}
int color = QColor::fromRgb(value, value, value).rgb();
this->working_copy->getImage()->setPixel(x,y,color);
}
this->setPixel(start_x, start_y, x - start_x, y - start_y);
}
};
@ -208,72 +208,121 @@ class HoughMachine {
if(new_dx < 0 && -new_dx >= new_dy) orientation_sector += 3;
return orientation_sector;
};
void drawLineBresenham(int start_x, int start_y, int end_x, int end_y) {
this->bresenham_orientation_sector = this->getOrientationSector(end_x - start_x, end_y - start_y);
std::cout << "START LINE: (" << start_x << ", " << start_y << ") -> (" << end_x << ", " << end_y << ") - O: " << this->bresenham_orientation_sector << std::endl;
// other implementation for now.
this->otherBresenham(start_x, end_x, start_y, end_y);
// Now adapt input
// Output will be adapted in setPixel()
/*switch(this->bresenham_orientation_sector) {
case 0:
// Going right, nothing to do here
this->bresenham(start_x, start_y, end_x, end_y);
break;
case 1:
// Going right, but more up
this->bresenham(start_y, start_x, end_y, end_x);
break;
case 2:
// Going left, but more up
this->bresenham(start_y, end_x, end_y, start_x);
break;
case 3:
// Going left
this->bresenham(end_x, start_y, start_x, end_y);
break;
case 4:
// Going left
this->bresenham(end_x, end_y, start_x, start_y);
break;
case 5:
// Going left, but more down
this->bresenham(end_y, end_x, start_y, start_x);
break;
case 6:
// Going right, but more down
this->bresenham(start_x, end_y, end_x, start_y);
break;
case 7:
// Going right
this->bresenham(start_x, end_y, end_x, start_y);
break;
}*/
};
void setPixel(int start_x, int start_y, int dx, int dy) {
/*void bresenham(int start_x, int start_y, int end_x, int end_y) {
std::cout << "BRESENHAM START LINE: (" << start_x << ", " << start_y << ") -> (" << end_x << ", " << end_y << ")" << std::endl;
int x, y;
int delta_x, delta_y;
int d;
int delta_ne, delta_e;
x = start_x;
y = start_y;
delta_x = end_x - start_x;
delta_y = end_y - start_y;
delta_ne = 2 * (delta_y - delta_x);
delta_e = 2 * delta_y;
d = 2 * delta_y - delta_x;
this->setPixel(start_x, start_y, x, y);
while(x < end_x) {
if(d >= 0) {
d += delta_ne; x++; y++;
} else {
d += delta_e; x++;
}
this->setPixel(start_x, start_y, x, y);
}
std::cout << std::endl;
};*/
/*void setPixel(int start_x, int start_y, int dx, int dy) {
std::cout << "setPixel(" << start_x << ", " << start_y << ", " << dx << ", " << dy << ")" << std::endl;
if(dx > start_x) dx = dx - start_x;
else dx = start_x - dx;
if(dy > start_y) dy = dy - start_y;
else dy = start_y - dy;
std::cout << " --> dx: " << dx << ", dy: " << dy << "" << std::endl;
int color = QColor::fromRgb(0, 255, 0).rgb();
//TODO: Fix this! This has to happen relative to the center point! :|
switch(this->bresenham_orientation_sector) {
case 0:
// Going right, nothing to do here
this->working_copy->getImage()->setPixel(start_x + dx, start_y + dy, color);
this->setPixelDebug(start_x + dx, start_y + dy, color);
break;
case 1:
// Going right, but more up
this->working_copy->getImage()->setPixel(start_x + dy, start_y + dx, color);
this->setPixelDebug(start_x + dy, start_y + dx, color);
break;
case 2:
// Going left, but more up
this->working_copy->getImage()->setPixel(start_x - dy, start_y + dx, color);
this->setPixelDebug(start_x - dy, start_y + dx, color);
break;
case 3:
// Going left
this->working_copy->getImage()->setPixel(start_x - dx, start_y + dy, color);
this->setPixelDebug(start_x - dx, start_y + dy, color);
break;
case 4:
// Going left
this->working_copy->getImage()->setPixel(start_x - dx, start_y - dy, color);
this->setPixelDebug(start_x - dx, start_y - dy, color);
break;
case 5:
// Going left, but more down
this->working_copy->getImage()->setPixel(start_x - dy, start_y - dx, color);
this->setPixelDebug(start_x + dy, start_y + dx, color); // ok
break;
case 6:
// Going right, but more down
this->working_copy->getImage()->setPixel(start_x + dy, start_y - dx, color);
this->setPixelDebug(start_x + dy, start_y - dx, color); // ?
break;
case 7:
// Going right
this->working_copy->getImage()->setPixel(start_x + dx, start_y - dy, color);
this->setPixelDebug(start_x + dx, start_y - dy, color);
break;
}
};
void debugShow() {
std::cout << "Dimensions: " << this->n_ang << " x " << this->n_rad << " | " << std::endl;
int max_value = 0;
for(int i=0; i<this->n_ang*this->n_rad; i++) {
int current_value = this->hough_arr[i];
if(current_value > max_value) max_value = current_value;
}
for(int x=0; x<this->n_ang; x++) {
for(int y=0; y<this->n_rad; y++) {
int value = qRound(255.0*this->hough_arr[x*this->n_ang+y] / max_value);
if(value < 0) {
std::cout << value << std::endl;
value = 0;
}
if(value > 255) {
std::cout << value << std::endl;
value = 255;
}
int color = QColor::fromRgb(value, value, value).rgb();
this->working_copy->getImage()->setPixel(x,y,color);
}
}
};
};*/
};

View File

@ -140,11 +140,17 @@ void ImageViewer::drawBlackLine() {
working_copy->getImage()->setPixel(i,i,0);
}*/
HoughMachine* hm = new HoughMachine(original, working_copy);
hm->drawLineBresenham(10, 10, 20, 20);
hm->drawLineBresenham(10, 10, 20, 10);
hm->drawLineBresenham(10, 10, 10, 20);
hm->drawLineBresenham(10, 10, 15, 20);
hm->drawLineBresenham(10, 10, 20, 15);
//star centered around 20/20
hm->drawLineBresenham(20, 20, 10, 10);
hm->drawLineBresenham(20, 20, 20, 10);
hm->drawLineBresenham(20, 20, 30, 10);
hm->drawLineBresenham(20, 20, 30, 20);
hm->drawLineBresenham(20, 20, 30, 30);
hm->drawLineBresenham(20, 20, 20, 30);
hm->drawLineBresenham(20, 20, 10, 30);
hm->drawLineBresenham(20, 20, 10, 20);
delete hm;
updateImageDisplay();
logFile << "Black line drawn." << std::endl;