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  * $Revision$ *
45  * $Date$ *
46  * *
47 \*===========================================================================*/
48 
49 #ifndef OPENMESH_KERNEL_OSG_PROPERTYT_HH
50 #define OPENMESH_KERNEL_OSG_PROPERTYT_HH
51 
52 
53 //== INCLUDES =================================================================
54 
56 #include <OpenMesh/Core/Mesh/BaseKernel.hh>
57 #include <OpenMesh/Core/Utils/GenProg.hh>
58 #include <OpenMesh/Core/Utils/Property.hh>
59 //
60 #include <osg/Geometry>
61 //
62 #include <stdexcept>
63 #include <vector>
64 
65 
66 //== NAMESPACES ===============================================================
67 
68 namespace OpenMesh {
69 namespace Kernel_OSG {
70 
71 
72 //== CLASS DEFINITION =========================================================
73 
74 
75 // ----------------------------------------------------------------------------
76 
91 template <typename GeoProperty>
92 class oPropertyT : public BaseProperty
93 {
94 public:
95 
96  // Type of the encapsulated OpenSG Geometry Property
97  typedef GeoProperty property_t;
98  typedef typename property_t::PtrType property_ptr_t;
99 
100  typedef typename property_t::StoredFieldType field_t;
101  typedef typename field_t::StoredType element_t;
102  typedef typename field_t::StoredType value_type;
103 
104 public:
105 
106  //
107  oPropertyT( property_ptr_t _geo_prop,
108  const std::string& _name = "<unknown>" )
109  : BaseProperty(_name), data_( _geo_prop )
110  {
111  osg_init_check();
112  }
113 
114  //
115  oPropertyT( const std::string& _name = "<unknown>" )
116  : BaseProperty(_name), data_(NULL)
117  {
118  data_ = property_t::create();
119 
120  // make sure data_ is not null. In that case most probably
121  // osg::osgInit() hasn't been executed!
122  osg_init_check();
123  }
124 
126  virtual ~oPropertyT()
127  { }
128 
129 public:
130 
131  oPropertyT& operator = (const oPropertyT& _rhs )
132  {
133  // Shallow copy! Remember, data_ is a osg pointer type, and the assign
134  // operator makes a shallow copy!
135  data_ = _rhs.data_;
136  return *this;
137 
138  }
139 
140 
141 public: // interface BaseProperty
142 
143  virtual void reserve(size_t _n) { data_->getField().reserve( _n ); }
144  virtual void resize(size_t _n) { data_->resize( _n ); }
145  virtual void push_back() { data_->resize( data_->size()+1 ); }
146  virtual void swap(size_t _i0, size_t _i1)
147  { std::swap( data_->getField()[_i0], data_->getField()[_i1] ); }
148 
149  virtual oPropertyT<property_t>* clone() const
150  {
152  if (n_elements() > 0)
153  {
154  // OSGGeoProperty does not provide a deep copy
155  dolly->resize(n_elements());
156  element_t *begin = const_cast<element_t*>(data());
157  element_t *end = begin+n_elements();
158  element_t *dst = const_cast<element_t*>(dolly->data());
159  std::copy( begin, end, dst );
160  }
161  return dolly;
162  }
163 
164 public:
165 
166  virtual void set_persistent( bool _yn )
167  {
168  check_and_set_persistent<element_t>(_yn);
169  }
170 
171  virtual size_t n_elements() const
172  { return data_==osg::NullFC ? UnknownSize : data_->getSize(); }
173 
174  virtual size_t element_size() const
175  { return UnknownSize; }
176 
177  virtual size_t store( std::ostream& _ostr, bool _swap ) const
178  { return 0; }
179 
180  virtual size_t restore( std::istream& _istr, bool _swap )
181  { return 0; }
182 
183 
184 public: // OpenSG GeoPropertyInterface compatibility
185 
186  void clear(void) { data_->clear(); }
187 
188 
189 public: // access to OpenSG GeoProperty
190 
191  property_ptr_t& osg_ptr()
192  { return data_; }
193 
194  const property_ptr_t& osg_ptr() const
195  { return data_; }
196 
197 
198  const element_t *data() const
199  { return &( (*this)[ 0 ] ); }
200 
201  element_t& operator[](size_t idx)
202  { return data_->getField()[ idx ]; }
203 
204  const element_t& operator[](size_t idx) const
205  { return data_->getField()[ idx ]; }
206 
207 
208 protected:
209 
210  property_ptr_t data_;
211 
212 
213 private:
214 
215  void osg_init_check(void)
216  {
217  // make sure data_ is not null. In that case most probably
218  // osg::osgInit() hasn't been executed!
219  if ( data_ == osg::NullFC )
220  throw std::logic_error("OpenSG Runtime Environment is not initialized: " \
221  "Use osg::osgInit()");
222  }
223 
224  oPropertyT( const oPropertyT& );
225 };
226 
227 // ----------------------------------------------------------------- class ----
228 
229 
230 // ------------------------------------------------------------ properties ----
231 
233 namespace VP {
234 
235  // ---------------------------------------- Positions
237 
246 
247  // ---------------------------------------- Normals
249 
253 
254  // ---------------------------------------- TexCoords
256 
262 
263  // ---------------------------------------- Colors
265 
272 
273 } // namespace VP
274 
275 
277 namespace FP {
278 
279  // ---------------------------------------- Types
282 
283  // ---------------------------------------- Lengths
286 
287  // ---------------------------------------- Indices
288 
290 
292  template < typename IsTriMesh >
293  class GeoIndicesUI32 : public _GeoIndicesUI32
294  {
295  public: // ---------------------------------------- typedefs
296 
297  typedef _GeoIndicesUI32 inherited_t;
298  typedef typename inherited_t::property_ptr_t property_ptr_t;
299 
300  public: // ---------------------------------------- ctor/dtor
301 
302  GeoIndicesUI32( property_ptr_t _geo_prop,
303  GeoPTypesUI8& _types,
304  GeoPLengthsUI32& _lengths)
305  : inherited_t( _geo_prop ), types_(_types), length_(_lengths)
306  { }
307 
308  GeoIndicesUI32( GeoPTypesUI8& _types,
309  GeoPLengthsUI32& _lengths)
310  : inherited_t(), types_(_types), length_(_lengths)
311  { }
312 
313  virtual ~GeoIndicesUI32()
314  { }
315 
316  public: // ---------------------------------------- inherited
317 
318  void swap(size_t _i0, size_t _i1) { _swap( _i0, _i1, IsTriMesh() ); }
319  virtual void reserve(size_t _n) { _reserve( _n, IsTriMesh() ); }
320  virtual void resize(size_t _n) { _resize( _n, IsTriMesh() ); }
321 
322  protected: // ------------------------------------- swap
323 
324  void _swap(size_t _i0, size_t _i1, GenProg::False )
325  {
326  omerr() << "Unsupported mesh type!" << std::endl;
327  assert(0);
328  }
329 
330  void _swap(size_t _i0, size_t _i1, GenProg::True )
331  {
332  size_t j0 = _i0 + _i0 + _i0;
333  size_t j1 = _i1 + _i1 + _i1;
334 
335  inherited_t::swap( j0, j1 );
336  inherited_t::swap( ++j0, ++j1 );
337  inherited_t::swap( ++j0, ++j1 );
338  }
339 
340  virtual void _reserve(size_t _n, GenProg::True )
341  { inherited_t::reserve( _n + _n + _n ); }
342 
343  virtual void _reserve(size_t _n, GenProg::False )
344  { assert( false ); }
345 
346  virtual void _resize(size_t _n, GenProg::True )
347  { inherited_t::resize( _n + _n + _n ); }
348 
349  virtual void _resize(size_t _n, GenProg::False )
350  { assert( false ); }
351 
352 
353  protected:
354 
355  GeoPTypesUI8 &types_;
356  GeoPLengthsUI32 &length_;
357 
358  };
359 
360 } // namespace FP
361 
362 
363 // ----------------------------------------------------------------------------
364 
365 #ifndef DOXY_IGNORE_THIS
366 
367 template <typename T> struct _t2vp;
368 template <> struct _t2vp< osg::Pnt2f >
369 { typedef osg::GeoPositions2f type; typedef VP::GeoPositions2f prop; };
370 
371 template <> struct _t2vp< osg::Pnt3f >
372 { typedef osg::GeoPositions3f type; typedef VP::GeoPositions3f prop; };
373 
374 template <> struct _t2vp< osg::Pnt4f >
375 { typedef osg::GeoPositions4f type; typedef VP::GeoPositions4f prop; };
376 
377 template <> struct _t2vp< osg::Pnt2d >
378 { typedef osg::GeoPositions2d type; typedef VP::GeoPositions2d prop; };
379 template <> struct _t2vp< osg::Pnt3d >
380 { typedef osg::GeoPositions3d type; typedef VP::GeoPositions3d prop; };
381 template <> struct _t2vp< osg::Pnt4d >
382 { typedef osg::GeoPositions4d type; typedef VP::GeoPositions4d prop; };
383 
384 template <typename T> struct _t2vn;
385 template <> struct _t2vn< osg::Vec3f >
386 { typedef osg::GeoNormals3f type; typedef VP::GeoNormals3f prop; };
387 
388 template <typename T> struct _t2vc;
389 template <> struct _t2vc< osg::Color3f >
390 { typedef osg::GeoColors3f type; typedef VP::GeoColors3f prop; };
391 
392 template <> struct _t2vc< osg::Color4f >
393 { typedef osg::GeoColors4f type; typedef VP::GeoColors4f prop; };
394 
395 template <> struct _t2vc< osg::Color3ub >
396 { typedef osg::GeoColors3ub type; typedef VP::GeoColors3ub prop; };
397 
398 template <> struct _t2vc< osg::Color4ub >
399 { typedef osg::GeoColors4ub type; typedef VP::GeoColors3ub prop; };
400 
401 template <typename T> struct _t2vtc;
402 template <> struct _t2vtc< osg::Vec2f >
403 { typedef osg::GeoTexCoords2f type; typedef VP::GeoTexCoords2f prop; };
404 
405 template <> struct _t2vtc< osg::Vec3f >
406 { typedef osg::GeoTexCoords3f type; typedef VP::GeoTexCoords3f prop; };
407 
408 #endif
409 
410 //=============================================================================
411 } // namespace Kernel_OSG
412 } // namespace OpenMesh
413 //=============================================================================
414 #endif // OPENMESH_PROPERTYT_HH defined
415 //=============================================================================
416 
virtual size_t restore(std::istream &_istr, bool _swap)
Definition: PropertyT.hh:180
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition: PropertyT.hh:174
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: PropertyT.hh:146
virtual size_t n_elements() const
Number of elements in property.
Definition: PropertyT.hh:171
virtual void push_back()
Extend the number of elements by one.
Definition: PropertyT.hh:145
void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: PropertyT.hh:318
Adaptor for osg::GeoIndicesUI32.
Definition: PropertyT.hh:293
virtual void set_persistent(bool _yn)
Definition: PropertyT.hh:166
void clear(void)
Clear all elements and free memory.
Definition: PropertyT.hh:186
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
Definition: BaseProperty.hh:70
virtual size_t store(std::ostream &_ostr, bool _swap) const
Store self as one binary block.
Definition: PropertyT.hh:177
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: PropertyT.hh:319
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: PropertyT.hh:320
virtual oPropertyT< property_t > * clone() const
Return a deep copy of self.
Definition: PropertyT.hh:149
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: PropertyT.hh:144
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: PropertyT.hh:143
BaseProperty(const std::string &_name="<unknown>")
Default constructor.
Definition: BaseProperty.hh:88