org.instantreality.InstantIO
Class Matrix4d

java.lang.Object
  extended by org.instantreality.InstantIO.Matrix4d

public class Matrix4d
extends java.lang.Object

Helper class for exchanging 4x4 matrices of double values.

Matrix4d is a basic helper class for exchanging matrices of 4x4 double values between different software components. It does not contain any means for matrix arithmetics, only methods for setting and getting the matrix elements. It is not meant to be used directly by software components for matrix representation. Instead, software components should use their own, appropriate classes for handling matrices. Only when sending matrices to an OutSlot, or when receiving matrices from an InSlot, the internal representation of matrices should be converted to Matrix4d's. This ensures the interoperability between different software components that use different internal representations for matrixes.

Author:
Patrick Dähne

Field Summary
static Matrix4d IDENTITY
          The identity matrix.
 
Constructor Summary
Matrix4d(double[] matrix)
          Creates a new Matrix4d object that gets initialized by a given 16 element double array representing a 4x4 matrix.
Matrix4d(double m11, double m12, double m13, double m14, double m21, double m22, double m23, double m24, double m31, double m32, double m33, double m34, double m41, double m42, double m43, double m44)
          Creates a new Matrix4d object that gets initialized by given 16 double values representing a 4x4 matrix.
Matrix4d(Matrix4d matrix)
          Constructor that initializes the components of the Matrix4d object with the elements taken from another Matrix4d object.
 
Method Summary
 boolean equals(java.lang.Object obj)
          Compares the matrix to another object.
 void get(double[] matrix)
          Returns the matrix elements.
 double get(int index)
          Returns one element of the matrix.
 java.lang.String toString()
          Converts the matrix to a string.
static Matrix4d valueOf(java.lang.String str)
          Converts a string to a Matrix4d object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

IDENTITY

public static final Matrix4d IDENTITY
The identity matrix.

Constructor Detail

Matrix4d

public Matrix4d(double m11,
                double m12,
                double m13,
                double m14,
                double m21,
                double m22,
                double m23,
                double m24,
                double m31,
                double m32,
                double m33,
                double m34,
                double m41,
                double m42,
                double m43,
                double m44)
Creates a new Matrix4d object that gets initialized by given 16 double values representing a 4x4 matrix.

The following example creates a new Matrix4d object that represents a translation of 1 unit along the x axis, 2 units along the y axis, and 3 units along the z axis:

 Matrix4d mat = new Matrix4d(1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0);
 

Parameters:
m11 - The value at row 1, column 1 of the matrix
m12 - The value at row 1, column 2 of the matrix
m13 - The value at row 1, column 3 of the matrix
m14 - The value at row 1, column 4 of the matrix
m21 - The value at row 2, column 1 of the matrix
m22 - The value at row 2, column 2 of the matrix
m23 - The value at row 2, column 3 of the matrix
m24 - The value at row 2, column 4 of the matrix
m31 - The value at row 3, column 1 of the matrix
m32 - The value at row 3, column 2 of the matrix
m33 - The value at row 3, column 3 of the matrix
m34 - The value at row 3, column 4 of the matrix
m41 - The value at row 4, column 1 of the matrix
m42 - The value at row 4, column 2 of the matrix
m43 - The value at row 4, column 3 of the matrix
m44 - The value at row 4, column 4 of the matrix

Matrix4d

public Matrix4d(double[] matrix)
Creates a new Matrix4d object that gets initialized by a given 16 element double array representing a 4x4 matrix.

The following example creates a new Matrix4d object that represents a translation of 1 unit along the x axis, 2 units along the y axis, and 3 units along the z axis:

 double[] m = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 2.0, 3.0, 1.0 };
 Matrix4d mat = new Matrix4d(m);
 

Parameters:
matrix - The sixteen components of the matrix

Matrix4d

public Matrix4d(Matrix4d matrix)
Constructor that initializes the components of the Matrix4d object with the elements taken from another Matrix4d object.

The following example demonstrates how to create a Matrix4d object that is an exact copy of another Matrix4d object:

 Matrix4d given_mat = ...;
 Matrix4d mat = new Matrix4d(given_mat);
 

Parameters:
matrix - The other matrix used to initialize the new matrix.
Method Detail

get

public final void get(double[] matrix)
Returns the matrix elements.

The following example extracts the translation vector from the matrix and prints it to the console:

 Matrix4d mat = ...;
 double[] m = new double[16];
 mat.get(m);
 double x = m[12];
 double y = m[13];
 double z = m[14];
 System.out.println("Translation = " + x + " " + y + " " + z);
 

Parameters:
matrix - A double array that gets filled with the elements of the matrix. This array must have at least 16 elements.

get

public final double get(int index)
Returns one element of the matrix.

The following example extracts the translation vector from the matrix and prints it to the console:

 Matrix4d mat = ...;
 double x = mat.get(12);
 double y = mat.get(13);
 double z = mat.get(14);
 System.out.println("Translation = " + x + " " + y + " " + z);
 

Parameters:
index - The index of the element. This must be between 0 and 15, inclusively.
Returns:
The value of the element.

equals

public final boolean equals(java.lang.Object obj)
Compares the matrix to another object. The result is true if the object is not null and is a Matrix4d object and the matrix elements of the other Matrix4d object are the same as the matrix elements of this matrix.

The following example compares two matrices and prints the result to the console:

 Matrix4d mat1 = ...;
 Matrix4d mat2 = ...;
 System.out.println("Matrices are the same: " + mat1.equals(mat2));
 

Overrides:
equals in class java.lang.Object
Parameters:
obj - the object to compare the matrix to
Returns:
true when the matrices are equal, false otherwise

toString

public final java.lang.String toString()
Converts the matrix to a string. The string representation consists of the 16 matrix elements, separated by space characters.

The following example prints the string representation of a Matrix4d object to the console:

 Matrix4d mat = ...;
 System.out.println("Matrix = " + mat.toString());
 

Overrides:
toString in class java.lang.Object
Returns:
A string representation of the matrix.

valueOf

public static final Matrix4d valueOf(java.lang.String str)
Converts a string to a Matrix4d object. The string must consist of the 16 matrix elements, separated by space characters.

The following example creates a new Matrix4d object that represents a translation of 1 unit along the x axis, 2 units along the y axis, and 3 units along the z axis:

 Matrix4d mat = Matrix4d.valueOf("1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 1.0 2.0 3.0 1.0");
 

Parameters:
str - The string representation of the matrix.
Returns:
The new Matrix4d object, parsed from the string.