fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / NodeCores / Drawables / Geometry / PropertiesBase / OSGGeoVectorPropertyConversion.h
blobbecbd000d4107318893a4fab35a0a333a674075e
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGGEOVECTORPROPERTYCONVERSION_H_
40 #define _OSGGEOVECTORPROPERTYCONVERSION_H_
41 #ifdef __sgi
42 #pragma once
43 #endif
45 #include "OSGConfig.h"
46 #include "OSGVector.h"
48 OSG_BEGIN_NAMESPACE
50 #if 0
51 namespace detail
54 // The following is quite involved because of the special handling the
55 // Vec*fx, Pnt*fx and Color*fx types (i.e. element type Fixed32) require.
57 // When performing a conversion with an fx type as source the
58 // Fixed32::getValue() method is called (which seems bogus - but so be it) to
59 // obtain an Int32 value used in the further computation - this leads to the
60 // VectorElementAccessFunc and Fixed32VectorElementAccessFunc types.
62 // Conversions with an fx type as destination only succeed for the few types
63 // Fixed32 accepts in its constructors (Real32, UInt32, Fixed32), but if
64 // source and destination are of type Fixed32 we must be careful to avoid the
65 // getValue call, because there is no public constructor that accepts Int32.
66 // To allow this behaviour the *VectorElementAccessFunc templates have two
67 // type parameters.
69 /*! \brief No-op normalization static function object.
71 template <class ValueTypeT>
72 struct NoNormalizationFunc
74 typedef ValueTypeT ValueType;
76 static inline ValueType
77 apply(const ValueType& src,
78 const Real64 scale = 1, const Real64 offset = 0);
81 /*! \brief "In" conversion normalization static function object.
83 template <class ValueTypeT>
84 struct InNormalizationFunc
86 typedef ValueTypeT ValueType;
88 static inline ValueType
89 apply(const ValueType& src,
90 const Real64 scale = 1, const Real64 offset = 0);
93 /*! \brief "Out" conversion normalization static function object.
95 template <class ValueTypeT>
96 struct OutNormalizationFunc
98 typedef ValueTypeT ValueType;
100 static inline ValueType
101 apply(const ValueType& src,
102 const Real64 scale = 1, const Real64 offset = 0);
105 /*! \brief Static function object for accessing an element of a vector with
106 Fixed32 elements.
108 template <class ValueTypeT>
109 struct Fixed32VectorElementAccessFunc
111 typedef ValueTypeT ValueType;
112 typedef Int32 ElementType;
114 static inline ElementType
115 apply(const ValueType& src, const UInt32 index);
118 /*! \brief Static function object for accessing an element of a vector.
120 template <class ValueTypeT>
121 struct VectorElementAccessFunc
123 typedef ValueTypeT ValueType;
124 typedef typename ValueType::ValueType ElementType;
126 static inline ElementType
127 apply(const ValueType& src, const UInt32 index);
130 /*! \brief Partial specialization for vectors with Fixed32 elements,
131 implemented via inheritance.
133 template <UInt32 SizeI>
134 struct VectorElementAccessFunc<Vector<Fixed32, SizeI> >
135 : public Fixed32VectorElementAccessFunc<Vector<Fixed32, SizeI> >
139 /*! \brief Partial specialization for points with Fixed32 elements,
140 implemented via inheritance.
142 template <UInt32 SizeI>
143 struct VectorElementAccessFunc<Point<Fixed32, SizeI> >
144 : public Fixed32VectorElementAccessFunc<Point<Fixed32, SizeI> >
148 /*! \brief Specialization for Color3fx, implemented via inheritance.
150 template <>
151 struct VectorElementAccessFunc<Color3fx>
152 : public Fixed32VectorElementAccessFunc<Color3fx>
156 /*! \brief Specialization for Color4fx, implemented via inheritance.
158 template <>
159 struct VectorElementAccessFunc<Color4fx>
160 : public Fixed32VectorElementAccessFunc<Color4fx>
164 /*! \brief Main conversion function.
166 template <class DestTypeT,
167 class SourceTypeT,
168 template <class> class NormalizeFuncT,
169 template <class> class ElementAccessFuncT>
170 struct GeoConvertFunc
172 typedef DestTypeT DestType;
173 typedef SourceTypeT SourceType;
175 typedef typename DestType::ValueType DestElementType;
176 typedef typename SourceType::ValueType SourceElementType;
178 typedef NormalizeFuncT<SourceElementType> NormFunc;
179 typedef ElementAccessFuncT<SourceType> AccessFunc;
181 static inline void
182 convert( DestType& dest, const SourceType& src,
183 const Real64 scale = 1, const Real64 offset = 0);
186 /*! \brief Partial specialization to optimize same type conversions without
187 normalization.
189 template <class ValueTypeT,
190 template <class> class ElementAccessFuncT>
191 struct GeoConvertFunc<ValueTypeT,
192 ValueTypeT,
193 NoNormalizationFunc,
194 ElementAccessFuncT >
196 typedef ValueTypeT DestType;
197 typedef ValueTypeT SourceType;
199 typedef typename DestType::ValueType DestElementType;
200 typedef typename SourceType::ValueType SourceElementType;
202 typedef NoNormalizationFunc<SourceElementType> NormFunc;
203 typedef ElementAccessFuncT<SourceType> AccessFunc;
205 static inline void
206 convert( DestType& dest, const SourceType& src,
207 const Real64 scale = 1, const Real64 offset = 0);
210 /*! \brief The "interface" of the implementation.
212 template <class DestTypeT,
213 class SourceTypeT,
214 template <class> class NormalizeFuncT>
215 struct GeoConvertImpl : public GeoConvertFunc<DestTypeT,
216 SourceTypeT,
217 NormalizeFuncT,
218 VectorElementAccessFunc>
222 } // namespace detail
224 OSG_END_NAMESPACE
226 // pull in implementation for inlining below
227 #include "OSGGeoVectorPropertyConversion.inl"
229 OSG_BEGIN_NAMESPACE
231 struct GeoConvert
233 template <class StoredTypeT, class ExternalTypeT>
234 inline static void
235 convertIn( StoredTypeT& dest, const ExternalTypeT& src,
236 const Real64 scale = 1, const Real64 offset = 0)
238 detail::GeoConvertImpl<StoredTypeT,
239 ExternalTypeT,
240 detail::NoNormalizationFunc>::convert(
241 dest, src, scale, offset);
244 template <class ExternalTypeT, class StoredTypeT>
245 inline static void
246 convertOut( ExternalTypeT& dest, const StoredTypeT& src,
247 const Real64 scale = 1, const Real64 offset = 0)
249 detail::GeoConvertImpl<ExternalTypeT,
250 StoredTypeT,
251 detail::NoNormalizationFunc>::convert(
252 dest, src, scale, offset);
256 struct GeoConvertNormalize
258 template <class StoredTypeT, class ExternalTypeT>
259 inline static void
260 convertIn( StoredTypeT& dest, const ExternalTypeT& src,
261 const Real64 scale = 1, const Real64 offset = 0)
263 detail::GeoConvertImpl<StoredTypeT,
264 ExternalTypeT,
265 detail::InNormalizationFunc>::convert(
266 dest, src, scale, offset);
269 template <class ExternalTypeT, class StoredTypeT>
270 inline static void
271 convertOut( ExternalTypeT& dest, const StoredTypeT& src,
272 const Real64 scale = 1, const Real64 offset = 0)
274 detail::GeoConvertImpl<ExternalTypeT,
275 StoredTypeT,
276 detail::OutNormalizationFunc>::convert(
277 dest, src, scale, offset);
281 #else
283 /* Helper classes for vector type conversions */
285 // Converter class for vector conversion
286 struct GeoConvert
288 template <class ExternalType, class StoredType>
289 inline static void convertIn(StoredType& dest, const ExternalType& src,
290 Real64 scale = 1, Real64 offset = 0)
292 if(ExternalType::_uiSize >= StoredType::_uiSize)
294 UInt32 i;
295 for(i = 0; i < StoredType::_uiSize; ++i)
296 dest[i] = static_cast<typename StoredType::ValueType>(src[i]);
298 else
300 UInt32 i;
301 for(i = 0; i < ExternalType::_uiSize; ++i)
302 dest[i] = static_cast<typename StoredType::ValueType>(src[i]);
303 for(; i < StoredType::_uiSize; ++i)
304 dest[i] = StoredType::Null[i];
308 template <class ExternalType, class StoredType>
309 inline static void convertOut(ExternalType& dest, const StoredType& src,
310 Real64 scale = 1, Real64 offset = 0)
312 if(StoredType::_uiSize >= ExternalType::_uiSize)
314 UInt32 i;
315 for(i = 0; i < ExternalType::_uiSize; ++i)
316 dest[i] = static_cast<typename ExternalType::ValueType>(src[i]);
318 else
320 UInt32 i;
321 for(i = 0; i < StoredType::_uiSize; ++i)
322 dest[i] = static_cast<typename ExternalType::ValueType>(src[i]);
323 for(; i < ExternalType::_uiSize; ++i)
324 dest[i] = ExternalType::Null[i];
328 template <class ExternalType, class StoredType>
329 inline static void convertCustomOut( ExternalType &dest,
330 const StoredType &src,
331 Real64 scale = 1,
332 Real64 offset = 0)
334 if(StoredType::_uiSize >= ExternalType::_uiSize)
336 UInt32 i;
337 for(i = 0; i < ExternalType::_uiSize; ++i)
339 dest[i] =
340 static_cast<typename ExternalType::ValueType>(
341 src[i].getValue());
344 else
346 UInt32 i;
347 for(i = 0; i < StoredType::_uiSize; ++i)
349 dest[i] =
350 static_cast<typename ExternalType::ValueType>(
351 src[i].getValue());
353 for(; i < ExternalType::_uiSize; ++i)
354 dest[i] = ExternalType::Null[i];
358 template <class ExternalType>
359 inline static void convertOut(ExternalType& dest, const Vec1fx& src,
360 Real64 scale = 1, Real64 offset = 0)
362 convertCustomOut(dest, src, scale, offset);
364 template <class ExternalType>
365 inline static void convertOut(ExternalType& dest, const Vec2fx& src,
366 Real64 scale = 1, Real64 offset = 0)
368 convertCustomOut(dest, src, scale, offset);
370 template <class ExternalType>
371 inline static void convertOut(ExternalType& dest, const Vec3fx& src,
372 Real64 scale = 1, Real64 offset = 0)
374 convertCustomOut(dest, src, scale, offset);
376 template <class ExternalType>
377 inline static void convertOut(ExternalType& dest, const Vec4fx& src,
378 Real64 scale = 1, Real64 offset = 0)
380 convertCustomOut(dest, src, scale, offset);
383 template <class ExternalType>
384 inline static void convertOut(ExternalType& dest, const Pnt1fx& src,
385 Real64 scale = 1, Real64 offset = 0)
387 convertCustomOut(dest, src, scale, offset);
389 template <class ExternalType>
390 inline static void convertOut(ExternalType& dest, const Pnt2fx& src,
391 Real64 scale = 1, Real64 offset = 0)
393 convertCustomOut(dest, src, scale, offset);
395 template <class ExternalType>
396 inline static void convertOut(ExternalType& dest, const Pnt3fx& src,
397 Real64 scale = 1, Real64 offset = 0)
399 convertCustomOut(dest, src, scale, offset);
401 template <class ExternalType>
402 inline static void convertOut(ExternalType& dest, const Pnt4fx& src,
403 Real64 scale = 1, Real64 offset = 0)
405 convertCustomOut(dest, src, scale, offset);
407 template <class ExternalType>
408 inline static void convertOut(ExternalType& dest, const Color3fx& src,
409 Real64 scale = 1, Real64 offset = 0)
411 convertCustomOut(dest, src, scale, offset);
413 template <class ExternalType>
414 inline static void convertOut(ExternalType& dest, const Color4fx& src,
415 Real64 scale = 1, Real64 offset = 0)
417 convertCustomOut(dest, src, scale, offset);
422 struct GeoConvertNormalize
424 template <class ExternalType, class StoredType>
425 static void convertIn(StoredType& dest, const ExternalType& src,
426 Real64 scale = 1, Real64 offset = 0)
428 if(ExternalType::_uiSize >= StoredType::_uiSize)
430 UInt32 i;
431 for(i = 0; i < StoredType::_uiSize; ++i)
432 dest[i] = static_cast<typename StoredType::ValueType>(
433 src[i] * scale + offset);
435 else
437 UInt32 i;
438 for(i = 0; i < ExternalType::_uiSize; ++i)
439 dest[i] = static_cast<typename StoredType::ValueType>(
440 src[i] * scale + offset);
441 for(; i < StoredType::_uiSize; ++i)
442 dest[i] = static_cast<typename StoredType::ValueType>(
443 StoredType::Null[i] * scale + offset);
447 template <class ExternalType, class StoredType>
448 static void convertOut(ExternalType& dest, const StoredType& src,
449 Real64 scale = 1, Real64 offset = 0)
451 if(StoredType::_uiSize >= ExternalType::_uiSize)
453 UInt32 i;
454 for(i = 0; i < StoredType::_uiSize; ++i)
455 dest[i] = static_cast<typename ExternalType::ValueType>(
456 (src[i] - offset) / scale);
458 else
460 UInt32 i;
461 for(i = 0; i < StoredType::_uiSize; ++i)
462 dest[i] = static_cast<typename ExternalType::ValueType>(
463 (src[i] - offset) / scale);
464 for(; i < ExternalType::_uiSize; ++i)
465 dest[i] = static_cast<typename ExternalType::ValueType>(
466 (ExternalType::Null[i] - offset) / scale);
471 #endif
473 OSG_END_NAMESPACE
475 #endif /* _OSGGEOVECTORPROPERTYCONVERSION_H_ */