org.geotoolkit.display.shape
Class TransformedShape

Object
  extended by AffineTransform
      extended by TransformedShape
All Implemented Interfaces:
Shape, Serializable, Cloneable

@NotThreadSafe
public final class TransformedShape
extends AffineTransform
implements Shape

Applies an arbitrary AffineTransform on a Shape. A TransformedShape instance is a view over a shape, i.e. the shape coordinates are transformed on the fly, never copied.

The shape to be transformed is specified by setOriginalShape(Shape). The transform to apply is specified by the inherited AffineTransform, which can be modified at any time using all its usual methods. Every changes to the original shape or to the inherited transform take immediate effect since this class is only a view.

Note: This class is final because extending directly AffineTransform is not a good example of object-oriented programming, since a transformed shape is not a special kind of affine transform. We did that as a convenience for allowing frequent modifications of the transform by direct access to the AffineTransform methods, but this is not an example that shoud be replicated.


Performance cost
When iterating over the shape boundary, the only performance cost is a matrix multiplication applied before the iterator is created - the cost of the iteration itself usually stay unchanged.


Example
The Arrow2D class is unconditionally oriented toward 0° arithmetic. In order to draw a field of arrows in arbitrary directions, the code below can be used:

protected void paint(Graphics2D graphics) {
    Arrow2D arrow = new Arrow2D(...);
    TransformedShape orientedArrow = new TransformedShape(arrow);
    for (int i=0; i<numArrows; i++) {
        double scale = ...
        double orientation = Math.toRadians(...);
        orientedArrow.setToScale(scale, scale);
        orientedArrow.rotate(orientation);
        graphics.fill(orientedArrow);
    }
}
An alternative to this TransformedShape class would be to change directly the Graphics2D transform. The main difference between those alternatives is that when using TransformedShape, the transform does not apply to the line thickness or paint texture. The preferred alternative depends on the desired rendering effect, for example if line thickness needs to be specified in pixel units or in "real world" units.

Since:
2.0
Version:
3.20
Author:
Martin Desruisseaux (IRD, Geomatys)
See Also:
ProjectedShape, 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
TransformedShape()
          Constructs a TransformedShape initialized to the identity transform with no original shape.
TransformedShape(AffineTransform transform)
          Constructs a TransformedShape initialized to the given transform with no original shape.
TransformedShape(Shape shape)
          Constructs a view of the given shape initialized to the identity transform.
TransformedShape(Shape shape, AffineTransform transform)
          Constructs a TransformedShape initialized to the given transform and shape.
 
Method Summary
 boolean contains(double x, double y)
          Tests if the specified coordinate is inside the boundary of this shape.
 boolean contains(double x, double y, double width, double height)
          Tests if the interior of this shape entirely contains the specified rectangular area.
 boolean contains(Point2D p)
          Tests if a specified Point2D is inside the boundary of this shape.
 boolean contains(Rectangle2D r)
          Tests if the interior of this shape entirely contains the specified rectangle.
 boolean equals(Object object)
          Compares this shape with the given object for equality.
 Rectangle getBounds()
          Returns an integer rectangle that completely encloses this shape.
 Rectangle2D getBounds2D()
          Returns a high precision and more accurate bounding box of the shape than the getBounds() method.
 void getMatrix(float[] matrix, int offset)
          Returns the 6 coefficients values as float numbers.
 Shape getOriginalShape()
          Returns the original shape for which this TransformedShape is a view.
 PathIterator getPathIterator(AffineTransform at)
          Returns an iterator object that iterates along the shape boundary and provides access to the geometry of the shape outline.
 PathIterator getPathIterator(AffineTransform at, double flatness)
          Returns an iterator object that iterates along the shape boundary and provides access to a flattened view of the shape outline geometry.
 int hashCode()
          Returns a hash code value for this shape.
 boolean intersects(double x, double y, double width, double height)
          Tests if the interior of this shape intersects the interior of a specified rectangular area.
 boolean intersects(Rectangle2D r)
          Tests if the interior of this shape intersects the interior of a specified rectangle.
 void setOriginalShape(Shape shape)
          Sets the shape for which this TransformedShape will be a view.
 void setTransform(float[] matrix, int offset)
          Sets the transform from a flat matrix.
 
Methods inherited from class AffineTransform
clone, concatenate, createInverse, createTransformedShape, deltaTransform, deltaTransform, getDeterminant, getMatrix, getQuadrantRotateInstance, getQuadrantRotateInstance, getRotateInstance, getRotateInstance, getRotateInstance, getRotateInstance, getScaleInstance, getScaleX, getScaleY, getShearInstance, getShearX, getShearY, getTranslateInstance, getTranslateX, getTranslateY, getType, inverseTransform, inverseTransform, invert, isIdentity, preConcatenate, quadrantRotate, quadrantRotate, rotate, rotate, rotate, rotate, scale, setToIdentity, setToQuadrantRotation, setToQuadrantRotation, setToRotation, setToRotation, setToRotation, setToRotation, setToScale, setToShear, setToTranslation, setTransform, setTransform, shear, toString, transform, transform, transform, transform, transform, transform, translate
 
Methods inherited from class Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TransformedShape

public TransformedShape()
Constructs a TransformedShape initialized to the identity transform with no original shape. The setOriginalShape(Shape) method must be invoked at least once before this object can be used.


TransformedShape

public TransformedShape(AffineTransform transform)
Constructs a TransformedShape initialized to the given transform with no original shape. The setOriginalShape(Shape) method must be invoked at least once before this object can be used.

Parameters:
transform - The initial affine transform.

TransformedShape

public TransformedShape(Shape shape)
Constructs a view of the given shape initialized to the identity transform.

Parameters:
shape - The shape to wrap in the new TransformedShape instance.

TransformedShape

public TransformedShape(Shape shape,
                        AffineTransform transform)
Constructs a TransformedShape initialized to the given transform and shape.

Parameters:
shape - The shape to wrap in the new TransformedShape instance.
transform - The initial affine transform.
Since:
3.20
Method Detail

getOriginalShape

public Shape getOriginalShape()
Returns the original shape for which this TransformedShape is a view.

Returns:
The shape wrapped by this TransformedShape instance.

setOriginalShape

public void setOriginalShape(Shape shape)
Sets the shape for which this TransformedShape will be a view.

Parameters:
shape - The shape to wrap in this TransformedShape instance.

getMatrix

public void getMatrix(float[] matrix,
                      int offset)
Returns the 6 coefficients values as float numbers. The coefficients are stored in the same order than AffineTransform.getMatrix(double[]), with the first coefficient stored in matrix[offset] and the last coefficient stored in matrix[offset+5]

Parameters:
matrix - The matrix where to store the coefficient values.
offset - Index of the first element where to write in the matrix array.

setTransform

public void setTransform(float[] matrix,
                         int offset)
Sets the transform from a flat matrix. The coefficients are read in the same order than they were stored by getMatrix(float[], int).

Parameters:
matrix - The flat matrix.
offset - Index of the first element to use in matrix array.

contains

public boolean contains(double x,
                        double y)
Tests if the specified coordinate is inside the boundary of this shape. This method might conservatively return false if the transform is not invertible.

The default implementation delegates to contains(Point2D).

Specified by:
contains in interface Shape
Parameters:
x - The x ordinate of the point to be tested.
y - The y ordinate of the point to be tested.
Returns:
true if this shape contains the given point.

contains

public boolean contains(Point2D p)
Tests if a specified Point2D is inside the boundary of this shape. This method might conservatively return false if the transform is not invertible.

Specified by:
contains in interface Shape
Parameters:
p - The point to be tested.
Returns:
true if this shape contains the given point.

contains

public boolean contains(double x,
                        double y,
                        double width,
                        double height)
Tests if the interior of this shape entirely contains the specified rectangular area. This method might conservatively return false if the transform is not invertible or if the calculation of originalShape.contains(...) is too expensive.

The default implementation delegates to contains(Rectangle2D).

Specified by:
contains in interface Shape
Parameters:
x - The minimal x ordinate of the rectangle to be tested.
y - The minimal y ordinate of the rectangle to be tested.
width - The width of the rectangle to be tested.
height - The height of the rectangle to be tested.
Returns:
true if this shape contains the given rectangle.

contains

public boolean contains(Rectangle2D r)
Tests if the interior of this shape entirely contains the specified rectangle. This method might conservatively return false if the transform is not invertible or if the calculation of originalShape.contains(...) is too expensive.

Specified by:
contains in interface Shape
Parameters:
r - The rectangle to be tested.
Returns:
true if this shape contains the given rectangle.

intersects

public boolean intersects(double x,
                          double y,
                          double width,
                          double height)
Tests if the interior of this shape intersects the interior of a specified rectangular area. This method might conservatively return true if the transform is not invertible or if the calculation of originalShape.intersects(...) is too expensive.

The default implementation delegates to intersects(Rectangle2D).

Specified by:
intersects in interface Shape
Parameters:
x - The minimal x ordinate of the rectangle to be tested.
y - The minimal y ordinate of the rectangle to be tested.
width - The width of the rectangle to be tested.
height - The height of the rectangle to be tested.
Returns:
true if this shape intersects the given rectangle.

intersects

public boolean intersects(Rectangle2D r)
Tests if the interior of this shape intersects the interior of a specified rectangle. This method might conservatively return true if the transform is not invertible or if the calculation of originalShape.intersects(...) is too expensive.

Specified by:
intersects in interface Shape
Parameters:
r - The rectangle to be tested.
Returns:
true if this shape intersects the given rectangle.

getBounds

public Rectangle getBounds()
Returns an integer rectangle that completely encloses this shape.

Specified by:
getBounds in interface Shape

getBounds2D

public Rectangle2D getBounds2D()
Returns a high precision and more accurate bounding box of the shape than the getBounds() method.

Specified by:
getBounds2D in interface Shape

getPathIterator

public PathIterator getPathIterator(AffineTransform at)
Returns an iterator object that iterates along the shape boundary and provides access to the geometry of the shape outline.

Specified by:
getPathIterator in interface Shape

getPathIterator

public PathIterator getPathIterator(AffineTransform at,
                                    double flatness)
Returns an iterator object that iterates along the shape boundary and provides access to a flattened view of the shape outline geometry.

Specified by:
getPathIterator in interface Shape

hashCode

public int hashCode()
Returns a hash code value for this shape.

Overrides:
hashCode in class AffineTransform
Since:
3.20

equals

public boolean equals(Object object)
Compares this shape with the given object for equality. Do not compare TransformedShape instances with plain AffineTransform instances, because the "be symmetric" part of the Object.equals(Object) contract can not be enforced.

Overrides:
equals in class AffineTransform
Parameters:
object - The object to compare with this shape.
Since:
3.20


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