OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PropertyT.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 /*===========================================================================*\
36 * *
37 * $Revision: 736 $ *
38 * $Date: 2012-10-08 09:30:49 +0200 (Mo, 08 Okt 2012) $ *
39 * *
40 \*===========================================================================*/
41
42 #ifndef OPENMESH_KERNEL_OSG_PROPERTYT_HH
43 #define OPENMESH_KERNEL_OSG_PROPERTYT_HH
44
45
46 //== INCLUDES =================================================================
47
48 #include <OpenMesh/Core/Mesh/Attributes.hh>
49 #include <OpenMesh/Core/Mesh/BaseKernel.hh>
50 #include <OpenMesh/Core/Utils/GenProg.hh>
51 #include <OpenMesh/Core/Utils/Property.hh>
52 //
53 #include <osg/Geometry>
54 //
55 #include <stdexcept>
56 #include <vector>
57
58
59 //== NAMESPACES ===============================================================
60
61 namespace OpenMesh {
62 namespace Kernel_OSG {
63
64
65 //== CLASS DEFINITION =========================================================
66
67
68 // ----------------------------------------------------------------------------
69
84 template <typename GeoProperty>
85 class oPropertyT : public BaseProperty
86 {
87 public:
88
89 // Type of the encapsulated OpenSG Geometry Property
90 typedef GeoProperty property_t;
91 typedef typename property_t::PtrType property_ptr_t;
92
93 typedef typename property_t::StoredFieldType field_t;
94 typedef typename field_t::StoredType element_t;
95 typedef typename field_t::StoredType value_type;
96
97 public:
98
99 //
100 oPropertyT( property_ptr_t _geo_prop,
101 const std::string& _name = "<unknown>" )
102 : BaseProperty(_name), data_( _geo_prop )
103 {
104 osg_init_check();
105 }
106
107 //
108 oPropertyT( const std::string& _name = "<unknown>" )
109 : BaseProperty(_name), data_(NULL)
110 {
111 data_ = property_t::create();
112
113 // make sure data_ is not null. In that case most probably
114 // osg::osgInit() hasn't been executed!
115 osg_init_check();
116 }
117
119 virtual ~oPropertyT()
120 { }
121
122 public:
123
124 oPropertyT& operator = (const oPropertyT& _rhs )
125 {
126 // Shallow copy! Remember, data_ is a osg pointer type, and the assign
127 // operator makes a shallow copy!
128 data_ = _rhs.data_;
129 return *this;
130
131 }
132
133
134 public: // interface BaseProperty
135
136 virtual void reserve(size_t _n) { data_->getField().reserve( _n ); }
137 virtual void resize(size_t _n) { data_->resize( _n ); }
138 virtual void push_back() { data_->resize( data_->size()+1 ); }
139 virtual void swap(size_t _i0, size_t _i1)
140 { std::swap( data_->getField()[_i0], data_->getField()[_i1] ); }
141
142 virtual oPropertyT<property_t>* clone() const
143 {
144 oPropertyT<property_t> *dolly = new oPropertyT<property_t>();
145 if (n_elements() > 0)
146 {
147 // OSGGeoProperty does not provide a deep copy
148 dolly->resize(n_elements());
149 element_t *begin = const_cast<element_t*>(data());
150 element_t *end = begin+n_elements();
151 element_t *dst = const_cast<element_t*>(dolly->data());
152 std::copy( begin, end, dst );
153 }
154 return dolly;
155 }
156
157 public:
158
159 virtual void set_persistent( bool _yn )
160 {
161 check_and_set_persistent<element_t>(_yn);
162 }
163
164 virtual size_t n_elements() const
165 { return data_==osg::NullFC ? UnknownSize : data_->getSize(); }
166
167 virtual size_t element_size() const
168 { return UnknownSize; }
169
170 virtual size_t store( std::ostream& _ostr, bool _swap ) const
171 { return 0; }
172
173 virtual size_t restore( std::istream& _istr, bool _swap )
174 { return 0; }
175
176
177 public: // OpenSG GeoPropertyInterface compatibility
178
179 void clear(void) { data_->clear(); }
180
181
182 public: // access to OpenSG GeoProperty
183
184 property_ptr_t& osg_ptr()
185 { return data_; }
186
187 const property_ptr_t& osg_ptr() const
188 { return data_; }
189
190
191 const element_t *data() const
192 { return &( (*this)[ 0 ] ); }
193
194 element_t& operator[](size_t idx)
195 { return data_->getField()[ idx ]; }
196
197 const element_t& operator[](size_t idx) const
198 { return data_->getField()[ idx ]; }
199
200
201 protected:
202
203 property_ptr_t data_;
204
205
206 private:
207
208 void osg_init_check(void)
209 {
210 // make sure data_ is not null. In that case most probably
211 // osg::osgInit() hasn't been executed!
212 if ( data_ == osg::NullFC )
213 throw std::logic_error("OpenSG Runtime Environment is not initialized: " \
214 "Use osg::osgInit()");
215 }
216
217 oPropertyT( const oPropertyT& );
218 };
219
220 // ----------------------------------------------------------------- class ----
221
222
223 // ------------------------------------------------------------ properties ----
224
226 namespace VP {
227
228 // ---------------------------------------- Positions
230
231
232 typedef oPropertyT< osg::GeoPositions2d > GeoPositions2d;
233 typedef oPropertyT< osg::GeoPositions2f > GeoPositions2f;
234 typedef oPropertyT< osg::GeoPositions3d > GeoPositions3d;
235 typedef oPropertyT< osg::GeoPositions3f > GeoPositions3f;
236 typedef oPropertyT< osg::GeoPositions4d > GeoPositions4d;
237 typedef oPropertyT< osg::GeoPositions4f > GeoPositions4f;
239
240 // ---------------------------------------- Normals
242
243
244 typedef oPropertyT< osg::GeoNormals3f > GeoNormals3f;
246
247 // ---------------------------------------- TexCoords
249
250
251 typedef oPropertyT< osg::GeoTexCoords1f > GeoTexCoords1f;
252 typedef oPropertyT< osg::GeoTexCoords2f > GeoTexCoords2f;
253 typedef oPropertyT< osg::GeoTexCoords3f > GeoTexCoords3f;
255
256 // ---------------------------------------- Colors
258
259
260 typedef oPropertyT< osg::GeoColors3f > GeoColors3f;
261 typedef oPropertyT< osg::GeoColors3ub > GeoColors3ub;
262 typedef oPropertyT< osg::GeoColors4f > GeoColors4f;
263 typedef oPropertyT< osg::GeoColors4ub > GeoColors4ub;
265
266 } // namespace VP
267
268
270 namespace FP {
271
272 // ---------------------------------------- Types
274 typedef oPropertyT< osg::GeoPTypesUI8 > GeoPTypesUI8;
275
276 // ---------------------------------------- Lengths
278 typedef oPropertyT< osg::GeoPLengthsUI32 > GeoPLengthsUI32;
279
280 // ---------------------------------------- Indices
281
282 typedef oPropertyT< osg::GeoIndicesUI32 > _GeoIndicesUI32;
283
285 template < typename IsTriMesh >
286 class GeoIndicesUI32 : public _GeoIndicesUI32
287 {
288 public: // ---------------------------------------- typedefs
289
290 typedef _GeoIndicesUI32 inherited_t;
291 typedef typename inherited_t::property_ptr_t property_ptr_t;
292
293 public: // ---------------------------------------- ctor/dtor
294
295 GeoIndicesUI32( property_ptr_t _geo_prop,
296 GeoPTypesUI8& _types,
297 GeoPLengthsUI32& _lengths)
298 : inherited_t( _geo_prop ), types_(_types), length_(_lengths)
299 { }
300
301 GeoIndicesUI32( GeoPTypesUI8& _types,
302 GeoPLengthsUI32& _lengths)
303 : inherited_t(), types_(_types), length_(_lengths)
304 { }
305
306 virtual ~GeoIndicesUI32()
307 { }
308
309 public: // ---------------------------------------- inherited
310
311 void swap(size_t _i0, size_t _i1) { _swap( _i0, _i1, IsTriMesh() ); }
312 virtual void reserve(size_t _n) { _reserve( _n, IsTriMesh() ); }
313 virtual void resize(size_t _n) { _resize( _n, IsTriMesh() ); }
314
315 protected: // ------------------------------------- swap
316
317 void _swap(size_t _i0, size_t _i1, GenProg::False )
318 {
319 omerr() << "Unsupported mesh type!" << std::endl;
320 assert(0);
321 }
322
323 void _swap(size_t _i0, size_t _i1, GenProg::True )
324 {
325 size_t j0 = _i0 + _i0 + _i0;
326 size_t j1 = _i1 + _i1 + _i1;
327
328 inherited_t::swap( j0, j1 );
329 inherited_t::swap( ++j0, ++j1 );
330 inherited_t::swap( ++j0, ++j1 );
331 }
332
333 virtual void _reserve(size_t _n, GenProg::True )
334 { inherited_t::reserve( _n + _n + _n ); }
335
336 virtual void _reserve(size_t _n, GenProg::False )
337 { assert( false ); }
338
339 virtual void _resize(size_t _n, GenProg::True )
340 { inherited_t::resize( _n + _n + _n ); }
341
342 virtual void _resize(size_t _n, GenProg::False )
343 { assert( false ); }
344
345
346 protected:
347
348 GeoPTypesUI8 &types_;
349 GeoPLengthsUI32 &length_;
350
351 };
352
353 } // namespace FP
354
355
356 // ----------------------------------------------------------------------------
357
358 #ifndef DOXY_IGNORE_THIS
359
360 template <typename T> struct _t2vp;
361 template <> struct _t2vp< osg::Pnt2f >
362 { typedef osg::GeoPositions2f type; typedef VP::GeoPositions2f prop; };
363
364 template <> struct _t2vp< osg::Pnt3f >
365 { typedef osg::GeoPositions3f type; typedef VP::GeoPositions3f prop; };
366
367 template <> struct _t2vp< osg::Pnt4f >
368 { typedef osg::GeoPositions4f type; typedef VP::GeoPositions4f prop; };
369
370 template <> struct _t2vp< osg::Pnt2d >
371 { typedef osg::GeoPositions2d type; typedef VP::GeoPositions2d prop; };
372 template <> struct _t2vp< osg::Pnt3d >
373 { typedef osg::GeoPositions3d type; typedef VP::GeoPositions3d prop; };
374 template <> struct _t2vp< osg::Pnt4d >
375 { typedef osg::GeoPositions4d type; typedef VP::GeoPositions4d prop; };
376
377 template <typename T> struct _t2vn;
378 template <> struct _t2vn< osg::Vec3f >
379 { typedef osg::GeoNormals3f type; typedef VP::GeoNormals3f prop; };
380
381 template <typename T> struct _t2vc;
382 template <> struct _t2vc< osg::Color3f >
383 { typedef osg::GeoColors3f type; typedef VP::GeoColors3f prop; };
384
385 template <> struct _t2vc< osg::Color4f >
386 { typedef osg::GeoColors4f type; typedef VP::GeoColors4f prop; };
387
388 template <> struct _t2vc< osg::Color3ub >
389 { typedef osg::GeoColors3ub type; typedef VP::GeoColors3ub prop; };
390
391 template <> struct _t2vc< osg::Color4ub >
392 { typedef osg::GeoColors4ub type; typedef VP::GeoColors3ub prop; };
393
394 template <typename T> struct _t2vtc;
395 template <> struct _t2vtc< osg::Vec2f >
396 { typedef osg::GeoTexCoords2f type; typedef VP::GeoTexCoords2f prop; };
397
398 template <> struct _t2vtc< osg::Vec3f >
399 { typedef osg::GeoTexCoords3f type; typedef VP::GeoTexCoords3f prop; };
400
401 #endif
402
403 //=============================================================================
404 } // namespace Kernel_OSG
405 } // namespace OpenMesh
406 //=============================================================================
407 #endif // OPENMESH_PROPERTYT_HH defined
408 //=============================================================================
409