org.geotoolkit.referencing.operation.matrix
Class XAffineTransform

Object
  extended by AffineTransform
      extended by XAffineTransform
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
AffineTransform2D

public class XAffineTransform
extends AffineTransform

Utility methods for affine transforms. This class provides two kind of services:

Since:
1.2
Version:
3.14
Author:
Martin Desruisseaux (IRD, Geomatys)
See Also:
Serialized Form
Module:
referencing/geotk-referencing (download)    View source code for this class

Field Summary
 
Fields inherited from class AffineTransform
TYPE_FLIP, TYPE_GENERAL_ROTATION, TYPE_GENERAL_SCALE, TYPE_GENERAL_TRANSFORM, TYPE_IDENTITY, TYPE_MASK_ROTATION, TYPE_MASK_SCALE, TYPE_QUADRANT_ROTATION, TYPE_TRANSLATION, TYPE_UNIFORM_SCALE
 
Constructor Summary
protected XAffineTransform()
          Constructs an identity affine transform.
  XAffineTransform(AffineTransform tr)
          Constructs a new XAffineTransform that is a copy of the specified AffineTransform object.
  XAffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)
          Constructs a new XAffineTransform from 6 values representing the 6 specifiable entries of the 3×3 transformation matrix.
 
Method Summary
protected  void checkPermission()
          Checks if the caller is allowed to change this XAffineTransform state.
 void concatenate(AffineTransform Tx)
          Checks for permission before concatenating this transform.
static int getFlip(AffineTransform tr)
          Returns -1 if one axis has been flipped, +1 if no axis has been flipped, or 0 if unknown.
static double getRotation(AffineTransform tr)
          Returns an estimation of the rotation angle in radians.
static double getScale(AffineTransform tr)
          Returns a global scale factor for the specified affine transform.
static AffineTransform getScaleInstance(double sx, double sy, double anchorx, double anchory)
          Returns an affine transform representing a zoom carried out around a anchor point (x, y).
static double getScaleX0(AffineTransform tr)
          Returns the magnitude of scale factor x by cancelling the effect of eventual flip and rotation.
static double getScaleY0(AffineTransform tr)
          Returns the magnitude of scale factor y by cancelling the effect of eventual flip and rotation.
static int getSwapXY(AffineTransform tr)
          Returns an estimation about whatever the specified transform swaps x and y axis.
static Point2D inverseDeltaTransform(AffineTransform transform, Point2D source, Point2D dest)
          Calculates the inverse affine transform of a point without without applying the translation components.
static Rectangle2D inverseTransform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest)
          Returns a rectangle which entirely contains the inverse transform of bounds.
 void invert()
          Checks for permission before inverting this transform.
static boolean isIdentity(AffineTransform tr, double tolerance)
          Returns true if the specified affine transform is an identity transform up to the specified tolerance.
 boolean isIdentity(double tolerance)
          Checks whether or not this XAffineTransform is the identity by using the provided tolerance.
 void preConcatenate(AffineTransform Tx)
          Checks for permission before concatenating this transform.
 void quadrantRotate(int numquadrants)
          Checks for permission before rotating this transform.
 void quadrantRotate(int numquadrants, double anchorx, double anchory)
          Checks for permission before rotating this transform.
 void rotate(double theta)
          Checks for permission before rotating this transform.
 void rotate(double vecx, double vecy)
          Checks for permission before rotating this transform.
 void rotate(double theta, double anchorx, double anchory)
          Checks for permission before rotating this transform.
 void rotate(double vecx, double vecy, double anchorx, double anchory)
          Checks for permission before rotating this transform.
static void roundIfAlmostInteger(AffineTransform tr, double tolerance)
          If scale and shear coefficients are close to integers, replaces their current values by their rounded values.
 void scale(double sx, double sy)
          Checks for permission before scaling this transform.
 void setToIdentity()
          Checks for permission before setting this transform.
 void setToQuadrantRotation(int numquadrants)
          Checks for permission before setting this transform.
 void setToQuadrantRotation(int numquadrants, double anchorx, double anchory)
          Checks for permission before setting this transform.
 void setToRotation(double theta)
          Checks for permission before setting this transform.
 void setToRotation(double vecx, double vecy)
          Checks for permission before setting this transform.
 void setToRotation(double theta, double anchorx, double anchory)
          Checks for permission before setting this transform.
 void setToRotation(double vecx, double vecy, double anchorx, double anchory)
          Checks for permission before setting this transform.
 void setToScale(double sx, double sy)
          Checks for permission before setting this transform.
 void setToShear(double shx, double shy)
          Checks for permission before setting this transform.
 void setToTranslation(double tx, double ty)
          Checks for permission before setting this transform.
 void setTransform(AffineTransform Tx)
          Checks for permission before setting this transform.
 void setTransform(double m00, double m10, double m01, double m11, double m02, double m12)
          Checks for permission before setting this transform.
 void shear(double shx, double shy)
          Checks for permission before shearing this transform.
static Rectangle2D transform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest)
          Returns a rectangle which entirely contains the direct transform of bounds.
static Shape transform(AffineTransform transform, Shape shape, boolean overwrite)
          Transforms the given shape.
 void translate(double tx, double ty)
          Checks for permission before translating this transform.
 
Methods inherited from class AffineTransform
clone, createInverse, createTransformedShape, deltaTransform, deltaTransform, equals, getDeterminant, getMatrix, getQuadrantRotateInstance, getQuadrantRotateInstance, getRotateInstance, getRotateInstance, getRotateInstance, getRotateInstance, getScaleInstance, getScaleX, getScaleY, getShearInstance, getShearX, getShearY, getTranslateInstance, getTranslateX, getTranslateY, getType, hashCode, inverseTransform, inverseTransform, isIdentity, toString, transform, transform, transform, transform, transform, transform
 
Methods inherited from class Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XAffineTransform

protected XAffineTransform()
Constructs an identity affine transform. This is for subclasses usage only, for example subclasses that override the checkPermission() method.
Note: This constructor is protected rather than public in order to avoid accidental instantation of identity transforms that are not of some subclass, which is likely to be useless since XAffineTransform is immutable.

Since:
3.00

XAffineTransform

public XAffineTransform(AffineTransform tr)
Constructs a new XAffineTransform that is a copy of the specified AffineTransform object.

Parameters:
tr - The affine transform to copy.

XAffineTransform

public XAffineTransform(double m00,
                        double m10,
                        double m01,
                        double m11,
                        double m02,
                        double m12)
Constructs a new XAffineTransform from 6 values representing the 6 specifiable entries of the 3×3 transformation matrix. Those values are given unchanged to the super class constructor.

Parameters:
m00 - the X coordinate scaling.
m10 - the Y coordinate shearing.
m01 - the X coordinate shearing.
m11 - the Y coordinate scaling.
m02 - the X coordinate translation.
m12 - the Y coordinate translation.
Since:
2.5
Method Detail

checkPermission

protected void checkPermission()
                        throws UnsupportedOperationException
Checks if the caller is allowed to change this XAffineTransform state. If this method is defined to thrown an exception in all case, then this XAffineTransform is immutable.

The default implementation throws the exception in all case, thus making this instance immutable.

Throws:
UnsupportedOperationException - if this affine transform is immutable.

translate

public void translate(double tx,
                      double ty)
Checks for permission before translating this transform.

Overrides:
translate in class AffineTransform

rotate

public void rotate(double theta)
Checks for permission before rotating this transform.

Overrides:
rotate in class AffineTransform

rotate

public void rotate(double theta,
                   double anchorx,
                   double anchory)
Checks for permission before rotating this transform.

Overrides:
rotate in class AffineTransform

rotate

public void rotate(double vecx,
                   double vecy)
Checks for permission before rotating this transform.

Overrides:
rotate in class AffineTransform

rotate

public void rotate(double vecx,
                   double vecy,
                   double anchorx,
                   double anchory)
Checks for permission before rotating this transform.

Overrides:
rotate in class AffineTransform

quadrantRotate

public void quadrantRotate(int numquadrants)
Checks for permission before rotating this transform.

Overrides:
quadrantRotate in class AffineTransform

quadrantRotate

public void quadrantRotate(int numquadrants,
                           double anchorx,
                           double anchory)
Checks for permission before rotating this transform.

Overrides:
quadrantRotate in class AffineTransform

scale

public void scale(double sx,
                  double sy)
Checks for permission before scaling this transform.

Overrides:
scale in class AffineTransform

shear

public void shear(double shx,
                  double shy)
Checks for permission before shearing this transform.

Overrides:
shear in class AffineTransform

setToIdentity

public void setToIdentity()
Checks for permission before setting this transform.

Overrides:
setToIdentity in class AffineTransform

setToTranslation

public void setToTranslation(double tx,
                             double ty)
Checks for permission before setting this transform.

Overrides:
setToTranslation in class AffineTransform

setToRotation

public void setToRotation(double theta)
Checks for permission before setting this transform.

Overrides:
setToRotation in class AffineTransform

setToRotation

public void setToRotation(double theta,
                          double anchorx,
                          double anchory)
Checks for permission before setting this transform.

Overrides:
setToRotation in class AffineTransform

setToRotation

public void setToRotation(double vecx,
                          double vecy)
Checks for permission before setting this transform.

Overrides:
setToRotation in class AffineTransform

setToRotation

public void setToRotation(double vecx,
                          double vecy,
                          double anchorx,
                          double anchory)
Checks for permission before setting this transform.

Overrides:
setToRotation in class AffineTransform

setToQuadrantRotation

public void setToQuadrantRotation(int numquadrants)
Checks for permission before setting this transform.

Overrides:
setToQuadrantRotation in class AffineTransform

setToQuadrantRotation

public void setToQuadrantRotation(int numquadrants,
                                  double anchorx,
                                  double anchory)
Checks for permission before setting this transform.

Overrides:
setToQuadrantRotation in class AffineTransform

setToScale

public void setToScale(double sx,
                       double sy)
Checks for permission before setting this transform.

Overrides:
setToScale in class AffineTransform

setToShear

public void setToShear(double shx,
                       double shy)
Checks for permission before setting this transform.

Overrides:
setToShear in class AffineTransform

setTransform

public void setTransform(AffineTransform Tx)
Checks for permission before setting this transform.

Overrides:
setTransform in class AffineTransform

setTransform

public void setTransform(double m00,
                         double m10,
                         double m01,
                         double m11,
                         double m02,
                         double m12)
Checks for permission before setting this transform.

Overrides:
setTransform in class AffineTransform

concatenate

public void concatenate(AffineTransform Tx)
Checks for permission before concatenating this transform.

Overrides:
concatenate in class AffineTransform

preConcatenate

public void preConcatenate(AffineTransform Tx)
Checks for permission before concatenating this transform.

Overrides:
preConcatenate in class AffineTransform

invert

public void invert()
            throws NoninvertibleTransformException
Checks for permission before inverting this transform.

Overrides:
invert in class AffineTransform
Throws:
NoninvertibleTransformException

isIdentity

public boolean isIdentity(double tolerance)
Checks whether or not this XAffineTransform is the identity by using the provided tolerance.

Parameters:
tolerance - The tolerance to use for this check.
Returns:
true if the transform is identity, false otherwise.
Since:
2.3.1

isIdentity

public static boolean isIdentity(AffineTransform tr,
                                 double tolerance)
Returns true if the specified affine transform is an identity transform up to the specified tolerance. This method is equivalent to computing the difference between this matrix and an identity matrix (as created by new AffineTransform()) and returning true if and only if all differences are smaller than or equal to tolerance.

This method is used for working around rounding error in affine transforms resulting from a computation, as in the example below:

┌                                                     ┐
│ 1.0000000000000000001  0.0                      0.0 │
│ 0.0                    0.999999999999999999999  0.0 │
│ 0.0                    0.0                      1.0 │
└                                                     ┘

Parameters:
tr - The affine transform to be checked for identity.
tolerance - The tolerance value to use when checking for identity.
Returns:
true if this transformation is close enough to the identity, false otherwise.
Since:
2.3.1

transform

public static Shape transform(AffineTransform transform,
                              Shape shape,
                              boolean overwrite)
Transforms the given shape. This method is similar to createTransformedShape except that:

Parameters:
transform - Affine transform to use.
shape - The shape to transform.
overwrite - If true, this method is allowed to overwrite shape with the transform result. If false, then shape is never modified.
Returns:
The direct transform of the given shape. May or may not be the same instance than the given shape.
Since:
2.5
See Also:
AffineTransform.createTransformedShape(java.awt.Shape)

transform

public static Rectangle2D transform(AffineTransform transform,
                                    Rectangle2D bounds,
                                    Rectangle2D dest)
Returns a rectangle which entirely contains the direct transform of bounds. This operation is equivalent to:
createTransformedShape(bounds).getBounds2D()
Note that if the source rectangle is an image bounds and the affine transform is a grid to CRS transform (i.e. if it converts pixel coordinates to "real world" coordinates), then that transform must map the upper-left corner of pixels (as in Java2D usage), not the center of pixels (OGC usage), if the intend is to get the image envelope.

Parameters:
transform - Affine transform to use.
bounds - Rectangle to transform. This rectangle will not be modified except if dest is the same reference.
dest - Rectangle in which to place the result. If null, a new rectangle will be created.
Returns:
The direct transform of the bounds rectangle.
See Also:
CRS.transform(MathTransform2D, Rectangle2D, Rectangle2D)

inverseTransform

public static Rectangle2D inverseTransform(AffineTransform transform,
                                           Rectangle2D bounds,
                                           Rectangle2D dest)
                                    throws NoninvertibleTransformException
Returns a rectangle which entirely contains the inverse transform of bounds. This operation is equivalent to:
createInverse().createTransformedShape(bounds).getBounds2D()

Parameters:
transform - Affine transform to use.
bounds - Rectangle to transform. This rectangle will not be modified.
dest - Rectangle in which to place the result. If null, a new rectangle will be created.
Returns:
The inverse transform of the bounds rectangle.
Throws:
NoninvertibleTransformException - if the affine transform can't be inverted.

inverseDeltaTransform

public static Point2D inverseDeltaTransform(AffineTransform transform,
                                            Point2D source,
                                            Point2D dest)
                                     throws NoninvertibleTransformException
Calculates the inverse affine transform of a point without without applying the translation components.

Parameters:
transform - Affine transform to use.
source - Point to transform. This rectangle will not be modified.
dest - Point in which to place the result. If null, a new point will be created.
Returns:
The inverse transform of the source point.
Throws:
NoninvertibleTransformException - if the affine transform can't be inverted.

getSwapXY

public static int getSwapXY(AffineTransform tr)
Returns an estimation about whatever the specified transform swaps x and y axis. This method assumes that the specified affine transform is built from arbitrary translations, scales or rotations, but no shear. It returns +1 if the (x, y) axis order seems to be preserved, -1 if the transform seems to swap axis to the (y, x) axis order, or 0 if this method can not make a decision.

Parameters:
tr - The affine transform to inspect.
Returns:
true if the given transform seems to swap axis order.

getRotation

public static double getRotation(AffineTransform tr)
Returns an estimation of the rotation angle in radians. This method assumes that the specified affine transform is built from arbitrary translations, scales or rotations, but no shear. If a flip has been applied, then this method assumes that the flipped axis is the y one in source CRS space. For a grid to world CRS transform, this is the row number in grid coordinates.

Parameters:
tr - The affine transform to inspect.
Returns:
An estimation of the rotation angle in radians, or NaN if the angle can not be estimated.

getFlip

public static int getFlip(AffineTransform tr)
Returns -1 if one axis has been flipped, +1 if no axis has been flipped, or 0 if unknown. A flipped axis in an axis with direction reversed (typically the y axis). This method assumes that the specified affine transform is built from arbitrary translations, scales or rotations, but no shear. Note that it is not possible to determine which of the x or y axis has been flipped.

This method can be used in order to set the sign of a scale according the flipping state. The example below choose to apply the sign on the y scale, but this is an arbitrary (while common) choice:

double scaleX0 = getScaleX0(transform);
double scaleY0 = getScaleY0(transform);
int    flip    = getFlip(transform);
if (flip != 0) {
    scaleY0 *= flip;
    // ... continue the process here.
}
This method is similar to the following code, except that this method distinguish between "unflipped" and "unknown" states.
boolean flipped = (tr.getType() & TYPE_FLIP) != 0;

Parameters:
tr - The affine transform to inspect.
Returns:
-1 if an axis has been flipped, +1 if no flipping, or 0 if unknown.

getScaleX0

public static double getScaleX0(AffineTransform tr)
Returns the magnitude of scale factor x by cancelling the effect of eventual flip and rotation. This factor is calculated by:

Parameters:
tr - The affine transform to inspect.
Returns:
The magnitude of scale factor x.

getScaleY0

public static double getScaleY0(AffineTransform tr)
Returns the magnitude of scale factor y by cancelling the effect of eventual flip and rotation. This factor is calculated by:

Parameters:
tr - The affine transform to inspect.
Returns:
The magnitude of scale factor y.

getScale

public static double getScale(AffineTransform tr)
Returns a global scale factor for the specified affine transform. This scale factor combines getScaleX0(tr) and getScaleY0(tr). The way to compute such a "global" scale is somewhat arbitrary and may change in a future version.

Parameters:
tr - The affine transform to inspect.
Returns:
The magnitude of scale factory.

getScaleInstance

public static AffineTransform getScaleInstance(double sx,
                                               double sy,
                                               double anchorx,
                                               double anchory)
Returns an affine transform representing a zoom carried out around a anchor point (x, y). The transforms will leave the specified anchor coordinate unchanged.

Parameters:
sx - Scale along x axis.
sy - Scale along y axis.
anchorx - x coordinates of the anchor point.
anchory - y coordinates of the anchor point.
Returns:
Affine transform of a zoom which leaves the anchor (x,y) coordinate unchanged.

roundIfAlmostInteger

public static void roundIfAlmostInteger(AffineTransform tr,
                                        double tolerance)
If scale and shear coefficients are close to integers, replaces their current values by their rounded values. The scale and shear coefficients are handled in a "all or nothing" way; either all of them are rounded, or either none of them. The translation terms are handled separately, provided that the scale and shear coefficients has been rounded.

This rounding up is useful for example in order to speedup image displays.

Parameters:
tr - The matrix to round. Rounding will be applied in place.
tolerance - The maximal departure from integers in order to allow rounding. It is typically a small number like 1E-6.
Since:
3.14 (derived from 2.3.1)
See Also:
XMath.roundIfAlmostInteger(double, int), MetadataHelper


Copyright © 2009-2011 Geotoolkit.org. All Rights Reserved.