Dzmitry Sevkovich
Dzmitry Sevkovich

Reputation: 901

Quaternion rotation in the old program

I need somebody to help me. About 10 years ago I wrote the Delphi code which makes 3d letter T to rotate. Recently I rewrote that program in java, but I can't figure out how the math work. If somebody familiar with quaternion rotation, could you, please explain me, how the math in the program works. p array holds all end points of each side of the letter T. Some of them repeat each other just to make it possible to draw the letter T in the for loop. All I need to know, is what is the constant 'a', and why did I called method 'qm' twice, and what it does.

Thanks in advance for any possible help.

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();
            }
        });
    }
}

Any help is appreciated.

Upvotes: 1

Views: 526

Answers (1)

Lone nebula
Lone nebula

Reputation: 4878

The maths behind the rotation

You are using quaternions when defining points and rotations. A quaternion q = a + bi + cj + dk would in your code be represented as:

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

Your array p consists of quaternions that represent points in space. The coordinates (x, y, z) are written as xi + yj + zk, or in your code as:

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

The vector (1/2, -1, 1/3) defines the axis of rotation. Let's call it v. The constant a is chosen such that av is a unit vector. I.e. it has length 1.

The o is a quaternion that represents the rotation. If θ is the rotation angle, and the unit vector av defines the axis of rotation, then:

o = cos(θ/2) + a(vxi + vyj + vzk) sin(θ/2)
   = cos(θ/2) + a(1/2 i - 1 j + 1/3 k) sin(θ/2)

In your code, it is written like this:

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);

where j = 90/π · θ.

Let p be the quaternion representing the point you want to rotate, and let o* be the quaternion conjugate of o. (I.e. if o = a + bi + cj + dk, then o* = a - bi - cj - dk).

Then the quaternion h obtained by the following equation, defines the rotated point:

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

Here, qp is the Hamilton product of any two quaternions q and p.

In your code, neo (NEgative O)? is the quaternion conjugate of o, and your method qm(q, p) (Quaternion Multiplication)? returns the Hamilton product of q and p.

The following piece of code is equivalent to equation (1) above:

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

Read more about quaternion rotation here.

Recommended changes to your code

  • The reason why the T doesn't show at the beginning, is that you've forgotten to shift pdraw by (240,120) in the constructor. You could replace the for-loop at the end of your constructor with this:

    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 components should only be invoked on the Event Dispatch Thread. You can achieve this by editing your main method:

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.
}

  • To avoid making your program unresponsive during the animation, you should do the timing on a separate thread. javax.swing.Timer is a good choice for this purpose.

A possible solution:

//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();
        }
    }
}


Feel free to comment if anything was unclear or if you have any further questions.

Upvotes: 4

Related Questions