1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
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. *
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. *
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. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 #ifndef _OSGGEOVECTORPROPERTYCONVERSION_H_
40 #define _OSGGEOVECTORPROPERTYCONVERSION_H_
45 #include "OSGConfig.h"
46 #include "OSGVector.h"
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
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
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.
151 struct VectorElementAccessFunc
<Color3fx
>
152 : public Fixed32VectorElementAccessFunc
<Color3fx
>
156 /*! \brief Specialization for Color4fx, implemented via inheritance.
159 struct VectorElementAccessFunc
<Color4fx
>
160 : public Fixed32VectorElementAccessFunc
<Color4fx
>
164 /*! \brief Main conversion function.
166 template <class DestTypeT
,
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
;
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
189 template <class ValueTypeT
,
190 template <class> class ElementAccessFuncT
>
191 struct GeoConvertFunc
<ValueTypeT
,
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
;
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
,
214 template <class> class NormalizeFuncT
>
215 struct GeoConvertImpl
: public GeoConvertFunc
<DestTypeT
,
218 VectorElementAccessFunc
>
222 } // namespace detail
226 // pull in implementation for inlining below
227 #include "OSGGeoVectorPropertyConversion.inl"
233 template <class StoredTypeT
, class ExternalTypeT
>
235 convertIn( StoredTypeT
& dest
, const ExternalTypeT
& src
,
236 const Real64 scale
= 1, const Real64 offset
= 0)
238 detail::GeoConvertImpl
<StoredTypeT
,
240 detail::NoNormalizationFunc
>::convert(
241 dest
, src
, scale
, offset
);
244 template <class ExternalTypeT
, class StoredTypeT
>
246 convertOut( ExternalTypeT
& dest
, const StoredTypeT
& src
,
247 const Real64 scale
= 1, const Real64 offset
= 0)
249 detail::GeoConvertImpl
<ExternalTypeT
,
251 detail::NoNormalizationFunc
>::convert(
252 dest
, src
, scale
, offset
);
256 struct GeoConvertNormalize
258 template <class StoredTypeT
, class ExternalTypeT
>
260 convertIn( StoredTypeT
& dest
, const ExternalTypeT
& src
,
261 const Real64 scale
= 1, const Real64 offset
= 0)
263 detail::GeoConvertImpl
<StoredTypeT
,
265 detail::InNormalizationFunc
>::convert(
266 dest
, src
, scale
, offset
);
269 template <class ExternalTypeT
, class StoredTypeT
>
271 convertOut( ExternalTypeT
& dest
, const StoredTypeT
& src
,
272 const Real64 scale
= 1, const Real64 offset
= 0)
274 detail::GeoConvertImpl
<ExternalTypeT
,
276 detail::OutNormalizationFunc
>::convert(
277 dest
, src
, scale
, offset
);
283 /* Helper classes for vector type conversions */
285 // Converter class for vector conversion
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
)
295 for(i
= 0; i
< StoredType::_uiSize
; ++i
)
296 dest
[i
] = static_cast<typename
StoredType::ValueType
>(src
[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
)
315 for(i
= 0; i
< ExternalType::_uiSize
; ++i
)
316 dest
[i
] = static_cast<typename
ExternalType::ValueType
>(src
[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
,
334 if(StoredType::_uiSize
>= ExternalType::_uiSize
)
337 for(i
= 0; i
< ExternalType::_uiSize
; ++i
)
340 static_cast<typename
ExternalType::ValueType
>(
347 for(i
= 0; i
< StoredType::_uiSize
; ++i
)
350 static_cast<typename
ExternalType::ValueType
>(
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
)
431 for(i
= 0; i
< StoredType::_uiSize
; ++i
)
432 dest
[i
] = static_cast<typename
StoredType::ValueType
>(
433 src
[i
] * scale
+ offset
);
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
)
454 for(i
= 0; i
< StoredType::_uiSize
; ++i
)
455 dest
[i
] = static_cast<typename
ExternalType::ValueType
>(
456 (src
[i
] - offset
) / scale
);
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
);
475 #endif /* _OSGGEOVECTORPROPERTYCONVERSION_H_ */