Fix rotation by using atan2().

- Add more check on polar conversion.
master
Fabien Freling 2014-06-24 19:14:59 +02:00
parent cc7ec6fef2
commit ce3592e857
1 changed files with 103 additions and 105 deletions

View File

@ -5,6 +5,7 @@
#include <cmath>
#include <cassert>
#include <cstring>
#include <chrono>
using namespace std;
@ -252,48 +253,38 @@ DPoint convert_grid_coord(Image const& img, Point const& p)
double convert_radian(Image const& img, Point const& p, double const ratio)
{
DPoint centered = convert_grid_coord(img, p);
cout << "-> grid " << centered << endl;
double const cos_value = centered.x * ratio;
cout << "cos = " << cos_value << endl;
double const sin_value = - (centered.y * ratio);
cout << "sin = " << sin_value << endl;
double angle = acos(cos_value);
if (sin_value < 0)
{
cout << "mirror angle" << endl;
angle = (2 * M_PI) - angle;
}
cout << "radian = " << angle << endl;
cout << "acos = " << acos(cos_value) << endl;
cout << "asin = " << asin(sin_value) << endl;
cout << "revert cos = " << cos(angle) << endl;
cout << "revert sin = " << sin(angle) << endl;
cout << "full cycle sin: " << sin(asin(sin_value)) << endl;
return angle;
}
DPoint convert_abs_coord(double const angle, double const ratio)
{
return DPoint(cos(angle) / ratio, sin(angle) / ratio);
return DPoint(cos(angle) / ratio, - sin(angle) / ratio);
}
APoint convert_img_coord(Image const& img, DPoint const& p)
{
int x = round(p.x + (img.width / 2.0f) - 0.5);
int y = round(p.y + (img.height / 2.0f) - 0.5);
APoint 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 APoint(x, y);
}
return p_mapped;
void convert_abs_to_polar_coord(DPoint const& p, double& angle, double& dist)
{
angle = atan2(p.y, p.x);
dist = sqrt(p.x * p.x + p.y * p.y);
}
DPoint convert_polar_to_grid_coord(double const angle, double const distance)
{
return DPoint(cos(angle) * distance, (sin(angle) * distance));
}
double compute_ratio(Image const& img)
@ -325,10 +316,10 @@ void compute_output_size(Image const& src, double const rotation, unsigned int&
min_h = min(min_h, tl.y);
max_h = max(max_h, tl.y);
// debug print
if (rotation == 0.0)
{
cout << "Rotated " << p << " = " << tl << endl << endl;
}
// if (rotation == 0.0)
// {
// cout << "Rotated " << p << " = " << tl << endl << endl;
// }
p = Point(src.width - 1, 0);
angle = convert_radian(src, p, ratio);
@ -337,10 +328,10 @@ 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);
if (rotation == 0.0)
{
cout << "Rotated " << p << " = " << tr << endl << endl;
}
// if (rotation == 0.0)
// {
// cout << "Rotated " << p << " = " << tr << endl << endl;
// }
p = Point(0, src.height - 1);
angle = convert_radian(src, p, ratio);
@ -349,10 +340,10 @@ 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);
if (rotation == 0.0)
{
cout << "Rotated " << p << " = " << bl << endl << endl;
}
// if (rotation == 0.0)
// {
// cout << "Rotated " << p << " = " << bl << endl << endl;
// }
p = Point(src.width - 1, src.height - 1);
angle = convert_radian(src, p, ratio);
@ -361,30 +352,15 @@ 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);
if (rotation == 0.0)
{
cout << "Rotated " << p << " = " << br << endl << endl;
}
// if (rotation == 0.0)
// {
// cout << "Rotated " << p << " = " << br << endl << endl;
// }
width = (int) (max_w - min_w) + 1;
height = (int) (max_h - min_h) + 1;
}
void convert_abs_to_polar_coord(DPoint const& p, double const ratio, double& angle, double& dist)
{
double const cos_value = p.x * ratio;
double const sin_value = - (p.y * ratio);
angle = acos(cos_value);
if (sin_value < 0)
angle = 2 * M_PI - angle;
dist = sqrt(p.x * p.x + p.y * p.y);
}
DPoint convert_polar_to_grid_coord(double const angle, double const distance)
{
return DPoint(cos(angle) * distance, - (sin(angle) * distance));
}
//
@ -399,6 +375,8 @@ APoint rotate(Image const& src, Point const& p, double const ratio, double const
return convert_img_coord(rotated, a_point);
}
//
//
// Drawing
@ -488,13 +466,11 @@ Image rotate(Image const& src, double angle)
Image rotated(w, h);
// debug print
if (rotation == 0.0)
{
cout << "src dimensions: " << src.width << " x " << src.height << endl;
cout << "rotated dimensions: " << w << " x " << h << endl;
}
double const ratio = compute_ratio(src);
// if (rotation == 0.0)
// {
// cout << "src dimensions: " << src.width << " x " << src.height << endl;
// cout << "rotated dimensions: " << w << " x " << h << endl;
// }
for (int y = 0; y < (int) rotated.height; ++y)
{
@ -504,16 +480,27 @@ Image rotate(Image const& src, double angle)
DPoint const d = convert_grid_coord(rotated, p);
double p_angle = 0;
double dist = 0;
convert_abs_to_polar_coord(d, ratio, p_angle, dist);
DPoint const src_rotated_point = convert_polar_to_grid_coord(p_angle - rotation, dist);
convert_abs_to_polar_coord(d, p_angle, dist);
DPoint const src_rotated_point = convert_polar_to_grid_coord(p_angle + rotation, dist);
// FIXME: get source points
APoint src_p = convert_img_coord(src, src_rotated_point);
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
// FIXME: bypass variables, access src pixels
src.get_pixel(src_p, r, g, b);
rotated.set_pixel(p, r, g, b);
// debug print
// if (rotation == 0.0)
// {
// if (x == 0)
// {
// cout << p << " -> " << d << " -> polar(" << p_angle << ", " << dist << ")";
// cout <<" -> " << src_rotated_point << " -> " << src_p << endl;
// }
// }
}
}
@ -542,33 +529,31 @@ bool check_trigo()
{
Image square(500, 500);
double const ratio = compute_ratio(square);
cout << "ratio: " << ratio << endl;
double const sigma = 1.0e-2;
if (!fequal(ratio, 1 / 707.106, sigma))
{
cerr << __LINE__ << " | Invalid ratio: " << ratio << " != " << 1 / 707.106 << endl;
return false;
}
// 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 (!fequal(angle, 3 * M_PI / 4, sigma))
{
cout << __LINE__ << " | Invalid angle value: " << angle << " != " << 3 * M_PI / 4 << endl;
cerr << __LINE__ << " | Invalid angle value: " << angle << " != " << 3 * M_PI / 4 << endl;
return false;
}
// Check that we can reverse the origin point.
DPoint const abs_reverse_point = convert_abs_coord(angle, ratio);
cout << "reversed abs origin: " << abs_reverse_point << endl;
APoint const reverse_point = convert_img_coord(square, abs_reverse_point);
cout << "reversed origin in square: " << reverse_point << endl;
if (abs(0.0 - reverse_point.x) > sigma)
if (!fequal(0.0, reverse_point.x, sigma)
|| !fequal(0.0, reverse_point.y, sigma))
{
cerr << "Reverse origin:" << endl;
cout << "Invalid x value: " << reverse_point.x << " != " << 0 << endl;
return false;
}
if (abs(0.0 - reverse_point.y) > sigma)
{
cerr << "Reverse origin:" << endl;
cout << "Invalid y value: " << reverse_point.y << " != " << 0 << endl;
cerr << __LINE__ << "Reverse origin fail" << endl;
cerr << " " << reverse_point << " != (0, 0)" << endl;
cerr << " abs point " << abs_reverse_point << endl;
return false;
}
@ -578,36 +563,49 @@ bool check_trigo()
unsigned int h = 0;
compute_output_size(square, rotation, w, h);
// failed check: is precision an issue?
if (true)
if (!fequal(w, square.width * sqrt(2), sigma * square.width)
|| !fequal(h, square.height * sqrt(2), sigma * square.height))
{
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;
return false;
}
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);
Image rotated(w, h);
DPoint const a_p45 = convert_abs_coord(angle + rotation, ratio);
APoint const p45 = convert_img_coord(rotated, a_p45);
if (!fequal(0, p45.x, sigma))
DPoint const a_p45 = convert_abs_coord(angle + rotation, ratio);
APoint const p45 = convert_img_coord(rotated, a_p45);
if (!fequal(0, p45.x, sigma))
{
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 (!fequal(p45.y, (h - 1) / 2.0f, sigma))
{
cerr << __LINE__ << " > Rotation origin by 45 degrees:" << endl;
cerr << "Invalid y value: " << p45.y << " != " << (h - 1) / 2.0f << endl;
cerr << " absolute point: " << a_p45 << endl;
cerr << " relative point: " << p45 << endl;
return false;
}
// Polar coordinates
{
DPoint const d(-42.5, 37.5);
double angle = 0;
double dist = 0;
convert_abs_to_polar_coord(d, angle, dist);
DPoint const reversed = convert_polar_to_grid_coord(angle, dist);
if (!fequal(d.x, reversed.x, sigma)
|| !fequal(d.y, reversed.y, sigma))
{
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 (!fequal(p45.y, h / 2.0f - 1, sigma))
{
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;
cerr << __LINE__ << " > Reverse polar coordinates:" << endl;
cerr << reversed << " != " << d << endl;
cerr << "polar (" << angle << ", " << dist << ")" << endl;
return false;
}
}
@ -643,7 +641,7 @@ void check_lines()
int main()
{
bool perform_check = false;
bool perform_check = true;
if (perform_check)
{
@ -653,12 +651,12 @@ int main()
if (!check_trigo())
return 1;
check_lines();
//check_lines();
}
Image img("img/luigi.ppm");
for (double rotation : {0, 15, 30})
for (double rotation : {0, 1, 5, 15, 30, 45, 60, 75, 90, 110, 140, 160, 180})
{
auto const before = chrono::high_resolution_clock::now();
@ -666,7 +664,7 @@ int main()
auto const after = chrono::high_resolution_clock::now();
auto const duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(after - before);
cout << "rotate(): " << duration_ms.count() << " ms" << endl;
cout << "rotate(" << rotation << "): " << duration_ms.count() << " ms" << endl;
stringstream filename;
filename << "rotated_" << rotation << ".ppm";