gpt4 book ai didi

java - 旧程序中的四元数旋转

转载 作者:行者123 更新时间:2023-11-29 03:34:47 24 4
gpt4 key购买 nike

我需要有人帮助我。大约 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();
}
});
}
}

感谢任何帮助。

最佳答案

旋转背后的数学原理

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

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

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

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

vector (1/2, -1, 1/3) 定义了 axis of rotation .我们称它为 v。选择常量 a 使得 av 是一个单位 vector 。 IE。它的长度为 1。

o 是表示旋转的四元数。如果θ是旋转角度,单位 vector av定义了旋转轴,那么:

o = cos(θ/2) + a(vxi + vyj + vzk) 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*quaternion conjugate o。 (即如果 o = a + bi + cj + dk,则 < strong>o* = a - bi - cj - dk).

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

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

这里,qpHamilton product任意两个四元数 qp

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

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

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

阅读更多关于四元数旋转的信息 here .

建议对您的代码进行更改

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

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


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

关于java - 旧程序中的四元数旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16138537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com