1 /*
   2  * Copyright (c) 2011, 2013, 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 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.test.TransformHelper;
  31 import javafx.beans.property.ObjectProperty;
  32 import javafx.beans.property.SimpleObjectProperty;
  33 import javafx.geometry.Point3D;
  34 import javafx.scene.Group;
  35 import javafx.scene.NodeTest;
  36 import javafx.scene.Scene;
  37 import javafx.scene.shape.Rectangle;
  38 import javafx.scene.shape.RectangleTest;
  39 import org.junit.Test;
  40 
  41 import java.lang.reflect.Method;
  42 
  43 import static javafx.scene.transform.TransformTest.assertTx;
  44 import static org.junit.Assert.assertFalse;
  45 import static org.junit.Assert.assertNotNull;
  46 
  47 public class RotateTest {
  48 
  49     @Test
  50     public void testRotate() {
  51         final Rotate trans = new Rotate() {{
  52             setAngle(90);
  53         }};
  54         final Rectangle n = new Rectangle();
  55         n.getTransforms().add(trans);
  56 
  57         Affine2D expTx1 = new Affine2D();
  58         expTx1.quadrantRotate(1);
  59         assertTx(n, expTx1);
  60         TransformHelper.assertMatrix(trans,
  61                 Math.cos(Math.PI / 2.0), -Math.sin(Math.PI / 2.0), 0, 0,
  62                 Math.sin(Math.PI / 2.0),  Math.cos(Math.PI / 2.0), 0, 0,
  63                          0,                          0,            1, 0);
  64 
  65         trans.setAngle(180);
  66         Affine2D expTx2 = new Affine2D();
  67         expTx2.quadrantRotate(2);
  68         assertTx(n, expTx2);
  69 
  70         trans.setPivotX(66);
  71         Affine2D expTx3 = new Affine2D();
  72         expTx3.setToRotation(Math.toRadians(trans.getAngle()), trans.getPivotX(), trans.getPivotY());
  73         assertTx(n, expTx3);
  74 
  75         trans.setPivotY(77);
  76         Affine2D expTx4 = new Affine2D();
  77         expTx4.setToRotation(Math.toRadians(trans.getAngle()), trans.getPivotX(), trans.getPivotY());
  78         assertTx(n, expTx4);
  79 
  80         trans.setAngle(45);
  81         trans.setPivotZ(88);
  82         trans.setAxis(new Point3D(20, 30, 40));
  83 
  84         final Point3D a = new Point3D(20.0 / Math.sqrt(2900.0),
  85                                 30.0 / Math.sqrt(2900.0),
  86                                 40.0 / Math.sqrt(2900.0));
  87         double sin = Math.sin(Math.PI / 4);
  88         double cos = Math.cos(Math.PI / 4);
  89 
  90         TransformHelper.assertMatrix(trans,
  91                 cos + a.getX() * a.getX() * (1 - cos), //mxx
  92                 a.getX() * a.getY() * (1 - cos) - a.getZ() * sin, //mxy
  93                 a.getX() * a.getZ() * (1 - cos) + a.getY() * sin, //mxz
  94                 66
  95                     - 66 * (cos + a.getX() * a.getX() * (1 - cos))
  96                     - 77 * (a.getX() * a.getY() * (1 - cos) - a.getZ() * sin)
  97                     - 88 * (a.getX() * a.getZ() * (1 - cos) + a.getY() * sin), //tx
  98                 a.getY() * a.getX() * (1 - cos) + a.getZ() * sin, //myx
  99                 cos + a.getY() * a.getY() * (1 - cos), //myy
 100                 a.getY() * a.getZ() * (1 - cos) - a.getX() * sin, //myz
 101                 77
 102                     - 66 * (a.getY() * a.getX() * (1 - cos) + a.getZ() * sin)
 103                     - 77 * (cos + a.getY() * a.getY() * (1 - cos))
 104                     - 88 * (a.getY() * a.getZ() * (1 - cos) - a.getX() * sin), //ty
 105                 a.getZ() * a.getX() * (1 - cos) - a.getY() * sin, //mzx
 106                 a.getZ() * a.getY() * (1 - cos) + a.getX() * sin, //mzy
 107                 cos + a.getZ() * a.getZ() * (1 - cos), //mzz
 108                 88
 109                     - 66 * (a.getZ() * a.getX() * (1 - cos) - a.getY() * sin)
 110                     - 77 * (a.getZ() * a.getY() * (1 - cos) + a.getX() * sin)
 111                     - 88 * (cos + a.getZ() * a.getZ() * (1 - cos)) //tz
 112 
 113                 );
 114     }
 115 
 116     @Test public void testRotateAxisCtor() {
 117         final Rotate trans = new Rotate(11, new Point3D(22, 33, 44));
 118         final Rectangle n = new Rectangle();
 119         n.getTransforms().add(trans);
 120 
 121         Affine3D expT = new Affine3D();
 122         expT.rotate(Math.toRadians(11), 22, 33, 44);
 123         assertTx(n, expT);
 124     }
 125 
 126     @Test public void testRotate3DPivotCtor() {
 127         final Rotate trans = new Rotate(11, 22, 33, 44);
 128         final Rectangle n = new Rectangle();
 129         n.getTransforms().add(trans);
 130 
 131         Affine3D expT = new Affine3D();
 132         expT.translate(22, 33, 44);
 133         expT.rotate(Math.toRadians(11));
 134         expT.translate(-22, -33, -44);
 135         assertTx(n, expT);
 136     }
 137 
 138    @Test public void testRotate3DPivotAxisCtor() {
 139         final Rotate trans = new Rotate(11, 22, 33, 44, new Point3D(55, 66, 77));
 140         final Rectangle n = new Rectangle();
 141         n.getTransforms().add(trans);
 142 
 143         Affine3D expT = new Affine3D();
 144         expT.translate(22, 33, 44);
 145         expT.rotate(Math.toRadians(11), 55, 66, 77);
 146         expT.translate(-22, -33, -44);
 147         assertTx(n, expT);
 148     }
 149 
 150     @Test public void testBoundPropertySynced_Angle() throws Exception {
 151         TransformTest.checkDoublePropertySynced(new Rotate(300, 300, 0), "angle", 30.0);
 152     }
 153 
 154     @Test public void testBoundPropertySynced_PivotX() throws Exception {
 155         TransformTest.checkDoublePropertySynced(new Rotate(300, 300, 0), "pivotX", 200.0);
 156     }
 157 
 158     @Test public void testBoundPropertySynced_PivotY() throws Exception {
 159         TransformTest.checkDoublePropertySynced(new Rotate(300, 300, 0), "pivotY", 200.0);
 160     }
 161 
 162     @Test public void testBoundPropertySynced_PivotZ() throws Exception {
 163         TransformTest.checkDoublePropertySynced(new Rotate(300, 300, 0), "pivotZ", 20.0);
 164     }
 165 
 166     @Test public void testBoundPropertySynced_Axis() throws Exception {
 167         checkObjectPropertySynced("axis", new Point3D(1, 0, 0));
 168     }
 169 
 170     @Test
 171     public void testCopying() {
 172         final Rotate trans = new Rotate();
 173 
 174         trans.setAngle(45);
 175         trans.setPivotX(66);
 176         trans.setPivotY(77);
 177         trans.setPivotZ(88);
 178         trans.setAxis(new Point3D(20, 30, 40));
 179 
 180         Transform copy = trans.clone();
 181 
 182         final Point3D a = new Point3D(20.0 / Math.sqrt(2900.0),
 183                                 30.0 / Math.sqrt(2900.0),
 184                                 40.0 / Math.sqrt(2900.0));
 185         double sin = Math.sin(Math.PI / 4);
 186         double cos = Math.cos(Math.PI / 4);
 187 
 188         TransformHelper.assertMatrix(copy,
 189                 cos + a.getX() * a.getX() * (1 - cos), //mxx
 190                 a.getX() * a.getY() * (1 - cos) - a.getZ() * sin, //mxy
 191                 a.getX() * a.getZ() * (1 - cos) + a.getY() * sin, //mxz
 192                 66
 193                     - 66 * (cos + a.getX() * a.getX() * (1 - cos))
 194                     - 77 * (a.getX() * a.getY() * (1 - cos) - a.getZ() * sin)
 195                     - 88 * (a.getX() * a.getZ() * (1 - cos) + a.getY() * sin), //tx
 196                 a.getY() * a.getX() * (1 - cos) + a.getZ() * sin, //myx
 197                 cos + a.getY() * a.getY() * (1 - cos), //myy
 198                 a.getY() * a.getZ() * (1 - cos) - a.getX() * sin, //myz
 199                 77
 200                     - 66 * (a.getY() * a.getX() * (1 - cos) + a.getZ() * sin)
 201                     - 77 * (cos + a.getY() * a.getY() * (1 - cos))
 202                     - 88 * (a.getY() * a.getZ() * (1 - cos) - a.getX() * sin), //ty
 203                 a.getZ() * a.getX() * (1 - cos) - a.getY() * sin, //mzx
 204                 a.getZ() * a.getY() * (1 - cos) + a.getX() * sin, //mzy
 205                 cos + a.getZ() * a.getZ() * (1 - cos), //mzz
 206                 88
 207                     - 66 * (a.getZ() * a.getX() * (1 - cos) - a.getY() * sin)
 208                     - 77 * (a.getZ() * a.getY() * (1 - cos) + a.getX() * sin)
 209                     - 88 * (cos + a.getZ() * a.getZ() * (1 - cos)) //tz
 210 
 211                 );
 212     }
 213 
 214     @Test public void testToString() {
 215         final Rotate trans = new Rotate(40);
 216 
 217         String s = trans.toString();
 218 
 219         assertNotNull(s);
 220         assertFalse(s.isEmpty());
 221     }
 222 
 223     private void checkObjectPropertySynced(String propertyName, Object val)
 224             throws Exception {
 225 
 226         final Rectangle r = new RectangleTest.StubRectangle(200, 200, 200, 200);
 227         final Rotate rotate = new Rotate(300, 300, 0);
 228 
 229         ObjectProperty v = new SimpleObjectProperty(0.0);
 230         Method m = Rotate.class.getMethod(propertyName + "Property", new Class[] {});
 231         ((ObjectProperty)m.invoke(rotate)).bind(v);
 232 
 233         r.getTransforms().add(rotate);
 234         ((Group)new Scene(new Group()).getRoot()).getChildren().add(r);
 235 
 236         v.set(val);
 237         NodeTest.syncNode(r);
 238 
 239         //check
 240         RectangleTest.StubNGRectangle pgR = r.impl_getPeer();
 241         assertTx(r, pgR.getTransformMatrix());
 242     }
 243 }