1 /* 2 * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4388199 27 * @summary Tests inverse transform of an array of points with 28 * shearing and translation components in the AffineTransform 29 */ 30 31 import java.awt.geom.AffineTransform; 32 import java.awt.geom.NoninvertibleTransformException; 33 import java.awt.geom.Point2D; 34 35 /* 36 * The AffineTransform method 37 * inverseTransform(double[],int,double[],int,int) produces incorrect 38 * results for pure shearing transformations or for shearing and 39 * translation transformations. The simpliest example of which is a 40 * rotation by 90 degrees. 41 */ 42 public class QuadRotInverseBug { main(String[] args)43 public static void main(String[] args) { 44 // First test a transform which rotates the coordinate system by 90 45 // degrees. 46 System.out.println("Using 90 degree rotation:"); 47 AffineTransform xform = AffineTransform.getRotateInstance(Math.PI/2); 48 boolean test1failed = test(xform); 49 // Next test the transform with an added translation component 50 System.out.println("Using 90 degree rotation with translation:"); 51 xform.translate(2,2); 52 boolean test2failed = test(xform); 53 if (test1failed || test2failed) { 54 throw new RuntimeException("test failed, see printout"); 55 } 56 } 57 test(AffineTransform xform)58 public static boolean test(AffineTransform xform) { 59 // Make needed arrays. 60 double[] originalPoint = new double[2]; 61 double[] transformedPoint = new double[2]; 62 double[] inverseFromOriginalXForm = new double[2]; 63 64 Point2D originalPoint2D = new Point2D.Double(); 65 Point2D transformedPoint2D = new Point2D.Double(); 66 Point2D inverseFromOriginalPoint2D = new Point2D.Double(); 67 68 // Make the original point to check (x,y)=(1,1). 69 originalPoint[0] = 1.; 70 originalPoint[1] = 1.; 71 72 try { 73 74 originalPoint2D.setLocation(originalPoint[0], originalPoint[1]); 75 76 // Make the transformed point. 77 xform.transform(originalPoint,0,transformedPoint,0,1); 78 xform.transform(originalPoint2D, transformedPoint2D); 79 80 // Transform the point back using the original transformation. 81 xform.inverseTransform(transformedPoint,0, 82 inverseFromOriginalXForm,0,1); 83 xform.inverseTransform(transformedPoint2D, 84 inverseFromOriginalPoint2D); 85 } catch (NoninvertibleTransformException e) { 86 throw new InternalError("transform wasn't invertible!"); 87 } 88 89 System.out.println("Both points should be identical:"); 90 System.out.println("Original Point: "+ 91 originalPoint[0]+" "+ 92 originalPoint[1]); 93 System.out.println("inverseTransform method used: "+ 94 inverseFromOriginalXForm[0]+" "+ 95 inverseFromOriginalXForm[1]); 96 System.out.println("Original Point2D: "+ originalPoint2D); 97 System.out.println("inverseTransform method used: "+ 98 inverseFromOriginalPoint2D); 99 return (originalPoint[0] != inverseFromOriginalXForm[0] || 100 originalPoint[1] != inverseFromOriginalXForm[1] || 101 !originalPoint2D.equals(inverseFromOriginalPoint2D)); 102 } 103 } 104