PxCoreUtilityTypes.h

Go to the documentation of this file.
00001 //
00002 // Redistribution and use in source and binary forms, with or without
00003 // modification, are permitted provided that the following conditions
00004 // are met:
00005 //  * Redistributions of source code must retain the above copyright
00006 //    notice, this list of conditions and the following disclaimer.
00007 //  * Redistributions in binary form must reproduce the above copyright
00008 //    notice, this list of conditions and the following disclaimer in the
00009 //    documentation and/or other materials provided with the distribution.
00010 //  * Neither the name of NVIDIA CORPORATION nor the names of its
00011 //    contributors may be used to endorse or promote products derived
00012 //    from this software without specific prior written permission.
00013 //
00014 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
00015 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00016 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00017 // PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00018 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00019 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00020 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00021 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00022 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00024 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025 //
00026 // Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
00027 // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
00028 // Copyright (c) 2001-2004 NovodeX AG. All rights reserved.  
00029 
00030 
00031 #ifndef PX_CORE_UTILTY_TYPES_H
00032 #define PX_CORE_UTILTY_TYPES_H
00033 
00037 #include "foundation/PxAssert.h"
00038 #include "foundation/PxMemory.h"
00039 
00040 #if !PX_DOXYGEN
00041 namespace physx
00042 {
00043 #endif
00044 
00045 
00046 struct PxStridedData
00047 {
00053     PxU32 stride;
00054     const void* data;
00055 
00056     PxStridedData() : stride( 0 ), data( NULL ) {}
00057 
00058     template<typename TDataType>
00059     PX_INLINE const TDataType& at( PxU32 idx ) const
00060     {
00061         PxU32 theStride( stride );
00062         if ( theStride == 0 )
00063             theStride = sizeof( TDataType );
00064         PxU32 offset( theStride * idx );
00065         return *(reinterpret_cast<const TDataType*>( reinterpret_cast< const PxU8* >( data ) + offset ));
00066     }
00067 };
00068 
00069 template<typename TDataType>
00070 struct PxTypedStridedData
00071 {
00072     PxU32 stride;
00073     const TDataType* data;
00074 
00075     PxTypedStridedData()
00076         : stride( 0 )
00077         , data( NULL )
00078     {
00079     }
00080 
00081 };
00082 
00083 struct PxBoundedData : public PxStridedData
00084 {
00085     PxU32 count;
00086     PxBoundedData() : count( 0 ) {}
00087 };
00088 
00089 template<PxU8 TNumBytes>
00090 struct PxPadding
00091 {
00092     PxU8 mPadding[TNumBytes];
00093     PxPadding()
00094     {
00095         for ( PxU8 idx =0; idx < TNumBytes; ++idx )
00096             mPadding[idx] = 0;
00097     }
00098 };
00099 
00100 template <PxU32 NB_ELEMENTS> class PxFixedSizeLookupTable
00101 {
00102 //= ATTENTION! =====================================================================================
00103 // Changing the data layout of this class breaks the binary serialization format.  See comments for 
00104 // PX_BINARY_SERIAL_VERSION.  If a modification is required, please adjust the getBinaryMetaData 
00105 // function.  If the modification is made on a custom branch, please change PX_BINARY_SERIAL_VERSION
00106 // accordingly.
00107 //==================================================================================================
00108 public:
00109     
00110     PxFixedSizeLookupTable() 
00111         : mNbDataPairs(0)
00112     {
00113     }
00114 
00115     PxFixedSizeLookupTable(const PxEMPTY) {}
00116 
00117     PxFixedSizeLookupTable(const PxReal* dataPairs, const PxU32 numDataPairs)
00118     {
00119         PxMemCopy(mDataPairs,dataPairs,sizeof(PxReal)*2*numDataPairs);
00120         mNbDataPairs=numDataPairs;
00121     }
00122 
00123     PxFixedSizeLookupTable(const PxFixedSizeLookupTable& src)
00124     {
00125         PxMemCopy(mDataPairs,src.mDataPairs,sizeof(PxReal)*2*src.mNbDataPairs);
00126         mNbDataPairs=src.mNbDataPairs;
00127     }
00128 
00129     ~PxFixedSizeLookupTable()
00130     {
00131     }
00132 
00133     PxFixedSizeLookupTable& operator=(const PxFixedSizeLookupTable& src)
00134     {
00135         PxMemCopy(mDataPairs,src.mDataPairs,sizeof(PxReal)*2*src.mNbDataPairs);
00136         mNbDataPairs=src.mNbDataPairs;
00137         return *this;
00138     }
00139 
00140     PX_FORCE_INLINE void addPair(const PxReal x, const PxReal y)
00141     {
00142         PX_ASSERT(mNbDataPairs<NB_ELEMENTS);
00143         mDataPairs[2*mNbDataPairs+0]=x;
00144         mDataPairs[2*mNbDataPairs+1]=y;
00145         mNbDataPairs++;
00146     }
00147 
00148     PX_FORCE_INLINE PxReal getYVal(const PxReal x) const
00149     {
00150         if(0==mNbDataPairs)
00151         {
00152             PX_ASSERT(false);
00153             return 0;
00154         }
00155 
00156         if(1==mNbDataPairs || x<getX(0))
00157         {
00158             return getY(0);
00159         }
00160 
00161         PxReal x0=getX(0);
00162         PxReal y0=getY(0);
00163 
00164         for(PxU32 i=1;i<mNbDataPairs;i++)
00165         {
00166             const PxReal x1=getX(i);
00167             const PxReal y1=getY(i);
00168 
00169             if((x>=x0)&&(x<x1))
00170             {
00171                 return (y0+(y1-y0)*(x-x0)/(x1-x0));
00172             }
00173 
00174             x0=x1;
00175             y0=y1;
00176         }
00177 
00178         PX_ASSERT(x>=getX(mNbDataPairs-1));
00179         return getY(mNbDataPairs-1);
00180     }
00181 
00182     PxU32 getNbDataPairs() const {return mNbDataPairs;}
00183     
00184     void clear()
00185     {
00186         memset(mDataPairs, 0, NB_ELEMENTS*2*sizeof(PxReal));
00187         mNbDataPairs = 0;
00188     }
00189 
00190     PX_FORCE_INLINE PxReal getX(const PxU32 i) const
00191     {
00192         return mDataPairs[2*i];
00193     }
00194     PX_FORCE_INLINE PxReal getY(const PxU32 i) const
00195     {
00196         return mDataPairs[2*i+1];
00197     }
00198 
00199     PxReal mDataPairs[2*NB_ELEMENTS];
00200     PxU32 mNbDataPairs;
00201     PxU32 mPad[3];
00202 
00203     
00204 };
00205 
00206 #if !PX_DOXYGEN
00207 } // namespace physx
00208 #endif
00209 
00211 #endif


Copyright © 2008-2018 NVIDIA Corporation, 2701 San Tomas Expressway, Santa Clara, CA 95050 U.S.A. All rights reserved. www.nvidia.com