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 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
43 #include "OSGFieldContainer.h"
44 #include "OSGMaterialMapFields.h"
45 #include "OSGPrimeMaterial.h"
46 #include "OSGContainerPtrFuncs.h"
48 #include "OSGTypeBasePredicates.h"
49 #include "OSGReflexiveContainerTypePredicates.h"
50 #include "OSGMapHelper.h"
52 #include "OSGSField.ins"
57 OSG_FIELD_DLLEXPORT_DEF1(SField
, MaterialMap
)
60 const FieldType
&GetSFieldHandle
<SFMaterialPtrMap
>::getType(void) const
62 return SFMaterialPtrMap::getClassType();
65 const FieldType
&EditSFieldHandle
<SFMaterialPtrMap
>::getType(void) const
67 return SFMaterialPtrMap::getClassType();
70 SizeT FieldTraits
<MaterialMap
>::getBinSize(const MaterialMap
&aMap
)
72 // number of elements in map + binding and pointer id for each element
73 MaterialMap::const_iterator mapIt
= aMap
.begin();
74 MaterialMap::const_iterator mapEnd
= aMap
.end ();
78 for(;mapIt
!= mapEnd
; ++mapIt
)
80 std::vector
<std::string
> vKeyList
;
82 KeyPool::the()->keyToStringList(mapIt
->first
, vKeyList
);
84 std::vector
<std::string
>::const_iterator kIt
= vKeyList
.begin();
85 std::vector
<std::string
>::const_iterator kEnd
= vKeyList
.end ();
87 uiKeySize
+= sizeof(UInt32
);
89 for(; kIt
!= kEnd
; ++kIt
)
91 uiKeySize
+= FieldTraits
<std::string
>::getBinSize(*kIt
);
95 return sizeof(UInt32
) + aMap
.size() * sizeof(UInt32
) + uiKeySize
;
98 void FieldTraits
<MaterialMap
>::copyToBin( BinaryDataHandler
&pMem
,
99 const MaterialMap
&aMap
)
101 MaterialMap::const_iterator mapIt
= aMap
.begin();
102 MaterialMap::const_iterator mapEnd
= aMap
.end ();
104 UInt32 numPublicObjects
= UInt32(aMap
.size());
107 pMem
.putValue(numPublicObjects
);
109 for(; mapIt
!= mapEnd
; ++mapIt
)
111 std::vector
<std::string
> vKeyList
;
113 KeyPool::the()->keyToStringList(mapIt
->first
, vKeyList
);
115 std::vector
<std::string
>::const_iterator kIt
= vKeyList
.begin();
116 std::vector
<std::string
>::const_iterator kEnd
= vKeyList
.end ();
118 UInt32 uiNumKeys
= UInt32(vKeyList
.size());
120 pMem
.putValue(uiNumKeys
);
122 for(; kIt
!= kEnd
; ++kIt
)
124 FieldTraits
<std::string
>::copyToBin(pMem
, *kIt
);
127 fcId
= mapIt
->second
->getId();
133 void FieldTraits
<MaterialMap
>::copyFromBin(BinaryDataHandler
&pMem
,
136 PrimeMaterial
*matPtr
;
137 MaterialMapKey key
= KeyPool::the()->getDefault();
141 pMem
.getValue(uiNumObjects
);
143 MaterialMap::const_iterator mapIt
= aMap
.begin();
144 MaterialMap::const_iterator mapEnd
= aMap
.end ();
146 for(; mapIt
!= mapEnd
; ++mapIt
)
148 if((*mapIt
).second
!= NULL
)
150 Thread::getCurrentChangeList()->addDelayedSubRef
<
151 UnrecordedRefCountPolicy
>((*mapIt
).second
);
158 for(UInt32 i
= 0; i
< uiNumObjects
; ++i
)
162 pMem
.getValue(uiNumKeys
);
164 for(UInt32 j
= 0; j
< uiNumKeys
; ++j
)
166 std::string tmpString
;
168 FieldTraits
<std::string
>::copyFromBin(pMem
, tmpString
);
170 MaterialMapKey tmpKey
= KeyPool::the()->getAuto(tmpString
.c_str());
177 matPtr
= dynamic_cast<PrimeMaterial
*>(
178 FieldContainerFactory::the()->getMappedContainer(fcId
));
182 UnrecordedRefCountPolicy::addRef(matPtr
);
184 aMap
.insert(MaterialMap::value_type(key
, matPtr
));
189 void EditSFieldHandle
<SFMaterialPtrMap
>::add(
191 const std::string
&szBindings
)
193 PrimeMaterial
*pVal
= dynamic_cast<PrimeMaterial
*>(rhs
);
194 KeyType oKey
= 0x0000;
198 MapHelper
*pMHelper
= dynamic_cast<MapHelper
*>(rhs
);
202 pVal
= dynamic_cast<PrimeMaterial
*>(pMHelper
->getContainer());
204 const MapHelper::MFKeysType
*pKeys
= pMHelper
->getMFKeys();
206 if(pKeys
->empty() == false)
208 KeyType tmpKey
= KeyPool::the()->getDefault();
210 for(UInt32 i
= 0; i
< pKeys
->size(); ++i
)
212 const std::string
&szKey
= pMHelper
->getKeys(i
);
214 tmpKey
= KeyPool::the()->getAuto(szKey
.c_str());
223 if(szBindings
.empty() == false)
225 oKey
= KeyPool::the()->getAuto(szBindings
.c_str());
229 if(rhs
!= NULL
&& pVal
== NULL
)
233 // for whatever reason VS2003 does not like == NULL
236 _fAddMethod(pVal
, oKey
);
240 void EditSFieldHandle
<SFMaterialPtrMap
>::traverse(TraverseCallback oCallback
)
242 const SFMaterialPtrMap
*pMap
= static_cast<SFMaterialPtrMap
*>(_pField
);
244 if(oCallback
&& pMap
!= NULL
)
246 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
247 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
249 for(; mapIt
!= mapEnd
; ++mapIt
)
251 oCallback(mapIt
->second
);
256 void EditSFieldHandle
<SFMaterialPtrMap
>::flatten(MapList
&vList
)
260 const SFMaterialPtrMap
*pMap
= static_cast<SFMaterialPtrMap
*>(_pField
);
264 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
265 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
267 for(; mapIt
!= mapEnd
; ++mapIt
)
271 KeyPool::the()->keyToStringList(mapIt
->first
,
274 tmpEntry
.second
= mapIt
->second
;
276 vList
.push_back(tmpEntry
);
281 void EditSFieldHandle
<SFMaterialPtrMap
>::flatten(ContainerList
&vList
)
285 const SFMaterialPtrMap
*pMap
= static_cast<SFMaterialPtrMap
*>(_pField
);
289 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
290 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
292 for(; mapIt
!= mapEnd
; ++mapIt
)
294 vList
.push_back(mapIt
->second
);
299 bool EditSFieldHandle
<SFMaterialPtrMap
>::loadFromBin(
300 BinaryDataHandler
*pMem
,
301 UInt32 uiNumElements
,
303 std::vector
<UInt16
> &vBindings
,
304 std::vector
<UInt32
> &vIds
)
306 SFMaterialPtrMap
*pMap
= static_cast<SFMaterialPtrMap
*>(_pField
);
311 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
312 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
314 for(; mapIt
!= mapEnd
; ++mapIt
)
316 if((*mapIt
).second
!= NULL
)
318 Thread::getCurrentChangeList()->addDelayedSubRef
<
319 UnrecordedRefCountPolicy
>((*mapIt
).second
);
323 pMap
->getValue().clear();
330 PrimeMaterial
*pointerVal
;
334 for(UInt32 i
= 0; i
< uiNumElements
; ++i
)
336 MaterialMapKey key
= KeyPool::the()->getDefault();
339 pMem
->getValue(uiNumKeys
);
341 for(UInt32 j
= 0; j
< uiNumKeys
; ++j
)
343 std::string tmpString
;
345 FieldTraits
<std::string
>::copyFromBin(*pMem
, tmpString
);
347 MaterialMapKey tmpKey
= KeyPool::the()->getAuto(tmpString
.c_str());
352 pMem
->getValue(fcId
);
354 tmpEntry
.uiVal
= fcId
;
356 pMap
->getValue().insert(MaterialMap::value_type(key
,
357 tmpEntry
.pointerVal
));
362 MaterialMap::iterator mapWIt
= pMap
->getValue().begin();
363 mapEnd
= pMap
->getValue().end ();
365 for(; mapWIt
!= mapEnd
; ++mapWIt
, ++uiCount
)
367 tmpEntry
.pointerVal
= mapWIt
->second
;
369 vIds
.push_back(tmpEntry
.uiVal
);
370 vBindings
.push_back(uiCount
);
372 mapWIt
->second
= NULL
;
378 void EditSFieldHandle
<SFMaterialPtrMap
>::fillFrom(
379 const std::vector
<UInt16
> &vBindings
,
380 const std::vector
<UInt32
> &vIds
,
381 const std::map
<UInt32
, UInt32
> &mIdMap
)
383 SFMaterialPtrMap
*pMap
= static_cast<SFMaterialPtrMap
*>(_pField
);
388 MaterialMap::iterator mapIt
= pMap
->getValue().begin();
389 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
391 std::vector
<UInt32
>::const_iterator idIt
= vIds
.begin();
392 std::map
<UInt32
, UInt32
>::const_iterator imapIt
;
393 std::map
<UInt32
, UInt32
>::const_iterator imapEnd
= mIdMap
.end();
395 OSG_ASSERT(vIds
.size() == pMap
->getValue().size() &&
396 vBindings
.size() == pMap
->getValue().size() );
398 PrimeMaterial
*pMat
= NULL
;
400 for(; mapIt
!= mapEnd
; ++mapIt
, ++idIt
)
402 imapIt
= mIdMap
.find(*idIt
);
404 if(imapIt
!= imapEnd
)
406 pMat
= dynamic_cast<PrimeMaterial
*>(
407 FieldContainerFactory::the()->getContainer(imapIt
->second
));
409 UnrecordedRefCountPolicy::addRef(pMat
);
416 mapIt
->second
= pMat
;
421 void EditSFieldHandle
<SFMaterialPtrMap
>::cloneValues(
422 GetFieldHandlePtr pSrc
,
423 const TypePtrVector
&shareTypes
,
424 const TypePtrVector
&ignoreTypes
,
425 const TypeIdVector
&shareGroupIds
,
426 const TypeIdVector
&ignoreGroupIds
) const
428 SFMaterialPtrMap::GetHandlePtr pGetHandle
=
429 boost::dynamic_pointer_cast
<
430 SFMaterialPtrMap::GetHandle
>(pSrc
);
432 if(pGetHandle
== NULL
|| pGetHandle
->isValid() == false)
435 const SFMaterialPtrMap
&pAttMap
= **pGetHandle
;
437 MaterialMap::const_iterator mapIt
= pAttMap
.getValue().begin();
438 MaterialMap::const_iterator mapEnd
= pAttMap
.getValue().end();
440 for(; mapIt
!= mapEnd
; ++mapIt
)
442 PrimeMaterialUnrecPtr att
= mapIt
->second
;
443 MaterialMapKey key
= mapIt
->first
;
447 const FieldContainerType
&attType
= att
->getType();
449 // test if att type should NOT be ignored
450 if(!TypePredicates::typeInGroupIds (ignoreGroupIds
.begin(),
451 ignoreGroupIds
.end(),
453 !TypePredicates::typeDerivedFrom(ignoreTypes
.begin(),
457 // test if att should cloned
458 if(!TypePredicates::typeInGroupIds (shareGroupIds
.begin(),
461 !TypePredicates::typeDerivedFrom(shareTypes
.begin(),
465 att
= dynamic_pointer_cast
<PrimeMaterial
>(
466 OSG::deepClone(att
, shareTypes
, ignoreTypes
,
467 shareGroupIds
, ignoreGroupIds
));
474 _fAddMethod(att
, key
);
480 void GetSFieldHandle
<SFMaterialPtrMap
>::traverse(TraverseCallback oCallback
)
482 SFMaterialPtrMap
const *pMap
=
483 static_cast<SFMaterialPtrMap
const *>(_pField
);
485 if(oCallback
&& pMap
!= NULL
)
487 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
488 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
490 for(; mapIt
!= mapEnd
; ++mapIt
)
492 oCallback(mapIt
->second
);
497 void GetSFieldHandle
<SFMaterialPtrMap
>::flatten(MapList
&vList
)
501 SFMaterialPtrMap
const *pMap
=
502 static_cast<SFMaterialPtrMap
const *>(_pField
);
506 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
507 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
509 for(; mapIt
!= mapEnd
; ++mapIt
)
513 KeyPool::the()->keyToStringList(mapIt
->first
,
516 tmpEntry
.second
= mapIt
->second
;
518 vList
.push_back(tmpEntry
);
523 void GetSFieldHandle
<SFMaterialPtrMap
>::flatten(ContainerList
&vList
)
527 SFMaterialPtrMap
const *pMap
=
528 static_cast<SFMaterialPtrMap
const *>(_pField
);
532 MaterialMap::const_iterator mapIt
= pMap
->getValue().begin();
533 MaterialMap::const_iterator mapEnd
= pMap
->getValue().end ();
535 for(; mapIt
!= mapEnd
; ++mapIt
)
537 vList
.push_back(mapIt
->second
);
542 #if !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)
544 DataType FieldTraits
<MaterialMap
>::_type(
546 "FieldContainerPtrMap");
548 OSG_FIELDTRAITS_GETTYPE(MaterialMap
)
550 #endif // !defined(OSG_DO_DOC) || (OSG_DOC_LEVEL >= 3)