2023-05-19 00:57:23 +02:00
|
|
|
package fr.alnotz;
|
|
|
|
|
|
|
|
public class Quaternion implements IsQuaternion{
|
|
|
|
private double s;
|
|
|
|
private double i;
|
|
|
|
private double j;
|
|
|
|
private double k;
|
|
|
|
public Quaternion(){
|
|
|
|
this.s = 0;
|
|
|
|
this.i = 0;
|
|
|
|
this.j = 0;
|
|
|
|
this.k = 0;
|
|
|
|
}
|
|
|
|
public Quaternion(double s, double i, double j, double k){
|
|
|
|
this.s = s;
|
|
|
|
this.i = i;
|
|
|
|
this.j = j;
|
|
|
|
this.k = k;
|
|
|
|
}
|
2023-05-19 02:55:12 +02:00
|
|
|
public static Quaternion plus(IsQuaternion quaternion0, IsQuaternion quaternion1){
|
|
|
|
return new Quaternion(quaternion0.getS() + quaternion1.getS(),
|
|
|
|
quaternion0.getI() + quaternion1.getI(),
|
|
|
|
quaternion0.getJ() + quaternion1.getJ(),
|
|
|
|
quaternion0.getK() + quaternion1.getK());
|
|
|
|
}
|
|
|
|
public static Quaternion times(IsQuaternion quaternion0, IsQuaternion quaternion1){
|
|
|
|
/*
|
|
|
|
* (a0 + b0i + c0j + d0k)(a1 + b1i + c1j + d1k)
|
|
|
|
* (a0a1 - b0b1 - c0c1 - d0d1) +
|
|
|
|
* (a0b1 + b0a1 + c0d1 - d0c1)i +
|
|
|
|
* (a0c1 - b0d1 + c0a1 + d0b1)j +
|
|
|
|
* (a0d1 + b0c1 - c0b1 + d0a1)k
|
|
|
|
*/
|
|
|
|
double newS = quaternion0.getS() * quaternion1.getS() -
|
|
|
|
quaternion1.getI() * quaternion1.getI() -
|
|
|
|
quaternion0.getJ() * quaternion1.getJ() -
|
|
|
|
quaternion0.getK() * quaternion1.getK();
|
|
|
|
double newI = quaternion0.getS() * quaternion1.getI() +
|
|
|
|
quaternion0.getI() * quaternion1.getK() +
|
|
|
|
quaternion0.getJ() * quaternion1.getS() -
|
|
|
|
quaternion0.getK() * quaternion1.getJ();
|
|
|
|
double newJ = quaternion0.getS() * quaternion1.getJ() -
|
|
|
|
quaternion0.getI() * quaternion1.getK() +
|
|
|
|
quaternion0.getJ() * quaternion1.getS() +
|
|
|
|
quaternion0.getK() * quaternion1.getI();
|
|
|
|
double newK = quaternion0.getS() * quaternion1.getK() +
|
|
|
|
quaternion0.getI() * quaternion1.getJ() -
|
|
|
|
quaternion0.getJ() * quaternion1.getI() +
|
|
|
|
quaternion0.getK() * quaternion1.getS();
|
|
|
|
return new Quaternion(newS, newI, newJ, newK);
|
|
|
|
}
|
|
|
|
public static Quaternion conjugate(IsQuaternion quaternion) {
|
|
|
|
return new Quaternion(quaternion.getS(),
|
|
|
|
-1 * quaternion.getI(),
|
|
|
|
-1 * quaternion.getJ(),
|
|
|
|
-1 * quaternion.getK());
|
|
|
|
}
|
2023-05-19 00:57:23 +02:00
|
|
|
@Override
|
|
|
|
public double getS() {
|
2023-05-19 02:11:21 +02:00
|
|
|
return this.s;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public double getI() {
|
2023-05-19 02:11:21 +02:00
|
|
|
return this.i;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public double getJ() {
|
2023-05-19 02:11:21 +02:00
|
|
|
return this.j;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public double getK() {
|
2023-05-19 02:11:21 +02:00
|
|
|
return this.k;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public void setS(double s) {
|
|
|
|
this.s = s;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public void setI(double i) {
|
|
|
|
this.i = i;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public void setJ(double j) {
|
|
|
|
this.j = j;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public void setK(double k) {
|
|
|
|
this.k = k;
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public Quaternion plus(IsQuaternion quaternion) {
|
|
|
|
return new Quaternion(this.s + quaternion.getS(),
|
|
|
|
this.i + quaternion.getI(),
|
|
|
|
this.j + quaternion.getJ(),
|
|
|
|
this.k + quaternion.getK());
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public Quaternion times(IsQuaternion quaternion) {
|
|
|
|
/*
|
|
|
|
* (a0 + b0i + c0j + d0k)(a1 + b1i + c1j + d1k)
|
|
|
|
* (a0a1 - b0b1 - c0c1 - d0d1) +
|
|
|
|
* (a0b1 + b0a1 + c0d1 - d0c1)i +
|
|
|
|
* (a0c1 - b0d1 + c0a1 + d0b1)j +
|
|
|
|
* (a0d1 + b0c1 - c0b1 + d0a1)k
|
|
|
|
*/
|
|
|
|
double newS = this.s * quaternion.getS() -
|
|
|
|
this.i * quaternion.getI() -
|
|
|
|
this.j * quaternion.getJ() -
|
|
|
|
this.k * quaternion.getK();
|
|
|
|
double newI = this.s * quaternion.getI() +
|
|
|
|
this.i * quaternion.getK() +
|
|
|
|
this.j * quaternion.getS() -
|
|
|
|
this.k * quaternion.getJ();
|
|
|
|
double newJ = this.s * quaternion.getJ() -
|
|
|
|
this.i * quaternion.getK() +
|
|
|
|
this.j * quaternion.getS() +
|
|
|
|
this.k * quaternion.getI();
|
|
|
|
double newK = this.s * quaternion.getK() +
|
|
|
|
this.i * quaternion.getJ() -
|
|
|
|
this.j * quaternion.getI() +
|
|
|
|
this.k * quaternion.getS();
|
|
|
|
return new Quaternion(newS, newI, newJ, newK);
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
2023-05-19 02:11:21 +02:00
|
|
|
public Quaternion conjugate() {
|
|
|
|
return new Quaternion(this.s, -1 * this.i, -1 * this.j, -1 * this.k);
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public boolean equals(IsQuaternion quaternion) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-19 02:11:21 +02:00
|
|
|
@Override
|
|
|
|
public String toString(){
|
|
|
|
return String.format("%+f %+fi %+fj %+fk\n", this.s, this.i, this.j, this.k);
|
|
|
|
}
|
2023-05-19 00:57:23 +02:00
|
|
|
}
|