JavaCAD
Validator.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates.
3  * All rights reserved. Use is subject to license terms.
4  *
5  * This file is available and licensed under the following license:
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * - Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the distribution.
16  * - Neither the name of Oracle Corporation nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package eu.mihosoft.vrl.v3d.ext.openjfx.importers;
33 
34 import javafx.collections.ObservableIntegerArray;
35 import javafx.scene.Node;
36 import javafx.scene.Parent;
37 import javafx.scene.shape.Mesh;
38 import javafx.scene.shape.MeshView;
39 import javafx.scene.shape.TriangleMesh;
40 
41 // TODO: Auto-generated Javadoc
45 public class Validator {
46 
52  public void validate(Node node) {
53  if (node instanceof MeshView) {
54  MeshView meshView = (MeshView) node;
55  validate(meshView.getMesh());
56  } else if (node instanceof Parent) {
57  for (Node child : ((Parent) node).getChildrenUnmodifiable()) {
58  validate(child);
59  }
60  }
61  }
62 
68  public void validate(Mesh mesh) {
69  if (!(mesh instanceof TriangleMesh)) {
70  throw new AssertionError("Mesh is not TriangleMesh: " + mesh.getClass() + ", mesh = " + mesh);
71  }
72  TriangleMesh tMesh = (TriangleMesh) mesh;
73  int numPoints = tMesh.getPoints().size() / tMesh.getPointElementSize();
74  int numTexCoords = tMesh.getTexCoords().size() / tMesh.getTexCoordElementSize();
75  int numFaces = tMesh.getFaces().size() / tMesh.getFaceElementSize();
76  if (numPoints == 0 || numPoints * tMesh.getPointElementSize() != tMesh.getPoints().size()) {
77  throw new AssertionError("Points array size is not correct: " + tMesh.getPoints().size());
78  }
79  if (numTexCoords == 0 || numTexCoords * tMesh.getTexCoordElementSize() != tMesh.getTexCoords().size()) {
80  throw new AssertionError("TexCoords array size is not correct: " + tMesh.getPoints().size());
81  }
82  if (numFaces == 0 || numFaces * tMesh.getFaceElementSize() != tMesh.getFaces().size()) {
83  throw new AssertionError("Faces array size is not correct: " + tMesh.getPoints().size());
84  }
85  if (numFaces != tMesh.getFaceSmoothingGroups().size() && tMesh.getFaceSmoothingGroups().size() > 0) {
86  throw new AssertionError("FaceSmoothingGroups array size is not correct: " + tMesh.getPoints().size() + ", numFaces = " + numFaces);
87  }
88  ObservableIntegerArray faces = tMesh.getFaces();
89  for (int i = 0; i < faces.size(); i += 2) {
90  int pIndex = faces.get(i);
91  if (pIndex < 0 || pIndex > numPoints) {
92  throw new AssertionError("Incorrect point index: " + pIndex + ", numPoints = " + numPoints);
93  }
94  int tcIndex = faces.get(i + 1);
95  if (tcIndex < 0 || tcIndex > numTexCoords) {
96  throw new AssertionError("Incorrect texCoord index: " + tcIndex + ", numTexCoords = " + numTexCoords);
97  }
98  }
99 // System.out.println("Validation successfull of " + mesh);
100  }
101 
102 }