RavEngine
Loading...
Searching...
No Matches
SnXmlDeserializer.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef SN_XML_DESERIALIZER_H
30#define SN_XML_DESERIALIZER_H
31
32#include "SnXmlVisitorReader.h"
33
34namespace physx { namespace Sn {
35
36 //Definitions needed internally in the Serializer headers.
37 template<typename TTriIndexElem>
38 struct Triangle
39 {
40 TTriIndexElem mIdx0;
41 TTriIndexElem mIdx1;
42 TTriIndexElem mIdx2;
43 Triangle( TTriIndexElem inIdx0 = 0, TTriIndexElem inIdx1 = 0, TTriIndexElem inIdx2 = 0)
44 : mIdx0( inIdx0 )
45 , mIdx1( inIdx1 )
46 , mIdx2( inIdx2 )
47 {
48 }
49 };
50
52 {
53 XmlMemoryAllocator* mAllocator;
54 XmlMemoryAllocateMemoryPoolAllocator( XmlMemoryAllocator* inAlloc ) : mAllocator( inAlloc ) {}
55
56 PxU8* allocate( PxU32 inSize ) { return mAllocator->allocate( inSize ); }
57 void deallocate( PxU8* inMem ) { mAllocator->deallocate( inMem ); }
58 };
59
60 inline bool isEmpty(const char *s)
61 {
62 while (*s != '\0')
63 {
64 if (!isspace(*s))
65 return false;
66 s++;
67 }
68 return true;
69 }
70
71 inline void strtoLong( Triangle<PxU32>& ioDatatype,const char*& ioData )
72 {
73 strto( ioDatatype.mIdx0, ioData );
74 strto( ioDatatype.mIdx1, ioData );
75 strto( ioDatatype.mIdx2, ioData );
76 }
77
78 inline void strtoLong( PxHeightFieldSample& ioDatatype,const char*& ioData )
79 {
80 PxU32 tempData;
81 strto( tempData, ioData );
82 if ( isBigEndian() )
83 {
84 PxU32& theItem(tempData);
85 PxU32 theDest = 0;
86 PxU8* theReadPtr( reinterpret_cast< PxU8* >( &theItem ) );
87 PxU8* theWritePtr( reinterpret_cast< PxU8* >( &theDest ) );
88 //A height field sample is a 16 bit number
89 //followed by two bytes.
90
91 //We write this out as a 32 bit integer, LE.
92 //Thus, on a big endian, we need to move the bytes
93 //around a bit.
94 //LE - 1 2 3 4
95 //BE - 4 3 2 1 - after convert from xml number
96 //Correct BE - 2 1 3 4, just like LE but with the 16 number swapped
97 theWritePtr[0] = theReadPtr[2];
98 theWritePtr[1] = theReadPtr[3];
99 theWritePtr[2] = theReadPtr[1];
100 theWritePtr[3] = theReadPtr[0];
101 theItem = theDest;
102 }
103 ioDatatype = *reinterpret_cast<PxHeightFieldSample*>( &tempData );
104 }
105
106 template<typename TDataType>
107 inline void readStridedFlagsProperty( XmlReader& ioReader, const char* inPropName, TDataType*& outData, PxU32& outStride, PxU32& outCount, XmlMemoryAllocator& inAllocator,
108 const PxU32ToName* inConversions)
109 {
110 const char* theSrcData;
111 outStride = sizeof( TDataType );
112 outData = NULL;
113 outCount = 0;
114 if ( ioReader.read( inPropName, theSrcData ) )
115 {
116 XmlMemoryAllocateMemoryPoolAllocator tempAllocator( &inAllocator );
117 MemoryBufferBase<XmlMemoryAllocateMemoryPoolAllocator> tempBuffer( &tempAllocator );
118
119 if ( theSrcData )
120 {
121 static PxU32 theCount = 0;
122 ++theCount;
123 char* theStartData = const_cast< char*>( copyStr( &tempAllocator, theSrcData ) );
124 char* aData = strtok(theStartData, " \n");
125 while( aData )
126 {
127 TDataType tempValue;
128 stringToFlagsType( aData, inAllocator, tempValue, inConversions );
129 aData = strtok(NULL," \n");
130 tempBuffer.write( &tempValue, sizeof(TDataType) );
131 }
132 outData = reinterpret_cast< TDataType* >( tempBuffer.mBuffer );
133 outCount = tempBuffer.mWriteOffset / sizeof( TDataType );
134 tempAllocator.deallocate( reinterpret_cast<PxU8*>(theStartData) );
135 }
136 tempBuffer.releaseBuffer();
137 }
138 }
139
140 template<typename TDataType>
141 inline void readStridedBufferProperty( XmlReader& ioReader, const char* inPropName, TDataType*& outData, PxU32& outStride, PxU32& outCount, XmlMemoryAllocator& inAllocator)
142 {
143 const char* theSrcData;
144 outStride = sizeof( TDataType );
145 outData = NULL;
146 outCount = 0;
147 if ( ioReader.read( inPropName, theSrcData ) )
148 {
149 XmlMemoryAllocateMemoryPoolAllocator tempAllocator( &inAllocator );
150 MemoryBufferBase<XmlMemoryAllocateMemoryPoolAllocator> tempBuffer( &tempAllocator );
151
152 if ( theSrcData )
153 {
154 static PxU32 theCount = 0;
155 ++theCount;
156 char* theStartData = const_cast< char*>( copyStr( &tempAllocator, theSrcData ) );
157 const char* theData = theStartData;
158 while( !isEmpty(theData) )
159 {
160 //These buffers are whitespace delimited.
161 TDataType theType;
162 strtoLong( theType, theData );
163 tempBuffer.write( &theType, sizeof(theType) );
164 }
165 outData = reinterpret_cast< TDataType* >( tempBuffer.mBuffer );
166 outCount = tempBuffer.mWriteOffset / sizeof( TDataType );
167 tempAllocator.deallocate( reinterpret_cast<PxU8*>(theStartData) );
168 }
169 tempBuffer.releaseBuffer();
170 }
171 }
172
173 template<typename TDataType>
174 inline void readStridedBufferProperty( XmlReader& ioReader, const char* inPropName, PxStridedData& ioData, PxU32& outCount, XmlMemoryAllocator& inAllocator)
175 {
176 TDataType* tempData = NULL;
177 readStridedBufferProperty<TDataType>( ioReader, inPropName, tempData, ioData.stride, outCount, inAllocator );
178 ioData.data = tempData;
179 }
180
181 template<typename TDataType>
182 inline void readStridedBufferProperty( XmlReader& ioReader, const char* inPropName, PxTypedStridedData<TDataType>& ioData, PxU32& outCount, XmlMemoryAllocator& inAllocator)
183 {
184 TDataType* tempData = NULL;
185 readStridedBufferProperty<TDataType>( ioReader, inPropName, tempData, ioData.stride, outCount, inAllocator );
186 ioData.data = reinterpret_cast<PxMaterialTableIndex*>( tempData );
187 }
188
189 template<typename TDataType>
190 inline void readStridedBufferProperty( XmlReader& ioReader, const char* inPropName, PxBoundedData& ioData, XmlMemoryAllocator& inAllocator)
191 {
192 return readStridedBufferProperty<TDataType>( ioReader, inPropName, ioData, ioData.count, inAllocator );
193 }
194
195} }
196
197#endif
Definition SnXmlMemoryAllocator.h:37
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition SnippetImmediateMode.cpp:303
Definition SnXmlDeserializer.h:39
Definition SnXmlDeserializer.h:52