Developer Documentation
Loading...
Searching...
No Matches
persistence.cc
1#include <iostream>
2#include <string>
3#include <map>
4// -------------------- OpenMesh
5#include <OpenMesh/Core/IO/MeshIO.hh>
6#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
7#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
8// -------------------- little helper
9#include "generate_cube.hh"
10#include "stats.hh"
11#include "fill_props.hh"
12
13// ----------------------------------------------------------------------------
14
15// Set to 1 to use an PolyMesh type.
16#define UsePolyMesh 1
17
18// ----------------------------------------------------------------------------
19
20using namespace OpenMesh;
21
22// ----------------------------------------------------------------------------
23
26
27#if UsePolyMesh
28typedef PolyMesh Mesh;
29#else
30typedef TriMesh Mesh;
31#endif
32
33// ----------------------------------------------------------------------------
34
35#ifndef DOXY_IGNORE_THIS
36struct MyData
37{
38 int ival;
39 double dval;
40 bool bval;
41 OpenMesh::Vec4f vec4fval;
42
43 MyData()
44 : ival(0), dval(0.0), bval(false)
45 { }
46
47 MyData( const MyData& _cpy )
48 : ival(_cpy.ival), dval(_cpy.dval), bval(_cpy.bval),
49 vec4fval(_cpy.vec4fval)
50 { }
51
52
53 // ---------- assignment
54
55 MyData& operator = (const MyData& _rhs)
56 {
57 ival = _rhs.ival;
58 dval = _rhs.dval;
59 bval = _rhs.bval;
60 vec4fval = _rhs.vec4fval;
61 return *this;
62 }
63
64 MyData& operator = (int _rhs) { ival = _rhs; return *this; }
65 MyData& operator = (double _rhs) { dval = _rhs; return *this; }
66 MyData& operator = (bool _rhs) { bval = _rhs; return *this; }
67 MyData& operator = (const OpenMesh::Vec4f& _rhs)
68 { vec4fval = _rhs; return *this; }
69
70
71 // ---------- comparison
72
73 bool operator == (const MyData& _rhs) const
74 {
75 return ival == _rhs.ival
76 && dval == _rhs.dval
77 && bval == _rhs.bval
78 && vec4fval == _rhs.vec4fval;
79 }
80
81 bool operator != (const MyData& _rhs) const { return !(*this == _rhs); }
82
83};
84#endif
85
86
87// ----------------------------------------------------------------------------
88
89typedef std::map< std::string, unsigned int > MyMap;
90
91
92// ----------------------------------------------------------------------------
93
94#ifndef DOXY_IGNORE_THIS
95
96namespace OpenMesh {
97 namespace IO {
98
99 // support persistence for struct MyData
100
101 template <> struct binary<MyData>
102 {
103 typedef MyData value_type;
104
105 static const bool is_streamable = true;
106
107 // return binary size of the value
108
109 static size_t size_of(void)
110 {
111 return sizeof(int)+sizeof(double)+sizeof(bool)+sizeof(OpenMesh::Vec4f);
112 }
113
114 static size_t size_of(const value_type&)
115 {
116 return size_of();
117 }
118
119 static std::string type_identifier(void)
120 {
121 return "RegisteredDataType";
122 }
123
124 static size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
125 {
126 size_t bytes;
127 bytes = IO::store( _os, _v.ival, _swap );
128 bytes += IO::store( _os, _v.dval, _swap );
129 bytes += IO::store( _os, _v.bval, _swap );
130 bytes += IO::store( _os, _v.vec4fval, _swap );
131 return _os.good() ? bytes : 0;
132 }
133
134 static size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
135 {
136 size_t bytes;
137 bytes = IO::restore( _is, _v.ival, _swap );
138 bytes += IO::restore( _is, _v.dval, _swap );
139 bytes += IO::restore( _is, _v.bval, _swap );
140 bytes += IO::restore( _is, _v.vec4fval, _swap );
141 return _is.good() ? bytes : 0;
142 }
143 };
144
145
146 template <> struct binary< MyMap >
147 {
148 typedef MyMap value_type;
149
150 static const bool is_streamable = true;
151
152 // return generic binary size of self, if known
153 static size_t size_of(void) { return UnknownSize; }
154
155 // return binary size of the value
156 static size_t size_of(const value_type& _v)
157 {
158 if (_v.empty())
159 return sizeof(unsigned int);
160
161 value_type::const_iterator it = _v.begin();
162 unsigned int N = _v.size();
163 size_t bytes = IO::size_of(N);
164
165 for(;it!=_v.end(); ++it)
166 {
167 bytes += IO::size_of( it->first );
168 bytes += IO::size_of( it->second );
169 }
170 return bytes;
171 }
172
173 static
174 size_t store(std::ostream& _os, const value_type& _v, bool _swap=false)
175 {
176 size_t bytes = 0;
177 unsigned int N = _v.size();
178
179 value_type::const_iterator it = _v.begin();
180
181 bytes += IO::store( _os, N, _swap );
182
183 for (; it != _v.end() && _os.good(); ++it)
184 {
185 bytes += IO::store( _os, it->first, _swap );
186 bytes += IO::store( _os, it->second, _swap );
187 }
188 return _os.good() ? bytes : 0;
189 }
190
191 static
192 size_t restore( std::istream& _is, value_type& _v, bool _swap=false)
193 {
194 size_t bytes = 0;
195 unsigned int N = 0;
196
197 _v.clear();
198
199 bytes += IO::restore( _is, N, _swap );
200 value_type::key_type key;
201 value_type::mapped_type val;
202
203 for (size_t i=0; i<N && _is.good(); ++i)
204 {
205 bytes += IO::restore( _is, key, _swap );
206 bytes += IO::restore( _is, val, _swap );
207 _v[key] = val;
208 }
209 return _is.good() ? bytes : 0;
210 }
211 };
212
213 }
214}
215
216#endif
217
218
219// ----------------------------------------------------------------------------
220
221int main(void)
222{
223 //
224 Mesh mesh;
225
226
227 // generate a geometry
228 generate_cube<Mesh>(mesh);
229
230
231 // should display 8 vertices, 18/12 edges, 12/6 faces (Tri/Poly)
232 mesh_stats(mesh);
233
234
235 // print out information about properties
236 mesh_property_stats(mesh);
237
238
239 std::cout << "Define some custom properties..\n";
240
246
247 std::cout << ".. and registrate them at the mesh object.\n";
248
249 mesh.add_property(vprop_float, "vprop_float");
250 mesh.add_property(eprop_bool, "eprop_bool");
251 mesh.add_property(fprop_string, "fprop_string");
252 mesh.add_property(hprop_mydata, "hprop_mydata");
253 mesh.add_property(mprop_map, "mprop_map");
254
255
256 mesh_property_stats(mesh);
257
258
259 std::cout << "Now let's fill the props..\n";
260
261 fill_props(mesh, vprop_float);
262 fill_props(mesh, eprop_bool);
263 fill_props(mesh, fprop_string);
264 fill_props(mesh, hprop_mydata);
265 fill_props(mesh, mprop_map);
266
267
268 std::cout << "Check props..\n";
269#define CHK_PROP( PH ) \
270 std::cout << " " << #PH << " " \
271 << (fill_props(mesh, PH, true)?"ok\n":"error\n")
272
273 CHK_PROP(vprop_float);
274 CHK_PROP(eprop_bool);
275 CHK_PROP(fprop_string);
276 CHK_PROP(hprop_mydata);
277 CHK_PROP(mprop_map);
278#undef CHK_PROP
279
280
281 std::cout << "Set persistent flag..\n";
282#define SET_PERS( PH ) \
283 mesh.property(PH).set_persistent(true); \
284 std::cout << " " << #PH << " " \
285 << (mesh.property(PH).persistent()?"ok\n":"failed!\n")
286
287 mesh.property(vprop_float).set_persistent(true);
288 std::cout << " vprop_float "
289 << (mesh.property(vprop_float).persistent()?"ok\n":"failed!\n");
290
291 SET_PERS( eprop_bool );
292 SET_PERS( fprop_string );
293 SET_PERS( hprop_mydata );
294 mesh.mproperty(mprop_map).set_persistent(true);
295 std::cout << " mprop_map "
296 << (mesh.mproperty(mprop_map).persistent()?"ok\n":"failed!\n");
297
298
299 std::cout << "Write mesh..";
300 if (IO::write_mesh( mesh, "persistence-check.om" ))
301 std::cout << " ok\n";
302 else
303 {
304 std::cout << " failed\n";
305 return 1;
306 }
307
308
309 std::cout << "Clear mesh\n";
310 mesh.clear();
311 mesh_stats(mesh, " ");
312
313
314 std::cout << "Read back mesh..";
315 try
316 {
317 if (IO::read_mesh( mesh, "persistence-check.om" ))
318 std::cout << " ok\n";
319 else
320 {
321 std::cout << " failed!\n";
322 return 1;
323 }
324 mesh_stats(mesh, " ");
325 }
326 catch( std::exception &x )
327 {
328 std::cerr << x.what() << std::endl;
329 return 1;
330 }
331
332
333 std::cout << "Check props..\n";
334#define CHK_PROP( PH ) \
335 std::cout << " " << #PH << " " \
336 << (fill_props(mesh, PH, true)?"ok\n":"error\n")
337 CHK_PROP(vprop_float);
338 CHK_PROP(eprop_bool);
339 CHK_PROP(fprop_string);
340 CHK_PROP(hprop_mydata);
341 CHK_PROP(mprop_map);
342#undef CHK_PROP
343
344 return 0;
345}
346
347// end of file
348// ============================================================================
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
Definition MeshIO.hh:190
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition MeshIO.hh:95
size_t size_of(void)
static size_t restore(std::istream &, value_type &, bool=false, bool=true)
Restore a value of T and return the number of bytes read.
Definition SR_binary.hh:125
static const bool is_streamable
Can we store T? Set this to true in your specialization.
Definition SR_binary.hh:101
static size_t store(std::ostream &, const value_type &, bool=false, bool=true)
Store a value of T and return the number of bytes written.
Definition SR_binary.hh:113
static std::string type_identifier(void)
A string that identifies the type of T.
Definition SR_binary.hh:109
static size_t size_of(void)
What's the size of T? If it depends on the actual value (e.g. for vectors) return UnknownSize.
Definition SR_binary.hh:104