View Javadoc
1   /*
2    * Copyright (C) 2012 Alberto Irurueta Carro (alberto@irurueta.com)
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 com.irurueta.geometry.io;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  
21  /**
22   * This class contains elements of the header of a PLY file.
23   * The header of a ply file is stored in text form at the beginning of the file.
24   */
25  public class HeaderPLY {
26      /**
27       * Indicates storage mode of the file. This can be either binary big-endian,
28       * binary little-endian or ascii text.
29       */
30      private PLYStorageMode storageMode;
31  
32      /**
33       * List containing all the elements forming the data of the file.
34       */
35      private final List<ElementPLY> elements;
36  
37      /**
38       * List containing all the string comments that the author might have stored
39       * in the file.
40       */
41      private final List<String> comments;
42  
43      /**
44       * List of strings containing addition object information.
45       */
46      private final List<String> objInfos;
47  
48      /**
49       * Constructor
50       */
51      public HeaderPLY() {
52          storageMode = PLYStorageMode.PLY_ASCII;
53          elements = new LinkedList<>();
54          comments = new LinkedList<>();
55          objInfos = new LinkedList<>();
56      }
57  
58      /**
59       * Returns storage mode of this file.
60       *
61       * @return Storage mode of this file.
62       */
63      public PLYStorageMode getStorageMode() {
64          return storageMode;
65      }
66  
67      /**
68       * Sets storage mode of this file.
69       *
70       * @param storageMode Storage mode to be set.
71       */
72      public void setStorageMode(final PLYStorageMode storageMode) {
73          this.storageMode = storageMode;
74      }
75  
76      /**
77       * Returns the structure of all the elements forming the data of this file.
78       *
79       * @return All the elements forming the data of this file.
80       */
81      public List<ElementPLY> getElements() {
82          return elements;
83      }
84  
85      /**
86       * Returns a list of strings containing all the comments set by the author
87       * of this file.
88       *
89       * @return Comments of this file.
90       */
91      public List<String> getComments() {
92          return comments;
93      }
94  
95      /**
96       * Returns list of strings containing additional object information.
97       *
98       * @return Additional object information.
99       */
100     public List<String> getObjInfos() {
101         return objInfos;
102     }
103 
104     /**
105      * Converts header data into string format ready to be saved on a PLY file.
106      *
107      * @return Header data into string format.
108      */
109     @Override
110     public String toString() {
111         final var builder = new StringBuilder("ply\n");
112 
113         if (storageMode != null) {
114             builder.append("format ");
115             switch (storageMode) {
116                 case PLY_ASCII:
117                     builder.append("ascii ");
118                     break;
119                 case PLY_LITTLE_ENDIAN:
120                     builder.append("binary_little_endian ");
121                     break;
122                 case PLY_BIG_ENDIAN:
123                     builder.append("binary_big_endian ");
124                     break;
125                 default:
126                     break;
127             }
128         }
129 
130         // add version
131         builder.append("1.0\n");
132 
133         // add comments
134         for (final var comment : comments) {
135             builder.append("comment ").append(comment).append("\n");
136         }
137 
138         // add obj_infos
139         for (final var objInfo : objInfos) {
140             builder.append("obj_info ").append(objInfo).append("\n");
141         }
142 
143         // dd elements
144         for (final var element : elements) {
145             // NOTE: elements already contain carrier return on their textual
146             // representation
147             builder.append(element.toString());
148         }
149 
150         builder.append("end_header\n");
151 
152         return builder.toString();
153     }
154 }