PLY reader/writer  1.2.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
example.h
1 //Example elements for the C++ ply reader/writer.
2 //Copyright (C) 2013 INRIA - Sophia Antipolis
3 //
4 //This program is free software: you can redistribute it and/or modify
5 //it under the terms of the GNU General Public License as published by
6 //the Free Software Foundation, either version 3 of the License, or
7 //(at your option) any later version.
8 //
9 //This program is distributed in the hope that it will be useful,
10 //but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 //GNU General Public License for more details.
13 //
14 //You should have received a copy of the GNU General Public License
15 //along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // Author(s): Thijs van Lankveld
18 //
19 //
20 // TODO: doxygen
21 
22 
23 #ifndef __PLY_EXAMPLES_H__
24 #define __PLY_EXAMPLES_H__
25 
26 
27 #include "unknown.h"
28 
29 namespace PLY {
30  // Example object classes.
31 
33  struct FloatValue: public Value {
34  float val;
35 
37  FloatValue(): val(0) {}
39 
41  FloatValue(const float& v): val(v) {}
42 
43  // Get the scalar value.
44  bool get_scalar(const Property& prop, double& value) const {
45  if (prop.type != SCALAR) return false;
46  value = val; return true;
47  }
48 
49  // Set the scalar value.
50  bool set_scalar(const Property& prop, const double& value) {
51  if (prop.type != SCALAR) return false;
52  val = (float)value; return true;
53  }
54  }; // struct FloatValue
55 
56 
58 
61  struct Vertex: public Object {
63 
64  FloatValue value_x, value_y, value_z;
67  Vertex(): value_x(0), value_y(0), value_z(0) {}
70 
74  Vertex(float x, float y, float z): value_x(x), value_y(y), value_z(z) {}
75 
76  // Get a Value.
77  Value* get_value(const Element& elem, const Property& prop) {
78  if (prop.name.compare(prop_x.name.c_str()) == 0) return &value_x;
79  else if (prop.name.compare(prop_y.name.c_str()) == 0) return &value_y;
80  else if (prop.name.compare(prop_z.name.c_str()) == 0) return &value_z;
81  return 0;
82  }
83 
84  // Construct an Element describing this Object.
85  bool make_element(Element& elem) const {
86  elem.name = name;
87  elem.props.push_back(prop_x);
88  elem.props.push_back(prop_y);
89  elem.props.push_back(prop_z);
90  return true;
91  }
92 
94 
95 
97  float x() const { double dval; value_x.get_scalar(prop_x, dval); return (float)dval; }
98 
100 
101  float y() const { double dval; value_y.get_scalar(prop_y, dval); return (float)dval; }
102 
104 
105  float z() const { double dval; value_z.get_scalar(prop_z, dval); return (float)dval; }
108 
110 
112  void x(float coord) { value_x.set_scalar(prop_x, coord); }
113 
115 
116  void y(float coord) { value_y.set_scalar(prop_y, coord); }
117 
119 
120  void z(float coord) { value_z.set_scalar(prop_z, coord); }
123  static const char* name;
126 
127  static const Property prop_x;
128  static const Property prop_y;
129  static const Property prop_z;
131  }; // struct Vertex
132 
133 
135  struct Face: public Object {
137 
139  Face() {}
141 
143  Face(const size_t& size) { indices.set_size(prop_ind, size); }
145 
148  Face(const Face& f) {
149  if (f.indices.data != 0) {
150  size_t size;
151  double val;
152  f.indices.get_size(prop_ind, size);
153  indices.set_size(prop_ind, size);
154  for (size_t n = 0; n < size; ++n) {
155  f.indices.get_item(prop_ind, n, val);
156  indices.set_item(prop_ind, n, val);
157  }
158  }
159  }
160 
161  // Get a Value.
162  Value* get_value(const Element& elem, const Property& prop) {
163  if (prop.name.compare(prop_ind.name.c_str()) == 0) return &indices;
164  return 0;
165  }
166 
167  // Construct an Element describing this Object.
168  bool make_element(Element& elem) const {
169  elem.name = name;
170  elem.props.push_back(prop_ind);
171  return true;
172  }
173 
175 
176 
178  size_t size() const { size_t s; indices.get_size(prop_ind, s); return s; }
179 
181 
184  size_t vertex(const size_t& num) const { double index; indices.get_item(prop_ind, num, index); return (size_t)index; }
187 
189 
191  void size(const size_t& size) { indices.set_size(prop_ind, size); }
192 
194 
197  void vertex(const size_t& num, const size_t& index) { indices.set_item(prop_ind, num, (double)index); }
200  static const char* name;
203 
204  static const Property prop_ind;
206  }; // struct Face
207 
208 
210  struct VertexArray: public AnyArray {
213 
214  // Get the next Object.
216  if (objects[incr] == 0)
217  objects[incr] = new Vertex;
218  return *objects[incr++];
219  }
220  }; // struct VertexArray
221 
222 
224  struct FaceArray: public AnyArray {
227 
228  // Get the next Object.
230  if (objects[incr] == 0)
231  objects[incr] = new Face;
232  return *objects[incr++];
233  }
234  }; // struct FaceArray
235 
236 
238 
240  struct VertexExternal: public Array {
241  std::vector<Vertex>& vertices;
242  size_t incr;
243 
245 
247  VertexExternal(std::vector<Vertex>& v): Array(), vertices(v), incr(0) {}
248 
249  size_t size() { return vertices.size(); }
250  void prepare(const size_t& size) { vertices.reserve(size); restart(); }
251  void clear() { vertices.clear(); }
252  void restart() { incr = 0; }
253 
254  // Get the next Object.
256  if (vertices.size() <= incr)
257  vertices.resize(incr+1);
258  return vertices[incr++];
259  }
260  }; // struct VertexExternal
261 
262 
264 
266  struct FaceExternal: public Array {
267  std::vector<Face>& faces;
268  size_t incr;
269 
271 
273  FaceExternal(std::vector<Face>& f): Array(), faces(f), incr(0) {}
274 
275  size_t size() { return faces.size(); }
276  void prepare(const size_t& size) { faces.reserve(size); restart(); }
277  void clear() { faces.clear(); }
278  void restart() { incr = 0; }
279 
280  // Get the next Object.
282  if (faces.size() <= incr)
283  faces.resize(incr+1);
284  return faces[incr++];
285  }
286  }; // struct FaceExternal
287 } // namespace PLY
288 
289 
290 #endif // __PLY_EXAMPLES_H__