Class Transform2D

  • All Implemented Interfaces:
    Serializable, Cloneable

    public class Transform2D
    extends Matrix3d
    A 2D transformation. Is represented internally as a 3x3 floating point matrix. The mathematical representation is row major, as in traditional matrix mathematics.

    The transformation matrix is:

    
     | cos(theta)   | -+sin(theta) | Tx |
     | -+sin(theta) |   cos(theta) | Ty |
     | 0            | 0            | 1  |
     
    Version:
    17.0 2020-01-04 14:41:43
    Author:
    Stéphane GALLAND
    See Also:
    Serialized Form
    Maven Group Id:
    org.arakhne.afc.core
    Maven Artifact Id:
    mathgeom
    • Field Detail

      • IDENTITY

        public static final Transform2D IDENTITY
        This is the identifity transformation.
    • Constructor Detail

      • Transform2D

        public Transform2D()
        Constructs a new Transform2D object and sets it to the identity transformation.
      • Transform2D

        public Transform2D​(Transform2D tranform)
        Constructs a new Transform2D object and initializes it from the specified transform.
        Parameters:
        tranform - the transformation to copy.
      • Transform2D

        public Transform2D​(Matrix3d matrix)
        Constructor by copy.
        Parameters:
        matrix - the matrix to copy.
      • Transform2D

        public Transform2D​(double m00,
                           double m01,
                           double m02,
                           double m10,
                           double m11,
                           double m12)
        Constructs and initializes a Matrix3f from the specified nine values.
        Parameters:
        m00 - the [0][0] element
        m01 - the [0][1] element
        m02 - the [0][2] element
        m10 - the [1][0] element
        m11 - the [1][1] element
        m12 - the [1][2] element
    • Method Detail

      • clone

        @Pure
        public Transform2D clone()
        Description copied from class: Matrix3d
        Creates a new object of the same class as this object.
        Overrides:
        clone in class Matrix3d
        Returns:
        a clone of this instance.
        See Also:
        Cloneable
      • setTranslation

        public void setTranslation​(double x,
                                   double y)
        Set the position.

        This function changes only the elements of the matrix related to the translation (m02, m12). The scaling and the shearing are not changed.

        After a call to this function, the matrix will contains (? means any value):

                  [   ?    ?    x   ]
                  [   ?    ?    y   ]
                  [   ?    ?    ?   ]
         
        Parameters:
        x - x translation.
        y - y translation.
        See Also:
        makeTranslationMatrix(double, double)
      • setTranslation

        public void setTranslation​(Tuple2D<?> translation)
        Set the position.

        This function changes only the elements of the matrix related to the translation (m02, m12). The scaling and the shearing are not changed.

        After a call to this function, the matrix will contains (? means any value):

                  [   ?    ?    t.x   ]
                  [   ?    ?    t.y   ]
                  [   ?    ?    ?     ]
         
        Parameters:
        translation - the translation.
        See Also:
        makeTranslationMatrix(double, double)
      • translate

        public void translate​(double dx,
                              double dy)
        Translate the position.

        This function is equivalent to:

         this = this *  [   1    0    dx   ]
                        [   0    1    dy   ]
                        [   0    0    1    ]
         
        Parameters:
        dx - the x translation
        dy - the y translation
      • translate

        public void translate​(Vector2D<?,​?> translation)
        Translate the position.

        This function is equivalent to:

         this = this *  [   1    0    t.x   ]
                        [   0    1    t.y   ]
                        [   0    0    1     ]
         
        Parameters:
        translation - the translation.
      • getTranslationX

        @Pure
        public double getTranslationX()
        Replies the X translation.
        Returns:
        the amount
      • getTranslationY

        @Pure
        public double getTranslationY()
        Replies the Y translation.
        Returns:
        the amount
      • getTranslationVector

        @Pure
        public void getTranslationVector​(Tuple2D<?> translation)
        Replies the translation.
        Parameters:
        translation - the vector to set with the translation component.
      • rotate

        public void rotate​(double theta)
        Rotate the object (theta).

        This function is equivalent to:

         this = this *  [   cos(theta)    -sin(theta)    0   ]
                        [   sin(theta)    cos(theta)     0   ]
                        [   0             0              1   ]
         
        Parameters:
        theta - the rotation angle, in radians.
      • getScaleRotate2x2

        protected void getScaleRotate2x2​(double[] scales,
                                         double[] rots)
        Perform SVD on the 2x2 matrix containing the rotation and scaling factors.
        Parameters:
        scales - the scaling factors.
        rots - the rotation factors.
      • getRotation

        @Pure
        public double getRotation()
        Performs an SVD normalization of this matrix to calculate and return the rotation angle.
        Returns:
        the rotation angle of this matrix. The value is in [-PI; PI]
      • setRotation

        public void setRotation​(double angle)
        Change the rotation of this matrix. Performs an SVD normalization of this matrix for determining and preserving the scaling.
        Parameters:
        angle - the rotation angle.
      • scale

        public void scale​(double scaleX,
                          double scaleY)
        Concatenates this transform with a scaling transformation.

        This function is equivalent to:

         this = this *  [   sx   0    0   ]
                        [   0    sy   0   ]
                        [   0    0    1   ]
         
        Parameters:
        scaleX - scaling along x axis.
        scaleY - scaling along y axis.
      • scale

        public void scale​(Tuple2D<?> tuple)
        Concatenates this transform with a scaling transformation.

        This function is equivalent to:

         this = this *  [   t.x   0     0   ]
                        [   0     t.y   0   ]
                        [   0     0     1   ]
         
        Parameters:
        tuple - the scaling factors.
      • scale

        public void scale​(double scale)
        Concatenates this transform with a scaling transformation.

        This function is equivalent to:

         this = this *  [   s    0    0   ]
                        [   0    s    0   ]
                        [   0    0    1   ]
         
        Parameters:
        scale - the scaling factor.
      • getScale

        @Pure
        public double getScale()
        Performs an SVD normalization of this matrix to calculate and return the uniform scale factor. If the matrix has non-uniform scale factors, the largest of the x, y scale factors will be returned.
        Returns:
        the scale factor of this matrix.
      • getScaleX

        @Pure
        public double getScaleX()
        Performs an SVD normalization of this matrix to calculate and return the scale factor for X axis.
        Returns:
        the x scale factor.
      • getScaleY

        @Pure
        public double getScaleY()
        Performs an SVD normalization of this matrix to calculate and return the scale factor for Y axis.
        Returns:
        the y scale factor.
      • getScaleVector

        @Pure
        public void getScaleVector​(Tuple2D<?> scale)
        Performs an SVD normalization of this matrix to calculate and return the scale factors for X and Y axess.
        Parameters:
        scale - the tuple to set.
      • setScale

        public void setScale​(double scaleX,
                             double scaleY)
        Change the scale of this matrix. Performs an SVD normalization of this matrix for determining and preserving the rotation.
        Parameters:
        scaleX - the scaling factor along x axis.
        scaleY - the scaling factor along y axis.
        See Also:
        makeScaleMatrix(double, double)
      • setScale

        public void setScale​(Tuple2D<?> tuple)
        Set the scale.

        This function changes only the elements of the matrix related to the scaling (m00, m11). The shearing and the translation are not changed. The rotation is lost.

        After a call to this function, the matrix will contains (? means any value):

                  [   t.x  ?    ?   ]
                  [   ?    t.y  ?   ]
                  [   ?    ?    ?   ]
         
        Parameters:
        tuple - the scaling factors.
        See Also:
        makeScaleMatrix(double, double)
      • shear

        public void shear​(double shearX,
                          double shearY)
        Concatenates this transform with a shearing transformation.

        This function is equivalent to:

         this = this *  [   1    shx  0   ]
                        [   shy  1    0   ]
                        [   0    0    1   ]
         
        Parameters:
        shearX - the shearing factory along x axis.
        shearY - the shearing factory along y axis.
      • shear

        public void shear​(Tuple2D<?> shear)
        Concatenates this transform with a shearing transformation.

        This function is equivalent to:

         this = this *  [   1    shx  0   ]
                        [   shy  1    0   ]
                        [   0    0    1   ]
         
        Parameters:
        shear - the shear factors.
      • makeRotationMatrix

        public void makeRotationMatrix​(double angle)
        Sets the value of this matrix to a counter clockwise rotation about the x axis, and no translation

        This function changes all the elements of the matrix, icluding the translation.

        After a call to this function, the matrix will contains (? means any value):

                  [   cos(theta)  -sin(theta)  0   ]
                  [   sin(theta)  cos(theta)   0   ]
                  [   0           0            1   ]
         
        Parameters:
        angle - the angle to rotate about the X axis in radians
        See Also:
        setRotation(double)
      • makeTranslationMatrix

        public void makeTranslationMatrix​(double dx,
                                          double dy)
        Sets the value of this matrix to the given translation, without rotation.

        This function changes all the elements of the matrix including the scaling and the shearing.

        After a call to this function, the matrix will contains (? means any value):

                  [   1    0    x   ]
                  [   0    1    y   ]
                  [   0    0    1   ]
         
        Parameters:
        dx - is the translation along X.
        dy - is the translation along Y.
        See Also:
        setTranslation(double, double), setTranslation(Tuple2D)
      • makeScaleMatrix

        public void makeScaleMatrix​(double scaleX,
                                    double scaleY)
        Sets the value of this matrix to the given scaling, without rotation.

        This function changes all the elements of the matrix, including the shearing and the translation.

        After a call to this function, the matrix will contains (? means any value):

                  [   sx  0   0   ]
                  [   0   sy  0   ]
                  [   0   0   1   ]
         
        Parameters:
        scaleX - is the scaling along X.
        scaleY - is the scaling along Y.
        See Also:
        setScale(double, double), setScale(Tuple2D)
      • transform

        public void transform​(Tuple2D<?> tuple)
        Multiply this matrix by the tuple t and place the result back into the tuple (t = this*t).
        Parameters:
        tuple - the tuple to be multiplied by this matrix and then replaced
      • transform

        public void transform​(Tuple2D<?> tuple,
                              Tuple2D<?> result)
        Multiply this matrix by the tuple t and and place the result into the tuple "result".

        This function is equivalent to:

         result = this *  [   t.x   ]
                          [   t.y   ]
                          [   1     ]
         
        Parameters:
        tuple - the tuple to be multiplied by this matrix
        result - the tuple into which the product is placed
      • createInverse

        @Pure
        public Transform2D createInverse()
        Returns an Transform2D object representing the inverse transformation. The inverse transform Tx' of this transform Tx maps coordinates transformed by Tx back to their original coordinates. In other words, Tx'(Tx(p)) = p = Tx(Tx'(p)).

        If this transform maps all coordinates onto a point or a line then it will not have an inverse, since coordinates that do not lie on the destination point or line will not have an inverse mapping. The determinant method can be used to determine if this transform has no inverse, in which case an exception will be thrown if the createInverse method is called.

        Returns:
        a new Transform2D object representing the inverse transformation.
        See Also:
        Matrix3d.determinant()
      • set

        public void set​(double m00,
                        double m01,
                        double m02,
                        double m10,
                        double m11,
                        double m12)
        Set the components of the transformation.
        Parameters:
        m00 - the [0][0] element
        m01 - the [0][1] element
        m02 - the [0][2] element
        m10 - the [1][0] element
        m11 - the [1][1] element
        m12 - the [1][2] element
      • invert

        public void invert()
        Invert this transformation. The inverse transform Tx' of this transform Tx maps coordinates transformed by Tx back to their original coordinates. In other words, Tx'(Tx(p)) = p = Tx(Tx'(p)).

        If this transform maps all coordinates onto a point or a line then it will not have an inverse, since coordinates that do not lie on the destination point or line will not have an inverse mapping. The determinant method can be used to determine if this transform has no inverse, in which case an exception will be thrown if the createInverse method is called.

        Overrides:
        invert in class Matrix3d
        See Also:
        Matrix3d.determinant()
      • invert

        public void invert​(Matrix3d matrix)
        Invert this transformation. The inverse transform Tx' of this transform Tx maps coordinates transformed by Tx back to their original coordinates. In other words, Tx'(Tx(p)) = p = Tx(Tx'(p)).

        If this transform maps all coordinates onto a point or a line then it will not have an inverse, since coordinates that do not lie on the destination point or line will not have an inverse mapping. The determinant method can be used to determine if this transform has no inverse, in which case an exception will be thrown if the createInverse method is called.

        Overrides:
        invert in class Matrix3d
        Parameters:
        matrix - is the matrix to invert
        See Also:
        Matrix3d.determinant()