1 /* 2 * Copyright (c) 2011, 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package test.javafx.scene.transform; 27 28 import com.sun.javafx.geom.transform.Affine2D; 29 import com.sun.javafx.geom.transform.Affine3D; 30 import com.sun.javafx.geom.transform.BaseTransform; 31 import com.sun.javafx.scene.NodeHelper; 32 import com.sun.javafx.scene.transform.TransformUtils; 33 import test.com.sun.javafx.test.TransformHelper; 34 import javafx.beans.property.DoubleProperty; 35 import javafx.beans.property.SimpleDoubleProperty; 36 import javafx.geometry.BoundingBox; 37 import javafx.geometry.Point3D; 38 import javafx.scene.Group; 39 import javafx.scene.Node; 40 import test.javafx.scene.NodeTest; 41 import javafx.scene.Scene; 42 import javafx.scene.shape.Rectangle; 43 import test.javafx.scene.shape.RectangleTest; 44 import org.junit.Assert; 45 import org.junit.Test; 46 47 import java.lang.reflect.Method; 48 import javafx.scene.transform.Affine; 49 import javafx.scene.transform.Rotate; 50 import javafx.scene.transform.Scale; 51 import javafx.scene.transform.Transform; 52 import javafx.scene.transform.TransformShim; 53 54 import static org.junit.Assert.*; 55 56 57 public class TransformTest { 58 59 public static void assertTx(Node n, BaseTransform trans) { 60 Assert.assertEquals(trans, n.impl_getLeafTransform()); 61 } 62 63 64 @Test 65 public void testTranslate() { 66 Transform tx = Transform.translate(25, 52); 67 final Rectangle n = new Rectangle(); 68 n.getTransforms().add(tx); 69 70 assertTx(n, BaseTransform.getTranslateInstance(25, 52)); 71 } 72 73 74 @Test 75 public void testScale1() { 76 Transform tx = Transform.scale(25, 52); 77 final Rectangle n = new Rectangle(); 78 n.getTransforms().add(tx); 79 80 assertTx(n, BaseTransform.getScaleInstance(25, 52)); 81 } 82 83 84 @Test 85 public void testScale2() { 86 Transform tx = Transform.scale(25, 52, 66, 77); 87 final Rectangle n = new Rectangle(); 88 n.getTransforms().add(tx); 89 90 Affine2D expTx = new Affine2D(); 91 expTx.setToTranslation(66, 77); 92 expTx.scale(25, 52); 93 expTx.translate(-66, -77); 94 assertTx(n, expTx); 95 } 96 97 98 @Test 99 public void testRotate() { 100 Rotate tx = Transform.rotate(90, 3, 5); 101 final Rectangle n = new Rectangle(); 102 n.getTransforms().add(tx); 103 104 Affine2D expTx = new Affine2D(); 105 expTx.quadrantRotate(1, 3, 5); 106 assertTx(n, expTx); 107 } 108 109 110 @Test 111 public void testShear() { 112 Transform tx = Transform.shear(25, 52); 113 final Rectangle n = new Rectangle(); 114 n.getTransforms().add(tx); 115 116 Affine2D expTx1 = new Affine2D(); 117 expTx1.setToShear(25, 52); 118 assertTx(n, expTx1); 119 } 120 121 @Test 122 public void testShearWithPivot() { 123 Transform tx = Transform.shear(25, 52, 35, 53); 124 final Rectangle n = new Rectangle(); 125 n.getTransforms().add(tx); 126 127 Affine2D expTx1 = new Affine2D(); 128 129 expTx1.translate(35, 53); 130 expTx1.shear(25, 52); 131 expTx1.translate(-35, -53); 132 133 assertTx(n, expTx1); 134 } 135 136 137 @Test 138 public void testAffine() { 139 final Transform trans = Transform.affine(11, 22, 33, 44, 55, 66); 140 final Rectangle n = new Rectangle(); 141 n.getTransforms().add(trans); 142 143 final Affine2D affine2D = new Affine2D(11, 22, 33, 44, 55, 66); 144 final Affine3D affine3D = new Affine3D(affine2D); 145 assertTx(n, affine3D); 146 } 147 148 @Test 149 public void testAffine3D() { 150 final Transform trans = Transform.affine(11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333); 151 final Rectangle n = new Rectangle(); 152 n.getTransforms().add(trans); 153 154 final Affine3D affine3D = new Affine3D(11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333); 155 assertTx(n, affine3D); 156 } 157 158 @Test 159 public void defaultTransformShouldBeIdentity() { 160 final Transform t = TransformShim.createRawTransform( 161 1, 0, 0, 0, 162 0, 1, 0, 0, 163 0, 0, 1, 0); 164 165 TransformHelper.assertMatrix(t, 166 1, 0, 0, 0, 167 0, 1, 0, 0, 168 0, 0, 1, 0); 169 } 170 171 @Test 172 public void testTransform() { 173 Transform t = TransformUtils.immutableTransform( 174 1, 2, 3, 4, 175 5, 6, 7, 8, 176 9, 10, 11, 12); 177 178 Point3D point = new Point3D(2, 5, 7); 179 180 Point3D transformed = t.transform(point); 181 182 assertEquals(37, transformed.getX(), 0.0001); 183 assertEquals(97, transformed.getY(), 0.0001); 184 assertEquals(157, transformed.getZ(), 0.0001); 185 } 186 187 @Test 188 public void eachCornerShouldBeConsideredBySimilarTo3D() { 189 final Transform t1 = new Affine(); 190 final Transform t2 = new Scale(11, 11, 11); 191 final double limit = Math.sqrt(200); 192 193 assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0, 0, 0), limit)); 194 assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0.1, 0.1, 0.1), limit)); 195 assertFalse(t1.similarTo(t2, new BoundingBox(-1, -1, -1, 0, 0, 0), limit)); 196 assertFalse(t1.similarTo(t2, new BoundingBox(0, -1, -1, 1, 0, 0), limit)); 197 assertFalse(t1.similarTo(t2, new BoundingBox(-1, 0, -1, 0, 1, 0), limit)); 198 assertFalse(t1.similarTo(t2, new BoundingBox(0, 0, -1, 1, 1, 0), limit)); 199 assertFalse(t1.similarTo(t2, new BoundingBox(-1, -1, 0, 0, 0, 1), limit)); 200 assertFalse(t1.similarTo(t2, new BoundingBox(0, -1, 0, 1, 0, 1), limit)); 201 assertFalse(t1.similarTo(t2, new BoundingBox(-1, 0, 0, 0, 1, 1), limit)); 202 assertFalse(t1.similarTo(t2, new BoundingBox(0, 0, 0, 1, 1, 1), limit)); 203 } 204 205 @Test 206 public void eachCornerShouldBeConsideredBySimilarTo2D() { 207 final Transform t1 = new Affine(); 208 final Transform t2 = new Scale(11, 11); 209 final double limit = Math.sqrt(200) - 1; 210 211 assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0, 0, 0), limit)); 212 assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0.1, 0.1, 0.1), limit)); 213 assertFalse(t1.similarTo(t2, new BoundingBox(-1, -1, 0, 0, 0, 0), limit)); 214 assertFalse(t1.similarTo(t2, new BoundingBox(0, -1, 0, 1, 0, 0), limit)); 215 assertFalse(t1.similarTo(t2, new BoundingBox(-1, 0, 0, 0, 1, 0), limit)); 216 assertFalse(t1.similarTo(t2, new BoundingBox(0, 0, 0, 1, 1, 0), limit)); 217 } 218 219 220 public static void checkDoublePropertySynced(Transform tr, String propertyName, double val) 221 throws Exception { 222 223 final Rectangle r = new RectangleTest.StubRectangle(200, 200, 200, 200); 224 225 DoubleProperty v = new SimpleDoubleProperty(0.0); 226 Method m = tr.getClass().getMethod(propertyName + "Property", new Class[] {}); 227 ((DoubleProperty)m.invoke(tr)).bind(v); 228 229 r.getTransforms().add(tr); 230 ((Group)new Scene(new Group()).getRoot()).getChildren().add(r); 231 232 v.set(val); 233 NodeTest.syncNode(r); 234 235 //check 236 RectangleTest.StubNGRectangle pgR = NodeHelper.getPeer(r); 237 assertTx(r, pgR.getTransformMatrix()); 238 } 239 }