00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Andrew Clinton 00008 * Side Effects Software Inc 00009 * 477 Richmond Street West 00010 * Toronto, Ontario 00011 * Canada M5V 3E7 00012 * 416-504-9876 00013 * 00014 * NAME: UT_SobolSequence.h ( UT Library, C++) 00015 * 00016 * COMMENTS: A sobol low-discrepancy sequence. 00017 */ 00018 00019 #ifndef __UT_SobolSequence__ 00020 #define __UT_SobolSequence__ 00021 00022 #include "UT_API.h" 00023 #include <SYS/SYS_Types.h> 00024 #include "UT_Assert.h" 00025 #include "UT_Vector3.h" 00026 00027 // Definitions are provided in UT for types: 00028 // - uint 00029 // - uint64 00030 template <class itype> 00031 class UT_API UT_SobolSequence 00032 { 00033 public: 00034 /// Initialize a new sequence with the given number of dimensions and 00035 /// bits. 00036 /// 00037 /// dim = vector size for each sample (max 6) 00038 /// bits = number of bits in each random value (max 32) 00039 UT_SobolSequence(uint dim, uint bits=sizeof(itype)*8); 00040 virtual ~UT_SobolSequence(); 00041 00042 uint dimension() const { return myDim; } 00043 uint bits() const { return myBits; } 00044 00045 /// Advance to the next sample, storing the result into x. When myN 00046 /// exceeds the range of the sequence, it will automatically wrap. 00047 void advance(); 00048 00049 /// Return the sequence value for dimension dim. 00050 itype x(uint dim) 00051 { UT_ASSERT_P(dim < myDim); return myX[dim]; } 00052 fpreal32 xf(uint dim) 00053 { 00054 SYS_FPReal32Union fval; 00055 00056 if (myBits < 23) 00057 fval.uval = 0x3f800000 | (uint)(x(dim) << (23-myBits)); 00058 else 00059 fval.uval = 0x3f800000 | (uint)(x(dim) >> (myBits-23)); 00060 return fval.fval - 1; 00061 } 00062 00063 /// Reset the sequence 00064 void rewind(itype n = 0); 00065 00066 private: 00067 itype *myIV; 00068 itype *myX; 00069 uint myDim; 00070 uint myBits; 00071 itype myN; 00072 itype myMask; 00073 }; 00074 00075 class UT_API UT_VectorSequence 00076 { 00077 public: 00078 UT_VectorSequence() 00079 : mySobol(3) 00080 { 00081 myVec[0] = mySobol.xf(0); 00082 myVec[1] = mySobol.xf(1); 00083 myVec[2] = mySobol.xf(2); 00084 } 00085 virtual ~UT_VectorSequence() {} 00086 00087 void advance() { mySobol.advance(); } 00088 void rewind(uint n = 0) { mySobol.rewind(n); } 00089 const UT_Vector3 &getVector() 00090 { 00091 myVec[0] = mySobol.xf(0); 00092 myVec[1] = mySobol.xf(1); 00093 myVec[2] = mySobol.xf(2); 00094 return myVec; 00095 } 00096 00097 private: 00098 UT_SobolSequence<uint64> mySobol; 00099 UT_Vector3 myVec; 00100 }; 00101 00102 #endif
1.5.9