OpenMesh: OpenMesh/Tools/Kernel_OSG/PropertyT.hh Source File

| OpenMesh

PropertyT.hh
1 /* ========================================================================= *
2 * *
3 * OpenMesh *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openmesh.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenMesh. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 * ========================================================================= */
41
42
43
44 #ifndef OPENMESH_KERNEL_OSG_PROPERTYT_HH
45 #define OPENMESH_KERNEL_OSG_PROPERTYT_HH
46
47
48 //== INCLUDES =================================================================
49
50 #include <OpenMesh/Core/Mesh/Attributes.hh>
51 #include <OpenMesh/Core/Mesh/BaseKernel.hh>
52 #include <OpenMesh/Core/Utils/GenProg.hh>
53 #include <OpenMesh/Core/Utils/Property.hh>
54 //
55 #include <osg/Geometry>
56 //
57 #include <stdexcept>
58 #include <vector>
59
60
61 //== NAMESPACES ===============================================================
62
63 namespace OpenMesh {
64 namespace Kernel_OSG {
65
66
67 //== CLASS DEFINITION =========================================================
68
69
70 // ----------------------------------------------------------------------------
71
86 template <typename GeoProperty>
87 class oPropertyT : public BaseProperty
88 {
89 public:
90
91 // Type of the encapsulated OpenSG Geometry Property
92 typedef GeoProperty property_t;
93 typedef typename property_t::PtrType property_ptr_t;
94
95 typedef typename property_t::StoredFieldType field_t;
96 typedef typename field_t::StoredType element_t;
97 typedef typename field_t::StoredType value_type;
98
99 public:
100
101 //
102 oPropertyT( property_ptr_t _geo_prop,
103 const std::string& _name = "<unknown>" )
104 : BaseProperty(_name), data_( _geo_prop )
105 {
106 osg_init_check();
107 }
108
109 //
110 oPropertyT( const std::string& _name = "<unknown>" )
111 : BaseProperty(_name), data_(nullptr)
112 {
113 data_ = property_t::create();
114
115 // make sure data_ is not null. In that case most probably
116 // osg::osgInit() hasn't been executed!
117 osg_init_check();
118 }
119
121 virtual ~oPropertyT()
122 { }
123
124 public:
125
126 oPropertyT& operator = (const oPropertyT& _rhs )
127 {
128 // Shallow copy! Remember, data_ is a osg pointer type, and the assign
129 // operator makes a shallow copy!
130 data_ = _rhs.data_;
131 return *this;
132
133 }
134
135
136 public: // interface BaseProperty
137
138 virtual void reserve(size_t _n) { data_->getField().reserve( _n ); }
139 virtual void resize(size_t _n) { data_->resize( _n ); }
140 virtual void push_back() { data_->resize( data_->size()+1 ); }
141 virtual void swap(size_t _i0, size_t _i1)
142 { std::swap( data_->getField()[_i0], data_->getField()[_i1] ); }
143
144 virtual oPropertyT<property_t>* clone() const
145 {
146 oPropertyT<property_t> *dolly = new oPropertyT<property_t>();
147 if (n_elements() > 0)
148 {
149 // OSGGeoProperty does not provide a deep copy
150 dolly->resize(n_elements());
151 element_t *begin = const_cast<element_t*>(data());
152 element_t *end = begin+n_elements();
153 element_t *dst = const_cast<element_t*>(dolly->data());
154 std::copy( begin, end, dst );
155 }
156 return dolly;
157 }
158
159 public:
160
161 virtual void set_persistent( bool _yn )
162 {
163 check_and_set_persistent<element_t>(_yn);
164 }
165
166 virtual size_t n_elements() const
167 { return data_==osg::NullFC ? UnknownSize : data_->getSize(); }
168
169 virtual size_t element_size() const
170 { return UnknownSize; }
171
172 virtual size_t store( std::ostream& _ostr, bool _swap ) const
173 { return 0; }
174
175 virtual size_t restore( std::istream& _istr, bool _swap )
176 { return 0; }
177
178
179 public: // OpenSG GeoPropertyInterface compatibility
180
181 void clear(void) { data_->clear(); }
182
183
184 public: // access to OpenSG GeoProperty
185
186 property_ptr_t& osg_ptr()
187 { return data_; }
188
189 const property_ptr_t& osg_ptr() const
190 { return data_; }
191
192
193 const element_t *data() const
194 { return &( (*this)[ 0 ] ); }
195
196 element_t& operator[](size_t idx)
197 { return data_->getField()[ idx ]; }
198
199 const element_t& operator[](size_t idx) const
200 { return data_->getField()[ idx ]; }
201
202
203 protected:
204
205 property_ptr_t data_;
206
207
208 private:
209
210 void osg_init_check(void)
211 {
212 // make sure data_ is not null. In that case most probably
213 // osg::osgInit() hasn't been executed!
214 if ( data_ == osg::NullFC )
215 throw std::logic_error("OpenSG Runtime Environment is not initialized: " \
216 "Use osg::osgInit()");
217 }
218
219 oPropertyT( const oPropertyT& );
220 };
221
222 // ----------------------------------------------------------------- class ----
223
224
225 // ------------------------------------------------------------ properties ----
226
228 namespace VP {
229
230 // ---------------------------------------- Positions
232
233 typedef oPropertyT< osg::GeoPositions2d > GeoPositions2d;
235 typedef oPropertyT< osg::GeoPositions2f > GeoPositions2f;
236 typedef oPropertyT< osg::GeoPositions3d > GeoPositions3d;
237 typedef oPropertyT< osg::GeoPositions3f > GeoPositions3f;
238 typedef oPropertyT< osg::GeoPositions4d > GeoPositions4d;
239 typedef oPropertyT< osg::GeoPositions4f > GeoPositions4f;
241
242 // ---------------------------------------- Normals
244
245 typedef oPropertyT< osg::GeoNormals3f > GeoNormals3f;
248
249 // ---------------------------------------- TexCoords
251
252 typedef oPropertyT< osg::GeoTexCoords1f > GeoTexCoords1f;
254 typedef oPropertyT< osg::GeoTexCoords2f > GeoTexCoords2f;
255 typedef oPropertyT< osg::GeoTexCoords3f > GeoTexCoords3f;
257
258 // ---------------------------------------- Colors
260
261 typedef oPropertyT< osg::GeoColors3f > GeoColors3f;
263 typedef oPropertyT< osg::GeoColors3ub > GeoColors3ub;
264 typedef oPropertyT< osg::GeoColors4f > GeoColors4f;
265 typedef oPropertyT< osg::GeoColors4ub > GeoColors4ub;
267
268 } // namespace VP
269
270
272 namespace FP {
273
274 // ---------------------------------------- Types
276 typedef oPropertyT< osg::GeoPTypesUI8 > GeoPTypesUI8;
277
278 // ---------------------------------------- Lengths
280 typedef oPropertyT< osg::GeoPLengthsUI32 > GeoPLengthsUI32;
281
282 // ---------------------------------------- Indices
283
284 typedef oPropertyT< osg::GeoIndicesUI32 > _GeoIndicesUI32;
285
287 template < typename IsTriMesh >
288 class GeoIndicesUI32 : public _GeoIndicesUI32
289 {
290 public: // ---------------------------------------- typedefs
291
292 typedef _GeoIndicesUI32 inherited_t;
293 typedef typename inherited_t::property_ptr_t property_ptr_t;
294
295 public: // ---------------------------------------- ctor/dtor
296
297 GeoIndicesUI32( property_ptr_t _geo_prop,
298 GeoPTypesUI8& _types,
299 GeoPLengthsUI32& _lengths)
300 : inherited_t( _geo_prop ), types_(_types), length_(_lengths)
301 { }
302
303 GeoIndicesUI32( GeoPTypesUI8& _types,
304 GeoPLengthsUI32& _lengths)
305 : inherited_t(), types_(_types), length_(_lengths)
306 { }
307
308 virtual ~GeoIndicesUI32()
309 { }
310
311 public: // ---------------------------------------- inherited
312
313 void swap(size_t _i0, size_t _i1) { _swap( _i0, _i1, IsTriMesh() ); }
314 virtual void reserve(size_t _n) { _reserve( _n, IsTriMesh() ); }
315 virtual void resize(size_t _n) { _resize( _n, IsTriMesh() ); }
316
317 protected: // ------------------------------------- swap
318
319 void _swap(size_t _i0, size_t _i1, GenProg::False )
320 {
321 omerr() << "Unsupported mesh type!" << std::endl;
322 assert(0);
323 }
324
325 void _swap(size_t _i0, size_t _i1, GenProg::True )
326 {
327 size_t j0 = _i0 + _i0 + _i0;
328 size_t j1 = _i1 + _i1 + _i1;
329
330 inherited_t::swap( j0, j1 );
331 inherited_t::swap( ++j0, ++j1 );
332 inherited_t::swap( ++j0, ++j1 );
333 }
334
335 virtual void _reserve(size_t _n, GenProg::True )
336 { inherited_t::reserve( _n + _n + _n ); }
337
338 virtual void _reserve(size_t _n, GenProg::False )
339 { assert( false ); }
340
341 virtual void _resize(size_t _n, GenProg::True )
342 { inherited_t::resize( _n + _n + _n ); }
343
344 virtual void _resize(size_t _n, GenProg::False )
345 { assert( false ); }
346
347
348 protected:
349
350 GeoPTypesUI8 &types_;
351 GeoPLengthsUI32 &length_;
352
353 };
354
355 } // namespace FP
356
357
358 // ----------------------------------------------------------------------------
359
360 #ifndef DOXY_IGNORE_THIS
361
362 template <typename T> struct _t2vp;
363 template <> struct _t2vp< osg::Pnt2f >
364 { typedef osg::GeoPositions2f type; typedef VP::GeoPositions2f prop; };
365
366 template <> struct _t2vp< osg::Pnt3f >
367 { typedef osg::GeoPositions3f type; typedef VP::GeoPositions3f prop; };
368
369 template <> struct _t2vp< osg::Pnt4f >
370 { typedef osg::GeoPositions4f type; typedef VP::GeoPositions4f prop; };
371
372 template <> struct _t2vp< osg::Pnt2d >
373 { typedef osg::GeoPositions2d type; typedef VP::GeoPositions2d prop; };
374 template <> struct _t2vp< osg::Pnt3d >
375 { typedef osg::GeoPositions3d type; typedef VP::GeoPositions3d prop; };
376 template <> struct _t2vp< osg::Pnt4d >
377 { typedef osg::GeoPositions4d type; typedef VP::GeoPositions4d prop; };
378
379 template <typename T> struct _t2vn;
380 template <> struct _t2vn< osg::Vec3f >
381 { typedef osg::GeoNormals3f type; typedef VP::GeoNormals3f prop; };
382
383 template <typename T> struct _t2vc;
384 template <> struct _t2vc< osg::Color3f >
385 { typedef osg::GeoColors3f type; typedef VP::GeoColors3f prop; };
386
387 template <> struct _t2vc< osg::Color4f >
388 { typedef osg::GeoColors4f type; typedef VP::GeoColors4f prop; };
389
390 template <> struct _t2vc< osg::Color3ub >
391 { typedef osg::GeoColors3ub type; typedef VP::GeoColors3ub prop; };
392
393 template <> struct _t2vc< osg::Color4ub >
394 { typedef osg::GeoColors4ub type; typedef VP::GeoColors3ub prop; };
395
396 template <typename T> struct _t2vtc;
397 template <> struct _t2vtc< osg::Vec2f >
398 { typedef osg::GeoTexCoords2f type; typedef VP::GeoTexCoords2f prop; };
399
400 template <> struct _t2vtc< osg::Vec3f >
401 { typedef osg::GeoTexCoords3f type; typedef VP::GeoTexCoords3f prop; };
402
403 #endif
404
405 //=============================================================================
406 } // namespace Kernel_OSG
407 } // namespace OpenMesh
408 //=============================================================================
409 #endif // OPENMESH_PROPERTYT_HH defined
410 //=============================================================================
411
Attributes.hh
This file provides some macros containing attribute usage.
OpenMesh
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
OpenMesh::Vec2f
VectorT< float, 2 > Vec2f
2-float vector
Definition: Vector11T.hh:833
OpenMesh::Vec3f
VectorT< float, 3 > Vec3f
3-float vector
Definition: Vector11T.hh:850
OpenMesh::Kernel_OSG::VP::GeoColors3f
oPropertyT< osg::GeoColors3f > GeoColors3f
Adaptor for osg::GeoColors.
Definition: PropertyT.hh:262
OpenMesh::Kernel_OSG::VP::GeoPositions2d
oPropertyT< osg::GeoPositions2d > GeoPositions2d
Adaptor for osg::GeoPositions.
Definition: PropertyT.hh:234
OpenMesh::Kernel_OSG::VP::GeoColors4ub
oPropertyT< osg::GeoColors4ub > GeoColors4ub
Adaptor for osg::GeoColors.
Definition: PropertyT.hh:265
OpenMesh::Kernel_OSG::VP::GeoPositions3f
oPropertyT< osg::GeoPositions3f > GeoPositions3f
Adaptor for osg::GeoPositions.
Definition: PropertyT.hh:237
OpenMesh::Kernel_OSG::VP::GeoTexCoords2f
oPropertyT< osg::GeoTexCoords2f > GeoTexCoords2f
Adaptor for osg::GeoTexCoords.
Definition: PropertyT.hh:254
OpenMesh::Kernel_OSG::VP::GeoTexCoords3f
oPropertyT< osg::GeoTexCoords3f > GeoTexCoords3f
Adaptor for osg::GeoTexCoords.
Definition: PropertyT.hh:255
OpenMesh::Kernel_OSG::VP::GeoColors4f
oPropertyT< osg::GeoColors4f > GeoColors4f
Adaptor for osg::GeoColors.
Definition: PropertyT.hh:264
OpenMesh::Kernel_OSG::VP::GeoPositions3d
oPropertyT< osg::GeoPositions3d > GeoPositions3d
Adaptor for osg::GeoPositions.
Definition: PropertyT.hh:236
OpenMesh::Kernel_OSG::VP::GeoTexCoords1f
oPropertyT< osg::GeoTexCoords1f > GeoTexCoords1f
Adaptor for osg::GeoTexCoords.
Definition: PropertyT.hh:253
OpenMesh::Kernel_OSG::VP::GeoNormals3f
oPropertyT< osg::GeoNormals3f > GeoNormals3f
Adaptor for osg::GeoNormals.
Definition: PropertyT.hh:246
OpenMesh::Kernel_OSG::VP::GeoPositions4d
oPropertyT< osg::GeoPositions4d > GeoPositions4d
Adaptor for osg::GeoPositions.
Definition: PropertyT.hh:238
OpenMesh::Kernel_OSG::VP::GeoColors3ub
oPropertyT< osg::GeoColors3ub > GeoColors3ub
Adaptor for osg::GeoColors.
Definition: PropertyT.hh:263
OpenMesh::Kernel_OSG::VP::GeoPositions2f
oPropertyT< osg::GeoPositions2f > GeoPositions2f
Adaptor for osg::GeoPositions.
Definition: PropertyT.hh:235
OpenMesh::Kernel_OSG::VP::GeoPositions4f
oPropertyT< osg::GeoPositions4f > GeoPositions4f
Adaptor for osg::GeoPositions.
Definition: PropertyT.hh:239
OpenMesh::Kernel_OSG::FP::GeoPTypesUI8
oPropertyT< osg::GeoPTypesUI8 > GeoPTypesUI8
Adaptor for osg::GeoPTypesUI8.
Definition: PropertyT.hh:276
OpenMesh::Kernel_OSG::FP::GeoPLengthsUI32
oPropertyT< osg::GeoPLengthsUI32 > GeoPLengthsUI32
Adaptor for osg::GeoPLengthsUI32.
Definition: PropertyT.hh:280
OpenMesh::BaseProperty
Abstract class defining the basic interface of a dynamic property.
Definition: BaseProperty.hh:61
OpenMesh::BaseProperty::BaseProperty
BaseProperty(const std::string &_name="<unknown>", const std::string &_internal_type_name="<unknown>")
Default constructor.
Definition: BaseProperty.hh:84
OpenMesh::BaseProperty::UnknownSize
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
Definition: BaseProperty.hh:65
OpenMesh::Kernel_OSG::oPropertyT
Property adaptor for OpenSG GeoProperties.
Definition: PropertyT.hh:88
OpenMesh::Kernel_OSG::oPropertyT::store
virtual size_t store(std::ostream &_ostr, bool _swap) const
Store self as one binary block.
Definition: PropertyT.hh:172
OpenMesh::Kernel_OSG::oPropertyT::n_elements
virtual size_t n_elements() const
Number of elements in property.
Definition: PropertyT.hh:166
OpenMesh::Kernel_OSG::oPropertyT::clone
virtual oPropertyT< property_t > * clone() const
Return a deep copy of self.
Definition: PropertyT.hh:144
OpenMesh::Kernel_OSG::oPropertyT::reserve
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: PropertyT.hh:138
OpenMesh::Kernel_OSG::oPropertyT::resize
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: PropertyT.hh:139
OpenMesh::Kernel_OSG::oPropertyT::clear
void clear(void)
Clear all elements and free memory.
Definition: PropertyT.hh:181
OpenMesh::Kernel_OSG::oPropertyT::push_back
virtual void push_back()
Extend the number of elements by one.
Definition: PropertyT.hh:140
OpenMesh::Kernel_OSG::oPropertyT::swap
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: PropertyT.hh:141
OpenMesh::Kernel_OSG::oPropertyT::set_persistent
virtual void set_persistent(bool _yn)
Enable or disable persistency.
Definition: PropertyT.hh:161
OpenMesh::Kernel_OSG::oPropertyT::element_size
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition: PropertyT.hh:169
OpenMesh::Kernel_OSG::oPropertyT::restore
virtual size_t restore(std::istream &_istr, bool _swap)
Restore self from a binary block.
Definition: PropertyT.hh:175
OpenMesh::Kernel_OSG::FP::GeoIndicesUI32
Adaptor for osg::GeoIndicesUI32.
Definition: PropertyT.hh:289
OpenMesh::Kernel_OSG::FP::GeoIndicesUI32::swap
void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: PropertyT.hh:313
OpenMesh::Kernel_OSG::FP::GeoIndicesUI32::reserve
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: PropertyT.hh:314
OpenMesh::Kernel_OSG::FP::GeoIndicesUI32::resize
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: PropertyT.hh:315