Skip to content

Commit

Permalink
Merge pull request #71 from matwey/fix/y-pixel-transform
Browse files Browse the repository at this point in the history
Fix WidgetToFitsOpenGLTransform in vertical direction
  • Loading branch information
hombit authored Nov 18, 2017
2 parents 0a0d1ff + 1840c31 commit 0f75a88
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/opengltransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void WidgetToFitsOpenGLTransform::updateTransform() const {
/* world unrotated */
matrix_.rotate(-angle_, static_cast<float>(0), static_cast<float>(0), static_cast<float>(1));
/* viewrect to world */
matrix_.translate(viewrect_.center().x(), viewrect_.center().y());
matrix_.translate(viewrect_.center().x(), -viewrect_.center().y());
matrix_.scale(viewrect_.width()/static_cast<float>(2), -viewrect_.height()/static_cast<float>(2));

/* widget pixel (0,0, w,h) to viewrect (-1,-1, 2,2)*/
Expand Down
108 changes: 108 additions & 0 deletions test/opengltransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ class TestWidgetToFitsOpenGLTransform: public QObject
Q_OBJECT
private slots:
void test_1x1();
void test_1x1a();
void test_1x1_rot90();
void test_1x1_vr2();
void test_1x1_vr2a();
void test_1x2();
};

Expand Down Expand Up @@ -47,6 +50,46 @@ void TestWidgetToFitsOpenGLTransform::test_1x1() {
}
}

void TestWidgetToFitsOpenGLTransform::test_1x1a() {
const QSize image_size{6, 3};
const QSize widget_size{6, 3};
const QRectF view_rect{-1, -0.5, 2, 1};

OpenGLPlane p(image_size);
WidgetToFitsOpenGLTransform t(image_size, p.scale(), widget_size, view_rect);

{
auto f = t.transform(0, 0);
QCOMPARE(static_cast<int>(f.x()), 0);
QCOMPARE(static_cast<int>(f.y()), 2);
}
{
auto f = t.transform(1, 1);
QCOMPARE(static_cast<int>(f.x()), 1);
QCOMPARE(static_cast<int>(f.y()), 1);
}
{
auto f = t.transform(2, 2);
QCOMPARE(static_cast<int>(f.x()), 2);
QCOMPARE(static_cast<int>(f.y()), 0);
}
{
auto f = t.transform(0, 2);
QCOMPARE(static_cast<int>(f.x()), 0);
QCOMPARE(static_cast<int>(f.y()), 0);
}
{
auto f = t.transform(2, 0);
QCOMPARE(static_cast<int>(f.x()), 2);
QCOMPARE(static_cast<int>(f.y()), 2);
}
{
auto f = t.transform(5, 0);
QCOMPARE(static_cast<int>(f.x()), 5);
QCOMPARE(static_cast<int>(f.y()), 2);
}
}

void TestWidgetToFitsOpenGLTransform::test_1x1_rot90() {
const QSize image_size{3, 3};
const QSize widget_size{3, 3};
Expand Down Expand Up @@ -83,6 +126,71 @@ void TestWidgetToFitsOpenGLTransform::test_1x1_rot90() {
}
}

void TestWidgetToFitsOpenGLTransform::test_1x1_vr2() {
const QSize image_size{4, 4};
const QSize widget_size{2, 2};
const QRectF view_rect{-1, -1, 1, 1};

OpenGLPlane p(image_size);
WidgetToFitsOpenGLTransform t(image_size, p.scale(), widget_size, view_rect);

{
auto f = t.transform(0, 0);
QCOMPARE(static_cast<int>(f.x()), 0);
QCOMPARE(static_cast<int>(f.y()), 3);
}
{
auto f = t.transform(0, 1);
QCOMPARE(static_cast<int>(f.x()), 0);
QCOMPARE(static_cast<int>(f.y()), 2);
}
{
auto f = t.transform(1, 0);
QCOMPARE(static_cast<int>(f.x()), 1);
QCOMPARE(static_cast<int>(f.y()), 3);
}
{
auto f = t.transform(1, 1);
QCOMPARE(static_cast<int>(f.x()), 1);
QCOMPARE(static_cast<int>(f.y()), 2);
}
}

void TestWidgetToFitsOpenGLTransform::test_1x1_vr2a() {
const QSize image_size{8, 4};
const QSize widget_size{4, 2};
const QRectF view_rect{-1, -0.5, 1, 0.5};

OpenGLPlane p(image_size);
WidgetToFitsOpenGLTransform t(image_size, p.scale(), widget_size, view_rect);

{
auto f = t.transform(0, 0);
QCOMPARE(static_cast<int>(f.x()), 0);
QCOMPARE(static_cast<int>(f.y()), 3);
}
{
auto f = t.transform(0, 1);
QCOMPARE(static_cast<int>(f.x()), 0);
QCOMPARE(static_cast<int>(f.y()), 2);
}
{
auto f = t.transform(2, 0);
QCOMPARE(static_cast<int>(f.x()), 2);
QCOMPARE(static_cast<int>(f.y()), 3);
}
{
auto f = t.transform(2, 1);
QCOMPARE(static_cast<int>(f.x()), 2);
QCOMPARE(static_cast<int>(f.y()), 2);
}
{
auto f = t.transform(3, 1);
QCOMPARE(static_cast<int>(f.x()), 3);
QCOMPARE(static_cast<int>(f.y()), 2);
}
}

void TestWidgetToFitsOpenGLTransform::test_1x2() {
const QSize image_size{3, 3};
const QSize widget_size{6, 6};
Expand Down

0 comments on commit 0f75a88

Please sign in to comment.