diff --git a/Makefile b/Makefile index 0ecdb90..42a76a3 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CXX = clang++ -CXXFLAGS = -std=c++11 -W -Wall -Werror -O3 +CXXFLAGS = -std=c++11 -W -Wall -O3 #-Werror BUILD_DIR=/tmp all: rotation.cpp diff --git a/TODO.md b/TODO.md index e69de29..0753e9e 100644 --- a/TODO.md +++ b/TODO.md @@ -0,0 +1 @@ +[ ] Quaternions diff --git a/rotation.cpp b/rotation.cpp index 0377584..6b80264 100644 --- a/rotation.cpp +++ b/rotation.cpp @@ -215,13 +215,21 @@ struct Image { void draw_line(Image& img, unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2) { - - assert(x1 < x2); - double const slope = (double) (y2 - y1) / (double) (x2 - x1); + 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; - img.set_pixel(i, y, 255, 0, 0); // set line to red + 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; } } @@ -230,24 +238,6 @@ void draw_line(Image& img, Point const& p1, Point const& p2) draw_line(img, p1.x, p1.y, p2.x, p2.y); } -Point rotate(Image const& img, Point const& src, double radian, double const ratio) -{ - cout << "rotate (" << src.x << ", " << src.y << ") x " << radian << endl; - double x = src.x - (img.width / 2.0f); - x *= ratio; - // double y = - src.y + (src.height / 2.0f); - // y *= ratio; - - double const angle_value = acos(x) + radian; - double const cos_x = cos(angle_value); - double const sin_x = sin(angle_value); - unsigned int const new_x = ceil(cos_x / ratio); - unsigned int const new_y = ceil(sin_x / ratio); - - cout << " = (" << new_x << ", " << new_y << ")" << endl; - return Point(new_x, new_y); -} - // // @@ -256,11 +246,8 @@ Point rotate(Image const& img, Point const& src, double radian, double const rat double convert_radian(Image const& img, Point const& p, double const ratio) { - cout << "X: " << p.x << " - " << img.width / 2.0f << endl; - cout << "Y: " << - (int) p.y << " + " << img.height / 2.0f << endl; double const centered_x = p.x - (img.width / 2.0f); double const centered_y = (- (int) p.y) + (img.height / 2.0f); - cout << "centered point (" << centered_x << ", " << centered_y << ")" << endl; double const cos_value = centered_x * ratio; double const sin_value = centered_y * ratio; double angle = acos(cos_value); @@ -272,33 +259,20 @@ double convert_radian(Image const& img, Point const& p, double const ratio) APoint convert_abs_coord(double const angle, double const ratio) { - cout << "Angle: " << angle << endl; - cout << "cos: " << cos(angle) << endl; - cout << "sin: " << sin(angle) << endl; - APoint tmp((int) ceil(cos(angle) / ratio), (int) ceil(sin(angle) / ratio)); - cout << "point: " << tmp << endl; return APoint((int) ceil(cos(angle) / ratio), (int) ceil(sin(angle) / ratio)); } Point convert_img_coord(Image const& img, APoint const& p) { - cout << "image: " << img.width << " x " << img.height << endl; - cout << "p: " << p << endl; - cout << "h / 2: " << img.height / 2.0f << endl; return Point(p.x + img.width / 2.0f, - p.y + img.height / 2.0f); } double compute_ratio(Image const& img) { - cout << "Compute ratio" << endl; unsigned int const nb_points = img.width * img.height; - cout << " " << nb_points << " points" << endl; double const square_side = sqrt(nb_points) - 1; - cout << " square side: " << square_side << endl; double const half_side = square_side / 2; - cout << " half side: " << half_side << endl; unsigned int const trigo_length = (unsigned int) ceil(half_side * sqrt(2)); - cout << " trigo length: " << trigo_length << endl; return 1.0f / trigo_length; } @@ -316,7 +290,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& int min_h = 0; int max_h = 0; - cout << "Image dimensions: " << src.width << " x " << src.height << endl; + //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); @@ -324,7 +298,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& max_w = max(max_w, tl.x); min_h = min(min_h, tl.y); max_h = max(max_h, tl.y); - cout << "Rotated " << p << " (" << angle << ") = " << tl << "(" << angle + rotation << ")" << endl << endl; + //cout << "Rotated " << p << " (" << angle << ") = " << tl << "(" << angle + rotation << ")" << endl << endl; p = Point(src.width - 1, 0); angle = convert_radian(src, p, ratio); @@ -333,7 +307,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& max_w = max(max_w, tr.x); min_h = min(min_h, tr.y); max_h = max(max_h, tr.y); - cout << "Rotated " << p << " (" << angle << ") = " << tr << "(" << angle + rotation << ")" << endl << endl; + //cout << "Rotated " << p << " (" << angle << ") = " << tr << "(" << angle + rotation << ")" << endl << endl; p = Point(0, src.height - 1); angle = convert_radian(src, p, ratio); @@ -342,7 +316,7 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& max_w = max(max_w, bl.x); min_h = min(min_h, bl.y); max_h = max(max_h, bl.y); - cout << "Rotated " << p << " (" << angle << ") = " << bl << "(" << angle + rotation << ")" << endl << endl; + //cout << "Rotated " << p << " (" << angle << ") = " << bl << "(" << angle + rotation << ")" << endl << endl; p = Point(src.width - 1, src.height - 1); angle = convert_radian(src, p, ratio); @@ -351,12 +325,39 @@ void compute_output_size(Image const& src, double const rotation, unsigned int& 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; + //cout << "Rotated " << p << " (" << angle << ") = " << br << "(" << angle + rotation << ")" << endl << endl; width = max_w - min_w + 1; height = max_h - min_h + 1; } + +// +// +// Rotation +// + +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); + return convert_img_coord(rotated, a_point); +} + +Image rotate(Image const& src, double angle) +{ + // FIXME + Image rot(0, 0); + cout << angle << src.width << endl; // to remove warnings + return rot; +} + + +// +// +// Check +// + bool check_trigo() { Image square(500, 500); @@ -397,71 +398,114 @@ bool check_trigo() compute_output_size(square, rotation, w, h); // failed check: is precision an issue? -// if (!fequal(w, square.width * sqrt(2), sigma) -// || !fequal(h, square.height * sqrt(2), sigma)) -// { -// 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; -// return false; -// } - - - Image rotated(w, h); - - APoint const a_p45 = convert_abs_coord(angle + rotation, ratio); - Point const p45 = convert_img_coord(rotated, a_p45); - if (abs((float) (-1) - p45.x) > sigma) + if (false) { - cerr << "Rotation origin by 45 degrees:" << endl; - cerr << "Invalid x value: " << p45.x << " != " << -1 << endl; - cerr << "Absolute point: (" << a_p45.x << ", " << a_p45.y << ")" << endl; - return false; - } - if (abs(0.0 - p45.y) > sigma) - { - cerr << "Rotation origin by 45 degrees:" << endl; - cerr << "Invalid y value: " << p45.y << " != " << 0 << endl; - return false; + if (!fequal(w, square.width * sqrt(2), sigma) + || !fequal(h, square.height * sqrt(2), sigma)) + { + 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; + return false; + } + + + Image rotated(w, h); + + APoint 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) + { + cerr << "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) + { + cerr << "Rotation origin by 45 degrees:" << endl; + cerr << "Invalid y value: " << p45.y << " != " << 0 << endl; + return false; + } } return true; } - -// -// -// Rotation -// - -Image rotate(Image const& src, double angle) +void check_lines() { - unsigned int const nb_points = src.width * src.height; - double const square_side = sqrt(nb_points); - double const half_side = square_side / 2; - unsigned int const trigo_length = (unsigned int) ceil(half_side * sqrt(2)); - double const ratio = 1.0f / trigo_length; + 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); - // top left - double const cos_value = - (src.width / 2.0f) * ratio; -// double const sin_value = (src.height / 2.0f) * ratio; - double const angle_value = acos(cos_value); - cout << "top left angle: " << angle_value << endl; + 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); - double const radian = (angle / 360.0f) * (2 * M_PI); + 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); - // FIXME - Image rot(trigo_length, trigo_length); - Point tl = rotate(src, Point(0, 0), radian, ratio); - Point tr = rotate(src, Point(src.width - 1, 0), radian, ratio); - draw_line(rot, tl, tr); - // draw_line(rot, 0, 0, rot.width - 1, 0); - // draw_line(rot, 0, rot.height - 1, rot.width - 1, rot.height - 1); - // draw_line(rot, 0, 0, rot.width - 1, rot.height - 1); - // rot.set_pixel(rot.width - 1, rot.height - 1, 0, 255, 0); -// draw_line(rot, 0, 0, 0, rot.height - 1); -// draw_line(rot, rot.width - 1, 0, rot.width - 1, rot.height - 1); - return rot; + 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"); + } } @@ -475,6 +519,8 @@ int main() if (!check_trigo()) return 1; + check_lines(); + return 0; Image img("img/luigi.ppm");