1

Qt对透视和仿射变换使用 3x3 变换矩阵。如果矩阵的最后一行等于 [0 0 1],则认为矩阵是仿射的。出于这个原因,x 轴和 y 轴旋转矩阵是“非仿射的”并且会导致透视失真。但还有进一步的影响。用户绘制的每个像素都有坐标 (x, y, 1)。无法将 z 坐标设置为 0,之后旋转并将 z 设置为其他值。是否仍有可能以某种方式伪造能够绕任意点旋转?也许通过将 z 设置为某个值“接近”为零然后旋转?

编辑:

我想做什么,确切地说。通过划掉“通常”透视投影矩阵的第 3 行和第 3 列,可以得到一个可用于Qt. 我还使用窗口矩阵转换为QGraphicsItem. 现在有 2 个矩阵:

window * projection

一些代码:

float const w(rect.width());
float const h(rect.height());

// aspect ratio
auto const ar(h / w);

// symmetrical infinite frustum
f_[4] = 1.f;
f_[1] = ::std::tan(.5f * hFov_) * f_[4];
f_[0] = -f_[1];
f_[3] = ar * f_[1];
f_[2] = -f_[3];

// perspective projection matrix
auto const rml(f_[1] - f_[0]);
auto const tmb(f_[3] - f_[2]);

::etl::matrix<float, 3, 3> const pMatrix{
  2.f * f_[4] / rml, 0.f              , (f_[1] + f_[0]) / rml,
  0.f              , 2.f * f_[4] / tmb, (f_[3] + f_[2]) / tmb,
  0.f              , 0.f              , -1.f};

auto const halfW(.5f * w);
auto const halfH(.5f * h);

// window matrix
::etl::matrix<float, 3, 3> const wMatrix{
  halfW, 0.f   , halfW,
  0.f  , -halfH, halfH,
  0.f  , 0.f   , 1.f};

wpvMatrix_ = wMatrix * pMatrix;

现在我们想将 aQPixmap转变为我们投影的世界。这是完成的,例如使用这个矩阵:

zMatrix =
  worldPixmapWidth / pixmap.width(), 0, p(0),
  0, -worldPixmapHeight / pixmap.height(), p(1),
  0, 0, p(2);

所以我们可以用它wMatrix * pMatrix * zMatrix来变换像素图。

p是我们要翻译的点QPixmap。现在像素图位于p = (p(0), p(1), p(2)). 我们想绕平行于 y 轴的轴旋转,经过p. 怎么做?通常,我们会这样做T(p) * Ry(phi) * T(-p),但我们不能,因为没有可以设置z坐标的平移矩阵。直接应用Ry将围绕原点旋转。即使根本不做任何转换,z也将是1,不是0,因为我们希望它是Ry

4

1 回答 1

2

如何围绕某个点旋转顶点?

执行平移、旋转和向后平移的顺序将有效地围绕您平移的任何点进行旋转。

这同样适用于矩阵变换以及它们被调用和应用的顺序。

更新:

再看了一遍你的问题,听起来你真正要求的是a的3D投影QPixmap来模拟它在翻转的过程中,就像纸牌游戏中的纸牌翻转一样。这种围绕平行于 y 轴的某个轴的旋转需要显示适当的失真,以使其看起来像图像的一侧更远而另一侧更近。

http://en.wikipedia.org/wiki/3D_projection

在此处输入图像描述

Qt 的旋转计划使用上面等式中的第三个 3x3 矩阵。绕 z 轴旋转。

或者更具体地说,你可以在这里看到它:

在此处输入图像描述

http://qt-project.org/doc/qt-5.1/qtgui/qmatrix.html#details,它显示了带有添加行的 2x2 矩阵以解释翻译。

现在说了这么多......这是我能找到的最接近的东西,可以在不使用完整的 3D 图形库(如 OpenGL)的情况下伪造它:

http://qt-project.org/forums/viewthread/18615/

http://gerrysweeney.com/horizo​​ntal-and-vertical-flip-transformations-of-a-qgraphicsitem-in-qt-qgraphicsview/

主文件

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtGui>
#include <QPropertyAnimation>

class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QGraphicsScene * m_pScene;
    QGraphicsView * m_pView;

    QSlider * sliderx;
    QSlider * slidery;
    QGraphicsPolygonItem* transformedItem;
    QPointF itemCenter;

public slots:
    void updateRotation();

};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setCentralWidget(new QWidget);
    QVBoxLayout * layout = new QVBoxLayout;


    m_pScene = new QGraphicsScene(0,0,800,480);

    m_pView = new QGraphicsView(m_pScene);

    m_pView->setFrameStyle(QFrame::NoFrame);
    m_pView->setGeometry(0,0,800,480);
    m_pView->setAutoFillBackground(false);

    layout->addWidget(m_pView);

    sliderx = new QSlider(Qt::Horizontal);
    slidery = new QSlider(Qt::Horizontal);

    sliderx->setRange(-100,100);
    sliderx->setSingleStep(1);
    sliderx->setValue(100);
    slidery->setRange(-100,100);
    slidery->setSingleStep(1);
    slidery->setValue(100);
    QObject::connect(sliderx, SIGNAL(valueChanged(int)),this, SLOT(updateRotation()));
    QObject::connect(slidery, SIGNAL(valueChanged(int)),this, SLOT(updateRotation()));


    layout->addWidget(sliderx);
    layout->addWidget(slidery);
    this->centralWidget()->setLayout(layout);
    QPolygonF polygon;
    polygon << QPointF(100.0,250.0);
    polygon << QPointF(170.0, 350.0);
    polygon << QPointF(30.0, 350.0);
    QGraphicsPolygonItem* testItem = new QGraphicsPolygonItem(polygon);
    m_pScene->addItem(testItem);

    transformedItem = new QGraphicsPolygonItem(polygon);
    transformedItem->setPen(QColor(Qt::red));
    m_pScene->addItem(transformedItem);


    // Here the fun starts:

    itemCenter = transformedItem->mapToParent(transformedItem->boundingRect().center());

//    // Method 1
//    QTransform transform = QTransform();
//    transform.translate(itemCenter.x(),
//                        itemCenter.y());
//    transform.scale(1.0, -1.0);
//    transform.translate(-itemCenter.x(),
//                        -itemCenter.y());
//    transformedItem->setTransform(transform);

//    // Method 2
//    transformedItem->setTransform(QTransform::fromTranslate(itemCenter.x(),
//                                                            itemCenter.y()),true);
//    transformedItem->setTransform(QTransform::fromScale(1.0, -1.0),true);
//    transformedItem->setTransform(QTransform::fromTranslate(-itemCenter.x(),
//                                                            -itemCenter.y()), true);

//    // Method 3
//    transformedItem->translate(itemCenter.x(),
//                               itemCenter.y());
//    transformedItem->scale(1.0, -1.0);
//    transformedItem->translate(-itemCenter.x(),
//                               -itemCenter.y());
}

void MainWindow::updateRotation()
{
    transformedItem->resetTransform();
    transformedItem->translate(itemCenter.x(),
                               itemCenter.y());
    transformedItem->scale((qreal)sliderx->value()/100., (qreal)slidery->value()/100.);
    transformedItem->translate(-itemCenter.x(),
                               -itemCenter.y());
}

MainWindow::~MainWindow() { }

希望有帮助。

于 2013-10-16T15:14:45.140 回答