1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2003 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 _OSGBASEFIELDTRAITS_H_
40 #define _OSGBASEFIELDTRAITS_H_
45 #include "OSGSysFieldTraits.h"
46 #include "OSGDataType.h"
50 #include "OSGSphereVolume.h"
51 #include "OSGBoxVolume.h"
54 #include "OSGGLDefineMapper.h"
60 /*! \ingroup GrpBaseFieldTraits
61 \ingroup GrpLibOSGBase
65 struct FieldTraits
<Color3f
> : public FieldTraitsVec3TemplateBase
<Color3f
>
69 static DataType _type
;
73 typedef FieldTraits
<Color3f
> Self
;
75 enum { Convertible
= (Self::ToStreamConvertible
|
76 Self::FromStringConvertible
) };
78 static OSG_BASE_DLLMAPPING
79 DataType
&getType (void);
81 static const Char8
*getSName (void) { return "SFColor3f"; }
83 static const Char8
*getMName (void) { return "MFColor3f"; }
85 static const Color3f
getDefault (void) { return Color3f(); }
89 /*! \ingroup GrpBaseFieldTraits
90 \ingroup GrpLibOSGBase
94 struct FieldTraits
<Color3fx
> : public FieldTraitsVec3TemplateBase
<Color3fx
>
98 static DataType _type
;
102 typedef FieldTraits
<Color3fx
> Self
;
104 enum { Convertible
= (Self::ToStreamConvertible
|
105 Self::FromStringConvertible
) };
107 static OSG_BASE_DLLMAPPING
108 DataType
&getType (void);
110 static const Char8
*getSName (void) { return "SFColor3fx"; }
112 static const Char8
*getMName (void) { return "MFColor3fx"; }
114 static const Color3fx
getDefault (void) { return Color3fx(); }
118 /*! \ingroup GrpBaseFieldTraits
119 \ingroup GrpLibOSGBase
123 struct FieldTraits
<Color4f
> : public FieldTraitsVec4TemplateBase
<Color4f
>
127 static DataType _type
;
131 typedef FieldTraits
<Color4f
> Self
;
133 enum { Convertible
= (Self::ToStreamConvertible
|
134 Self::FromStringConvertible
) };
136 static OSG_BASE_DLLMAPPING
137 DataType
&getType (void);
139 static const Char8
*getSName (void) { return "SFColor4f"; }
141 static const Char8
*getMName (void) { return "MFColor4f"; }
143 static Color4f
getDefault (void){ return Color4f(); }
147 /*! \ingroup GrpBaseFieldTraits
148 \ingroup GrpLibOSGBase
152 struct FieldTraits
<Color4fx
> : public FieldTraitsVec4TemplateBase
<Color4fx
>
156 static DataType _type
;
160 typedef FieldTraits
<Color4fx
> Self
;
162 enum { Convertible
= (Self::ToStreamConvertible
|
163 Self::FromStringConvertible
) };
165 static OSG_BASE_DLLMAPPING
166 DataType
&getType (void);
168 static const Char8
*getSName (void) { return "SFColor4fx"; }
170 static const Char8
*getMName (void) { return "MFColor4fx"; }
172 static Color4fx
getDefault (void){ return Color4fx(); }
176 /*! \ingroup GrpBaseFieldTraits
177 \ingroup GrpLibOSGBase
181 struct FieldTraits
<Color3ub
> : public FieldTraitsVec3TemplateBase
<Color3ub
>
185 static DataType _type
;
189 typedef FieldTraits
<Color3ub
> Self
;
191 enum { Convertible
= (Self::ToStreamConvertible
|
192 Self::FromStringConvertible
) };
194 static OSG_BASE_DLLMAPPING
195 DataType
&getType (void);
197 static const Char8
*getSName (void) { return "SFColor3ub"; }
198 static const Char8
*getMName (void) { return "MFColor3ub"; }
200 static Color3ub
getDefault (void) { return Color3ub(); }
204 /*! \ingroup GrpBaseFieldTraits
205 \ingroup GrpLibOSGBase
209 struct FieldTraits
<Color4ub
> : public FieldTraitsVec4TemplateBase
<Color4ub
>
213 static DataType _type
;
217 typedef FieldTraits
<Color4ub
> Self
;
219 enum { Convertible
= (Self::ToStreamConvertible
|
220 Self::FromStringConvertible
) };
222 static OSG_BASE_DLLMAPPING
223 DataType
&getType (void);
225 static const Char8
*getSName (void) { return "SFColor4ub"; }
227 static const Char8
*getMName (void) { return "MFColor4ub"; }
229 static Color4ub
getDefault (void) { return Color4ub(); }
233 /*! \ingroup GrpBaseFieldTraits
234 \ingroup GrpLibOSGBase
238 struct FieldTraits
<std::string
> : public FieldTraitsTemplateBase
<std::string
>
242 static DataType _type
;
246 typedef FieldTraits
<std::string
> Self
;
248 enum { Convertible
= (Self::FromStringConvertible
|
249 Self::ToStreamConvertible
) };
251 static OSG_BASE_DLLMAPPING
252 DataType
&getType (void);
254 static const Char8
*getSName (void) { return "SFString"; }
256 static const Char8
*getMName (void) { return "MFString"; }
258 static std::string
getDefault (void) { return std::string(); }
260 static void putToStream(const std::string
&val
,
268 static bool getFromCString( std::string
&outVal
,
271 outVal
.assign(inVal
);
276 static SizeT
getBinSize (const std::string
&oObject
)
278 return oObject
.length() + 1 + sizeof(UInt32
);
281 static SizeT
getBinSize (const std::string
*pObjectStore
,
286 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
288 size
+= getBinSize(pObjectStore
[i
]);
295 static void copyToBin( BinaryDataHandler
&pMem
,
296 const std::string
&oObject
)
298 pMem
.putValue(oObject
);
301 static void copyToBin( BinaryDataHandler
&pMem
,
302 const std::string
*pObjectStore
,
305 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
307 copyToBin(pMem
, pObjectStore
[i
]);
311 static void copyFromBin( BinaryDataHandler
&pMem
,
312 std::string
&oObject
)
314 pMem
.getValue(oObject
);
317 static void copyFromBin( BinaryDataHandler
&pMem
,
318 std::string
*pObjectStore
,
321 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
323 copyFromBin(pMem
, pObjectStore
[i
]);
329 /*! \ingroup GrpBaseFieldTraits
330 \ingroup GrpLibOSGBase
334 struct FieldTraits
<Time
, 1> : public FieldTraitsPODTemplateBase
<Time
, 1>
338 static DataType _type
;
342 typedef FieldTraits
<Time
, 1> Self
;
344 enum { Convertible
= (Self::ToStreamConvertible
|
345 Self::FromStringConvertible
) };
347 static OSG_BASE_DLLMAPPING
348 DataType
&getType (void);
350 static const Char8
*getSName (void) { return "SFTime"; }
352 static const Char8
*getMName (void) { return "MFTime"; }
354 static Time
getDefault (void) { return Time(); }
358 /*! \ingroup GrpBaseFieldTraits
359 \ingroup GrpLibOSGBase
363 struct FieldTraits
<BoxVolume
> :
364 public FieldTraitsTemplateBase
<BoxVolume
>
368 static DataType _type
;
372 typedef FieldTraits
<BoxVolume
> Self
;
374 enum { Convertible
= (Self::FromStringConvertible
|
375 Self::ToStreamConvertible
) };
377 static OSG_BASE_DLLMAPPING
378 DataType
&getType (void);
380 static const Char8
*getSName (void) { return "SFBoxVolume"; }
382 static const Char8
*getMName (void) { return "MFBoxVolume"; }
384 static const BoxVolume
getDefault (void) { return BoxVolume(); }
387 static bool getFromCString( BoxVolume
&outVal
,
393 SizeT length
= strlen(inVal
);
398 std::cerr
<< "FieldDataTraits<BoxVolume>::getFromString(): "
399 << "Input too long" << std::endl
;
404 strncpy(str
, inVal
, length
);
420 Int16 count
= sscanf(str
, "%d %f %f %f %f %f %f",
431 outVal
.setState(iState
);
433 outVal
.setBounds(valStore
[0],
444 outVal
.setBounds(0.f
, 0.f
, 0.f
,
452 static void putToStream(const BoxVolume
&val
,
457 typedef TypeTraits
<Pnt3f::ValueType
> TypeTrait
;
458 typedef TypeTraits
<UInt16
> StateTypeTrait
;
460 StateTypeTrait::putToStream(val
.getState(), str
);
463 val
.getBounds(min
, max
);
465 TypeTrait::putToStream(min
[0], str
);
468 TypeTrait::putToStream(min
[1], str
);
471 TypeTrait::putToStream(min
[2], str
);
474 TypeTrait::putToStream(max
[0], str
);
477 TypeTrait::putToStream(max
[1], str
);
480 TypeTrait::putToStream(max
[2], str
);
483 static SizeT
getBinSize(const BoxVolume
&oObject
)
485 SizeT size
= sizeof(UInt16
);
487 size
+= sizeof(Pnt3f
) + sizeof(Pnt3f
);
493 static SizeT
getBinSize(const BoxVolume
*pObjectStore
,
498 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
500 size
+= getBinSize(pObjectStore
[i
]);
506 static void copyToBin( BinaryDataHandler
&pMem
,
507 const BoxVolume
&oObject
)
510 UInt16 state
= oObject
.getState();
512 pMem
.putValue(state
);
514 pMem
.putValues(&(oObject
.getMin()[0]), 3);
515 pMem
.putValues(&(oObject
.getMax()[0]), 3);
518 static void copyToBin( BinaryDataHandler
&pMem
,
519 const BoxVolume
*pObjectStore
,
522 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
524 copyToBin(pMem
, pObjectStore
[i
]);
528 static void copyFromBin(BinaryDataHandler
&pMem
,
534 pMem
.getValue (state
);
535 pMem
.getValues(&(min
[0]), 3);
536 pMem
.getValues(&(max
[0]), 3);
538 oObject
.setBounds(min
, max
);
539 oObject
.setState (state
);
542 static void copyFromBin(BinaryDataHandler
&pMem
,
543 BoxVolume
*pObjectStore
,
546 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
548 copyFromBin(pMem
, pObjectStore
[i
]);
554 /*! \ingroup GrpBaseFieldTraits
555 \ingroup GrpLibOSGBase
559 struct FieldTraits
<BitVector
, 1> :
560 public FieldTraitsPODTemplateBase
<BitVector
, 1>
564 static DataType _type
;
568 typedef FieldTraits
<BitVector
, 1> Self
;
570 enum { Convertible
= Self::NotConvertible
};
572 static OSG_BASE_DLLMAPPING
573 DataType
&getType (void);
575 static const Char8
*getSName(void) { return "SFBitVector"; }
577 static const Char8
*getMName(void) { return "MFBitVector"; }
579 static BitVector
getDefault(void) { return BitVector(); }
583 /*! \ingroup GrpBaseFieldTraits
584 \ingroup GrpLibOSGBase
588 struct FieldTraits
<GLenum
, 1> : public FieldTraitsPODTemplateBase
<GLenum
, 1>
592 static DataType _type
;
596 typedef FieldTraits
<GLenum
, 1> Self
;
598 enum { Convertible
= (Self::ToStreamConvertible
|
599 Self::FromStringConvertible
) };
601 static OSG_BASE_DLLMAPPING
602 DataType
&getType (void);
604 static const Char8
*getSName (void) { return "SFGLenum"; }
605 static const Char8
*getMName (void) { return "MFGLenum"; }
607 static GLenum
getDefault (void) { return 0; }
609 static void putToStream(const GLenum
&val
,
612 #ifdef OSG_GL_DEFMAPPER
613 const std::string
&oVal
= GLDefineMapper::the()->toString(val
);
615 str
<< "GL_" << oVal
;
617 str
<< std::setbase(16);
620 TypeTraits
<GLenum
>::putToStream(val
, str
);
622 str
<< std::setbase(10);
626 #ifdef OSG_GL_DEFMAPPER
627 static bool getFromCString( GLenum
&outVal
,
628 const Char8
*&inVal
)
630 outVal
= GLDefineMapper::the()->fromString(inVal
);
638 /*! \ingroup GrpBaseFieldTraits
639 \ingroup GrpLibOSGBase
643 struct FieldTraits
<Plane
> : public FieldTraitsTemplateBase
<Plane
>
647 static DataType _type
;
651 typedef FieldTraits
<Plane
> Self
;
653 enum { Convertible
= Self::NotConvertible
};
655 static OSG_BASE_DLLMAPPING
656 DataType
&getType (void);
658 static const Char8
*getSName (void) { return "SFPlane"; }
660 static const Char8
*getMName (void) { return "MFPlane"; }
662 static Plane
getDefault (void) { return Plane(); }
664 static SizeT
getBinSize (const Plane
&)
666 return sizeof(Real32
) * 4;
669 static SizeT
getBinSize (const Plane
*,
672 return sizeof(Real32
) * 4 * uiNumObjects
;
675 static void copyToBin ( BinaryDataHandler
&pMem
,
676 const Plane
&oObject
)
678 const Vec3f
&normal
= oObject
.getNormal ();
679 Real32 distance
= oObject
.getDistanceFromOrigin();
681 pMem
.putValues(&normal
[0], 3);
682 pMem
.putValue ( distance
);
685 static void copyToBin( BinaryDataHandler
&pMem
,
686 const Plane
*pObjectStore
,
689 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
691 copyToBin(pMem
, pObjectStore
[i
]);
695 static void copyFromBin( BinaryDataHandler
&pMem
,
701 pMem
.getValues(&normal
[0], 3);
702 pMem
.getValue ( distance
);
704 oObject
.set(normal
, distance
);
707 static void copyFromBin(BinaryDataHandler
&pMem
,
711 for(SizeT i
= 0; i
< uiNumObjects
; ++i
)
713 copyFromBin(pMem
, pObjectStore
[i
]);
719 /*! \ingroup GrpBaseFieldTraits
720 \ingroup GrpLibOSGBase
724 struct FieldTraits
<RenderPropBitVector
, 3> :
725 public FieldTraitsPODTemplateBase
<RenderPropBitVector
, 3>
729 static DataType _type
;
733 typedef FieldTraits
<RenderPropBitVector
, 3> Self
;
735 enum { Convertible
= (Self::ToStreamConvertible
|
736 Self::FromStringConvertible
) };
738 static OSG_BASE_DLLMAPPING
739 DataType
&getType (void);
741 static const Char8
*getSName (void) { return "SFRenderPropBitVector"; }
742 static const Char8
*getMName (void) { return "MFRenderPropBitVector"; }
744 static RenderPropBitVector
getDefault(void) { return 0; }
746 static void putToStream(const RenderPropBitVector
&val
,
749 FieldTraits
<RenderPropBitVector
, 0>::putToStream(val
, str
);
752 static bool getFromCString( RenderPropBitVector
&outVal
,
753 const Char8
*&inVal
)
755 return FieldTraits
<RenderPropBitVector
, 0>::getFromCString(outVal
,
762 #endif /* _OSGBASEFIELDTRAITS_H_ */