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