PLY reader/writer  1.2.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
io.h
1 /*
2 A C++ reader/writer of .ply files.
3 
4 Addapted to C++ streams by Thijs van Lankveld.
5 
6 Header for PLY polygon files.
7 
8 - Greg Turk
9 
10 A PLY file contains a single polygonal _object_.
11 
12 An object is composed of lists of _elements_. Typical elements are
13 vertices, faces, edges and materials.
14 
15 Each type of element for a given object has one or more _properties_
16 associated with the element type. For instance, a vertex element may
17 have as properties three floating-point values x,y,z and three unsigned
18 chars for red, green and blue.
19 
20 -----------------------------------------------------------------------
21 
22 Copyright (c) 1998 Georgia Institute of Technology. All rights reserved.
23 
24 Permission to use, copy, modify and distribute this software and its
25 documentation for any purpose is hereby granted without fee, provided
26 that the above copyright notice and this permission notice appear in
27 all copies of this software and that you do not sell the software.
28 
29 THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
31 WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
32 */
33 
34 
35 #ifndef __PLY_IO_H__
36 #define __PLY_IO_H__
37 
38 
39 #include "object.h"
40 
41 namespace PLY {
43 
45  struct Reader {
47  std::istream* stream;
48 
50 
55  Reader(Header& h): header(h), stream(0) {}
57 
64  Reader(Header& h, std::istream& is): header(h), stream(&is) {}
66 
71  Reader(Header& h, const char* file_name): header(h), stream(0) {if (!open_file(file_name)) close_file();}
72 
74 
78  bool open_file(const char* file_name);
79 
81 
84  void close_file();
85 
87 
89  bool read_header();
90 
92 
95  bool read_data(Storage* store);
96 
97  private:
98  // Read storage and scalar types.
99  bool read_stream_type(const std::string& word, Stream_type& type);
100  bool read_scalar(const std::string& word, Scalar_type& type);
101 
102  // Read a value from an ASCII or binary stream.
103  bool read_ascii_value(const Scalar_type& type, double& value);
104  bool read_binary_value(const Scalar_type& type, double& value);
105  void read(char* ptr, size_t n);
106 
107  // Read a value.
108  inline bool read_value(const Scalar_type& type, double& value) {
109  if (header.stream_type == ASCII)
110  return read_ascii_value(type, value);
111  else
112  return read_binary_value(type, value);
113  }
114 
115  // Read a size.
116  inline bool read_size(const Scalar_type& type, size_t& size) {
117  double value;
118  if (!read_value(type, value))
119  return false;
120  size = (size_t)value;
121  return true;
122  }
123 
124  // Read a string from an ASCII stream.
125  void read_ascii_string(char* str);
126  }; // struct Reader
127 
128 
130 
132  struct Writer {
134  std::ostream* stream;
135 
137 
142  Writer(Header& h): header(h), stream(0) {}
144 
151  Writer(Header& h, std::ostream& os): header(h), stream(&os) {}
153 
158  Writer(Header& h, const char* file_name, const Stream_type& type = ASCII)
159  : header(h), stream(0) {if (!open_file(file_name, type)) close_file();}
160 
162 
167  bool open_file(const char* file_name, const Stream_type& type = ASCII);
168 
170 
173  void close_file();
174 
176 
181  bool write_header();
182 
184 
189  bool write_data(Storage* store);
190 
192 
196  bool write_element(const Element& elem, Array* collect);
197 
199 
203  inline bool write_object(const Element& elem, Object* obj) {
204  if (header.stream_type == ASCII)
205  return write_ascii_object(elem, obj);
206  else
207  return write_binary_object(elem, obj);
208  }
209 
210  private:
211  // Write a value to an ASCII or binary stream.
212  bool write_ascii_value(const Scalar_type& type, const double& value);
213  bool write_binary_value(const Scalar_type& type, const double& value);
214  void write(char* ptr, size_t n);
215 
216  // Write an element to an ASCII or binary stream.
217  bool write_ascii_object(const Element& elem, Object* obj);
218  bool write_binary_object(const Element& elem, Object* obj);
219  }; // class Writer
220 } // namespace PLY
221 
222 
223 #endif // __PLY_IO_H__