Class Transform2d

java.lang.Object
org.djutils.draw.Transform2d
All Implemented Interfaces:
Cloneable

public class Transform2d extends Object implements Cloneable
Transform2d contains a MUTABLE transformation object that can transform points (x,y) based on e.g, rotation and translation. It uses an affine transform matrix that can be built up from different components (translation, rotation, scaling, reflection, shearing).

Copyright (c) 2020-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See DJUTILS License.

Author:
Alexander Verbraeck, Peter Knoppers
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    double[]
    Get a safe copy of the affine transformation matrix.
    int
    protected static double[]
    mulMatMat(double[] m1, double[] m2)
    Multiply a 3x3 matrix (stored as a 9-value array by row) with another 3x3-matrix.
    protected static double[]
    mulMatVec(double[] m, double[] v)
    Multiply a 3x3 matrix (stored as a 9-value array by row) with a 4-value vector.
    protected static double[]
    mulMatVec2(double[] m, double[] v)
    Multiply a 3x3 matrix (stored as a 9-value array by row) with a 3-value vector and a 1 for the 3rd value.
    The reflection of the x-coordinate, by mirroring it in the yz-plane (the plane with x=0).
    The reflection of the y-coordinate, by mirroring it in the xz-plane (the plane with y=0).
    rotation(double angle)
    The rotation around the origin with an angle in radians.
    scale(double sx, double sy)
    Scale all coordinates with a factor for x, and y.
    shear(double sx, double sy)
    The 2d shear leaves the xy-coordinate plane for z=0 untouched.
    double[]
    transform(double[] xy)
    Apply the stored transform on the xy-vector and return the transformed vector.
    transform(Iterator<Point2d> pointIterator)
    Apply the stored transform on the provided point and return a point with the transformed coordinate.
    transform(Bounds2d boundingRectangle)
    Apply the stored transform on the provided Bounds2d and return a new Bounds2d with the bounds of the transformed coordinates.
    Apply the stored transform on the provided point and return a point with the transformed coordinate.
    translate(double tx, double ty)
    Transform coordinates by a vector (tx, ty).
    Translate coordinates by a the x and y values contained in a Point2d.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Transform2d

      public Transform2d()
  • Method Details

    • mulMatVec

      protected static double[] mulMatVec(double[] m, double[] v)
      Multiply a 3x3 matrix (stored as a 9-value array by row) with a 4-value vector.
      Parameters:
      m - double[]; the matrix
      v - double[]; the vector
      Returns:
      double[3]; the result of m x v
    • mulMatVec2

      protected static double[] mulMatVec2(double[] m, double[] v)
      Multiply a 3x3 matrix (stored as a 9-value array by row) with a 3-value vector and a 1 for the 3rd value.
      Parameters:
      m - double[]; the matrix
      v - double[]; the vector
      Returns:
      double[2]; the result of m x (v1, v2, 1), with the last value left out
    • mulMatMat

      protected static double[] mulMatMat(double[] m1, double[] m2)
      Multiply a 3x3 matrix (stored as a 9-value array by row) with another 3x3-matrix.
      Parameters:
      m1 - double[]; the first matrix
      m2 - double[]; the second matrix
      Returns:
      double[9]; the result of m1 x m2
    • getMat

      public double[] getMat()
      Get a safe copy of the affine transformation matrix.
      Returns:
      double[]; a safe copy of the affine transformation matrix
    • translate

      public Transform2d translate(double tx, double ty)
      Transform coordinates by a vector (tx, ty). Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Parameters:
      tx - double; the translation value for the x-coordinates
      ty - double; the translation value for the y-coordinates
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • translate

      public Transform2d translate(Point2d point)
      Translate coordinates by a the x and y values contained in a Point2d. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Parameters:
      point - Point2d; the point containing the x and y translation values
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • scale

      public Transform2d scale(double sx, double sy)
      Scale all coordinates with a factor for x, and y. A scale factor of 1 leaves the coordinate unchanged. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Parameters:
      sx - double; the scale factor for the x-coordinates
      sy - double; the scale factor for the y-coordinates
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • rotation

      public Transform2d rotation(double angle)
      The rotation around the origin with an angle in radians. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Parameters:
      angle - double; the angle to rotate the coordinates with with around the origin
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • shear

      public Transform2d shear(double sx, double sy)
      The 2d shear leaves the xy-coordinate plane for z=0 untouched. An x-coordinate with a value of 1 is translated by sx, and an x-coordinate with another value is translated by x*sx. Similarly, a y-coordinate with a value of 1 is translated by xy and a y-coordinate with another value is translated by y*sy. Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Parameters:
      sx - double; the shear factor in the x-direction
      sy - double; the shear factor in the y-direction
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • reflectX

      public Transform2d reflectX()
      The reflection of the x-coordinate, by mirroring it in the yz-plane (the plane with x=0). Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • reflectY

      public Transform2d reflectY()
      The reflection of the y-coordinate, by mirroring it in the xz-plane (the plane with y=0). Note that to carry out multiple operations, the steps have to be built in the OPPOSITE order since matrix multiplication operates from RIGHT to LEFT.
      Returns:
      Transform2d; the new transformation matrix after applying this transform
    • transform

      public double[] transform(double[] xy)
      Apply the stored transform on the xy-vector and return the transformed vector. For speed reasons, no checks on correct size of the vector is done.
      Parameters:
      xy - double[]; double[2] the provided vector
      Returns:
      double[2]; the transformed vector
    • transform

      public Point2d transform(Point2d point)
      Apply the stored transform on the provided point and return a point with the transformed coordinate.
      Parameters:
      point - Point2d; the point to be transformed
      Returns:
      Point2d; a point with the transformed coordinates
    • transform

      public Iterator<Point2d> transform(Iterator<Point2d> pointIterator)
      Apply the stored transform on the provided point and return a point with the transformed coordinate.
      Parameters:
      pointIterator - Iterator<Point2d>; generates the points to be transformed
      Returns:
      Iterator<Point2d>; an iterator that will generator all transformed points
    • transform

      public Bounds2d transform(Bounds2d boundingRectangle)
      Apply the stored transform on the provided Bounds2d and return a new Bounds2d with the bounds of the transformed coordinates. All 4 corner points have to be transformed, since we do not know which of the 4 points will result in the lowest and highest x and y coordinates.
      Parameters:
      boundingRectangle - Bounds2d; the bounds to be transformed
      Returns:
      Bounds2d; the new bounds based on the transformed coordinates
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object