public class RigidTransform2D
extends java.lang.Object
A rigid 2D real double-precision transform.
This class is intended to be mutable but to enforce that the represented
transform is always rigid. The fields x
and y
give the
translation, and may be modified freely. The rotation is internally
represented three different ways for performance: sine s
, cosine
c
, and an un-normalized CCW rotation angle in radians t
.
Keeping these consistent with each other means that the rotation can only be
read and written through the methods setRotation(double)
and getRotation()
, respectively.
This class is designed primarily for speed of most operations. Arguments
are not checked, but the only invalid arguments would be null pointers.
Synchronization is not provided. A little memory (two extra doubles) are
sacrificed relative to a minimal representation, and the setRotation(double)
method is relatively slow, as it must call
Math.cos()
and Math.sin()
. All other operations
perform only simple algebra, though slightly more bookeeping is required for
the multiple representation of rotation.
Points are transform(java.awt.geom.Point2D.Double)
ed in the usual way: first they are rotated
about the origin of their original coordinate system, and then the
translation vector (x
, y
) is added.
Copyright (C) 2004 Marsette A. Vona, III
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Modifier and Type | Field and Description |
---|---|
protected double |
c
Cosine of the rotation angle
t . |
private static java.lang.String |
cvsid
CVS id.
|
static double |
DEFAULT_RANDOM_TRANSLATION_MAGNITUDE
Default random transformation translation magnitude.
|
protected double |
s
Sine of the rotation angle
t . |
protected double |
t
The CCW rotation angle in radians.
|
double |
x
The x component of translation.
|
double |
y
The y component of translation.
|
Constructor and Description |
---|
RigidTransform2D()
Create a new rigid transform initialized to identity.
|
RigidTransform2D(double t)
Create a new rigid transform initialized to a given rotation and no
translation.
|
RigidTransform2D(double x,
double y)
Create a new rigid transform initialized to a given translation and no
rotation.
|
RigidTransform2D(double x,
double y,
double t)
Create a new rigid transform initialized to a given translation and
rotation.
|
RigidTransform2D(RigidTransform2D o)
Create a new rigid transform initialized from another.
|
Modifier and Type | Method and Description |
---|---|
RigidTransform2D |
append(RigidTransform2D o)
this = o*this |
boolean |
closeToIdentity()
Covers
closeToIdentity(double) but uses default epsilon of
VonaMath.epsilonEquals(double, double) . |
boolean |
closeToIdentity(double epsilon)
Check whether this transform is within epsilon of an identity
transform.
|
RigidTransform2D |
copyFrom(RigidTransform2D o)
this = o |
RigidTransform2D |
dup()
Clone.
|
protected void |
formatField(double value,
java.text.NumberFormat nf,
int fieldWidth,
java.lang.StringBuffer buf)
Format a field for
matrixToString(NumberFormat, int) . |
double |
getRotation()
Get the rotation of this transform.
|
RigidTransform2D |
invert()
this = this' |
static void |
main(java.lang.String[] argv)
Test driver.
|
java.lang.String |
matrixToString(int fieldWidth)
Covers
matrixToString(NumberFormat, int) , uses no
NumberFormat. |
java.lang.String |
matrixToString(java.text.NumberFormat nf,
int fieldWidth)
Convert to a matrix as a multi-line string.
|
RigidTransform2D |
prepend(RigidTransform2D o)
this = this*o |
RigidTransform2D |
reset()
Set the identity transform.
|
static boolean |
selfTest(int trials,
double maxTranslationMagnitude)
Run a randomized self-test.
|
RigidTransform2D |
setIdentity()
Convenience cover of
reset() . |
RigidTransform2D |
setRandom()
Covers
setRandom(double) but uses [-DEFAULT_RANDOM_TRANSLATION_MAGNITUDE ,-DEFAULT_RANDOM_TRANSLATION_MAGNITUDE ] for translation range. |
RigidTransform2D |
setRandom(double maxTranslationMagnitude)
Covers
setRandom(double, double) but uses [-Math.PI,Math.PI]
for rotation range. |
RigidTransform2D |
setRandom(double maxTranslationMagnitude,
double maxRotationMagnitude)
Covers
setRandom(double, double, double) but uses same range
for x and y. |
RigidTransform2D |
setRandom(double maxXMagnitude,
double maxYMagnitude,
double maxRotationMagnitude)
Covers
setRandom(double, double, double, double, double,
double) but uses symmetric ranges. |
RigidTransform2D |
setRandom(double minX,
double maxX,
double minY,
double maxY,
double minRotation,
double maxRotation)
Set to a random transformation.
|
RigidTransform2D |
setRotation(double t)
Set the rotation of this transform.
|
java.lang.String |
toString()
Covers
toString(NumberFormat) , uses no NumberFormat. |
java.lang.String |
toString(java.text.NumberFormat nf)
Convert to a human-readable string.
|
double[] |
transform(double[] p)
Same function as
transform(Point2D.Double) but represents
point as a double array {x, y}. |
float[] |
transform(float[] p)
Same function as
transform(Point2D.Double) but represents
point as a float array {x, y}. |
java.awt.geom.Point2D.Double |
transform(java.awt.geom.Point2D.Double p)
Transform a point.
|
RigidTransform2D |
unappend(RigidTransform2D o)
this = o^(-1)*this |
RigidTransform2D |
unprepend(RigidTransform2D o)
this = this*o^(-1) |
private static final java.lang.String cvsid
CVS id.
public static final double DEFAULT_RANDOM_TRANSLATION_MAGNITUDE
Default random transformation translation magnitude.
protected double c
Cosine of the rotation angle t
.
protected double s
Sine of the rotation angle t
.
protected double t
The CCW rotation angle in radians.
public double x
The x component of translation.
public double y
The y component of translation.
public RigidTransform2D()
Create a new rigid transform initialized to identity.
public RigidTransform2D(RigidTransform2D o)
Create a new rigid transform initialized from another.
o
- the transform to copy, not nullpublic RigidTransform2D(double t)
Create a new rigid transform initialized to a given rotation and no translation.
t
- the CCW rotation in radianspublic RigidTransform2D(double x, double y)
Create a new rigid transform initialized to a given translation and no rotation.
x
- the x component of the translationy
- the y component of the translationpublic RigidTransform2D(double x, double y, double t)
Create a new rigid transform initialized to a given translation and rotation.
x
- the x component of the translationy
- the y component of the translationt
- the CCW rotation in radianspublic RigidTransform2D setRotation(double t)
Set the rotation of this transform.
This is the only relatively slow method in this class, as it must call
Math.cos()
and Math.sin()
.
t
- the CCW rotation in radianspublic double getRotation()
Get the rotation of this transform.
public RigidTransform2D append(RigidTransform2D o)
this = o*this
o
- the other transformpublic RigidTransform2D unappend(RigidTransform2D o)
this = o^(-1)*this
o
- the other transformpublic RigidTransform2D prepend(RigidTransform2D o)
this = this*o
o
- the other transformpublic RigidTransform2D unprepend(RigidTransform2D o)
this = this*o^(-1)
o
- the other transformpublic RigidTransform2D invert()
this = this'
public RigidTransform2D reset()
Set the identity transform.
public RigidTransform2D setIdentity()
Convenience cover of reset()
.
public RigidTransform2D copyFrom(RigidTransform2D o)
this = o
o
- the other transformpublic RigidTransform2D dup()
Clone.
public java.awt.geom.Point2D.Double transform(java.awt.geom.Point2D.Double p)
Transform a point.
The point is transformed in the usual way: first it is rotated about
the origin of its original coordinate system, and then the translation
vector (x
, y
) is added.
This corresponds to a left-multiply by the transform matrix if
p
is taken to be a homogenous column vector (x, y, 1)'.
p
- the point to transformpublic double[] transform(double[] p)
Same function as transform(Point2D.Double)
but represents
point as a double array {x, y}.
public float[] transform(float[] p)
Same function as transform(Point2D.Double)
but represents
point as a float array {x, y}.
public RigidTransform2D setRandom(double minX, double maxX, double minY, double maxY, double minRotation, double maxRotation)
Set to a random transformation.
minX
- the minimum x translationmaxX
- the maximum x translationminY
- the minimum y translationmaxY
- the maximum y translationminRotation
- the minimum rotation in radiansmaxRotation
- the maximum rotation in radianspublic RigidTransform2D setRandom(double maxXMagnitude, double maxYMagnitude, double maxRotationMagnitude)
Covers setRandom(double, double, double, double, double,
double)
but uses symmetric ranges.
public RigidTransform2D setRandom(double maxTranslationMagnitude, double maxRotationMagnitude)
Covers setRandom(double, double, double)
but uses same range
for x and y.
public RigidTransform2D setRandom(double maxTranslationMagnitude)
Covers setRandom(double, double)
but uses [-Math.PI,Math.PI]
for rotation range.
public RigidTransform2D setRandom()
Covers setRandom(double)
but uses [-DEFAULT_RANDOM_TRANSLATION_MAGNITUDE
,-DEFAULT_RANDOM_TRANSLATION_MAGNITUDE
] for translation range.
public boolean closeToIdentity(double epsilon)
Check whether this transform is within epsilon of an identity transform.
epsilon
- the fudge factorpublic boolean closeToIdentity()
Covers closeToIdentity(double)
but uses default epsilon of
VonaMath.epsilonEquals(double, double)
.
public static boolean selfTest(int trials, double maxTranslationMagnitude)
Run a randomized self-test.
trials
- number of trialsmaxTranslationMagnitude
- the maximum translation magnitude for the
random transformspublic static void main(java.lang.String[] argv)
Test driver.
Command line:
RigidTransform2D [numTrials [maxTranslationMagnitude]]
public java.lang.String toString(java.text.NumberFormat nf)
Convert to a human-readable string.
nf
- optional NumberFormat, may be nullpublic java.lang.String toString()
Covers toString(NumberFormat)
, uses no NumberFormat.
toString
in class java.lang.Object
public java.lang.String matrixToString(java.text.NumberFormat nf, int fieldWidth)
Convert to a matrix as a multi-line string.
nf
- optional NumberFormat, may be nullfieldWidth
- matrix column widthpublic java.lang.String matrixToString(int fieldWidth)
Covers matrixToString(NumberFormat, int)
, uses no
NumberFormat.
protected void formatField(double value, java.text.NumberFormat nf, int fieldWidth, java.lang.StringBuffer buf)
Format a field for matrixToString(NumberFormat, int)
.
value
- the field valuenf
- the optional NumberFormat, may be nullfieldWidth
- the field widthbuf
- output is written here