1 /*
   2  * Copyright (c) 2010, 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 test.javafx.geometry;
  27 
  28 import javafx.geometry.Point3D;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertFalse;
  31 import static org.junit.Assert.assertNotNull;
  32 import static org.junit.Assert.assertTrue;
  33 
  34 import org.junit.Test;
  35 
  36 public class Point3DTest {
  37 
  38     @Test
  39     public void testDistance() {
  40         Point3D p1 = new Point3D(0, 0, 0);
  41         Point3D p2 = new Point3D(1, 0, 0);
  42         Point3D p3 = new Point3D(1, 1, 1);
  43         
  44         assertEquals(1, p2.distance(p1), 1e-100);
  45         assertEquals(1, p2.distance(0, 0, 0), 1e-100);
  46         assertEquals(1.41421356, p2.distance(p3), 1e-5);
  47         assertEquals(1.7320508, p1.distance(p3), 1e-5);
  48         assertEquals(1.7320508, p1.distance(1, 1, 1), 1e-5);
  49     }
  50 
  51     @Test
  52     public void testEquals() {
  53         Point3D p1 = new Point3D(0, 0, 0);
  54         Point3D p2 = new Point3D(0, 1, 0);
  55         Point3D p3 = new Point3D(1, 0, 1);
  56         Point3D p4 = new Point3D(1, 0, 0);
  57 
  58         assertTrue(p1.equals(p1));
  59         assertTrue(p1.equals(new Point3D(0, 0, 0)));
  60         assertFalse(p1.equals(new Object()));
  61         assertFalse(p1.equals(p2));
  62         assertFalse(p1.equals(p3));
  63         assertFalse(p3.equals(p4));
  64     }
  65 
  66     @Test
  67     public void testHash() {
  68         Point3D p1 = new Point3D(0, 0, 0);
  69         Point3D p2 = new Point3D(0, 1, 0);
  70         Point3D p3 = new Point3D(0, 1, 0);
  71 
  72         assertEquals(p3.hashCode(), p2.hashCode());
  73         assertFalse(p1.hashCode() == p2.hashCode());
  74     }
  75 
  76     @Test
  77     public void testAdd() {
  78         Point3D p1 = new Point3D(2, 4, 8);
  79         Point3D p2 = new Point3D(-1, 3, 5);
  80 
  81         assertEquals(new Point3D(1, 7, 13), p1.add(p2));
  82         assertEquals(new Point3D(1, 7, 13), p1.add(-1, 3, 5));
  83     }
  84 
  85     @Test(expected=NullPointerException.class)
  86     public void testAddNull() {
  87         Point3D point = new Point3D(1, 2, 3);
  88         point.add(null);
  89     }
  90 
  91     @Test
  92     public void testSubtract() {
  93         Point3D p1 = new Point3D(2, 4, 8);
  94         Point3D p2 = new Point3D(-1, 3, 6);
  95 
  96         assertEquals(new Point3D(3, 1, 2), p1.subtract(p2));
  97         assertEquals(new Point3D(3, 1, 2), p1.subtract(-1, 3, 6));
  98     }
  99 
 100     @Test(expected=NullPointerException.class)
 101     public void testSubtractNull() {
 102         Point3D point = new Point3D(1, 2, 3);
 103         point.subtract(null);
 104     }
 105 
 106     @Test
 107     public void testMultiplyByNumber() {
 108         Point3D p1 = new Point3D(2, 4, 8);
 109         Point3D p2 = new Point3D(-1, 3, 6);
 110 
 111         assertEquals(new Point3D(4, 8, 16), p1.multiply(2));
 112         assertEquals(new Point3D(1, -3, -6), p2.multiply(-1));
 113         assertEquals(new Point3D(0, 0, 0), p1.multiply(0));
 114     }
 115 
 116     @Test
 117     public void testNormalize() {
 118         Point3D p1 = new Point3D(0, 0, 0);
 119         Point3D p2 = new Point3D(0, 0, 1);
 120         Point3D p3 = new Point3D(1, 1, 1);
 121         Point3D p4 = new Point3D(120, -350, 430);
 122 
 123         double sqrt3 = Math.sqrt(3);
 124         double sqrt4 = Math.sqrt(321800);
 125 
 126         assertEquals(new Point3D(0, 0, 0), p1.normalize());
 127         assertEquals(new Point3D(0, 0, 1), p2.normalize());
 128         assertEquals(new Point3D(1 / sqrt3, 1 / sqrt3, 1 /sqrt3), p3.normalize());
 129         assertEquals(new Point3D(120 / sqrt4, -350 / sqrt4, 430 / sqrt4), p4.normalize());
 130     }
 131 
 132     @Test
 133     public void testMidpoint() {
 134         Point3D p1 = new Point3D(0, 0, 0);
 135         Point3D p2 = new Point3D(1, -2, 3);
 136 
 137         assertEquals(new Point3D(0.5, -1, 1.5), p1.midpoint(p2));
 138         assertEquals(new Point3D(0.5, -1, 1.5), p1.midpoint(1, -2, 3));
 139     }
 140 
 141     @Test(expected=NullPointerException.class)
 142     public void testMidpointNull() {
 143         Point3D point = new Point3D(1, 2, 3);
 144         point.midpoint(null);
 145     }
 146 
 147     @Test
 148     public void testVectorAngle() {
 149         Point3D p1 = new Point3D(0, 0, 0);
 150         Point3D p2 = new Point3D(0, 0, 1);
 151         Point3D p3 = new Point3D(0, 1, 1);
 152         Point3D p4 = new Point3D(-1, 0, 1);
 153         Point3D p5 = new Point3D(0, 1, 1);
 154 
 155         assertEquals(Double.NaN, p1.angle(p2), 0.000001);
 156         assertEquals(0, p3.angle(p3), 0.000001);
 157         assertEquals(0, p3.angle(p5), 0.000001);
 158         assertEquals(45, p2.angle(p3), 0.000001);
 159         assertEquals(45, p2.angle(p4), 0.000001);
 160         assertEquals(135, p2.angle(-1, 0, -1), 0.000001);
 161     }
 162 
 163     @Test(expected=NullPointerException.class)
 164     public void testVectorAngleNull() {
 165         Point3D point = new Point3D(1, 2, 3);
 166         point.angle(null);
 167     }
 168 
 169     @Test
 170     public void testVectorAngleTooClose() {
 171         Point3D p1 = new Point3D(-0.8944271909999159, 0.0, 0.4472135954999579);
 172         Point3D p2 = new Point3D(-0.894427190999924, 4.061090000458082E-14, 0.4472135954999417);
 173         assertEquals(0.0, p1.angle(p2), 0.000001);
 174         assertEquals(0.0, p2.angle(p1), 0.000001);
 175     }
 176 
 177     @Test
 178     public void testVectorAngleTooOpposite() {
 179         Point3D p1 = new Point3D(-0.8944271909999159, 0.0, 0.4472135954999579);
 180         Point3D p2 = new Point3D(0.894427190999924, -4.061090000458082E-14, -0.4472135954999417);
 181         assertEquals(180.0, p1.angle(p2), 0.000001);
 182         assertEquals(180.0, p2.angle(p1), 0.000001);
 183     }
 184 
 185     @Test
 186     public void testPointAngle() {
 187         Point3D p1 = new Point3D(2, 2, 2);
 188         Point3D p2 = new Point3D(0, 2, 0);
 189         Point3D p3 = new Point3D(-3, 2, -3);
 190         Point3D p4 = new Point3D(-7, 2, -7);
 191         Point3D p5 = new Point3D(-3, 4, -3);
 192         Point3D p6 = new Point3D(-3, 2, -10);
 193 
 194         assertEquals(180, p2.angle(p1, p3), 0.000001);
 195         assertEquals(90, p3.angle(p5, p6), 0.000001);
 196         assertEquals(0, p2.angle(p3, p4), 0.000001);
 197         assertEquals(Double.NaN, p2.angle(p2, p4), 0.000001);
 198     }
 199 
 200     @Test(expected=NullPointerException.class)
 201     public void testPointAngle1Null() {
 202         Point3D point = new Point3D(1, 2, 3);
 203         point.angle(null, new Point3D(2, 8, 4));
 204     }
 205 
 206     @Test(expected=NullPointerException.class)
 207     public void testPointAngle2Null() {
 208         Point3D point = new Point3D(1, 2, 3);
 209         point.angle(new Point3D(8, 5, 3), null);
 210     }
 211 
 212     @Test
 213     public void testPointAngleTooClose() {
 214         Point3D p1 = new Point3D(-0.8944271909999159, 0.0, 0.4472135954999579);
 215         Point3D v = new Point3D(0.0, 0.0, 0.0);
 216         Point3D p2 = new Point3D(-0.894427190999924, 4.061090000458082E-14, 0.4472135954999417);
 217         assertEquals(0.0, v.angle(p1, p2), 0.000001);
 218         assertEquals(0.0, v.angle(p2, p1), 0.000001);
 219     }
 220 
 221     @Test
 222     public void testPointAngleTooOpposite() {
 223         Point3D p1 = new Point3D(-0.8944271909999159, 0.0, 0.4472135954999579);
 224         Point3D v = new Point3D(0.0, 0.0, 0.0);
 225         Point3D p2 = new Point3D(0.894427190999924, -4.061090000458082E-14, -0.4472135954999417);
 226         assertEquals(180.0, v.angle(p1, p2), 0.000001);
 227         assertEquals(180.0, v.angle(p2, p1), 0.000001);
 228     }
 229 
 230     @Test
 231     public void testMagnitude() {
 232         Point3D p1 = new Point3D(0, 0, 0);
 233         Point3D p2 = new Point3D(0, 1, 0);
 234         Point3D p3 = new Point3D(1, -10, 20);
 235 
 236         assertEquals(0, p1.magnitude(), 0.000001);
 237         assertEquals(1, p2.magnitude(), 0.000001);
 238         assertEquals(Math.sqrt(501), p3.magnitude(), 0.000001);
 239     }
 240 
 241     @Test
 242     public void testDotProduct() {
 243         Point3D p1 = new Point3D(0, 0, 0);
 244         Point3D p2 = new Point3D(1, 1, 1);
 245         Point3D p3 = new Point3D(2, -2, 3);
 246         Point3D p4 = new Point3D(-4, 5, 6);
 247 
 248         assertEquals(0, p1.dotProduct(p4), 0.000001);
 249         assertEquals(7, p2.dotProduct(p4), 0.000001);
 250         assertEquals(0, p3.dotProduct(p4), 0.000001);
 251         assertEquals(20, p3.dotProduct(-4, -5, 6), 0.000001);
 252     }
 253 
 254     @Test(expected=NullPointerException.class)
 255     public void testDotProductNull() {
 256         Point3D point = new Point3D(1, 2, 3);
 257         point.dotProduct(null);
 258     }
 259 
 260     @Test
 261     public void testCrossProduct() {
 262         Point3D p1 = new Point3D(0, 0, 0);
 263         Point3D p2 = new Point3D(0, 0, 3);
 264         Point3D p3 = new Point3D(0, 2, 0);
 265 
 266         assertEquals(new Point3D(0, 0, 0), p1.crossProduct(p3));
 267         assertEquals(new Point3D(0, 0, 0), p2.crossProduct(p1));
 268         assertEquals(new Point3D(-6, 0, 0), p2.crossProduct(p3));
 269         assertEquals(new Point3D(6, 0, 0), p3.crossProduct(p2));
 270     }
 271 
 272     @Test(expected=NullPointerException.class)
 273     public void testCrossProductNull() {
 274         Point3D point = new Point3D(1, 2, 3);
 275         point.crossProduct(null);
 276     }
 277 
 278     @Test
 279     public void testToString() {
 280         Point3D p1 = new Point3D(0, 0, 0);
 281         assertNotNull(p1.toString());
 282     }
 283 }