GeomConverter/src/org/lcsim/detector
diff -u -r1.1 -r1.2
--- IRotation3D.java 26 Feb 2007 19:25:19 -0000 1.1
+++ IRotation3D.java 27 Feb 2007 01:08:20 -0000 1.2
@@ -4,28 +4,141 @@
import hep.physics.vec.Hep3Matrix;
import hep.physics.vec.Hep3Vector;
-// interface to rotations in 3D space
+/**
+ * An interface to rotations in 3D space,
+ * using interfaces and base classes within
+ * the hep.physics.vec package.
+ *
+ * @see hep.physics.vec
+ * @see hep.physics.vec.BasicHep3Matrix
+ * @see hep.physics.vec.Hep3Matrix
+ * @see hep.physics.vec.Hep3Vector
+ *
+ * @author Tim Nelson <[log in to unmask]>
+ * @author Jeremy McCormick <[log in to unmask]>
+ * @version $Id: IRotation3D.java,v 1.2 2007/02/27 01:08:20 jeremy Exp $
+ */
interface IRotation3D
-{
+{
+ /**
+ * Numerical constants for the X and Y column indices.
+ */
public static final int XCol=0, YCol=1, ZCol=2,XRow=0,YRow=1,ZRow=2;
+
+ /**
+ * The numbered components of the rotation matrix.
+ */
public static final int XX=0, XY=1, XZ=2, YX=3, YY=4, YZ=5, ZX=6, ZY=7, ZZ=8;
- // returns a copy of the rotation matrix
+ /**
+ * Total number of components in the matrix.
+ */
+ public static final int NComponents=9;
+
+ /**
+ * The number of rows and columns in the matrix.
+ */
+ public static final int NRows=3,NCols=3;
+
+ /**
+ * The rotation in standard rotation matrix form.
+ * @return The BasicHep3Matrix representing this rotation.
+ */
public BasicHep3Matrix getRotationMatrix();
+ /**
+ * Set the matrix from a Hep3Matrix interface.
+ * @param matrix
+ */
public void setRotationMatrix(Hep3Matrix matrix);
+
+ /**
+ * Set the matrix from a BasicHep3Matrix class.
+ * @param matrix
+ */
public void setRotationMatrix(BasicHep3Matrix matrix);
- public void setRotationMatrix(double [][] arr);
-
- public void setTaitBryan(double alpha, double beta, double gamma);
+
+ /**
+ * Set the matrix components from an array of at least length 9.
+ * @param components
+ */
+ public void setRotationMatrix(double[] components);
+
+ /**
+ * Set the rotation using successive passive rotations
+ * of X, Y, and Z, which matches the GDML/LCDD/Geant4 conventions.
+ * @param alpha Angle about X axis.
+ * @param beta Angle about Y axis.
+ * @param gamma Angle about Z axis.
+ */
+ public void setPassiveXYZ(double alpha, double beta, double gamma);
+
+ /**
+ * Set passive Euler rotation.
+ * @param phi
+ * @param theta
+ * @param psi
+ */
public void setPassiveEuler(double phi, double theta, double psi);
+
+ /**
+ * Set active Euler rotation.
+ * @param phi
+ * @param theta
+ * @param psi
+ */
public void setActiveEuler(double phi, double theta, double psi);
+ /**
+ * Multiply this rotation in place with another IRotation3D,
+ * modifying this IRotation3D in place.
+ * @param rotation
+ */
+ public void multiply(IRotation3D rotation);
+
+ /**
+ * Compare this IRotation3D with another.
+ * @param rotation
+ * @return True if all components of the matrices are equal.
+ */
+ public boolean equals(IRotation3D rotation);
+
+ /**
+ * Set all 9 components of the matrix.
+ * @param xx
+ * @param xy
+ * @param xz
+ * @param yx
+ * @param yy
+ * @param yz
+ * @param zx
+ * @param zy
+ * @param zz
+ */
public void setComponents(double xx, double xy, double xz, double yx, double yy, double yz, double zx, double zy, double zz);
+
+ /**
+ * Get the components as an array of length 9.
+ * @return
+ */
public double[] getComponents();
+ /**
+ * Apply inverse transformation in place.
+ */
public void invert();
+
+ /**
+ * Apply inverse transformation, returning
+ * a new IRotation3D, not altering this matrix.
+ * @return
+ */
public IRotation3D inverse();
+
+ /**
+ * The trace of the matrix.
+ * @return
+ */
public double trace();
public double xx();
@@ -40,9 +153,6 @@
public double zy();
public double zz();
- // these return new objects
- // and don't effect the rotation
- // in place; use set methods
public Hep3Vector getColumnX();
public Hep3Vector getColumnY();
public Hep3Vector getColumnZ();
@@ -83,15 +193,38 @@
public void setRowZ(double [] arr);
public void setRowZ(double x, double y, double z);
- // set element
- public void set(int row, int col, double v);
-
- // get element
- public double get(int row, int col);
-
- // get ith element
- public double get(int i);
+ /**
+ * Set matrix component by row and column.
+ * @param row
+ * @param col
+ * @param v
+ */
+ public void setComponent(int row, int col, double v);
+
+ /**
+ * Get matrix component by row and column.
+ * @param row
+ * @param col
+ * @return
+ */
+ public double getComponent(int row, int col);
+
+ /**
+ * Get the ith component.
+ * Use the symbolic constants XX, XY, etc.
+ * @param i
+ * @return
+ */
+ public double getComponent(int i);
- // resets the rotation to the identity matrix
+ /**
+ * Reset this IRotation3D to the identity matrix.
+ */
public void resetToIdentity();
+
+ /**
+ * True if this IRotation3D is equivalent to the identity matrix.
+ * @return
+ */
+ public boolean isIdentity();
}
\ No newline at end of file
GeomConverter/src/org/lcsim/detector
diff -u -r1.5 -r1.6
--- Rotation3D.java 26 Feb 2007 23:41:58 -0000 1.5
+++ Rotation3D.java 27 Feb 2007 01:08:20 -0000 1.6
@@ -1,106 +1,102 @@
-/*
- * Hep3Rotation.java
- */
-
package org.lcsim.detector;
import hep.physics.vec.BasicHep3Matrix;
import hep.physics.vec.BasicHep3Vector;
-import hep.physics.vec.Hep3Vector;
import hep.physics.vec.Hep3Matrix;
-import hep.physics.vec.VecOp;
+import hep.physics.vec.Hep3Vector;
import java.io.PrintStream;
/**
- *
+ * Implementation of the @see IRotation3D interface.
+ *
* @author Tim Nelson <[log in to unmask]>
* @author Jeremy McCormick <[log in to unmask]>
+ * @version $Id: Rotation3D.java,v 1.6 2007/02/27 01:08:20 jeremy Exp $
*/
public class Rotation3D implements IRotation3D
{
- public static final int XCol=0, YCol=1, ZCol=2,XRow=0,YRow=1,ZRow=2;
-
+ /**
+ * The 3x3 rotation matrix representing the state of this Rotation3D.
+ */
private BasicHep3Matrix matrix = BasicHep3Matrix.identity();
-
+
+ /**
+ * Cache an immutable copy of the identity matrix.
+ */
+ private static final BasicHep3Matrix identityMatrix = BasicHep3Matrix.identity();
+
+ /**
+ * Construct a Rotation3D with the identity matrix.
+ */
public Rotation3D()
{}
-
- public void setRotationMatrix(Hep3Matrix matrix)
- {
- this.matrix = (BasicHep3Matrix)matrix;
- }
/**
- * Transform rotation using Tait-Bryan convention.
- * @param alpha
- * @param beta
- * @param gamma
+ * Construct a Rotation3D from a BasicHep3Matrix.
+ * @param matrix
*/
- public void setTaitBryan(double alpha, double beta, double gamma)
+ public Rotation3D(BasicHep3Matrix matrix)
{
- matrix = passiveTaitBryan(alpha,beta,gamma);
+ setRotationMatrix(matrix);
}
- public void setPassiveEuler(double phi, double theta, double psi)
+ /**
+ * Construct a Rotation3D from a Hep3Matrix interface.
+ * @param matrix
+ */
+ public Rotation3D(Hep3Matrix matrix)
{
- matrix.setPassiveEuler(phi,theta,psi);
+ setRotationMatrix((BasicHep3Matrix)matrix);
}
-
- public void setActiveEuler(double phi, double theta, double psi)
- {
- matrix.setActiveEuler(phi,theta,psi);
+
+ /**
+ * Construct a Rotation3D from the individual components.
+ * @param xx
+ * @param xy
+ * @param xz
+ * @param yx
+ * @param yy
+ * @param yz
+ * @param zx
+ * @param zy
+ * @param zz
+ */
+ public Rotation3D(double xx, double xy, double xz, double yx, double yy, double yz, double zx, double zy, double zz)
+ {
+ setComponents(xx,xy,xz,yx,yy,yz,zx,zy,zz);
}
- // Static Methods
- //===============
- public static BasicHep3Matrix passiveEuler(double phi, double theta, double psi)
+ /**
+ * Set the rotation matrix from a Hep3Matrix.
+ */
+ public void setRotationMatrix(Hep3Matrix matrix)
{
- BasicHep3Matrix matrix = new BasicHep3Matrix();
- matrix.setPassiveEuler(phi,theta,psi);
- return matrix;
+ this.matrix = (BasicHep3Matrix)matrix;
}
-
- public static BasicHep3Matrix passiveTaitBryan(double alpha, double beta, double gamma)
+
+ public void setRotationMatrix(double[] components)
{
- return (BasicHep3Matrix)mult(passiveZRotation(gamma),mult(passiveYRotation(beta),passiveXRotation(alpha)));
- }
+ if (components.length < 9)
+ {
+ throw new IllegalArgumentException("Cannot construct matrix from less than 9 components.");
+ }
+ }
- public static BasicHep3Matrix passiveXRotation(double angle)
+ public void setPassiveXYZ(double alpha, double beta, double gamma)
{
- double sin = Math.sin(angle);
- double cos = Math.cos(angle);
- BasicHep3Matrix rotation = BasicHep3Matrix.identity();
- rotation.setElement(1,1,cos);
- rotation.setElement(1,2,sin);
- rotation.setElement(2,1,-sin);
- rotation.setElement(2,2,cos);
- return rotation;
+ matrix = passiveXYZ(alpha,beta,gamma);
}
- public static BasicHep3Matrix passiveYRotation(double angle)
+ public void setPassiveEuler(double phi, double theta, double psi)
{
- double sin = Math.sin(angle);
- double cos = Math.cos(angle);
- BasicHep3Matrix rotation = BasicHep3Matrix.identity();
- rotation.setElement(0,0,cos);
- rotation.setElement(0,2,-sin);
- rotation.setElement(2,0,sin);
- rotation.setElement(2,2,cos);
- return rotation;
+ matrix.setPassiveEuler(phi,theta,psi);
}
- public static BasicHep3Matrix passiveZRotation(double angle)
+ public void setActiveEuler(double phi, double theta, double psi)
{
- double sin = Math.sin(angle);
- double cos = Math.cos(angle);
- BasicHep3Matrix rotation = BasicHep3Matrix.identity();
- rotation.setElement(0,0,cos);
- rotation.setElement(0,1,sin);
- rotation.setElement(1,0,-sin);
- rotation.setElement(1,1,cos);
- return rotation;
- }
+ matrix.setActiveEuler(phi,theta,psi);
+ }
public void invert()
{
@@ -212,7 +208,7 @@
assert(arr.length>2);
for (int i=0; i<3; i++)
{
- set(i,col,arr[i]);
+ setComponent(i,col,arr[i]);
}
}
@@ -227,7 +223,7 @@
assert(row >=0 && row <=3);
for (int i=0; i<3; i++)
{
- set(row, i, arr[i]);
+ setComponent(row, i, arr[i]);
}
}
@@ -237,7 +233,7 @@
}
public void setColumnX(double[] arr) {
- setColumn(0,arr);
+ setColumn(XCol,arr);
}
public void setColumnX(Hep3Vector vec) {
@@ -246,7 +242,7 @@
public void setColumnY(double[] arr)
{
- setColumn(1,arr);
+ setColumn(YCol,arr);
}
public void setColumnY(Hep3Vector vec) {
@@ -255,7 +251,7 @@
public void setColumnZ(double[] arr)
{
- setColumn(2,arr);
+ setColumn(ZCol,arr);
}
public void setColumnZ(Hep3Vector vec) {
@@ -263,7 +259,7 @@
}
public void setRowX(double[] arr) {
- setRow(0,arr);
+ setRow(XRow,arr);
}
public void setRowX(Hep3Vector vec)
@@ -273,7 +269,7 @@
public void setRowY(double[] arr)
{
- setRow(1,arr);
+ setRow(YRow,arr);
}
public void setRowY(Hep3Vector vec) {
@@ -281,26 +277,16 @@
}
public void setRowZ(double[] arr) {
- setRow(2,arr);
+ setRow(ZRow,arr);
}
public void setRowZ(Hep3Vector vec) {
setRowZ(vec.v());
}
- public Rotation3D(BasicHep3Matrix matrix)
- {
- setRotationMatrix(matrix);
- }
-
- public Rotation3D(Hep3Matrix matrix)
- {
- setRotationMatrix((BasicHep3Matrix)matrix);
- }
-
- public void set(int i, int j, double v)
+ public void setComponent(int row, int col, double v)
{
- matrix.setElement(i,j,v);
+ matrix.setElement(row,col,v);
}
public void setColumn(int col, double x, double y, double z)
@@ -349,36 +335,23 @@
public void setRowX(double xx, double xy, double xz)
{
- set(0,0,xx);
- set(0,1,xy);
- set(0,2,xz);
+ setComponent(XRow,XCol,xx);
+ setComponent(XRow,YCol,xy);
+ setComponent(XRow,ZCol,xz);
}
public void setRowY(double yx, double yy, double yz)
{
- set(1,0,yx);
- set(1,1,yy);
- set(1,2,yz);
+ setComponent(YRow,XCol,yx);
+ setComponent(YRow,YCol,yy);
+ setComponent(YRow,ZCol,yz);
}
public void setRowZ(double zx, double zy, double zz)
{
- set(2,0,zx);
- set(2,1,zy);
- set(2,2,zz);
- }
-
- // set from row,col array of at least size 3x3
- public void setRotationMatrix(double [][] a)
- {
- matrix = new BasicHep3Matrix();
- for (int i=0; i<3; i++)
- {
- for (int j=0; j<3; j++)
- {
- set(i, j, a[i][j]);
- }
- }
+ setComponent(ZRow,XCol,zx);
+ setComponent(ZRow,YCol,zy);
+ setComponent(ZRow,ZCol,zz);
}
public void resetToIdentity()
@@ -400,54 +373,155 @@
{
this.matrix = matrix;
}
+
+ public double getComponent(int row, int col)
+ {
+ return matrix.e(row,col);
+ }
+
+ public double getComponent(int i)
+ {
+ switch (i)
+ {
+ case XX:
+ return xx();
+ case XY:
+ return xy();
+ case XZ:
+ return xz();
+ case YX:
+ return yx();
+ case YY:
+ return yy();
+ case YZ:
+ return yz();
+ case ZX:
+ return zx();
+ case ZY:
+ return zy();
+ case ZZ:
+ return zz();
+ default:
+ throw new IllegalArgumentException("Invalid matrix element : " + i );
+ }
+ }
- public double get(int r, int c)
+ public void multiply(IRotation3D rotation)
{
- return matrix.e(r,c);
+ this.setRotationMatrix(
+ Rotation3D.mult(rotation.getRotationMatrix(), this.getRotationMatrix()));
}
- public double get(int i)
+ public static Rotation3D multiply(IRotation3D rot1, IRotation3D rot2)
{
- switch (i)
+ return new Rotation3D(Rotation3D.mult(rot1.getRotationMatrix(), rot2.getRotationMatrix()));
+ }
+
+ /**
+ * Rewrite of hep.physics.vec.VecOp.mult by Tim Nelson.
+ *
+ * TODO: Calls to this method should be replaced with the
+ * freehep method @see hep.physics.vec.VecOp#mult,
+ * once this has been fixed!
+ *
+ * @param m1 The left matrix.
+ * @param m2 The right matrix.
+ * @return The product of the two matrices.
+ */
+ private static Hep3Matrix mult(Hep3Matrix m1, Hep3Matrix m2)
+ {
+ double e0 = m1.e(0,0) * m2.e(0,0) + m1.e(0,1) * m2.e(1,0) + m1.e(0,2) * m2.e(2,0);
+ double e1 = m1.e(0,0) * m2.e(0,1) + m1.e(0,1) * m2.e(1,1) + m1.e(0,2) * m2.e(2,1);
+ double e2 = m1.e(0,0) * m2.e(0,2) + m1.e(0,1) * m2.e(1,2) + m1.e(0,2) * m2.e(2,2);
+
+ double e3 = m1.e(1,0) * m2.e(0,0) + m1.e(1,1) * m2.e(1,0) + m1.e(1,2) * m2.e(2,0);
+ double e4 = m1.e(1,0) * m2.e(0,1) + m1.e(1,1) * m2.e(1,1) + m1.e(1,2) * m2.e(2,1);
+ double e5 = m1.e(1,0) * m2.e(0,2) + m1.e(1,1) * m2.e(1,2) + m1.e(1,2) * m2.e(2,2);
+
+ double e6 = m1.e(2,0) * m2.e(0,0) + m1.e(2,1) * m2.e(1,0) + m1.e(2,2) * m2.e(2,0);
+ double e7 = m1.e(2,0) * m2.e(0,1) + m1.e(2,1) * m2.e(1,1) + m1.e(2,2) * m2.e(2,1);
+ double e8 = m1.e(2,0) * m2.e(0,2) + m1.e(2,1) * m2.e(1,2) + m1.e(2,2) * m2.e(2,2);
+
+ return new BasicHep3Matrix(e0,e1,e2,e3,e4,e5,e6,e7,e8);
+ }
+
+ public boolean equals(IRotation3D rotation)
+ {
+ boolean result=true;
+ for (int row=XRow; row<3; row++)
{
- case XX:
- return xx();
- case XY:
- return xy();
- case XZ:
- return xz();
- case YX:
- return yx();
- case YY:
- return yy();
- case YZ:
- return yz();
- case ZX:
- return zx();
- case ZY:
- return zy();
- case ZZ:
- return zz();
- default:
- throw new IllegalArgumentException("Invalid matrix element : " + i );
+ for (int col=YCol; col<3; col++)
+ {
+ if (this.getComponent(row,col) != rotation.getComponent(row,col))
+ {
+ result=false;
+ break;
+ }
+ }
}
- }
-
- public static Hep3Matrix mult(Hep3Matrix m1, Hep3Matrix m2)
- {
- double e0 = m1.e(0,0) * m2.e(0,0) + m1.e(0,1) * m2.e(1,0) + m1.e(0,2) * m2.e(2,0);
- double e1 = m1.e(0,0) * m2.e(0,1) + m1.e(0,1) * m2.e(1,1) + m1.e(0,2) * m2.e(2,1);
- double e2 = m1.e(0,0) * m2.e(0,2) + m1.e(0,1) * m2.e(1,2) + m1.e(0,2) * m2.e(2,2);
-
- double e3 = m1.e(1,0) * m2.e(0,0) + m1.e(1,1) * m2.e(1,0) + m1.e(1,2) * m2.e(2,0);
- double e4 = m1.e(1,0) * m2.e(0,1) + m1.e(1,1) * m2.e(1,1) + m1.e(1,2) * m2.e(2,1);
- double e5 = m1.e(1,0) * m2.e(0,2) + m1.e(1,1) * m2.e(1,2) + m1.e(1,2) * m2.e(2,2);
-
- double e6 = m1.e(2,0) * m2.e(0,0) + m1.e(2,1) * m2.e(1,0) + m1.e(2,2) * m2.e(2,0);
- double e7 = m1.e(2,0) * m2.e(0,1) + m1.e(2,1) * m2.e(1,1) + m1.e(2,2) * m2.e(2,1);
- double e8 = m1.e(2,0) * m2.e(0,2) + m1.e(2,1) * m2.e(1,2) + m1.e(2,2) * m2.e(2,2);
-
- return new BasicHep3Matrix(e0,e1,e2,e3,e4,e5,e6,e7,e8);
- }
-
+ return result;
+ }
+
+ public boolean isIdentity()
+ {
+ return equals(identityMatrix);
+ }
+
+ // Static Method
+ //===============
+ public static BasicHep3Matrix passiveEuler(double phi, double theta, double psi)
+ {
+ BasicHep3Matrix matrix = new BasicHep3Matrix();
+ matrix.setPassiveEuler(phi,theta,psi);
+ return matrix;
+ }
+
+ // Static Method
+ //===============
+ public static BasicHep3Matrix passiveXYZ(double alpha, double beta, double gamma)
+ {
+ return (BasicHep3Matrix)mult(passiveZRotation(gamma),mult(passiveYRotation(beta),passiveXRotation(alpha)));
+ }
+
+ // Static Method
+ //===============
+ public static BasicHep3Matrix passiveXRotation(double angle)
+ {
+ double sin = Math.sin(angle);
+ double cos = Math.cos(angle);
+ BasicHep3Matrix rotation = BasicHep3Matrix.identity();
+ rotation.setElement(1,1,cos);
+ rotation.setElement(1,2,sin);
+ rotation.setElement(2,1,-sin);
+ rotation.setElement(2,2,cos);
+ return rotation;
+ }
+
+ // Static Method
+ //===============
+ public static BasicHep3Matrix passiveYRotation(double angle)
+ {
+ double sin = Math.sin(angle);
+ double cos = Math.cos(angle);
+ BasicHep3Matrix rotation = BasicHep3Matrix.identity();
+ rotation.setElement(0,0,cos);
+ rotation.setElement(0,2,-sin);
+ rotation.setElement(2,0,sin);
+ rotation.setElement(2,2,cos);
+ return rotation;
+ }
+
+ // Static Method
+ //===============
+ public static BasicHep3Matrix passiveZRotation(double angle)
+ {
+ double sin = Math.sin(angle);
+ double cos = Math.cos(angle);
+ BasicHep3Matrix rotation = BasicHep3Matrix.identity();
+ rotation.setElement(0,0,cos);
+ rotation.setElement(0,1,sin);
+ rotation.setElement(1,0,-sin);
+ rotation.setElement(1,1,cos);
+ return rotation;
+ }
}
\ No newline at end of file