1 /*
   2  * Copyright (c) 2011, 2015, 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.transform.TransformUtils;
  32 import test.com.sun.javafx.test.TransformHelper;
  33 import javafx.beans.property.DoubleProperty;
  34 import javafx.beans.property.SimpleDoubleProperty;
  35 import javafx.geometry.BoundingBox;
  36 import javafx.geometry.Point3D;
  37 import javafx.scene.Group;
  38 import javafx.scene.Node;
  39 import test.javafx.scene.NodeTest;
  40 import javafx.scene.Scene;
  41 import javafx.scene.shape.Rectangle;
  42 import test.javafx.scene.shape.RectangleTest;
  43 import org.junit.Assert;
  44 import org.junit.Test;
  45 
  46 import java.lang.reflect.Method;
  47 import javafx.scene.transform.Affine;
  48 import javafx.scene.transform.Rotate;
  49 import javafx.scene.transform.Scale;
  50 import javafx.scene.transform.Transform;
  51 
  52 import static org.junit.Assert.*;
  53 
  54 
  55 public class TransformTest {
  56 
  57     public static void assertTx(Node n, BaseTransform trans) {
  58         Assert.assertEquals(trans, n.impl_getLeafTransform());
  59     }
  60 
  61 
  62     @Test
  63     public void testTranslate() {
  64         Transform tx = Transform.translate(25, 52);
  65         final Rectangle n = new Rectangle();
  66         n.getTransforms().add(tx);
  67 
  68         assertTx(n, BaseTransform.getTranslateInstance(25, 52));
  69     }
  70 
  71 
  72     @Test
  73     public void testScale1() {
  74         Transform tx = Transform.scale(25, 52);
  75         final Rectangle n = new Rectangle();
  76         n.getTransforms().add(tx);
  77 
  78         assertTx(n, BaseTransform.getScaleInstance(25, 52));
  79     }
  80 
  81 
  82     @Test
  83     public void testScale2() {
  84         Transform tx = Transform.scale(25, 52, 66, 77);
  85         final Rectangle n = new Rectangle();
  86         n.getTransforms().add(tx);
  87 
  88         Affine2D expTx = new Affine2D();
  89         expTx.setToTranslation(66, 77);
  90         expTx.scale(25, 52);
  91         expTx.translate(-66, -77);
  92         assertTx(n, expTx);
  93     }
  94 
  95 
  96     @Test
  97     public void testRotate() {
  98         Rotate tx = Transform.rotate(90, 3, 5);
  99         final Rectangle n = new Rectangle();
 100         n.getTransforms().add(tx);
 101 
 102         Affine2D expTx = new Affine2D();
 103         expTx.quadrantRotate(1, 3, 5);
 104         assertTx(n, expTx);
 105     }
 106 
 107 
 108     @Test
 109     public void testShear() {
 110         Transform tx = Transform.shear(25, 52);
 111         final Rectangle n = new Rectangle();
 112         n.getTransforms().add(tx);
 113 
 114         Affine2D expTx1 = new Affine2D();
 115         expTx1.setToShear(25, 52);
 116         assertTx(n, expTx1);
 117     }
 118 
 119     @Test
 120     public void testShearWithPivot() {
 121         Transform tx = Transform.shear(25, 52, 35, 53);
 122         final Rectangle n = new Rectangle();
 123         n.getTransforms().add(tx);
 124 
 125         Affine2D expTx1 = new Affine2D();
 126 
 127         expTx1.translate(35, 53);
 128         expTx1.shear(25, 52);
 129         expTx1.translate(-35, -53);
 130 
 131         assertTx(n, expTx1);
 132     }
 133 
 134 
 135     @Test
 136     public void testAffine() {
 137         final Transform trans = Transform.affine(11, 22, 33, 44, 55, 66);
 138         final Rectangle n = new Rectangle();
 139         n.getTransforms().add(trans);
 140 
 141         final Affine2D affine2D = new Affine2D(11, 22, 33, 44, 55, 66);
 142         final Affine3D affine3D = new Affine3D(affine2D);
 143         assertTx(n, affine3D);
 144     }
 145 
 146     @Test
 147     public void testAffine3D() {
 148         final Transform trans = Transform.affine(11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333);
 149         final Rectangle n = new Rectangle();
 150         n.getTransforms().add(trans);
 151 
 152         final Affine3D affine3D = new Affine3D(11, 22, 33, 44, 55, 66, 77, 88, 99, 111, 222, 333);
 153         assertTx(n, affine3D);
 154     }
 155 
 156     @Test
 157     public void defaultTransformShouldBeIdentity() {
 158         final Transform t = new Transform() {
 159             @Override
 160             public void impl_apply(com.sun.javafx.geom.transform.Affine3D ad) {}
 161 
 162             @Override
 163             public BaseTransform impl_derive(BaseTransform ad) { return null; }
 164         };
 165 
 166         TransformHelper.assertMatrix(t,
 167                 1, 0, 0, 0,
 168                 0, 1, 0, 0,
 169                 0, 0, 1, 0);
 170     }
 171 
 172     @Test
 173     public void testTransform() {
 174         Transform t = TransformUtils.immutableTransform(
 175                 1,  2,  3,  4,
 176                 5,  6,  7,  8,
 177                 9, 10, 11, 12);
 178 
 179         Point3D point = new Point3D(2, 5, 7);
 180 
 181         Point3D transformed = t.transform(point);
 182 
 183         assertEquals(37, transformed.getX(), 0.0001);
 184         assertEquals(97, transformed.getY(), 0.0001);
 185         assertEquals(157, transformed.getZ(), 0.0001);
 186     }
 187 
 188     @Test
 189     public void eachCornerShouldBeConsideredBySimilarTo3D() {
 190         final Transform t1 = new Affine();
 191         final Transform t2 = new Scale(11, 11, 11);
 192         final double limit = Math.sqrt(200);
 193 
 194         assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0, 0, 0), limit));
 195         assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0.1, 0.1, 0.1), limit));
 196         assertFalse(t1.similarTo(t2, new BoundingBox(-1, -1, -1, 0, 0, 0), limit));
 197         assertFalse(t1.similarTo(t2, new BoundingBox(0, -1, -1, 1, 0, 0), limit));
 198         assertFalse(t1.similarTo(t2, new BoundingBox(-1, 0, -1, 0, 1, 0), limit));
 199         assertFalse(t1.similarTo(t2, new BoundingBox(0, 0, -1, 1, 1, 0), limit));
 200         assertFalse(t1.similarTo(t2, new BoundingBox(-1, -1, 0, 0, 0, 1), limit));
 201         assertFalse(t1.similarTo(t2, new BoundingBox(0, -1, 0, 1, 0, 1), limit));
 202         assertFalse(t1.similarTo(t2, new BoundingBox(-1, 0, 0, 0, 1, 1), limit));
 203         assertFalse(t1.similarTo(t2, new BoundingBox(0, 0, 0, 1, 1, 1), limit));
 204     }
 205 
 206     @Test
 207     public void eachCornerShouldBeConsideredBySimilarTo2D() {
 208         final Transform t1 = new Affine();
 209         final Transform t2 = new Scale(11, 11);
 210         final double limit = Math.sqrt(200) - 1;
 211 
 212         assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0, 0, 0), limit));
 213         assertTrue(t1.similarTo(t2, new BoundingBox(0, 0, 0, 0.1, 0.1, 0.1), limit));
 214         assertFalse(t1.similarTo(t2, new BoundingBox(-1, -1, 0, 0, 0, 0), limit));
 215         assertFalse(t1.similarTo(t2, new BoundingBox(0, -1, 0, 1, 0, 0), limit));
 216         assertFalse(t1.similarTo(t2, new BoundingBox(-1, 0, 0, 0, 1, 0), limit));
 217         assertFalse(t1.similarTo(t2, new BoundingBox(0, 0, 0, 1, 1, 0), limit));
 218     }
 219 
 220 
 221     public static void checkDoublePropertySynced(Transform tr, String propertyName, double val)
 222         throws Exception {
 223 
 224         final Rectangle r = new RectangleTest.StubRectangle(200, 200, 200, 200);
 225 
 226         DoubleProperty v = new SimpleDoubleProperty(0.0);
 227         Method m = tr.getClass().getMethod(propertyName + "Property", new Class[] {});
 228         ((DoubleProperty)m.invoke(tr)).bind(v);
 229 
 230         r.getTransforms().add(tr);
 231         ((Group)new Scene(new Group()).getRoot()).getChildren().add(r);
 232 
 233         v.set(val);
 234         NodeTest.syncNode(r);
 235 
 236         //check
 237         RectangleTest.StubNGRectangle pgR = r.impl_getPeer();
 238         assertTx(r, pgR.getTransformMatrix());
 239     }
 240 }