diff --git a/rotation.cpp b/rotation.cpp index 6b80264..cf0a220 100644 --- a/rotation.cpp +++ b/rotation.cpp @@ -1,12 +1,14 @@ #include #include #include +#include #include #include #include using namespace std; + template struct TPoint { T x; @@ -99,6 +101,12 @@ struct Image { void set_pixel(unsigned int x, unsigned int y, uint8_t r, uint8_t g, uint8_t b) { + if (x >= width || y >= height) + { + cerr << "Point (" << x << ", " << y << ") out of bounds" << endl; + cerr << " Image dimensions: " << width << " x " << height << endl; + assert(false); + } int const index = y * width + x; r_chan[index] = r; g_chan[index] = g; @@ -208,48 +216,22 @@ struct Image { }; -// -// -// Drawing -// - -void draw_line(Image& img, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) -{ - if (x1 > x2) - { - swap(x1, x2); - swap(y1, y2); - } - double const slope = ((double) y2 - y1) / ((double) x2 - x1); - cout << "draw slope = " << slope << endl; - unsigned int previous_y = y1; - for (unsigned int i = x1; i <= x2; ++i) - { - unsigned int y = slope * (i - x1) + y1; - int inc = slope > 0 ? 1 : -1; - for (unsigned int runner = previous_y; runner != y; runner+= inc) - img.set_pixel(i, runner, 255, 0, 0); // set line to red - previous_y = y; - } -} - -void draw_line(Image& img, Point const& p1, Point const& p2) -{ - draw_line(img, p1.x, p1.y, p2.x, p2.y); -} - // // // Trigonometry // +DPoint convert_grid_coord(Image const& img, Point const& p) +{ + return DPoint(p.x - img.width / 2.0f + 0.5, p.y - img.height / 2.0f + 0.5); +} + double convert_radian(Image const& img, Point const& p, double const ratio) { - double const centered_x = p.x - (img.width / 2.0f); - double const centered_y = (- (int) p.y) + (img.height / 2.0f); - double const cos_value = centered_x * ratio; - double const sin_value = centered_y * ratio; + DPoint centered = convert_grid_coord(img, p); + double const cos_value = centered.x * ratio; + double const sin_value = - (centered.y * ratio); double angle = acos(cos_value); if (sin_value < 0) angle = 2 * M_PI - angle; @@ -257,14 +239,26 @@ double convert_radian(Image const& img, Point const& p, double const ratio) return angle; } -APoint convert_abs_coord(double const angle, double const ratio) +DPoint convert_abs_coord(double const angle, double const ratio) { - return APoint((int) ceil(cos(angle) / ratio), (int) ceil(sin(angle) / ratio)); + return DPoint(cos(angle) / ratio, - (sin(angle) / ratio)); } -Point convert_img_coord(Image const& img, APoint const& p) +Point convert_img_coord(Image const& img, DPoint const& p) { - return Point(p.x + img.width / 2.0f, - p.y + img.height / 2.0f); + int x = round(p.x + (img.width / 2.0f) - 0.5); + int y = round(p.y + (img.height / 2.0f) - 0.5); + Point p_mapped(x, y); +// if (p_mapped.x >= img.width || p_mapped.y >= img.height) +// { +// cerr << "Point coord mapping" << endl; +// cerr << " Input point:" << p << endl; +// cerr << " Point " << p_mapped << " out of bounds" << endl; +// cerr << " Image dimensions: " << img.width << " x " << img.height << endl; +// assert(false); +// } + + return p_mapped; } double compute_ratio(Image const& img) @@ -272,7 +266,7 @@ double compute_ratio(Image const& img) unsigned int const nb_points = img.width * img.height; double const square_side = sqrt(nb_points) - 1; double const half_side = square_side / 2; - unsigned int const trigo_length = (unsigned int) ceil(half_side * sqrt(2)); + double const trigo_length = ceil(half_side * sqrt(2)); return 1.0f / trigo_length; } @@ -285,15 +279,15 @@ bool fequal(float a, float b, float sigma) void compute_output_size(Image const& src, double const rotation, unsigned int& width, unsigned int& height) { double const ratio = compute_ratio(src); - int min_w = 0; - int max_w = 0; - int min_h = 0; - int max_h = 0; + double min_w = 0; + double max_w = 0; + double min_h = 0; + double max_h = 0; //cout << "Image dimensions: " << src.width << " x " << src.height << endl; Point p(0, 0); double angle = convert_radian(src, p, ratio); - APoint tl = convert_abs_coord(angle + rotation, ratio); + DPoint tl = convert_abs_coord(angle + rotation, ratio); min_w = min(min_w, tl.x); max_w = max(max_w, tl.x); min_h = min(min_h, tl.y); @@ -302,7 +296,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& p = Point(src.width - 1, 0); angle = convert_radian(src, p, ratio); - APoint tr = convert_abs_coord(angle + rotation, ratio); + DPoint tr = convert_abs_coord(angle + rotation, ratio); min_w = min(min_w, tr.x); max_w = max(max_w, tr.x); min_h = min(min_h, tr.y); @@ -311,7 +305,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& p = Point(0, src.height - 1); angle = convert_radian(src, p, ratio); - APoint bl = convert_abs_coord(angle + rotation, ratio); + DPoint bl = convert_abs_coord(angle + rotation, ratio); min_w = min(min_w, bl.x); max_w = max(max_w, bl.x); min_h = min(min_h, bl.y); @@ -320,15 +314,15 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& p = Point(src.width - 1, src.height - 1); angle = convert_radian(src, p, ratio); - APoint br = convert_abs_coord(angle + rotation, ratio); + DPoint br = convert_abs_coord(angle + rotation, ratio); min_w = min(min_w, br.x); max_w = max(max_w, br.x); min_h = min(min_h, br.y); max_h = max(max_h, br.y); //cout << "Rotated " << p << " (" << angle << ") = " << br << "(" << angle + rotation << ")" << endl << endl; - width = max_w - min_w + 1; - height = max_h - min_h + 1; + width = (int) (max_w - min_w) + 1; + height = (int) (max_h - min_h) + 1; } @@ -340,7 +334,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& Point rotate(Image const& src, Point const& p, double const ratio, double const rotation, Image const& rotated) { double angle = convert_radian(src, p, ratio); - APoint a_point = convert_abs_coord(angle + rotation, ratio); + DPoint a_point = convert_abs_coord(angle + rotation, ratio); return convert_img_coord(rotated, a_point); } @@ -353,28 +347,102 @@ Image rotate(Image const& src, double angle) } + +// +// +// Drawing +// + +void draw_line(Image& img, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) +{ + if (x1 > x2) + { + swap(x1, x2); + swap(y1, y2); + } + unsigned int const y_min = min(y1, y2); + unsigned int const y_max = max(y1, y2); + double const slope = ((double) y2 - y1) / ((double) x2 - x1); + int y_inc = slope > 0 ? 1 : -1; + + unsigned int previous_y = y1; + for (unsigned int i = x1; i <= x2; ++i) + { + unsigned int y = slope * (i - x1) + y1; + y = min(y, y_max); + y = max(y, y_min); + for (unsigned int runner = previous_y; runner != y; runner+= y_inc) + img.set_pixel(i, runner, 255, 0, 0); // set line to red + previous_y = y; + } +} + +void draw_line(Image& img, Point const& p1, Point const& p2) +{ + draw_line(img, p1.x, p1.y, p2.x, p2.y); +} + +void draw_outline(Image const& input, unsigned int degrees, string const& name) +{ + double const rotation = (degrees / 180.0f) * M_PI; + unsigned int w = 0; + unsigned int h = 0; + compute_output_size(input, rotation, w, h); + cout << "rotation(" << degrees << ") -> " << w << " x " << h << endl; + Image rotated(w, h); + + double const ratio = compute_ratio(input); + Point tl = rotate(input, Point(0, 0), ratio, rotation, rotated); + Point tr = rotate(input, Point(input.width - 1, 0), ratio, rotation, rotated); + Point bl = rotate(input, Point(0, input.height - 1), ratio, rotation, rotated); + Point br = rotate(input, Point(input.width - 1, input.height - 1), ratio, rotation, rotated); + cout << tl << " " << tr << " " << bl << " " << br << endl; + + draw_line(rotated, tl, tr); + draw_line(rotated, tr, br); + draw_line(rotated, br, bl); + draw_line(rotated, bl, tl); + + stringstream ss; + ss << "check_lines_" << name << "_" << degrees << ".ppm"; + rotated.save(ss.str()); +} + + // // // Check // +bool check_points() +{ + Image five(5, 5); + Point origin(0, 0); + DPoint d1 = convert_grid_coord(five, origin); + assert(d1.x == -2); + assert(d1.y == -2); + + return true; +} + bool check_trigo() { Image square(500, 500); double const ratio = compute_ratio(square); + cout << "ratio: " << ratio << endl; // Check that the origin of a square image is at sqrt(2) / 2 double const angle = convert_radian(square, Point(0, 0), ratio); double const sigma = 1.0e-2; - if (abs(angle - (3 * M_PI / 4)) > sigma) + if (!fequal(angle, 3 * M_PI / 4, sigma)) { - cout << "Invalid angle value: " << angle << " != " << 3 * M_PI / 4 << endl; + cout << __LINE__ << " | Invalid angle value: " << angle << " != " << 3 * M_PI / 4 << endl; return false; } // Check that we can reverse the origin point. - APoint const abs_reverse_point = convert_abs_coord(angle, ratio); + DPoint const abs_reverse_point = convert_abs_coord(angle, ratio); cout << "reversed abs origin: " << abs_reverse_point << endl; Point const reverse_point = convert_img_coord(square, abs_reverse_point); cout << "reversed origin in square: " << reverse_point << endl; @@ -398,10 +466,10 @@ bool check_trigo() compute_output_size(square, rotation, w, h); // failed check: is precision an issue? - if (false) + if (true) { - if (!fequal(w, square.width * sqrt(2), sigma) - || !fequal(h, square.height * sqrt(2), sigma)) + if (!fequal(w, square.width * sqrt(2), sigma * square.width) + || !fequal(h, square.height * sqrt(2), sigma * square.height)) { cerr << "Invalid rotated image dimensions " << w << " x " << h << endl; cerr << " expected " << (int) ceil(square.width * sqrt(2)) << " x " << (int) ceil(square.height * sqrt(2)) << endl; @@ -411,20 +479,22 @@ bool check_trigo() Image rotated(w, h); - APoint const a_p45 = convert_abs_coord(angle + rotation, ratio); + DPoint const a_p45 = convert_abs_coord(angle + rotation, ratio); Point const p45 = convert_img_coord(rotated, a_p45); - if (abs((float) (0) - p45.x) > sigma) + if (!fequal(0, p45.x, sigma)) { - cerr << "Rotation origin by 45 degrees:" << endl; + cerr << __LINE__ << " > Rotation origin by 45 degrees:" << endl; cerr << " invalid x value: " << p45.x << " != " << 0 << endl; cerr << " absolute point: " << a_p45 << endl; cerr << " relative point: " << p45 << endl; return false; } - if (abs(0.0 - p45.y) > sigma) + if (!fequal(p45.y, h / 2.0f - 1, sigma)) { - cerr << "Rotation origin by 45 degrees:" << endl; - cerr << "Invalid y value: " << p45.y << " != " << 0 << endl; + cerr << __LINE__ << " > Rotation origin by 45 degrees:" << endl; + cerr << "Invalid y value: " << p45.y << " != " << h / 2.0f - 1 << endl; + cerr << " absolute point: " << a_p45 << endl; + cerr << " relative point: " << p45 << endl; return false; } } @@ -435,80 +505,20 @@ bool check_trigo() void check_lines() { Image const square(500, 500); - double const ratio = compute_ratio(square); - unsigned int w = 0; - unsigned int h = 0; - { - double const rotation = M_PI / 8; // 22 degrees - compute_output_size(square, rotation, w, h); - Image rotated(w, h); + draw_outline(square, 22, "square"); + draw_outline(square, 33, "square"); + draw_outline(square, 45, "square"); + draw_outline(square, 90, "square"); - Point tl = rotate(square, Point(0, 0), ratio, rotation, rotated); - Point tr = rotate(square, Point(square.width - 1, 0), ratio, rotation, rotated); - Point bl = rotate(square, Point(0, square.height - 1), ratio, rotation, rotated); - Point br = rotate(square, Point(square.width - 1, square.height - 1), ratio, rotation, rotated); - - draw_line(rotated, tl, tr); - draw_line(rotated, tr, br); - draw_line(rotated, br, bl); - draw_line(rotated, bl, tl); - - rotated.save("check_lines_01.ppm"); - } - { - double const rotation = M_PI / 6; // 33 degrees - compute_output_size(square, rotation, w, h); - Image rotated(w, h); - - Point tl = rotate(square, Point(0, 0), ratio, rotation, rotated); - Point tr = rotate(square, Point(square.width - 1, 0), ratio, rotation, rotated); - Point bl = rotate(square, Point(0, square.height - 1), ratio, rotation, rotated); - Point br = rotate(square, Point(square.width - 1, square.height - 1), ratio, rotation, rotated); - - draw_line(rotated, tl, tr); - draw_line(rotated, tr, br); - draw_line(rotated, br, bl); - draw_line(rotated, bl, tl); - - rotated.save("check_lines_02.ppm"); - } - { - double const rotation = M_PI / 4; // 45 degrees - compute_output_size(square, rotation, w, h); - Image rotated(w, h); - - Point tl = rotate(square, Point(0, 0), ratio, rotation, rotated); - Point tr = rotate(square, Point(square.width - 1, 0), ratio, rotation, rotated); - Point bl = rotate(square, Point(0, square.height - 1), ratio, rotation, rotated); - Point br = rotate(square, Point(square.width - 1, square.height - 1), ratio, rotation, rotated); - - draw_line(rotated, tl, tr); - draw_line(rotated, tr, br); - draw_line(rotated, br, bl); - draw_line(rotated, bl, tl); - - rotated.save("check_lines_03.ppm"); - } - { - double const rotation = M_PI / 2; // 90 degrees - compute_output_size(square, rotation, w, h); - Image rotated(w, h); - - Point tl = rotate(square, Point(0, 0), ratio, rotation, rotated); - Point tr = rotate(square, Point(square.width - 1, 0), ratio, rotation, rotated); - Point bl = rotate(square, Point(0, square.height - 1), ratio, rotation, rotated); - Point br = rotate(square, Point(square.width - 1, square.height - 1), ratio, rotation, rotated); - - draw_line(rotated, tl, tr); - draw_line(rotated, tr, br); - draw_line(rotated, br, bl); - draw_line(rotated, bl, tl); - - rotated.save("check_lines_04.ppm"); - } + Image const rect1(640, 480); + draw_outline(rect1, 22, "rect1"); + draw_outline(rect1, 33, "rect1"); + draw_outline(rect1, 45, "rect1"); + draw_outline(rect1, 90, "rect1"); } + // // // Main @@ -516,6 +526,8 @@ void check_lines() int main() { + check_points(); + if (!check_trigo()) return 1;