29#ifndef SN_XML_VISITOR_READER_H
30#define SN_XML_VISITOR_READER_H
32#include "foundation/PxArray.h"
33#include "foundation/PxUtilities.h"
34#include "RepXMetaDataPropertyVisitor.h"
35#include "SnPxStreamOperators.h"
36#include "SnXmlMemoryPoolStreams.h"
37#include "SnXmlReader.h"
39#include "SnXmlMemoryAllocator.h"
40#include "SnXmlStringToType.h"
42namespace physx {
namespace Sn {
45 inline PxU32 findEnumByName(
const char* inName,
const PxU32ToName* inTable )
47 for ( PxU32 idx = 0; inTable[idx].mName != NULL; ++idx )
49 if ( physx::Pxstricmp( inTable[idx].mName, inName ) == 0 )
50 return inTable[idx].mValue;
55 PX_INLINE void stringToFlagsType(
const char* strData, XmlMemoryAllocator& alloc, PxU32& ioType,
const PxU32ToName* inTable )
57 if ( inTable == NULL )
60 if ( strData && *strData)
63 char* theValue =
const_cast<char*
>( copyStr( &alloc, strData ) );
64 char* theMarker = theValue;
65 char* theNext = theValue;
66 while( theNext && *theNext )
73 ioType |=
static_cast< PxU32
> ( findEnumByName( theMarker, inTable ) );
77 if ( theMarker && *theMarker )
78 ioType |=
static_cast< PxU32
> ( findEnumByName( theMarker, inTable ) );
79 alloc.deallocate(
reinterpret_cast<PxU8*
>( theValue ) );
83 template<
typename TDataType>
84 PX_INLINE void stringToEnumType(
const char* strData, TDataType& ioType,
const PxU32ToName* inTable )
86 ioType =
static_cast<TDataType
>( findEnumByName( strData, inTable ) );
89 template<
typename TDataType>
90 PX_INLINE bool readProperty( XmlReader& inReader,
const char* pname, TDataType& ioType )
93 if ( inReader.read( pname, value ) )
95 stringToType( value, ioType );
101 template<
typename TObjType>
102 inline TObjType* findReferencedObject( PxCollection& collection,
PxSerialObjectId id)
105 TObjType* outObject =
static_cast<TObjType*
>(
const_cast<PxBase*
>(collection.find(
id)));
106 if (outObject == NULL)
109 "PxSerialization::createCollectionFromXml: "
110 "Reference to ID %d cannot be resolved. Make sure externalRefs collection is specified if required and "
111 "check Xml file for completeness.",
117 template<
typename TObjType>
118 inline bool readReference( XmlReader& inReader, PxCollection& collection, TObjType*& outObject )
121 const char* theValue = inReader.getCurrentItemValue();
122 strto( theId, theValue );
131 outObject = findReferencedObject<TObjType>(collection, theId);
132 return outObject != NULL;
136 template<
typename TObjType>
137 inline bool readReference( XmlReader& inReader, PxCollection& inCollection,
const char* pname, TObjType*& outObject )
141 if (readProperty ( inReader, pname, theId ) && theId )
143 outObject = findReferencedObject<TObjType>(inCollection, theId);
146 return (outObject != NULL) || 0 == theId;
149 template<
typename TEnumType,
typename TStorageType>
150 inline bool readFlagsProperty( XmlReader& reader, XmlMemoryAllocator& allocator,
const char* pname,
const PxU32ToName* inConversions, PxFlags<TEnumType,TStorageType>& outFlags )
153 if ( reader.read( pname, value ) )
156 stringToFlagsType( value, allocator, tempValue, inConversions );
157 outFlags = PxFlags<TEnumType,TStorageType>(PxTo16(tempValue) );
163 template<
typename TObjType,
typename TReaderType,
typename TInfoType>
164 inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj, TInfoType& info);
165 template<
typename TObjType,
typename TReaderType>
166 inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj);
168 template<
typename TReaderType,
typename TGeomType>
169 inline PxGeometry* parseGeometry( TReaderType& reader, TGeomType& )
171 PxAllocatorCallback& inAllocator = reader.mAllocator.getAllocator();
173 TGeomType* shape = PX_PLACEMENT_NEW((inAllocator.allocate(
sizeof(TGeomType),
"parseGeometry", __FILE__, __LINE__ )), TGeomType);
174 PxClassInfoTraits<TGeomType> info;
175 readComplexObj( reader, shape);
180 template<
typename TReaderType>
181 inline void parseShape( TReaderType& visitor, PxGeometry*& outResult, PxArray<PxMaterial*>& outMaterials)
183 XmlReader& theReader( visitor.mReader );
184 PxCollection& collection = visitor.mCollection;
185 visitor.pushCurrentContext();
186 if ( visitor.gotoTopName() )
188 visitor.pushCurrentContext();
189 if ( visitor.gotoChild(
"Materials" ) )
191 for(
bool matSuccess = visitor.gotoFirstChild(); matSuccess;
192 matSuccess = visitor.gotoNextSibling() )
194 PxMaterial* material = NULL;
195 if(!readReference<PxMaterial>( theReader, collection, material ))
196 visitor.mHadError =
true;
198 outMaterials.pushBack( material );
201 visitor.popCurrentContext();
202 visitor.pushCurrentContext();
204 PxPlaneGeometry plane;
205 PxHeightFieldGeometry heightField;
206 PxSphereGeometry sphere;
207 PxTriangleMeshGeometry mesh;
208 PxConvexMeshGeometry convex;
210 PxCapsuleGeometry capsule;
211 if ( visitor.gotoChild(
"Geometry" ) )
213 if ( visitor.gotoFirstChild() )
215 const char* geomTypeName = visitor.getCurrentItemName();
217 if ( physx::Pxstricmp( geomTypeName,
"PxSphereGeometry" ) == 0 ) outResult = parseGeometry(visitor, sphere);
218 else if ( physx::Pxstricmp( geomTypeName,
"PxPlaneGeometry" ) == 0 ) outResult = parseGeometry(visitor, plane);
219 else if ( physx::Pxstricmp( geomTypeName,
"PxCapsuleGeometry" ) == 0 ) outResult = parseGeometry(visitor, capsule);
220 else if ( physx::Pxstricmp( geomTypeName,
"PxBoxGeometry" ) == 0 ) outResult = parseGeometry(visitor, box);
221 else if ( physx::Pxstricmp( geomTypeName,
"PxConvexMeshGeometry" ) == 0 ) outResult = parseGeometry(visitor, convex);
222 else if ( physx::Pxstricmp( geomTypeName,
"PxTriangleMeshGeometry" ) == 0 ) outResult = parseGeometry(visitor, mesh);
223 else if ( physx::Pxstricmp( geomTypeName,
"PxHeightFieldGeometry" ) == 0 ) outResult = parseGeometry(visitor, heightField);
228 visitor.popCurrentContext();
230 visitor.popCurrentContext();
235 template<
typename TReaderType,
typename TObjType>
236 inline void readShapesProperty( TReaderType& visitor, TObjType* inObj,
const PxRigidActorShapeCollection* inProp = NULL,
bool isSharedShape =
false )
238 PX_UNUSED(isSharedShape);
241 XmlReader& theReader( visitor.mReader );
242 PxCollection& collection( visitor.mCollection );
244 visitor.pushCurrentContext();
245 if ( visitor.gotoTopName() )
249 for (
bool success = visitor.gotoFirstChild(); success;
250 success = visitor.gotoNextSibling() )
252 if( 0 == physx::Pxstricmp( visitor.getCurrentItemName(),
"PxShapeRef" ) )
254 PxShape* shape = NULL;
255 if(!readReference<PxShape>( theReader, collection, shape ))
256 visitor.mHadError =
true;
258 inObj->attachShape( *shape );
262 PxArray<PxMaterial*> materials;
263 PxGeometry* geometry = NULL;
264 parseShape( visitor, geometry, materials);
265 PxShape* theShape = NULL;
266 if ( materials.size() )
268 theShape = visitor.mArgs.physics.createShape( *geometry, materials.begin(), PxTo16(materials.size()),
true );
271 readComplexObj( visitor, theShape );
275 inObj->attachShape(*theShape);
276 collection.add( *theShape );
281 switch(geometry->getType())
283 case PxGeometryType::eSPHERE :
284 static_cast<PxSphereGeometry*
>(geometry)->~PxSphereGeometry();
286 case PxGeometryType::ePLANE :
287 static_cast<PxPlaneGeometry*
>(geometry)->~PxPlaneGeometry();
289 case PxGeometryType::eCAPSULE :
290 static_cast<PxCapsuleGeometry*
>(geometry)->~PxCapsuleGeometry();
292 case PxGeometryType::eBOX :
293 static_cast<PxBoxGeometry*
>(geometry)->~PxBoxGeometry();
295 case PxGeometryType::eCONVEXMESH :
296 static_cast<PxConvexMeshGeometry*
>(geometry)->~PxConvexMeshGeometry();
298 case PxGeometryType::eTRIANGLEMESH :
299 static_cast<PxTriangleMeshGeometry*
>(geometry)->~PxTriangleMeshGeometry();
301 case PxGeometryType::eHEIGHTFIELD :
302 static_cast<PxHeightFieldGeometry*
>(geometry)->~PxHeightFieldGeometry();
304 case PxGeometryType::eTETRAHEDRONMESH :
305 static_cast<PxTetrahedronMeshGeometry*
>(geometry)->~PxTetrahedronMeshGeometry();
307 case PxGeometryType::ePARTICLESYSTEM:
308 static_cast<PxParticleSystemGeometry*
>(geometry)->~PxParticleSystemGeometry();
310 case PxGeometryType::eHAIRSYSTEM:
311 static_cast<PxHairSystemGeometry*
>(geometry)->~PxHairSystemGeometry();
313 case PxGeometryType::eCUSTOM :
314 static_cast<PxCustomGeometry*
>(geometry)->~PxCustomGeometry();
321 visitor.mAllocator.getAllocator().deallocate(geometry);
325 visitor.popCurrentContext();
336 template<
typename TObjType>
356 , mContexts( contexts )
360 , mAllocator( alloc )
361 , mCollection( collection )
363 , mHadError(hadError)
367 : mNames( other.mNames )
368 , mContexts( other.mContexts )
369 , mArgs( other.mArgs )
370 , mReader( other.mReader )
372 , mAllocator( other.mAllocator )
373 , mCollection( other.mCollection )
374 , mValid( other.mValid )
375 , mHadError( other.mHadError )
380 void pushName(
const char* name )
385 void pushBracketedName(
const char* name ) { pushName( name ); }
390 if ( mNames.
back().mOpen && mNames.
back().mValid )
395 if ( mNames.
size() && mNames.
back().mValid ==
false )
399 void pushCurrentContext()
401 mContexts.
pushBack(
static_cast<PxU32
>( mNames.
size() ) );
403 void popCurrentContext()
405 if ( mContexts.
size() )
407 PxU32 depth = mContexts.
back();
408 PX_ASSERT( mNames.
size() >= depth );
409 while( mNames.
size() > depth )
415 bool updateLastEntryAfterOpen()
417 mNames.
back().mValid = mValid;
418 mNames.
back().mOpen = mValid;
424 if ( mNames.
size() && mNames.
back().mOpen ==
false )
428 updateLastEntryAfterOpen();
433 bool isValid()
const {
return mValid; }
435 bool gotoChild(
const char* name )
438 return gotoTopName();
441 bool gotoFirstChild()
443 pushName(
"__child" );
445 return updateLastEntryAfterOpen();
448 bool gotoNextSibling()
450 bool retval = mValid;
455 const char* getCurrentItemName() {
if (mValid )
return mReader.
getCurrentItemName();
return ""; }
457 const char* topName()
const
459 if ( mNames.
size() )
return mNames.
back().mName;
461 return "bad__repx__name";
464 const char* getCurrentValue()
466 const char* value = NULL;
467 if ( isValid() && mReader.
read( topName(), value ) )
472 template<
typename TDataType>
473 bool readProperty(TDataType& outType)
475 const char* value = getCurrentValue();
476 if ( value && *value )
478 stringToType( value, outType );
484 template<
typename TDataType>
485 bool readExtendedIndexProperty(TDataType& outType)
488 if ( value && *value )
490 stringToType( value, outType );
497 template<
typename TRefType>
498 bool readReference(TRefType*& outRef)
500 return physx::Sn::readReference<TRefType>( mReader, mCollection, topName(), outRef );
503 inline bool readProperty(
const char*& outProp )
506 const char* value = getCurrentValue();
507 if ( value && *value && mArgs.stringTable )
517 return readReference<PxConvexMesh>( outProp );
522 return readReference<PxTriangleMesh>( outProp );
526 return readReference<PxBVH33TriangleMesh>( outProp );
530 return readReference<PxBVH34TriangleMesh>( outProp );
535 return readReference<PxHeightField>( outProp );
540 return readReference<PxRigidActor>( outProp );
543 template<
typename TAccessorType>
544 void simpleProperty( PxU32 , TAccessorType& inProp )
546 typedef typename TAccessorType::prop_type TPropertyType;
548 if ( readProperty( value ) )
549 inProp.set( mObj, value );
552 template<
typename TAccessorType>
553 void enumProperty( PxU32 , TAccessorType& inProp,
const PxU32ToName* inConversions )
555 typedef typename TAccessorType::prop_type TPropertyType;
556 const char* strVal = getCurrentValue();
557 if ( strVal && *strVal )
560 stringToEnumType( strVal, pval, inConversions );
561 inProp.set( mObj, pval );
565 template<
typename TAccessorType>
566 void flagsProperty( PxU32 ,
const TAccessorType& inProp,
const PxU32ToName* inConversions )
568 typedef typename TAccessorType::prop_type TPropertyType;
569 typedef typename TPropertyType::InternalType TInternalType;
571 const char* strVal = getCurrentValue();
572 if ( strVal && *strVal )
575 stringToFlagsType( strVal, mAllocator, tempValue, inConversions );
576 inProp.set( mObj, TPropertyType(TInternalType( tempValue )));
580 template<
typename TAccessorType,
typename TInfoType>
581 void complexProperty( PxU32* ,
const TAccessorType& inProp, TInfoType& inInfo )
583 typedef typename TAccessorType::prop_type TPropertyType;
586 TPropertyType propVal = inProp.get( mObj );
587 readComplexObj( *
this, &propVal, inInfo );
588 inProp.set( mObj, propVal );
592 template<
typename TAccessorType,
typename TInfoType>
593 void bufferCollectionProperty( PxU32* ,
const TAccessorType& inProp, TInfoType& inInfo )
595 typedef typename TAccessorType::prop_type TPropertyType;
598 this->pushCurrentContext();
599 if ( this->gotoTopName() )
601 for (
bool success = this->gotoFirstChild(); success;
602 success = this->gotoNextSibling() )
604 TPropertyType propVal;
605 readComplexObj( *
this, &propVal, inInfo );
609 this->popCurrentContext();
611 inProp.set( mObj, theData.
begin(), theData.
size() );
615 template<
typename TAccessorType,
typename TInfoType>
616 void extendedIndexedProperty( PxU32* ,
const TAccessorType& inProp, TInfoType& inInfo )
618 typedef typename TAccessorType::prop_type TPropertyType;
620 this->pushCurrentContext();
621 if ( this->gotoTopName() )
624 for (
bool success = this->gotoFirstChild(); success;
625 success = this->gotoNextSibling() )
627 TPropertyType propVal;
628 readComplexObj( *
this, &propVal, inInfo );
629 inProp.set(mObj, index, propVal);
633 this->popCurrentContext();
636 template<
typename TAccessorType,
typename TInfoType>
637 void PxFixedSizeLookupTableProperty( PxU32* ,
const TAccessorType& inProp, TInfoType& inInfo )
639 typedef typename TAccessorType::prop_type TPropertyType;
640 const_cast<TAccessorType&
>(inProp).clear( mObj );
642 this->pushCurrentContext();
643 if ( this->gotoTopName() )
645 for (
bool success = this->gotoFirstChild(); success;
646 success = this->gotoNextSibling() )
648 TPropertyType propXVal;
649 readComplexObj( *
this, &propXVal, inInfo );
651 if(this->gotoNextSibling())
653 TPropertyType propYVal;
654 readComplexObj( *
this, &propYVal, inInfo );
655 const_cast<TAccessorType&
>(inProp).addPair(mObj, propXVal, propYVal);
659 this->popCurrentContext();
664 physx::Sn::readShapesProperty( *
this, mObj, &inProp );
670 bool isReducedCoordinateLink = (link != NULL);
671 if (!isReducedCoordinateLink)
674 simpleProperty(PxPropertyInfoName::PxRigidActor_GlobalPose, theAccessor);
679 template<
typename TObjType>
715 if (parentReader->
read(
"RigidBodyFlags", value ))
717 if(strstr(value,
"eKINEMATIC"))
723 physx::Sn::readShapesProperty( *
this, mObj, &inProp );
724 parentReader->release();
728 template<
typename TAccessorType>
729 void simpleProperty( PxU32 , TAccessorType& inProp )
731 typedef typename TAccessorType::prop_type TPropertyType;
733 if (readProperty(value))
737 if(kinematic && (inProp.mProperty.mKey == PxPropertyInfoName::PxRigidDynamic_LinearVelocity || inProp.mProperty.mKey == PxPropertyInfoName::PxRigidDynamic_AngularVelocity))
740 inProp.set(mObj, value );
788 readComplexObj(*
this, theJoint);
790 mCollection.
add(*theJoint);
800 template<
typename ArticulationType>
807 inSerializer.pushCurrentContext();
808 if( inSerializer.gotoTopName() )
810 for (
bool links = inSerializer.gotoFirstChild();
812 links = inSerializer.gotoNextSibling() )
817 if ( inSerializer.mReader.
read(
"Parent", theParentPtr ) )
821 PX_ASSERT( theRemappedParent );
822 theParentLink = theRemappedParent->second;
826 inSerializer.mReader.
read(
"Id", theIdPtr );
828 linkRemapMap.insert( theIdPtr, newLink );
829 readComplexObj( inSerializer, newLink );
832 collection.add( *newLink, theIdPtr );
835 inSerializer.popCurrentContext();
851 physx::Sn::readProperty(*
this, mObj, inProp);
855 template<
typename TObjType,
typename TInfoType>
858 bool hadError =
false;
861 info.visitBaseProperties( theOp );
862 info.visitInstanceProperties( theOp );
867 template<
typename TObjType>
874 return readAllProperties( args, names, contexts, reader, obj, alloc, collection, info.Info );
877 template<
typename TObjType,
typename TReaderType,
typename TInfoType>
878 inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj, TInfoType& info)
880 if(!readAllProperties( oldVisitor.mArgs, oldVisitor.mNames, oldVisitor.mContexts, oldVisitor.mReader, inObj, oldVisitor.mAllocator, oldVisitor.mCollection, info ))
881 oldVisitor.mHadError =
true;
884 template<
typename TObjType,
typename TReaderType,
typename TInfoType>
885 inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj,
const TInfoType& info)
887 if(!readAllProperties( oldVisitor.mArgs, oldVisitor.mNames, oldVisitor.mContexts, oldVisitor.mReader, inObj, oldVisitor.mAllocator, oldVisitor.mCollection, info ))
888 oldVisitor.mHadError =
true;
891 template<
typename TObjType,
typename TReaderType>
892 inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj,
const PxUnknownClassInfo& )
894 const char* value = oldVisitor.mReader.getCurrentItemValue();
895 if ( value && *value )
897 stringToType( value, *inObj );
900 oldVisitor.mHadError =
true;
903 template<
typename TObjType,
typename TReaderType>
904 inline void readComplexObj( TReaderType& oldVisitor, TObjType* inObj)
906 PxClassInfoTraits<TObjType> info;
907 if(!readAllProperties( oldVisitor.mArgs, oldVisitor.mNames, oldVisitor.mContexts, oldVisitor.mReader, inObj, oldVisitor.mAllocator, oldVisitor.mCollection, info.Info ))
908 oldVisitor.mHadError =
true;
PX_FORCE_INLINE uint32_t size() const
Definition PxArray.h:242
PX_FORCE_INLINE ConstIterator begin() const
Definition PxArray.h:176
PX_FORCE_INLINE T & pushBack(const T &a)
Definition PxArray.h:296
PX_INLINE T popBack()
Definition PxArray.h:311
PX_FORCE_INLINE const T & back() const
Definition PxArray.h:224
A joint between two links in an articulation.
Definition PxArticulationJointReducedCoordinate.h:49
A component of an articulation that represents a rigid body.
Definition PxArticulationLink.h:54
A tree structure of bodies connected by joints that is treated as a unit by the dynamics solver....
Definition PxArticulationReducedCoordinate.h:448
A triangle mesh containing the PxMeshMidPhase::eBVH33 structure.
Definition PxTriangleMesh.h:308
A triangle mesh containing the PxMeshMidPhase::eBVH34 structure.
Definition PxTriangleMesh.h:324
Collection class for serialization.
Definition PxCollection.h:104
virtual void add(PxBase &object, PxSerialObjectId id=PX_SERIAL_OBJECT_ID_INVALID)=0
Adds a PxBase object to the collection.
A convex mesh.
Definition PxConvexMesh.h:83
A height field class.
Definition PxHeightField.h:84
Definition PxInlineArray.h:43
Definition PxBasicTemplates.h:67
PxRigidActor represents a base class shared between dynamic and static rigid bodies in the physics SD...
Definition PxRigidActor.h:53
PxRigidDynamic represents a dynamic rigid simulation object in the physics SDK.
Definition PxRigidDynamic.h:82
Abstract class for collision shapes.
Definition PxShape.h:146
virtual const char * allocateStr(const char *inSrc)=0
Allocate a new string.
A triangle mesh, also called a 'polygon soup'.
Definition PxTriangleMesh.h:118
Definition SnXmlMemoryAllocator.h:37
Definition SnXmlReader.h:78
Definition SnXmlReader.h:42
virtual const char * getCurrentItemName()=0
virtual bool gotoNextSibling()=0
virtual bool read(const char *inName, const char *&outData)=0
virtual const char * getCurrentItemValue()=0
virtual bool leaveChild()=0
virtual XmlReader * getParentReader()=0
virtual bool gotoFirstChild()=0
virtual bool gotoChild(const char *inName)=0
PX_C_EXPORT PX_FOUNDATION_API physx::PxFoundation &PX_CALL_CONV PxGetFoundation()
Retrieves the Foundation SDK after it has been created.
Definition FdFoundation.cpp:279
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
PxU64 PxSerialObjectId
ID type for PxBase objects in a PxCollection.
Definition PxSerialFramework.h:61
Definition PxMetaDataObjects.h:575
Definition PxMetaDataObjects.h:478
@ eINVALID_PARAMETER
method called with invalid parameter(s)
Definition PxErrors.h:62
@ eGEOMETRY_COUNT
internal use only!
Definition PxGeometry.h:65
@ eINVALID
internal use only!
Definition PxGeometry.h:66
Definition PxMetaDataObjects.h:173
Definition PxMetaDataObjects.h:149
Arguments required to instantiate a serializable object from RepX.
Definition PxRepXSimpleType.h:83
Definition RepXMetaDataPropertyVisitor.h:37
Definition PxMetaDataObjects.h:559
@ eKINEMATIC
Enables kinematic mode for the actor.
Definition PxRigidBody.h:74
Definition PxMetaDataObjects.h:520
Definition PxMetaDataObjects.h:540
Definition PxMetaDataObjects.h:124
Definition RepXMetaDataPropertyVisitor.h:84
Definition SnXmlImpl.h:233
Definition SnXmlVisitorReader.h:329
Definition SnXmlVisitorReader.h:338
Definition SnXmlVisitorReader.h:681
Helper struct to encapsulate the user allocator callback Useful for array and hash templates.
Definition PxProfileAllocatorWrapper.h:46
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:152
Helper struct to encapsulate the array.
Definition PxProfileAllocatorWrapper.h:171