Class AffineTransforms2D
-
public final class AffineTransforms2D extends Static
Bridge betweenMatrixand Java2DAffineTransforminstances. ThoseAffineTransforminstances can be viewed as 3×3 matrices.- Since:
- 0.4
Defined in the
sis-referencingmodule
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static AffineTransformcastOrCopy(Matrix matrix)Returns the given matrix as a Java2D affine transform.static intgetFlip(AffineTransform transform)Returns-1if one axis has been flipped,+1if no axis has been flipped, or 0 if unknown.static doublegetRotation(AffineTransform transform)Returns an estimation of the rotation angle in radians.static doublegetScaleX0(AffineTransform transform)Returns the magnitude of scale factor x by canceling the effect of eventual flip and rotation.static doublegetScaleY0(AffineTransform transform)Returns the magnitude of scale factor y by canceling the effect of eventual flip and rotation.static intgetSwapXY(AffineTransform transform)Returns an estimation about whether the specified transform swaps x and y axes.static Point2DinverseDeltaTransform(AffineTransform transform, Point2D vector, Point2D dest)Calculates the inverse transform of a point without applying the translation components.static Rectangle2DinverseTransform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest)Calculates a rectangle which entirely contains the inverse transform ofbounds.static Matrix3toMatrix(AffineTransform transform)Creates a 3×3 matrix from the given affine transform.static Rectangle2Dtransform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest)Calculates a rectangle which entirely contains the direct transform ofbounds.static Shapetransform(AffineTransform transform, Shape shape, boolean allowOverwrite)Transforms the given shape.
-
-
-
Method Detail
-
castOrCopy
public static AffineTransform castOrCopy(Matrix matrix) throws IllegalArgumentException
Returns the given matrix as a Java2D affine transform. If the given matrix is already an instance ofAffineTransform, then it is returned directly. Otherwise the values are copied in a newAffineTransforminstance.- Parameters:
matrix- the matrix to returns as an affine transform, ornull.- Returns:
- the matrix argument if it can be safely casted (including
nullargument), or a copy of the given matrix otherwise. - Throws:
IllegalArgumentException- if the given matrix size is not 3×3 or if the matrix is not affine.- See Also:
Matrices.isAffine(Matrix)
-
toMatrix
public static Matrix3 toMatrix(AffineTransform transform)
Creates a 3×3 matrix from the given affine transform.- Parameters:
transform- the affine transform to copy as a matrix.- Returns:
- a matrix containing the same terms than the given affine transform.
-
transform
public static Shape transform(AffineTransform transform, Shape shape, boolean allowOverwrite)
Transforms the given shape. This method is similar toAffineTransform.createTransformedShape(Shape)except that:- It tries to preserve the shape kind when possible. For example if the given shape
is an instance of
RectangularShapeand the given transform does not involve rotation, then the returned shape may be some instance of the same class. - It tries to recycle the given object if
overwriteistrue.
- Parameters:
transform- the affine transform to use.shape- the shape to transform, ornull.allowOverwrite- iftrue, this method is allowed to overwriteshapewith the transform result. Iffalse, thenshapeis never modified.- Returns:
- the transform of the given shape, or
nullif the given shape was null. May or may not be the same instance than the given shape. - See Also:
AffineTransform.createTransformedShape(Shape)
- It tries to preserve the shape kind when possible. For example if the given shape
is an instance of
-
transform
public static Rectangle2D transform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest)
Calculates a rectangle which entirely contains the direct transform ofbounds. This operation is equivalent to the following code, except that it can reuse the givendestrectangle and is potentially more efficient:
Note that if the given rectangle is an image bounds, then the given transform shall map the upper-left corner of pixels (as in Java2D usage), not the center of pixels (OGC usage).return transform.createTransformedShape(bounds).getBounds2D();
- Parameters:
transform- the affine transform to use.bounds- the rectangle to transform, ornull. this rectangle will not be modified except ifdestreferences the same object.dest- rectangle in which to place the result. Ifnull, a new rectangle will be created.- Returns:
- the direct transform of the
boundsrectangle, ornullifboundswas null. - See Also:
Shapes2D.transform(MathTransform2D, Rectangle2D, Rectangle2D)
-
inverseTransform
public static Rectangle2D inverseTransform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest) throws NoninvertibleTransformException
Calculates a rectangle which entirely contains the inverse transform ofbounds. This operation is equivalent to the following code, except that it can reuse the givendestrectangle and is potentially more efficient:return createInverse().createTransformedShape(bounds).getBounds2D();
- Parameters:
transform- the affine transform to use.bounds- the rectangle to transform, ornull. this rectangle will not be modified except ifdestreferences the same object.dest- rectangle in which to place the result. Ifnull, a new rectangle will be created.- Returns:
- the inverse transform of the
boundsrectangle, ornullifboundswas null. - Throws:
NoninvertibleTransformException- if the affine transform can't be inverted.
-
inverseDeltaTransform
public static Point2D inverseDeltaTransform(AffineTransform transform, Point2D vector, Point2D dest) throws NoninvertibleTransformException
Calculates the inverse transform of a point without applying the translation components. In other words, calculates the inverse transform of a displacement vector.- Parameters:
transform- the affine transform to use.vector- the vector to transform stored as a point. this point will not be modified except ifdestreferences the same object.dest- point in which to place the result. Ifnull, a new point will be created.- Returns:
- the inverse transform of the
vector, ornullifsourcewas null. - Throws:
NoninvertibleTransformException- if the affine transform can't be inverted.
-
getSwapXY
public static int getSwapXY(AffineTransform transform)
Returns an estimation about whether the specified transform swaps x and y axes. This method assumes that the specified affine transform is built from arbitrary translations, scales or rotations, but no shear. It returns+1if the (x, y) axis order seems to be preserved,-1if the transform seems to swap axis to the (y, x) axis order, or0if this method can not make a decision.- Parameters:
transform- the affine transform to inspect.- Returns:
trueif the given transform seems to swap axis order.
-
getRotation
public static double getRotation(AffineTransform transform)
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:
transform- the affine transform to inspect.- Returns:
- an estimation of the rotation angle in radians,
or
NaNif the angle can not be estimated.
-
getFlip
public static int getFlip(AffineTransform transform)
Returns-1if one axis has been flipped,+1if 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:
This method is similar to the following code, except that this method distinguishes between "unflipped" and "unknown" states.double scaleX0 = getScaleX0(transform); double scaleY0 = getScaleY0(transform); int flip = getFlip(transform); if (flip != 0) { scaleY0 *= flip; // ... continue the process here. }boolean flipped = (tr.getType() & TYPE_FLIP) != 0;
- Parameters:
transform- 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 transform)
Returns the magnitude of scale factor x by canceling the effect of eventual flip and rotation. This factor is calculated by:
- Parameters:
transform- the affine transform to inspect.- Returns:
- the magnitude of scale factor x.
-
getScaleY0
public static double getScaleY0(AffineTransform transform)
Returns the magnitude of scale factor y by canceling the effect of eventual flip and rotation. This factor is calculated by:
- Parameters:
transform- the affine transform to inspect.- Returns:
- the magnitude of scale factor y.
-
-