org.proteinshader.math
Class QuaternionDemo

java.lang.Object
  extended by org.proteinshader.math.QuaternionDemo

public class QuaternionDemo
extends Object

Performs several tests on the Quaternion class, including a test of the SLERP (Spherical Linear intERPolation) algorithm.

TEST A - Matrix to Quaternion Conversion
TEST B - Quaternion to Matrix Conversion
TEST C - Multiply a Point by a Quaternion
TEST D - Interpolation between 2 Quaternions


Some of the SLERP tests are based on figures and examples of quaternion interpolation (SLERP) on the "EuclideanSpace - building a 3D world" web site by Martin Baker:

http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ (cited December 2006).


Field Summary
static int BANNER_WIDTH
          The banner width is the number of asterisks to print on the line line above or below a banner title.
 
Constructor Summary
QuaternionDemo()
          Creates a QuaternionDemo object with several vector and quaternions that can be used for testing purposes.
 
Method Summary
 void convertToQuaternionAndPrint(Vec3d N, Vec3d B, Vec3d T)
          Converts rotation matrix [N B T] to a quaternion and prints it.
 String createRepeats(char ch, int n)
          Returns a String with the character given as an argument repeated n number of times.
static void main(String[] args)
          Creates a QuaternionDemo object and uses it to test several operations on class Quaternion.
 void printBanner(String title, String message)
          Prints a banner with a row of asterisks above and below the title, and then a message right below the banner.
 void printColumnVectors(Vec3d N, Vec3d B, Vec3d T)
          Prints the 3 column vectors to standard out.
 void printInterpolation(Quaternion start, Quaternion end, double[] t)
          For each value in the array t, the slerp() method of class Quaternion will be used to calculate and print an interpolated Quaternion between the start and end Quaternions.
 void testMatrixToQuaternionConversion()
          TEST A: Creates two matrices with known quaternion solutions and prints the matrices, the known solutions, and the solutions generated by class Quaternion.
 void testMultiplyPointByQuaternion()
          TEST C: Prints the results of several Point3d x Quaternion multiplications.
 void testQuaternionToMatrixConversion()
          TEST B: Prints the result of converting a Matrix to a Quaternion and then converting the Quaternion back to a Matrix.
 void testSlerp()
          TEST D: Tests the SLERP (Spherical Linear intERPolation) algorithm of class Quaternion.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BANNER_WIDTH

public static final int BANNER_WIDTH
The banner width is the number of asterisks to print on the line line above or below a banner title. The value is set to 70.

See Also:
Constant Field Values
Constructor Detail

QuaternionDemo

public QuaternionDemo()
Creates a QuaternionDemo object with several vector and quaternions that can be used for testing purposes.

Method Detail

testMatrixToQuaternionConversion

public void testMatrixToQuaternionConversion()
TEST A: Creates two matrices with known quaternion solutions and prints the matrices, the known solutions, and the solutions generated by class Quaternion.

The first rotation matrix used for testing is M = [N B T], where N, B, and T are column vectors:

N = (0, -1, 0)
B = (1, 0, 0)
T = (0, 0, 1)


This rotation matrix is a -90 degree z-roll and should convert to the quaternion Q where

Q = (x, y, z, w)
Q = (0, 0, -sqrt(2)/2, sqrt(2)/2)
Q = (0, 0, -0.707, 0.707)


The column vectors are then changed to the equivalent of a 90 degree x-roll matrix

N = (1, 0, 0)
B = (0, 0, 1)
T = (0, -1, 0)


which should result in quaternion = (0.707, 0, 0, 0.707)


testQuaternionToMatrixConversion

public void testQuaternionToMatrixConversion()
TEST B: Prints the result of converting a Matrix to a Quaternion and then converting the Quaternion back to a Matrix. Two example are given.


testMultiplyPointByQuaternion

public void testMultiplyPointByQuaternion()
TEST C: Prints the results of several Point3d x Quaternion multiplications.


testSlerp

public void testSlerp()
TEST D: Tests the SLERP (Spherical Linear intERPolation) algorithm of class Quaternion. There are seven matrices used for tesing:

1. identity quaternion = (0.000, 0.000, 0.000, 1.000)
2. +90 degree x-roll = (0.707, 0.000, 0.000, 0.707)
3. -90 degree x-roll = (-0.707, 0.000, 0.000, 0.707)
4. +90 degree y-roll = (0.000, 0.707, 0.000, 0.707)
5. -90 degree y-roll = (0.000, -0.707, 0.000, 0.707)
6. +90 degree z-roll = (0.000, 0.000, 0.707, 0.707)
7. -90 degree z-roll = (0.000, 0.000, -0.707, 0.707)


printInterpolation

public void printInterpolation(Quaternion start,
                               Quaternion end,
                               double[] t)
For each value in the array t, the slerp() method of class Quaternion will be used to calculate and print an interpolated Quaternion between the start and end Quaternions.

Parameters:
start - the start quaternion for the interpolation.
end - the end quaternion for the interpolation.
t - a parameter between 0.0 and 1.0, inclusive.

printBanner

public void printBanner(String title,
                        String message)
Prints a banner with a row of asterisks above and below the title, and then a message right below the banner.

Parameters:
title - the title to place in the banner.
message - a message to print right below the banner.

createRepeats

public String createRepeats(char ch,
                            int n)
Returns a String with the character given as an argument repeated n number of times.

Parameters:
ch - the char to repeat.
n - the number of times to repeat ch.
Returns:
A String with n repeats of ch.

printColumnVectors

public void printColumnVectors(Vec3d N,
                               Vec3d B,
                               Vec3d T)
Prints the 3 column vectors to standard out.

Parameters:
N - normal - the 3rd column vector of the rotation matrix.
B - binormal - the 2nd column vector of the rotation matrix.
T - tangent - the 1st column vector of the rotation matrix.

convertToQuaternionAndPrint

public void convertToQuaternionAndPrint(Vec3d N,
                                        Vec3d B,
                                        Vec3d T)
Converts rotation matrix [N B T] to a quaternion and prints it.

Parameters:
N - normal - the 1st column vector of the rotation matrix.
B - binormal - the 2nd column vector of the rotation matrix.
T - tangent - the 3rd column vector of the rotation matrix.

main

public static void main(String[] args)
Creates a QuaternionDemo object and uses it to test several operations on class Quaternion.



Copyright © 2007-2008