Object
Static
AffineTransforms2D

public final class AffineTransforms2D extends Static
Bridge between Matrix and Java2D Affine­Transform instances. Those Affine­Transform instances can be viewed as 3×3 matrices. Contains also utility methods operating on Affine­Transform instances.
Since:
0.4
  • Method Details

    • castOrCopy

      public static AffineTransform castOrCopy(MathTransform transform) throws IllegalArgumentException
      Returns the given transform as a Java2D affine transform.
      Parameters:
      transform - the transform to convert, or null.
      Returns:
      the transform argument if it can be safely casted (including null argument) or converted.
      Throws:
      Illegal­Argument­Exception - if the given transform cannot be casted or converted.
      See Also:
    • 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 of Affine­Transform, then it is returned directly. Otherwise the values are copied in a new Affine­Transform instance.
      Parameters:
      matrix - the matrix to return as an affine transform, or null.
      Returns:
      the matrix argument if it can be safely casted (including null argument), or a copy of the given matrix otherwise.
      Throws:
      Illegal­Argument­Exception - if the given matrix size is not 3×3 or if the matrix is not affine.
      See Also:
    • 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.
    • toMathTransform

      public static LinearTransform toMathTransform(AffineTransform transform)
      Creates a math transform from the given affine transform. This method is the converse of cast­Or­Copy(Math­Transform).
      Parameters:
      transform - the affine transform to cast or copy as a Math­Transform, or null.
      Returns:
      a Math­Transform doing the same operation than the given Affine­Transform, or null if the given transform was null.
      Since:
      1.1
      See Also:
    • transform

      public static Shape transform(AffineTransform transform, Shape shape, boolean allowOverwrite)
      Transforms the given shape. This method is similar to Affine­Transform​.create­Transformed­Shape(Shape) except that:
      • It tries to preserve the shape kind when possible. For example if the given shape is an instance of Rectangular­Shape and 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 overwrite is true.
      Parameters:
      transform - the affine transform to use.
      shape - the shape to transform, or null.
      allow­Overwrite - if true, this method is allowed to overwrite shape with the transform result. If false, then shape is never modified.
      Returns:
      the transform of the given shape, or null if the given shape was null. May or may not be the same instance than the given shape.
      See Also:
    • transform

      public static Rectangle2D transform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest)
      Calculates a rectangle which entirely contains the direct transform of bounds. This operation is equivalent to the following code, except that it can reuse the given dest rectangle and is potentially more efficient:
      return transform.createTransformedShape(bounds).getBounds2D();
      
      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).
      Parameters:
      transform - the affine transform to use.
      bounds - the rectangle to transform, or null. this rectangle will not be modified except if dest references the same object.
      dest - rectangle in which to place the result. If null, a new rectangle will be created.
      Returns:
      the direct transform of the bounds rectangle, or null if bounds was null.
      See Also:
    • inverseTransform

      public static Rectangle2D inverseTransform(AffineTransform transform, Rectangle2D bounds, Rectangle2D dest) throws NoninvertibleTransformException
      Calculates a rectangle which entirely contains the inverse transform of bounds. This operation is equivalent to the following code, except that it can reuse the given dest rectangle and is potentially more efficient:
      return createInverse().createTransformedShape(bounds).getBounds2D();
      
      Parameters:
      transform - the affine transform to use.
      bounds - the rectangle to transform, or null. this rectangle will not be modified except if dest references the same object.
      dest - rectangle in which to place the result. If null, a new rectangle will be created.
      Returns:
      the inverse transform of the bounds rectangle, or null if bounds was null.
      Throws:
      Noninvertible­Transform­Exception - if the affine transform cannot 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 if dest references the same object.
      dest - point in which to place the result. If null, a new point will be created.
      Returns:
      the inverse transform of the vector, or null if source was null.
      Throws:
      Noninvertible­Transform­Exception - if the affine transform cannot 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 +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 cannot make a decision.
      Parameters:
      transform - the affine transform to inspect.
      Returns:
      true if 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 Na­N if the angle cannot be estimated.
    • getFlip

      public static int getFlip(AffineTransform transform)
      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 distinguishes between "unflipped" and "unknown" states.
      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:

      Scale factor on x axis

      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:

      Scale factor on y axis

      Parameters:
      transform - 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 get­Scale­X0(tr) and get­Scale­Y0(tr). The way to compute such a "global" scale is somewhat arbitrary and may change in any future version.
      Parameters:
      tr - the affine transform to inspect.
      Returns:
      a "global" scale factor.