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