CubeTwister 2.0alpha142 2012-02-11

ch.randelshofer.geom3d
Class Transform3D

java.lang.Object
  extended by ch.randelshofer.geom3d.Transform3D
All Implemented Interfaces:
java.lang.Cloneable

public class Transform3D
extends java.lang.Object
implements java.lang.Cloneable

This class represents a 3D transform which performs a mapping from 3D coordinates to other 3D coordinates.

Such a coordinate transformation can be represented by a 4 row by 4 column matrix with an implied last row of [ 0 0 0 1 ] which transforms source coordinates (x, y) into destination coordinates (x', y') by considering them to be a column vector and multiplying the coordinate vector by the matrix according to the following process.

 [ x'] = [  m00  m10  m20  m30 ] [ x ] = [ m00x + m10y + m20z + m30 ]
 [ y'] = [  m01  m11  m21  m31 ] [ y ] = [ m01x + m11y + m21z + m31 ]
 [ z'] = [  m02  m12  m22  m32 ] [ z ] = [ m02x + m12y + m22z + m32 ]
 [ 1 ] = [   0    0    0    1  ] [ 1 ] = [             1            ]
 

Version:
1.3 2009-01-04 Added method fromToRotation.
1.2 2006-01-05 Method hashCode added.
1.1 2004-08-23 Workaround for VirtualPC removed.
1.0 2002-05-06 Method setTransform added.
0.4 2001-10-27 Method isNaN added. FIXME Workaround for VirtualPC Math.sin and Math.cos produce NaN: Use of Math instead of Math avoids this.
0.3.1 2000-03-25 Method rotate(double, double, double) fixed.
0.3 2000-02-27 Comments revised.
0.2 1999-01-01 Package renamed.
0.1 1998-12-15 Interface revised.
0.0 1998-02-15 Created.
Author:
Werner Randelshofer, Hausmatt 10, CH-6405 Immensee, Switzerland

Field Summary
 double m00
           
 double m01
           
 double m02
           
 double m10
           
 double m11
           
 double m12
           
 double m20
           
 double m21
           
 double m22
           
 double m30
           
 double m31
           
 double m32
           
 
Constructor Summary
Transform3D()
          Constructs a new Transform3D representing the Identity transformation.
Transform3D(double[][] matrix)
          Constructs a new transform from 12 double precision values representing the 12 specifiable entries of the 4x4 transformation matrix.
Transform3D(double rx, double ry, double rz)
          Constructs a Transform3D representing the specified rotation transformation.
Transform3D(double m00, double m10, double m20, double m30, double m01, double m11, double m21, double m31, double m02, double m12, double m22, double m32)
          Constructs a new transform from 12 double precision values representing the 12 specifiable entries of the 4x4 transformation matrix.
 
Method Summary
 java.lang.Object clone()
          Creates a clone of the object.
 void concatenate(Transform3D t)
          Concatenates a transform Tx to this transform Cx.
 boolean equals(java.lang.Object obj)
          Compares two Objects for equality.
static Transform3D fromToRotation(Point3D from, Point3D to)
           
 Transform3D getInverse()
           
 double[][] getMatrix()
          Returns the complete 4x4 matrix representing this transform.
 int hashCode()
           
 boolean isNaN()
          Returns true, when at least one of the elements of the transform is NaN.
 void rotate(double rx, double ry, double rz)
          Concatenates this transform with a rotation transformation.
 void rotateX(double rx)
          Concatenates this transform with a rotation transformation.
 void rotateY(double ry)
          Concatenates this transform with a rotation transformation.
 void rotateZ(double rz)
          Concatenates this transform with a rotation transformation.
 void scale(double sx, double sy, double sz)
          Concatenates this transform with a scaling transformation.
 void setToIdentity()
          Resets this transform to the Identity transform.
 void setTransform(Transform3D Tx)
          Sets this transform to a copy of the transform in the specified Transform3D object.
 java.lang.String toString()
           
 void transform(float[] src, int srcOffset, float[] dest, int destOffset, int count)
          Transforms the vertices of source array and stores the result in the dest array.
 void transform(Point3D pt)
          Transforms the specified point.
 Point3D transform(Point3D ptSrc, Point3D ptDst)
          Transforms the specified ptSrc and stores the result in ptDst.
 Polygon3D transform(Polygon3D polySrc, Polygon3D polyDst)
          Transforms the specified polySrc and stores the result in polyDst.
 void translate(double tx, double ty, double tz)
          Concatenates this transform with a translation transformation.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

m00

public double m00

m10

public double m10

m20

public double m20

m30

public double m30

m01

public double m01

m11

public double m11

m21

public double m21

m31

public double m31

m02

public double m02

m12

public double m12

m22

public double m22

m32

public double m32
Constructor Detail

Transform3D

public Transform3D()
Constructs a new Transform3D representing the Identity transformation.


Transform3D

public Transform3D(double rx,
                   double ry,
                   double rz)
Constructs a Transform3D representing the specified rotation transformation.

Parameters:
rx - Rotation along the x axis in radians.
ry - Rotation along the y axis in radians.
rz - Rotation along the z axis in radians.

Transform3D

public Transform3D(double m00,
                   double m10,
                   double m20,
                   double m30,
                   double m01,
                   double m11,
                   double m21,
                   double m31,
                   double m02,
                   double m12,
                   double m22,
                   double m32)
Constructs a new transform from 12 double precision values representing the 12 specifiable entries of the 4x4 transformation matrix.


Transform3D

public Transform3D(double[][] matrix)
Constructs a new transform from 12 double precision values representing the 12 specifiable entries of the 4x4 transformation matrix.

Method Detail

setToIdentity

public void setToIdentity()
Resets this transform to the Identity transform.


rotate

public void rotate(double rx,
                   double ry,
                   double rz)
Concatenates this transform with a rotation transformation. This is equivalent to calling concatenate(R), where R is an Transform3D represented by the following matrix:
    [ cos(rz)*cos(ry)     cos(rz)*sin(ry)*sin(rx)     cos(rz)*-sin(ry)*cos(rx)    0 ]
                            +sin(rz)*cos(rx)            +sin(rz)*-sin(ry)*cos(rx)

    [ -sin(rz)*cos(ry)    -sin(rz)*sin(ry)*sin(rx)    -sin(rz)*-sin(ry)*cos(rx)   0 ]
                            +cos(rz)*cos(rx)            +cos(rz)*sin(rx)

    [ sin(ry)             cos(ry)*-sin(rx)            cos(ry)*cos(rx)            0 ]

      [ 0                   0                           0                        1 ]
 
Positive values rotate counterclockwise.

Parameters:
rx - Rotation along the x axis in radians.
ry - Rotation along the y axis in radians.
rz - Rotation along the z axis in radians.

rotateX

public void rotateX(double rx)
Concatenates this transform with a rotation transformation. This is equivalent to calling concatenate(R), where R is an Transform3D represented by the following matrix:
    [ 1   0         0        0 ]
    [ 0   cos(rx)    sin(rx)   0 ]
    [ 0   -sin(rx)   cos(rx)   0 ]
      [ 0   0         0        1 ]
 
Positive values rotate counterclockwise.

Parameters:
rx - Rotation along the x axis in radians.

rotateY

public void rotateY(double ry)
Concatenates this transform with a rotation transformation. This is equivalent to calling concatenate(R), where R is an Transform3D represented by the following matrix:
    [ cos(ry)   0   -sin(ry)   0 ]
    [ 0        1   0         0 ]
    [ sin(ry)   0   cos(ry)    0 ]
      [ 0        0   0         1 ]
 
Positive values rotate counterclockwise.

Parameters:
ry - Rotation along the y axis in radians.

rotateZ

public void rotateZ(double rz)
Concatenates this transform with a rotation transformation. This is equivalent to calling concatenate(R), where R is an Transform3D represented by the following matrix:
    [ cos(rz)    sin(rz)   0   0 ]
    [ -sin(rz)   cos(rz)   0   0 ]
    [ 0         0        1   0 ]
      [ 0         0        0   1 ]
 
Positive values rotate counterclockwise.

Parameters:
rz - Rotation along the z axis in radians.

scale

public void scale(double sx,
                  double sy,
                  double sz)
Concatenates this transform with a scaling transformation. This is equivalent to calling concatenate(S), where S is an Transform3D represented by the following matrix:
    [   sx   0    0    0   ]
    [   0    sy   0    0   ]
    [   0    0    sz   0   ]
    [   0    0    0    1   ]
 


translate

public void translate(double tx,
                      double ty,
                      double tz)
Concatenates this transform with a translation transformation. This is equivalent to calling concatenate(T), where T is an Transform3D represented by the following matrix:
    [   1    0    0    tx  ]
    [   0    1    0    ty  ]
    [   0    0    1    tz  ]
    [   0    0    0    1   ]
 


concatenate

public void concatenate(Transform3D t)
Concatenates a transform Tx to this transform Cx. Cx is updated to perform the combined transformation. Transforming a point p by the updated transform Cx' is equivalent to first transforming p by Tx and then transforming the result by the original transform Cx. In other words, Cx'(p) = Cx(Tx(p)). In matrix notation, if this transform Cx is represented by the matrix [this] and Tx is represented by the matrix [Tx], then this method does the following:
    [this] = [this] x [Tx]
 

Parameters:
t - The transform object to be concatenated with this transform object.

getMatrix

public double[][] getMatrix()
Returns the complete 4x4 matrix representing this transform.


equals

public boolean equals(java.lang.Object obj)
Compares two Objects for equality. Returns a boolean that indicates whether this Object is equivalent to the specified Object. This method is used when an Object is stored in a hashtable.

Overrides:
equals in class java.lang.Object
Parameters:
obj - the Object to compare with
Returns:
true if these Objects are equal; false otherwise.
See Also:
Hashtable

transform

public void transform(Point3D pt)
Transforms the specified point.


transform

public Point3D transform(Point3D ptSrc,
                         Point3D ptDst)
Transforms the specified ptSrc and stores the result in ptDst. If ptDst is null, a new Point3D object will be allocated before storing. In either case, ptDst containing the transformed point is returned for convenience. Note that ptSrc and ptDst can be the same. In this case, the input point will be overwritten with the transformed point.


transform

public Polygon3D transform(Polygon3D polySrc,
                           Polygon3D polyDst)
Transforms the specified polySrc and stores the result in polyDst. If polyDst is null, a new Polygon3D object will be allocated before storing. In either case, polyDst containing the transformed polygon is returned for convenience. Note that polySrc and polyDst can be the same. In this case, the input point will be overwritten with the transformed point. When the polyDst does not have enough capacity to store the data, its capacity will be increased.


transform

public void transform(float[] src,
                      int srcOffset,
                      float[] dest,
                      int destOffset,
                      int count)
Transforms the vertices of source array and stores the result in the dest array.

Parameters:
src - Each group of three entries in this array describe a vector x, y, z.
srcOffset - Index of the first vector.
dest - Each group of three entries in this array describe a vector x, y, z.
destOffset - Index of the first vector.
count - Number of vectors to be transformed.

clone

public java.lang.Object clone()
Creates a clone of the object. A new instance is allocated and a bitwise clone of the current object is place in the new object.

Overrides:
clone in class java.lang.Object
Returns:
a clone of this Object.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

isNaN

public boolean isNaN()
Returns true, when at least one of the elements of the transform is NaN.


setTransform

public void setTransform(Transform3D Tx)
Sets this transform to a copy of the transform in the specified Transform3D object.

Parameters:
Tx - the Transform3D object from which to copy the transform

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

fromToRotation

public static Transform3D fromToRotation(Point3D from,
                                         Point3D to)

getInverse

public Transform3D getInverse()

(c) Werner Randelshofer.
All rights reserved.