Update of /cvsroot/bprocessor/model/src/net/sourceforge/bprocessor/model
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17470/src/net/sourceforge/bprocessor/model
Added Files:
Matrix.java
Log Message:
Added a rotation matrix
--- NEW FILE: Matrix.java ---
//---------------------------------------------------------------------------------
// $Id: Matrix.java,v 1.1 2005/11/24 10:30:45 henryml Exp $
//
// Copyright (c) 2005 The BProcessor Team (http://bprocessor.sourceforge.net)
// Released under the Lesser GNU Public License v2.1
//---------------------------------------------------------------------------------
package net.sourceforge.bprocessor.model;
/**
* The Matrix is a 4x4 matrix, represented as
* a an array of 16 double values in column-major
* order. This matches the representation of
* matrices in OpenGL.
*/
public class Matrix {
/** The storage - 16 double precision floating point values in
* column-major order.
*/
private double[] storage;
/** The id matrix */
public static final double[] ID =
{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
/**
* Construct a rotation matrix that rotates around the (x, y, z)
* vector, which is expected to have length = 1.
* the constructed matrix has the following layout:
* x^2(1-c)+c xy(1-c)-zs xz(1-c)+ys 0
* yx(1-c)+zs y^2(1-c)+c yz(1-c)-xs 0
* xz(1-c)-ys yz(1-c)+xs z^2(1-c)+c 0
* 0 0 0 1
* where c = cos(angle) and s = sin(angle).
* @param angle The angle to rotate
* @param x The x
* @param y The y
* @param z The z
* @return The Matrix
*/
public static Matrix rotation(double angle, double x, double y, double z) {
double s = Math.sin(angle);
double c = Math.cos(angle);
double[] v = new double[16];
v[0] = x * x * (1 - c) + c;
v[1] = y * x * (1 - c) + z * s;
v[2] = x * z * (1 - c) - y * s;
v[3] = 0;
v[4] = x * y * (1 - c) - z * s;
v[5] = y * y * (1 - c) + c;
v[6] = y * z * (1 - c) + x * s;
v[7] = 0;
v[8] = x * z * (1 - c) + y * s;
v[9] = y * z * (1 - c) - x * s;
v[10] = z * z * (1 - c) + c;
v[11] = 0;
v[12] = 0;
v[13] = 0;
v[14] = 0;
v[15] = 1;
return new Matrix(v);
}
/**
* Construct a matrix with specified values
* @param values The values in column-major order
*/
public Matrix(double[] values) {
super();
this.storage = values;
}
/**
* Construct a default matrix - the ID
*/
public Matrix() {
this((double[]) ID.clone());
}
/**
* Multiply a Matrix to a vector: Mx
* @param vector The vector
* @return A vector multiplied by this Matrix
*/
public double[] multiply(double[] vector) {
double[] result = new double[4];
result[0] = storage[0] * vector[0]
+ storage[4] * vector[1]
+ storage[8] * vector[2]
+ storage[12] * vector[3];
result[1] = storage[1] * vector[0]
+ storage[5] * vector[1]
+ storage[9] * vector[2]
+ storage[13] * vector[3];
result[2] = storage[2] * vector[0]
+ storage[6] * vector[1]
+ storage[10] * vector[2]
+ storage[14] * vector[3];
result[3] = storage[3] * vector[0]
+ storage[7] * vector[1]
+ storage[11] * vector[2]
+ storage[15] * vector[3];
return result;
}
}
|