3

Affine class made as extending Transform class in JavaFX.

This makes an illusion, that there can be some types of transforms, other than affine.

UPDATE

In JavaFX 2.x Transform class contains only getters for specific matrix elements, which includes 12 elements of 3x4 matrix for 3D affine transform. Any class usage can rely only on these elements, so any represented transformation is affine.

This means that 2.x version has bad design, because basic class can represent only affine transforms, while Affine is made it's subclass. This is wrong, since Affine should be made basic class.

In JavaFX 8 Transform class has better design. Additionally to getters, it has indexed access to matrix elements and determination of matrix type. Also it has transform() methods, which do actual transformation.

This means, that usage can rely both on transform() methods, or on matrix element getters.

If it can be guaranteed, that all internal parts of JavaFX rely on transform() methods, then this class can be extended and perspective transformation can be implemented. May be it even can be implemented in release version of JavaFX8.

The question is: can it be guaranteed, that JavaFX relies only on transform() methods?

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

1 Answers1

3

Yes, there are non-affine transforms in Mathematics.

No, these non-affine transforms are not supported by the JavaFX 2.2 Transform class, nor are they supported by the JavaFX 8 Transform class. There is no way in the Transform class or any of it's subclasses to set the transform matrix elements required to perform a non-affine transform.

You can apply a PerspectiveTransform to 2D nodes. For a usage sample, see my answer to Stretching Polygon to other Polygon with Java.

To understand the math of non-affine transforms, see Petzold's explanation. You can use similar math for 2d transforms. Yes, I realize Petzold's examples are not JavaFX examples. However, the math still holds and you can apply it to JavaFX through either a PerspectiveTransform or by modifying the points in a TriangleMesh using a custom matrix definition.

Note, a limitation of PerspectiveTransform is picking (mouse selection) doesn't work. Picking will work for a transformed TriangleMesh, but the mesh itself is not a general JavaFX node, just a collection of shaded faces.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 1
    If I can't extend `Transform` to obtain non-affine transform, then class hierarchy is wrong in JavaFX, right? They should do `Affine` as the base, and other as descendants. – Suzan Cioc Nov 21 '13 at 09:49
  • I think the hierarchy is right. With Transform as the base, a future (e.g. Java 9) version could extend Transform with a NonAffineTransform subclass, but if Affine was root, then that would not make sense. – jewelsea Nov 21 '13 at 10:10
  • So, it IS possible to extend `Transform` to some non-affine? – Suzan Cioc Nov 21 '13 at 11:01
  • You could extend Transform to a non-affine transform, but, unless they were recoded, other JavaFX components (such as the rendering engine) would not be aware of the extra non-affine elements exposed in your custom transform and hence would not use them. – jewelsea Nov 21 '13 at 11:20
  • So, other JavaFX library parts are inconsistent with general nature of `Transform` class? They expect it is affine? – Suzan Cioc Nov 21 '13 at 11:22
  • The Transform class in JavaFX 2.2 doesn't provide the API (getters) to get non-affine matrix elements - there is no way that existing components could be coded to use elements they can't get. You can extend Transform to add getters for non-affine elements, but nothing would know to call your additional getters, so they would be ignored. – jewelsea Nov 21 '13 at 11:29