Maths - Quaternion Code

individual methods

complete sfrotation class

This class can represent a 3D rotation. The class has 4 double numbers which represent the rotation as either quaternion, axis-angle or euler number depending on the cde int/enum

The class has methods to combine with other rotations. Also many other methods, including the ability to load and save to from VRML and x3d

There are 3 versions available depending on language:

See also the following related classes

The full source code is available on Sourceforge here:

Conjugate

public final void conjugate(Quat4d q1) {
x = -q1.x;
y = -q1.y;
z = -q1.z;
w = q1.w;
}

Normalising Quaternions


public final void normalise() {
double n = Math.sqrt(x*x + y*y + z*z + w*w);
x /= n;
y /= n;
z /= n;
w /= n;
}

Quaternion Scalar Multiplication


public final void scale(double s){
x *= s;
y *= s;
z *= s;
w *= s;
}

Quaternion Multiplication

public final void mul(Quat4d q1,Quat4d q2) {
x = q1.x * q2.w + q1.y * q2.z - q1.z * q2.y + q1.w * q2.x;
y = -q1.x * q2.z + q1.y * q2.w + q1.z * q2.x + q1.w * q2.y;
z = q1.x * q2.y - q1.y * q2.x + q1.z * q2.w + q1.w * q2.z;
w = -q1.x * q2.x - q1.y * q2.y - q1.z * q2.z + q1.w * q2.w;
}
Information about the equivalence of quaternion multiplication and orthogonal matrix multiplication.

Quaternion Add

public final void add(Quat4d q1,Quat4d q2) {
x = q1.x + q2.x;
y = q1.y + q2.y;
z = q1.z + q2.z;
w = q1.w + q2.w;
}

metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

 

cover us uk de jp fr ca Quaternions and Rotation Sequences.

Terminology and Notation

Specific to this page here:

 

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2023 Martin John Baker - All rights reserved - privacy policy.