Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00015
00016
00017 #include "pcl/surface/3rdparty/opennurbs/opennurbs.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 #define N 624
00070 #define M 397
00071 #define MATRIX_A 0x9908b0dfUL
00072 #define UPPER_MASK 0x80000000UL
00073 #define LOWER_MASK 0x7fffffffUL
00074
00075
00076
00077
00078
00079
00080
00081
00082 void on_random_number_seed(ON__UINT32 s,ON_RANDOM_NUMBER_CONTEXT* randcontext)
00083 {
00084 ON__UINT32 i, u;
00085
00086 #if defined(ON_COMPILER_MSC)
00087 #pragma warning( push )
00088 #pragma warning( disable : 4127 ) // warning C4127: conditional expression is constant
00089 #endif
00090 if ( N*sizeof(randcontext->mt[0]) != sizeof(randcontext->mt) )
00091 {
00092 ON_ERROR("the mt[] array in struct ON_RANDOM_NUMBER_CONTEXT must have length N.");
00093 }
00094 #if defined(ON_COMPILER_MSC)
00095 #pragma warning( pop )
00096 #endif
00097
00098 randcontext->mt[0] = u = s & 0xffffffffUL;
00099 for (i=1; i<N; i++)
00100 {
00101 u = (1812433253UL * (u ^ (u >> 30)) + i);
00102
00103
00104
00105
00106
00107 u &= 0xffffffffUL;
00108
00109
00110 randcontext->mt[i] = u;
00111 }
00112
00113 randcontext->mti = N;
00114 }
00115
00116
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147 ON__UINT32 on_random_number(struct ON_RANDOM_NUMBER_CONTEXT* randcontext)
00148 {
00149 static const ON__UINT32 mag01[2]={0x0UL, MATRIX_A};
00150 ON__UINT32 kk, y;
00151
00152
00153 if (randcontext->mti >= N)
00154 {
00155
00156 if (randcontext->mti >= N+1)
00157 {
00158
00159 on_random_number_seed(5489UL,randcontext);
00160 }
00161
00162 for (kk=0;kk<N-M;kk++)
00163 {
00164 y = (randcontext->mt[kk]&UPPER_MASK)|(randcontext->mt[kk+1]&LOWER_MASK);
00165 randcontext->mt[kk] = randcontext->mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
00166 }
00167 for (;kk<N-1;kk++)
00168 {
00169 y = (randcontext->mt[kk]&UPPER_MASK)|(randcontext->mt[kk+1]&LOWER_MASK);
00170 randcontext->mt[kk] = randcontext->mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
00171 }
00172 y = (randcontext->mt[N-1]&UPPER_MASK)|(randcontext->mt[0]&LOWER_MASK);
00173 randcontext->mt[N-1] = randcontext->mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
00174
00175 randcontext->mti = 0;
00176 }
00177
00178 y = randcontext->mt[randcontext->mti++];
00179
00180
00181 y ^= (y >> 11);
00182 y ^= (y << 7) & 0x9d2c5680UL;
00183 y ^= (y << 15) & 0xefc60000UL;
00184 y ^= (y >> 18);
00185
00186 return y;
00187 }
00188
00189
00190
00191
00192
00193
00194 static struct ON_RANDOM_NUMBER_CONTEXT static_randcontext = {N+1,{0}};
00195
00196 void on_srand(ON__UINT32 s)
00197 {
00198
00199
00200 struct ON_RANDOM_NUMBER_CONTEXT randcontext;
00201 on_random_number_seed(s,&randcontext);
00202 memcpy(&static_randcontext,&randcontext,sizeof(static_randcontext));
00203 }
00204
00205
00206 ON__UINT32 on_rand(void)
00207 {
00208
00209
00210 return on_random_number(&static_randcontext);
00211 }
00212
00213
00214
00215
00216
00217
00219
00220
00221
00222
00223
00225
00226
00227
00228
00229
00230
00232
00233
00234
00235
00236
00237
00239
00240
00241
00242
00243
00244
00246
00247
00248
00249
00250
00252
00253
00254 ON_RandomNumberGenerator::ON_RandomNumberGenerator()
00255 {
00256 m_rand_context.mti = 0xFFFFFFFF;
00257 }
00258
00259 void ON_RandomNumberGenerator::Seed( ON__UINT32 s )
00260 {
00261 on_random_number_seed(s,&m_rand_context);
00262 }
00263
00264 ON__UINT32 ON_RandomNumberGenerator::RandomNumber()
00265 {
00266 return on_random_number(&m_rand_context);
00267 }
00268
00269 double ON_RandomNumberGenerator::RandomDouble()
00270 {
00271 return ((double)on_random_number(&m_rand_context))/4294967295.0;
00272 }
00273
00274 double ON_RandomNumberGenerator::RandomDouble(double t0, double t1)
00275 {
00276 const double s = ((double)on_random_number(&m_rand_context))/4294967295.0;
00277 return ((1.0-s)*t0 + s*t1);
00278 }
00279
00280 static void Swap1(size_t count, unsigned char* a, unsigned char* b)
00281 {
00282 unsigned char t;
00283 while (count--)
00284 {
00285 t = *a;
00286 *a = *b;
00287 *b = t;
00288 a++;
00289 b++;
00290 }
00291 }
00292
00293 static void Swap4(size_t count, ON__UINT32* a, ON__UINT32* b)
00294 {
00295 ON__UINT32 t;
00296 while (count--)
00297 {
00298 t = *a;
00299 *a = *b;
00300 *b = t;
00301 a++;
00302 b++;
00303 }
00304 }
00305
00306 static void Swap8(size_t count, ON__UINT64* a, ON__UINT64* b)
00307 {
00308 ON__UINT64 t;
00309 while (count--)
00310 {
00311 t = *a;
00312 *a = *b;
00313 *b = t;
00314 a++;
00315 b++;
00316 }
00317 }
00318
00319 void ON_RandomNumberGenerator::RandomPermutation(void* base, size_t nel, size_t sizeof_element )
00320 {
00321 ON__UINT32 i, j, n;
00322
00323 if ( 0 == base || nel <= 1 || sizeof_element <= 0 )
00324 return;
00325
00326 #if defined(ON_64BIT_POINTER)
00327 if ( nel > 0xFFFFFFFF || sizeof_element > 0xFFFFFFFF)
00328 return;
00329 #endif
00330
00331 n = (ON__UINT32)nel;
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 if ( 0 == sizeof_element % sizeof(ON__UINT64) )
00346 {
00347 ON__UINT64* a = (ON__UINT64*)base;
00348 sizeof_element /= sizeof(a[0]);
00349 for ( i = 0; i < n; i++ )
00350 {
00351 j = on_random_number(&m_rand_context) % (n-i);
00352 if ( j )
00353 {
00354 Swap8(sizeof_element, a+i, a+i+j);
00355 }
00356 }
00357 }
00358 else if ( 0 == sizeof_element % sizeof(ON__UINT32) )
00359 {
00360 ON__UINT32* a = (ON__UINT32*)base;
00361 sizeof_element /= sizeof(a[0]);
00362 for ( i = 0; i < n; i++ )
00363 {
00364 j = on_random_number(&m_rand_context) % (n-i);
00365 if ( j )
00366 {
00367 Swap4(sizeof_element, a+i, a+i+j);
00368 }
00369 }
00370 }
00371 else
00372 {
00373 unsigned char* a = (unsigned char*)base;
00374 for ( i = 0; i < n; i++ )
00375 {
00376 j = on_random_number(&m_rand_context) % (n-i);
00377 if ( j )
00378 {
00379 Swap1(sizeof_element, a+i, a+i+j);
00380 }
00381 }
00382 }
00383 }
00384