Developer Documentation
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 
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  {
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 
241 
242  // ---------------------------------------- Normals
244 
248 
249  // ---------------------------------------- TexCoords
251 
257 
258  // ---------------------------------------- Colors
260 
267 
268 } // namespace VP
269 
270 
272 namespace FP {
273 
274  // ---------------------------------------- Types
277 
278  // ---------------------------------------- Lengths
281 
282  // ---------------------------------------- Indices
283 
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 
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: PropertyT.hh:141
virtual void set_persistent(bool _yn)
Definition: PropertyT.hh:161
virtual void push_back()
Extend the number of elements by one.
Definition: PropertyT.hh:140
void clear(void)
Clear all elements and free memory.
Definition: PropertyT.hh:181
virtual oPropertyT< property_t > * clone() const
Return a deep copy of self.
Definition: PropertyT.hh:144
BaseProperty(const std::string &_name="<unknown>", const std::string &_internal_type_name="<unknown>")
Default constructor.
Definition: BaseProperty.hh:83
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: PropertyT.hh:139
Adaptor for osg::GeoIndicesUI32.
Definition: PropertyT.hh:288
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: PropertyT.hh:138
void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: PropertyT.hh:313
virtual size_t restore(std::istream &_istr, bool _swap)
Definition: PropertyT.hh:175
virtual size_t n_elements() const
Number of elements in property.
Definition: PropertyT.hh:166
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: PropertyT.hh:315
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
Definition: BaseProperty.hh:65
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition: PropertyT.hh:169
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: PropertyT.hh:314
virtual size_t store(std::ostream &_ostr, bool _swap) const
Store self as one binary block.
Definition: PropertyT.hh:172