1

我需要有人帮助我。大约 10 年前,我编写了使 3d 字母 T 旋转的 Delphi 代码。最近我用java重写了那个程序,但我不知道数学是如何工作的。如果有人熟悉四元数旋转,请您解释一下程序中的数学是如何工作的。p 数组包含字母 T 每一侧的所有端点。其中一些相互重复只是为了可以在 for 循环中绘制字母 T。我只需要知道什么是常数'a',为什么我两次调用方法'qm',以及它的作用。

提前感谢您提供任何可能的帮助。

package quaternion;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class FourPoint{
    public double x, y, z, w;

}

class RotationPanel extends JPanel{
    private FourPoint[] p, pdraw;
    private int i, j;
    private FourPoint o,neo,m,h;
    final double a = Math.sqrt(1/(0.25+1+1.0/9));
    static int count= 0;
    public RotationPanel(){
        o = new FourPoint();
        neo = new FourPoint();
        m = new FourPoint();
        h = new FourPoint();
        p = new FourPoint[33];
        pdraw = new FourPoint[33];
        for (int i = 0; i < 33; i++){
            p[i] = new FourPoint();
            pdraw[i] = new FourPoint();
        }
        p[0].x = 0;
        p[0].y = 0;
        p[0].z = -40;
        p[0].w = 0;

        p[1].x = 0;
        p[1].y = 0;
        p[1].z = 0;
        p[1].w = 0;

        p[2].x = -100;
        p[2].y = 0;
        p[2].z = 0;
        p[2].w = 0;

        p[3].x = -100;
        p[3].y = 0;
        p[3].z = -40;
        p[3].w = 0;

        p[4].x = 0;
        p[4].y = 0;
        p[4].z = -40;
        p[4].w = 0;

        p[5].x = 0;
        p[5].y = 20;
        p[5].z = -40;
        p[5].w = 0;

        p[6].x = 0;
        p[6].y = 20;
        p[6].z = 0;
        p[6].w = 0;

        p[7].x = 0;
        p[7].y = 0;
        p[7].z = 0;
        p[7].w = 0;

        p[8].x = 0;
        p[8].y = 20;
        p[8].z = 0;
        p[8].w = 0;

        p[9].x = -40;
        p[9].y = 20;
        p[9].z = 0;
        p[9].w = 0;

        p[10].x = -40;
        p[10].y = 20;
        p[10].z = -40;
        p[10].w = 0;

        p[11].x = 0;
        p[11].y = 20;
        p[11].z = -40;
        p[11].w = 0;

        p[12].x = -40;
        p[12].y = 20;
        p[12].z = -40;
        p[12].w = 0;

        p[13].x = -40;
        p[13].y = 120;
        p[13].z = -40;
        p[13].w = 0;

        p[14].x = -60;
        p[14].y = 120;
        p[14].z = -40;
        p[14].w = 0;

        p[15].x = -60;
        p[15].y = 120;
        p[15].z = 0;
        p[15].w = 0;

        p[16].x = -40;
        p[16].y = 120;
        p[16].z = 0;
        p[16].w = 0;

        p[17].x = -40;
        p[17].y = 120;
        p[17].z = -40;
        p[17].w = 0;

        p[18].x = -40;
        p[18].y = 120;
        p[18].z = 0;
        p[18].w = 0;

        p[19].x = -40;
        p[19].y = 20;
        p[19].z = 0;
        p[19].w = 0;

        p[20].x = -40;
        p[20].y = 120;
        p[20].z = 0;
        p[20].w = 0;

        p[21].x = -60;
        p[21].y = 120;
        p[21].z = 0;
        p[21].w = 0;

        p[22].x = -60;
        p[22].y = 20;
        p[22].z = 0;
        p[22].w = 0;

        p[23].x = -100;
        p[23].y = 20;
        p[23].z = 0;
        p[23].w = 0;

        p[24].x = -100;
        p[24].y = 0;
        p[24].z = 0;
        p[24].w = 0;

        p[25].x = -100;
        p[25].y = 20;
        p[25].z = 0;
        p[25].w = 0;

        p[26].x = -100;
        p[26].y = 20;
        p[26].z = -40;
        p[26].w = 0;

        p[27].x = -100;
        p[27].y = 0;
        p[27].z = -40;
        p[27].w = 0;

        p[28].x = -100;
        p[28].y = 20;
        p[28].z = -40;
        p[28].w = 0;

        p[29].x = -60;
        p[29].y = 20;
        p[29].z = -40;
        p[29].w = 0;

        p[30].x = -60;
        p[30].y = 120;
        p[30].z = -40;
        p[30].w = 0;

        p[31].x = -60;
        p[31].y = 20;
        p[31].z = -40;
        p[31].w = 0;

        p[32].x = -60;
        p[32].y = 20;
        p[32].z = 0;
        p[32].w = 0;
        for(int i = 0; i < 32; i++){
            pdraw[i].x = p[i].x;
            pdraw[i].y = p[i].y;
            pdraw[i].z = p[i].z;
            pdraw[i].w = p[i].w;

        }

    }

    private FourPoint qm(FourPoint q, FourPoint p){
        FourPoint l = new FourPoint();
        l.x = q.w*p.x-q.z*p.y+q.y*p.z+q.x*p.w;
        l.y = q.z*p.x+q.w*p.y-q.x*p.z+q.y*p.w;
        l.z =-q.y*p.x+q.x*p.y+q.w*p.z+q.z*p.w;
        l.w =-q.x*p.x-q.y*p.y-q.z*p.z+q.w*p.w;
        return l;
    }
    public void rotate(){

        for(int j = 0; j <= 180; j++){
            o.x =(a*0.5)*Math.sin(Math.PI*j/180);
            neo.x =-o.x;
            o.y =-(a)*Math.sin(Math.PI*j/180);
            neo.y =-o.y;
            o.z =(a/3)*Math.sin(Math.PI*j/180);
            neo.z =-o.z;
            o.w =Math.cos(Math.PI*j/180);
            neo.w =o.w;
            for(int i = 0; i < 33; i++){
                m = qm(o, p[i]);
                h = qm(m, neo);
                pdraw[i].x = (int)Math.round(h.x) + 240;
                pdraw[i].y = (int)Math.round(h.y) + 120;
            }

            //The problem is here
            //When I'm trying to add delay, it does not 
            //repaint the canvas
            //Timer timer = new Timer(10000, new TimerListener());
            try{
                Thread.currentThread().sleep(20);
            }
            catch(InterruptedException e){

            }
            Graphics g = this.getGraphics();
            this.paintComponent(g);

            //repaint();

        }


    }


    protected void paintComponent(Graphics g){
        super.paintComponent(g);

        g.drawLine(240,120, 180, 240);

        for(int i = 0; i < 32; i++){
            //System.out.println((count++) + " " + pdraw[i].x + " " + pdraw[i].y + " " + pdraw[i+1].x + " " + pdraw[i+1].y);
            g.drawLine((int)pdraw[i].x, (int)pdraw[i].y, (int)pdraw[i+1].x, (int)pdraw[i+1].y);
        }


    }
}

class TimerListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            //repaint();
        }
    }
public class Quaternion {
    private static RotationPanel rp;
    public static void main(String[] args) {
        JFrame jfMainFrame = new JFrame();
        jfMainFrame.setSize(400, 400);
        jfMainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jfMainFrame.setLayout(new BorderLayout());
        JButton jb = new JButton("Rotate");
        jfMainFrame.add(jb, BorderLayout.SOUTH);
        jfMainFrame.setLocationRelativeTo(null);
        jfMainFrame.setVisible(true);
        rp = new RotationPanel();
        jfMainFrame.add(rp, BorderLayout.CENTER);
        jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                rp.rotate();
            }
        });
    }
}

任何帮助表示赞赏。

4

1 回答 1

4

旋转背后的数学

您在定义点和旋转时使用四元数。四元数q = a + bi + cj + dk在您的代码中将表示为:

q.x = b;
q.y = c;
q.z = d;
q.w = a;

您的数组p由代表空间点的四元数组成。坐标(xyz)写为xi + yj + zk,或在您的代码中为:

p[i].x = x;
p[i].y = y;
p[i].z = z;
p[i].w = 0;

向量 (1/2, -1, 1/3) 定义了旋转轴。我们称之为v。选择常数a使得a v是单位向量。即它的长度为1。

o是代表旋转的四元数。如果θ是旋转角度,单位向量a v定义旋转轴,则:

o = cos( θ /2) + a ( v x i + v y j + v z k ) sin( θ /2)
   = cos( θ /2) + a (1/2 i - 1 j + 1/3 k ) sin( θ /2)

在您的代码中,它是这样编写的:

o.x =(a*0.5)*Math.sin(Math.PI*j/180);
o.y =-(a)*Math.sin(Math.PI*j/180);
o.z =(a/3)*Math.sin(Math.PI*j/180);
o.w =Math.cos(Math.PI*j/180);

其中 j = 90/ π · θ

p为表示要旋转的点的四元数,令o*o四元数共轭。(即如果o = a + bi + cj + dk,则o* = a - bi - cj - dk)。

然后由下式得到的四元数h,定义了旋转点:

h = opo*。. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (1)

这里,qp是任意两个四元数qp的Hamilton 积

在您的代码中,neo(否定 O)?是的四元数共轭o,和你的方法qm(q, p)(四元数乘法)?q返回和的 Hamilton 积p

以下代码等价于上面的等式(1):

m = qm(o, p[i]);
h = qm(m, neo);

在此处阅读有关四元数旋转的更多信息。

对代码的建议更改

  • T 开头没有显示的原因是您忘记pdraw在构造函数中移动 (240,120)。您可以将for构造函数末尾的 -loop 替换为:

    for(int i = 0; i < 33; i++){ //i < 33, instead of i < 32
        pdraw[i].x = p[i].x + 240; //Shift along x-axis by 240
        pdraw[i].y = p[i].y + 120; //Shift along y-axis by 120
        //pdraw[i].z and pdraw[i].w aren't used
    }

  • Swing 组件只能在Event Dispatch Thread上调用。您可以通过编辑您的main方法来实现这一点:

public static void main(String[] args) {
    SwingUtilities.invokeLater() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    }
}
private void createAndShowGUI() {
    //Put the old content of your main method here instead.
}

  • 为避免在动画期间使程序无响应,您应该在单独的线程上进行计时。javax.swing.Timer是一个很好的选择。

一个可能的解决方案:

//In your RotationPanel class
private final Timer timer = new Timer(20, new TimerListener());
public void rotate(){
    timer.start();
}
private void setFrame(int j) {
    //Copied from old rotate method
    o.x =(a*0.5)*Math.sin(Math.PI*j/180);
    ... //13 more lines
}
private class TimerListener implements ActionListener{
    private int frame = 0;

    public void actionPerformed(ActionEvent e){
        RotationPanel.this.setFrame(++frame);
        RotationPanel.this.repaint();
        if(frame == 180) {
            frame = 0;
            timer.stop();
        }
    }
}


如果有任何不清楚的地方或您有任何其他问题,请随时发表评论。

于 2013-04-22T17:39:12.823 回答