Reads a mesh from this file. @return the mesh contained in the file @throws IOException if an I/O error occurs @throws InvalidPlyFormatException if the format of the file is incorrect @throws IllegalStateException if the file doesn't contain any faces (#hasFaces() returns false)
()
| 656 | |
| 657 | /** |
| 658 | * Reads a mesh from this file. |
| 659 | * @return the mesh contained in the file |
| 660 | * @throws IOException if an I/O error occurs |
| 661 | * @throws InvalidPlyFormatException if the format of the file is incorrect |
| 662 | * @throws IllegalStateException if the file doesn't contain any faces ({@link #hasFaces()} returns {@code false}) |
| 663 | */ |
| 664 | public Pair<List<Vertex>, List<int[]>> readMesh() throws IOException, InvalidPlyFormatException { |
| 665 | if (!hasFaces) throw new IllegalStateException("No faces"); |
| 666 | |
| 667 | List<Vertex> vertices=new ArrayList<>(vertexElement.count); |
| 668 | List<int[]> triangles=new ArrayList<>(faceElement.count); |
| 669 | |
| 670 | try (Input input=getInput()) { |
| 671 | |
| 672 | for (Element currentElement: elements) { |
| 673 | if (currentElement==vertexElement) { |
| 674 | /* Parse vertices */ |
| 675 | for (int elemIndex=0; elemIndex<currentElement.count; elemIndex++) { |
| 676 | Vertex v=new Vertex(); |
| 677 | vertices.add(v); |
| 678 | for (int propIndex=0; propIndex<currentElement.properties.size(); propIndex++) { |
| 679 | Property prop=currentElement.properties.get(propIndex); |
| 680 | if (propIndex==vertexXPropIndex) { |
| 681 | v.x(input.read(((ScalarProperty)prop).type).doubleValue()); |
| 682 | } else if (propIndex==vertexYPropIndex) { |
| 683 | v.y(input.read(((ScalarProperty)prop).type).doubleValue()); |
| 684 | } else if (propIndex==vertexZPropIndex) { |
| 685 | v.z(input.read(((ScalarProperty)prop).type).doubleValue()); |
| 686 | } else { |
| 687 | if (prop instanceof ListProperty) { |
| 688 | List<Float> d = new ArrayList<>(); |
| 689 | int count=input.read(((ListProperty)prop).countType).intValue(); |
| 690 | if (count<0) throw new InvalidPlyFormatException("List with negative number of elements"); |
| 691 | for (int i=0; i<count; i++) { |
| 692 | d.add(input.read(((ListProperty)prop).elemType).floatValue()); |
| 693 | } |
| 694 | v.attributes.put(prop.name, d); |
| 695 | } else { |
| 696 | float d = input.read(((ScalarProperty)prop).type).floatValue(); |
| 697 | v.attributes.put(prop.name, Collections.singletonList(d)); |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | } else if (currentElement==faceElement) { |
| 703 | /* Parse faces */ |
| 704 | for (int elemIndex=0; elemIndex<currentElement.count; elemIndex++) { |
| 705 | for (int propIndex=0; propIndex<currentElement.properties.size(); propIndex++) { |
| 706 | Property prop=currentElement.properties.get(propIndex); |
| 707 | if (propIndex==vertexIndicesPropIndex) { |
| 708 | ListProperty lp=(ListProperty)prop; |
| 709 | int count=input.read(lp.countType).intValue(); |
| 710 | if (count<3) throw new InvalidPlyFormatException("Face with "+count+" vertices"); |
| 711 | switch (count) { |
| 712 | case 3: |
| 713 | Number v1,v2,v3,v4; |
| 714 | v1=input.read(lp.elemType); |
| 715 | if (v1.longValue()<0 || v1.longValue()>=vertexElement.count) throw new InvalidPlyFormatException("Invalid vertex index: "+v1.longValue()); |