View Javadoc

1   /*
2    * Copyright 2011 Stefan C. Mueller.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.smurn.jply.consoledemo;
17  
18  import java.io.IOException;
19  import org.smurn.jply.Element;
20  import org.smurn.jply.ElementReader;
21  import org.smurn.jply.ElementType;
22  import org.smurn.jply.PlyReader;
23  import org.smurn.jply.PlyReaderFile;
24  
25  /**
26   * Console application that prints out the positions of all vertices.
27   */
28  public class ConsoleDemo {
29  
30      public static void main(String[] args) throws IOException {
31  
32          PlyReader ply;
33  
34          if (args.length != 1) {
35              System.err.println("--Usage: ConsoleDemo [PathToPLYFile]");
36              System.err.println("--Will use a demo file of a cube instead.");
37  
38              // Reads a PLY file from an InputStream. Handy if, for example,
39              // you receive PLY data via network sockets.
40              
41              ply = new PlyReaderFile(
42                      ClassLoader.getSystemResourceAsStream("cube.ply"));
43          } else {
44  
45              // Opens the PLY file.
46              // If the file name ends with .gz its automatically decompressed.
47  
48              String file = args[0];
49              ply = new PlyReaderFile(file);
50          }
51  
52          // The elements are stored in order of their types. For each
53          // type we get a reader that reads the elements of this type.
54          ElementReader reader = ply.nextElementReader();
55          while (reader != null) {
56  
57              ElementType type = reader.getElementType();
58  
59              // In PLY files vertices always have a type named "vertex".
60              if (type.getName().equals("vertex")) {
61                  printVertices(reader);
62              }
63  
64              // Close the reader for the current type before getting the next one.
65              reader.close();
66  
67              reader = ply.nextElementReader();
68          }
69  
70          ply.close();
71      }
72  
73      private static void printVertices(ElementReader reader) throws IOException {
74  
75          // The number of elements is known in advance. This is great for
76          // allocating buffers and a like.
77          // You can even get the element counts for each type before getting 
78          // the corresponding reader via the PlyReader.getElementCount(..)
79          // method.
80          System.out.println("There are " + reader.getCount() + " vertices:");
81  
82          // Read the elements. They all share the same type.
83          Element element = reader.readElement();
84          while (element != null) {
85  
86              // Use the the 'get' methods to access the properties.
87              // jPly automatically converts the various data types supported
88              // by PLY for you.
89  
90              System.out.print("x=");
91              System.out.print(element.getDouble("x"));
92              System.out.print(" y=");
93              System.out.print(element.getDouble("y"));
94              System.out.print(" z=");
95              System.out.print(element.getDouble("z"));
96              System.out.println();
97  
98              element = reader.readElement();
99          }
100     }
101 }