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 * Contains an element of the header in a PLY file.
23 */
24 public class ElementPLY {
25 /**
26 * Name of the element (i.e. 'vertex' or 'face').
27 */
28 private final String name;
29
30 /**
31 * Number of instances of this element (i.e. number of vertices or faces).
32 */
33 private final long nInstances;
34
35 /**
36 * List of properties forming this element. For a property it could be x, y,
37 * z properties for vertex coordinates, nx, ny, nz for vertex normals or
38 * red, green, blue, alpha for vertex color.
39 */
40 private final List<PropertyPLY> properties;
41
42 /**
43 * Constructor.
44 *
45 * @param name Name of this element (i.e. 'vertex' or 'face').
46 * @param nInstances Number of instances of this element (i.e. number of
47 * vertices or faces).
48 */
49 public ElementPLY(final String name, final long nInstances) {
50 this.name = name;
51 this.nInstances = nInstances;
52 properties = new LinkedList<>();
53 }
54
55 /**
56 * Constructor.
57 *
58 * @param name Name of this element (i.e. 'vertex' or 'face').
59 * @param nInstances Number of instances of this element (i.e. number of
60 * vertices or faces).
61 * @param property Property to be set on this element.
62 */
63 public ElementPLY(final String name, final long nInstances, final PropertyPLY property) {
64 this.name = name;
65 this.nInstances = nInstances;
66 properties = new LinkedList<>();
67 properties.add(property);
68 }
69
70 /**
71 * Constructor.
72 *
73 * @param name Name of this element (i.e. 'vertex' or 'face').
74 * @param nInstances Number of instances of this element (i.e. number of
75 * vertices or faces).
76 * @param properties List of properties to be assigned to this element.
77 */
78 public ElementPLY(final String name, final long nInstances, final List<PropertyPLY> properties) {
79 this.name = name;
80 this.nInstances = nInstances;
81 this.properties = properties;
82 }
83
84 /**
85 * Returns name of this element.
86 *
87 * @return name of this element.
88 * @throws NotAvailableException Raised if name is not available.
89 */
90 public String getName() throws NotAvailableException {
91 if (!isNameAvailable()) {
92 throw new NotAvailableException();
93 }
94 return name;
95 }
96
97 /**
98 * Determines whether a name for this element has been provided or not.
99 *
100 * @return True if element is set (different of null), false otherwise.
101 */
102 public boolean isNameAvailable() {
103 return (name != null);
104 }
105
106 /**
107 * Returns number of instances of this element (i.e. number of vertices or
108 * number of faces).
109 *
110 * @return Number of instances of this element in the file.
111 */
112 public long getNumberOfInstances() {
113 return nInstances;
114 }
115
116 /**
117 * Returns list of properties contained in this element.
118 *
119 * @return List of properties contained in this element.
120 * @throws NotAvailableException Raised if no properties have already been
121 * set.
122 */
123 public List<PropertyPLY> getProperties() throws NotAvailableException {
124 if (!arePropertiesAvailable()) {
125 throw new NotAvailableException();
126 }
127 return properties;
128 }
129
130 /**
131 * Indicates whether properties have already been set and are available for
132 * retrieval.
133 *
134 * @return True if properties are available, false otherwise.
135 */
136 public boolean arePropertiesAvailable() {
137 return (properties != null);
138 }
139
140 /**
141 * Indicates if this element is valid (i.e. it has a name)
142 *
143 * @return True if element is valid, false otherwise.
144 */
145 public boolean isValidElement() {
146 return isNameAvailable() && arePropertiesAvailable();
147 }
148
149 /**
150 * Converts this element to string representation ready to be written in the
151 * header of a PLY file.
152 *
153 * @return String representation of this element.
154 */
155 @Override
156 public String toString() {
157 // if element is invalid, return empty string
158 if (!isValidElement()) {
159 return "";
160 }
161
162 final var builder = new StringBuilder("element ");
163
164 // add name and number of instances
165 builder.append(name).append(" ").append(nInstances).append("\n");
166
167 // add properties of this element
168
169 for (final var property : properties) {
170 // NOTE: properties already contain carrier return on their textual
171 // representation
172 builder.append(property.toString());
173 }
174
175 return builder.toString();
176 }
177 }