1 /*
   2  * Copyright (c) 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 package javafx.scene.shape;
  26 
  27 import java.util.Arrays;
  28 import static org.junit.Assert.*;
  29 import org.junit.Test;
  30 
  31 public class TriangleMeshTest {
  32 
  33     /**
  34      * Test of setFaceSmoothingGroups method, of class TriangleMesh.
  35      */
  36     @Test
  37     public void testSetFaceSmoothingGroups_intArr() {
  38         int divX = 10;
  39         int divY = 10;
  40         TriangleMesh instance = buildTriangleMesh(divX, divY);
  41         int[] faceSmoothingGroups = new int[divX * divY * 2];
  42         Arrays.fill(faceSmoothingGroups, 1);
  43         instance.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
  44         assertTrue(instance.getFaceSmoothingGroups().size() == faceSmoothingGroups.length);
  45         assertArrayEquals(faceSmoothingGroups, instance.getFaceSmoothingGroups().toArray(null));
  46     }
  47 
  48     /**
  49      * Test of setFaceSmoothingGroups method, of class TriangleMesh.
  50      */
  51     @Test
  52     public void testSetFaceSmoothingGroups_4args() {
  53         int divX = 10;
  54         int divY = 10;
  55         TriangleMesh instance = buildTriangleMesh(divX, divY);
  56         int[] faceSmoothingGroups = new int[divX * divY * 2];
  57         Arrays.fill(faceSmoothingGroups, 1);
  58         int[] setterArray = new int[]{2, 4, 8};
  59         int[] expected = new int[setterArray.length];
  60         int index = 1;
  61         int start = 0;
  62         int length = setterArray.length;
  63         instance.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
  64         instance.getFaceSmoothingGroups().set(index, setterArray, start, length);
  65         assertArrayEquals(setterArray, instance.getFaceSmoothingGroups().toArray(index, expected, length));
  66     }
  67 
  68     /**
  69      * Test faceSmoothingGroups with illegal value of setFaceSmoothingGroups
  70      * method, of class TriangleMesh.
  71      */
  72     @Test (expected=ArrayIndexOutOfBoundsException.class)
  73     public void testSetFaceSmoothingGroups_4argsValueOutOfRange() {
  74         int divX = 10;
  75         int divY = 10;
  76         TriangleMesh instance = buildTriangleMesh(divX, divY);
  77         int[] faceSmoothingGroups = new int[divX * divY * 2];
  78         Arrays.fill(faceSmoothingGroups, 1);
  79         int[] setterArray = new int[]{2, 0, -1};
  80         int index = 0;
  81         int start = 0;
  82         int length = setterArray.length;
  83         instance.getFaceSmoothingGroups().set(index, setterArray, start, length); // expect IllegalArgumentException
  84         // faceSmoothingGroups should not change
  85         assertArrayEquals(faceSmoothingGroups, instance.getFaceSmoothingGroups().toArray(null));
  86     }
  87 
  88     /**
  89      * Test setFaceSmoothingGroups with illegal value of setFaceSmoothingGroups
  90      * method, of class TriangleMesh.
  91      */
  92     @Test(expected = ArrayIndexOutOfBoundsException.class)
  93     public void testSetFaceSmoothingGroups_4argsIllegalArgument() {
  94         int divX = 10;
  95         int divY = 10;
  96         TriangleMesh instance = buildTriangleMesh(divX, divY);
  97         int[] faceSmoothingGroups = new int[divX * divY * 2];
  98         Arrays.fill(faceSmoothingGroups, 1);
  99         instance.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
 100         int[] setterArray = new int[]{2, 0, 1};
 101         int index = 0;
 102         int start = 0;
 103         instance.getFaceSmoothingGroups().set(index, setterArray, start, -1); // expect IllegalArgumentException
 104         // faceSmoothingGroups should not change
 105         assertArrayEquals(faceSmoothingGroups, instance.getFaceSmoothingGroups().toArray(null));
 106     }
 107 
 108     /**
 109      * Test setFaceSmoothingGroups with illegal value of setFaceSmoothingGroups
 110      * method, of class TriangleMesh.
 111      */
 112     @Test(expected = ArrayIndexOutOfBoundsException.class)
 113     public void testSetFaceSmoothingGroups_4argsIndexOutOfRange() {
 114         int divX = 10;
 115         int divY = 10;
 116         TriangleMesh instance = buildTriangleMesh(divX, divY);
 117         int[] faceSmoothingGroups = new int[divX * divY * 2];
 118         Arrays.fill(faceSmoothingGroups, 1);
 119         int[] setterArray = new int[]{2, 0, 1};
 120         int start = 0;
 121         int length = setterArray.length;
 122         instance.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
 123         instance.getFaceSmoothingGroups().set(198, setterArray, start, length); // expect ArrayIndexOutOfBoundsException
 124         // faceSmoothingGroups should not change
 125         assertArrayEquals(faceSmoothingGroups, instance.getFaceSmoothingGroups().toArray(null));
 126     }
 127 
 128     /**
 129      * Test setFaceSmoothingGroups with illegal value of setFaceSmoothingGroups
 130      * method, of class TriangleMesh.
 131      */
 132     @Test(expected = ArrayIndexOutOfBoundsException.class)
 133     public void testSetFaceSmoothingGroups_4argsStartOutOfRange() {
 134         int divX = 10;
 135         int divY = 10;
 136         TriangleMesh instance = buildTriangleMesh(divX, divY);
 137         int[] faceSmoothingGroups = new int[divX * divY * 2];
 138         Arrays.fill(faceSmoothingGroups, 1);
 139         int[] setterArray = new int[]{2, 0, 1};
 140         int index = 0;
 141         int length = setterArray.length;
 142         instance.getFaceSmoothingGroups().setAll(faceSmoothingGroups);
 143         instance.getFaceSmoothingGroups().set(index, setterArray, 2, length); // expect IllegalArgumentException
 144         // faceSmoothingGroups should not change
 145         assertArrayEquals(faceSmoothingGroups, instance.getFaceSmoothingGroups().toArray(null));
 146     }
 147 
 148     /**
 149      * Test of setFaces method, of class TriangleMesh.
 150      */
 151     @Test
 152     public void testSetFaces_4args() {
 153         int divX = 10;
 154         int divY = 10;
 155         TriangleMesh instance = buildTriangleMesh(divX, divY); // 1200 faces
 156         int faces[] = {
 157             0, 0, 2, 2, 1, 1,
 158             2, 2, 3, 3, 1, 1,
 159             4, 0, 5, 1, 6, 2,
 160             6, 2, 5, 1, 7, 3,
 161             0, 0, 1, 1, 4, 2,
 162             4, 2, 1, 1, 5, 3,
 163             2, 0, 6, 2, 3, 1,
 164             3, 1, 6, 2, 7, 3,
 165             0, 0, 4, 1, 2, 2,
 166             2, 2, 4, 1, 6, 3,
 167             1, 0, 3, 1, 5, 2,
 168             5, 2, 3, 1, 7, 3,};
 169         int index = 6;
 170         int start = 0;
 171         int length = faces.length;
 172         instance.getFaces().set(index, faces, start, length);
 173         int[] expected = new int[faces.length];
 174         assertArrayEquals(instance.getFaces().toArray(index, expected, length), faces);
 175     }
 176 
 177     /**
 178      * Test setFaces with illegal value of setFaceSmoothingGroups method, of
 179      * class TriangleMesh.
 180      */
 181     @Test(expected = ArrayIndexOutOfBoundsException.class)
 182     public void testSetFaces_4argsIllegalArgument() {
 183         int divX = 10;
 184         int divY = 10;
 185         TriangleMesh instance = buildTriangleMesh(divX, divY); // 1200 faces
 186         int faces[] = {0, 0, 2, 2, 1, 1,};
 187         int[] expecteds = instance.getFaces().toArray(null);
 188         int length = faces.length;
 189         instance.getFaces().set(-1, faces, -1, length);
 190         // faces should not change
 191         assertArrayEquals(expecteds, instance.getFaces().toArray(null));
 192     }
 193 
 194     /**
 195      * Test setFaces with index argument out of range of setFaceSmoothingGroups
 196      * method, of class TriangleMesh.
 197      */
 198     @Test(expected = ArrayIndexOutOfBoundsException.class)
 199     public void testSetFaces_4argsIndexOutOfRange() {
 200         int divX = 10;
 201         int divY = 10;
 202         TriangleMesh instance = buildTriangleMesh(divX, divY); // 1200 faces
 203         int faces[] = {0, 0, 2, 2, 1, 1,};
 204         int[] expecteds = instance.getFaces().toArray(null);
 205         int start = 0;
 206         int length = faces.length;
 207         instance.getFaces().set(1200, faces, start, length);
 208         // faces should not change
 209         assertArrayEquals(expecteds, instance.getFaces().toArray(null));
 210     }
 211 
 212     /**
 213      * Test setFaces with start argument out of range of setFaceSmoothingGroups
 214      * method, of class TriangleMesh.
 215      */
 216     @Test(expected = ArrayIndexOutOfBoundsException.class)
 217     public void testSetFaces_4argsStartOutOfRange() {
 218         int divX = 10;
 219         int divY = 10;
 220         TriangleMesh instance = buildTriangleMesh(divX, divY); // 1200 faces
 221         int faces[] = {
 222             0, 0, 2, 2, 1, 1,
 223             2, 2, 3, 3, 1, 1,};
 224         int[] expecteds = instance.getFaces().toArray(null);
 225         int index = 6;
 226         int length = faces.length;
 227         instance.getFaces().set(index, faces, 1, length);
 228         // faces should not change
 229         assertArrayEquals(expecteds, instance.getFaces().toArray(null));
 230     }
 231 
 232     /**
 233      * Test of setTexCoords method, of class TriangleMesh.
 234      */
 235     @Test
 236     public void testsetTexCoords_4args() {
 237         int divX = 10;
 238         int divY = 10;
 239         TriangleMesh instance = buildTriangleMesh(divX, divY); // 242 texCoords
 240         float texCoords[] = {0, 0,
 241                              0, 1,
 242                              1, 0,
 243                              1, 1};
 244         float[] expecteds = new float[texCoords.length];
 245         int index = 2;
 246         int start = 0;
 247         int length = texCoords.length;
 248         instance.getTexCoords().set(index, texCoords, start, length);
 249         assertArrayEquals(instance.getTexCoords().toArray(index, expecteds, length), texCoords, 1e-3f);
 250     }
 251 
 252     /**
 253      * Test setTexCoords with illegal value of setTexCoordsmoothingGroups
 254      * method, of class TriangleMesh.
 255      */
 256     @Test(expected = ArrayIndexOutOfBoundsException.class)
 257     public void testsetTexCoords_4argsIllegalArgument() {
 258         int divX = 10;
 259         int divY = 10;
 260         TriangleMesh instance = buildTriangleMesh(divX, divY); // 242 texCoords
 261         float texCoords[] = {0, 0,
 262                              0, 1,
 263                              1, 0,
 264                              1, 1};
 265         float[] expecteds = instance.getTexCoords().toArray(null);
 266         int length = texCoords.length;
 267         instance.getTexCoords().set(-1, texCoords, -1, length);
 268         // texCoords should not change
 269         assertArrayEquals(instance.getTexCoords().toArray(null), expecteds, 1e-3f);
 270     }
 271 
 272     /**
 273      * Test setTexCoords with index argument out of range of
 274      * setTexCoordsmoothingGroups method, of class TriangleMesh.
 275      */
 276     @Test(expected = ArrayIndexOutOfBoundsException.class)
 277     public void testsetTexCoords_4argsIndexOutOfRange() {
 278         int divX = 10;
 279         int divY = 10;
 280         TriangleMesh instance = buildTriangleMesh(divX, divY); // 242 texCoords
 281         float texCoords[] = {0, 0,
 282                              0, 1,
 283                              1, 0,
 284                              1, 1};
 285         float[] expecteds = instance.getTexCoords().toArray(null);
 286         int start = 0;
 287         int length = texCoords.length;
 288         instance.getTexCoords().set(240, texCoords, start, length);
 289         // texCoords should not change
 290         assertArrayEquals(instance.getTexCoords().toArray(null), expecteds, 1e-3f);
 291     }
 292 
 293     /**
 294      * Test setTexCoords with start argument out of range of
 295      * setTexCoordsmoothingGroups method, of class TriangleMesh.
 296      */
 297     @Test(expected = ArrayIndexOutOfBoundsException.class)
 298     public void testsetTexCoords_4argsStartOutOfRange() {
 299         int divX = 10;
 300         int divY = 10;
 301         TriangleMesh instance = buildTriangleMesh(divX, divY); // 242 texCoords
 302         float texCoords[] = {0, 0,
 303                              0, 1,
 304                              1, 0,
 305                              1, 1};
 306         float[] expecteds = instance.getTexCoords().toArray(null);
 307         int index = 2;
 308         int length = texCoords.length;
 309         instance.getTexCoords().set(index, texCoords, 1, length);
 310         // texCoords should not change
 311         assertArrayEquals(instance.getTexCoords().toArray(null), expecteds, 1e-3f);
 312     }
 313 
 314     /**
 315      * Test of setPoints method, of class TriangleMesh.
 316      */
 317     @Test
 318     public void testSetPoints_4args() {
 319         int divX = 10;
 320         int divY = 10;
 321         TriangleMesh instance = buildTriangleMesh(divX, divY); // 121 points
 322         float points[] = {
 323             1, 1, 1,
 324             1, 1, -1,
 325             1, -1, 1,
 326             1, -1, -1,
 327             -1, 1, 1,
 328             -1, 1, -1,
 329             -1, -1, 1,
 330             -1, -1, -1,};
 331         float[] expecteds = new float[points.length];
 332         int index = 3;
 333         int start = 0;
 334         int length = points.length;
 335         instance.getPoints().set(index, points, start, length);
 336         assertArrayEquals(instance.getPoints().toArray(index, expecteds, length), points, 1e-3f);
 337     }
 338 
 339     /**
 340      * Test setPoints with illegal value of setPointsmoothingGroups method, of
 341      * class TriangleMesh.
 342      */
 343     @Test(expected = ArrayIndexOutOfBoundsException.class)
 344     public void testSetPoints_4argsIllegalArgument() {
 345         int divX = 10;
 346         int divY = 10;
 347         TriangleMesh instance = buildTriangleMesh(divX, divY); // 121 points
 348         float points[] = {
 349             1, 1, 1,
 350             1, 1, -1,
 351             1, -1, 1,
 352             1, -1, -1,
 353             -1, 1, 1,
 354             -1, 1, -1,
 355             -1, -1, 1,
 356             -1, -1, -1,};
 357         float[] expecteds = instance.getPoints().toArray(null);
 358         int length = points.length;
 359         instance.getPoints().set(-1, points, -1, length);
 360         // points should not change
 361         assertArrayEquals(instance.getPoints().toArray(null), expecteds, 1e-3f);
 362     }
 363 
 364     /**
 365      * Test setPoints with index argument out of range of
 366      * setPointsmoothingGroups method, of class TriangleMesh.
 367      */
 368     @Test(expected = ArrayIndexOutOfBoundsException.class)
 369     public void testSetPoints_4argsIndexOutOfRange() {
 370         int divX = 10;
 371         int divY = 10;
 372         TriangleMesh instance = buildTriangleMesh(divX, divY); // 121 points
 373         float points[] = {
 374             1, 1, 1,
 375             1, 1, -1,
 376             1, -1, 1,
 377             1, -1, -1,
 378             -1, 1, 1,
 379             -1, 1, -1,
 380             -1, -1, 1,
 381             -1, -1, -1,};
 382         float[] expecteds = instance.getPoints().toArray(null);
 383         int start = 0;
 384         int length = points.length;
 385         instance.getPoints().set(120 * 3, points, start, length);
 386         // points should not change
 387         assertArrayEquals(instance.getPoints().toArray(null), expecteds, 1e-3f);
 388     }
 389 
 390     /**
 391      * Test setPoints with start argument out of range of
 392      * setPointsmoothingGroups method, of class TriangleMesh.
 393      */
 394     @Test(expected = ArrayIndexOutOfBoundsException.class)
 395     public void testSetPoints_4argsStartOutOfRange() {
 396         int divX = 10;
 397         int divY = 10;
 398         TriangleMesh instance = buildTriangleMesh(divX, divY); // 121 points
 399         float points[] = {
 400             1, 1, 1,
 401             1, 1, -1,
 402             1, -1, 1,
 403             1, -1, -1,
 404             -1, 1, 1,
 405             -1, 1, -1,
 406             -1, -1, 1,
 407             -1, -1, -1,};
 408         float[] expecteds = instance.getPoints().toArray(null);
 409         int index = 3;
 410         int length = points.length;
 411         instance.getPoints().set(index, points, 1, length);
 412         // points should not change
 413         assertArrayEquals(instance.getPoints().toArray(null), expecteds, 1e-3f);
 414     }
 415 
 416     /**
 417      * Test the vertex format length (point, texcoord and face) of default TriangleMesh.
 418      */
 419     @Test
 420     public void testVertexFormatOfDefaultTriangleMesh() {
 421         TriangleMesh triMesh = new TriangleMesh();
 422         // x, y, z
 423         assertEquals(3, triMesh.getPointElementSize());
 424         // u, v
 425         assertEquals(2, triMesh.getTexCoordElementSize());
 426         // 3 point indices and 3 texCoord indices per triangle
 427         assertEquals(6, triMesh.getFaceElementSize());
 428     }
 429 
 430     TriangleMesh buildTriangleMesh(int subDivX, int subDivY) {
 431         TriangleMesh triangleMesh = new TriangleMesh();
 432         final int pointSize = triangleMesh.getPointElementSize();
 433         final int texCoordSize = triangleMesh.getTexCoordElementSize();
 434         final int faceSize = triangleMesh.getFaceElementSize();
 435         int numDivX = subDivX + 1;
 436         int numVerts = (subDivY + 1) * numDivX;
 437         float points[] = new float[numVerts * pointSize];
 438         float texCoords[] = new float[numVerts * texCoordSize];
 439         int faceCount = subDivX * subDivY * 2;
 440         int faces[] = new int[faceCount * faceSize];
 441 
 442         triangleMesh.getPoints().setAll(points);
 443         triangleMesh.getTexCoords().setAll(texCoords);
 444         triangleMesh.getFaces().setAll(faces);
 445 
 446         return triangleMesh;
 447     }
 448 }