Max 5 API Reference
00001 /* 00002 * Copyright 2001-2005 - Cycling '74 00003 * Derek Gerstmann - derek@cycling74.com 00004 * 00005 * Coherent simplex based noise functor. 00006 * 00007 * Based on Ken Perlin's replacement for his classic noise algorithm, 00008 * presented at GDC 2001. 00009 * 00010 * "In one dimension, the simplex is the line segment. In two dimensions, 00011 * the simplex is the convex hull of the equilateral triangle. In three 00012 * dimensions, the simplex is the convex hull of the tetrahedron. The 00013 * simplex in four dimensions (the pentatope) is a regular tetrahedron 00014 * ABCD in which a point E along the fourth dimension through the center 00015 * of ABCD is chosen so that EA = EB = EC = ED = AB." 00016 * -- http://mathworld.wolfram.com/Simplex.html 00017 */ 00018 00019 /*************************************************************************/ 00020 00021 #include "jit.noise.h" 00022 #include "jit.common.h" 00023 #include "jit.functor.h" 00024 #include "ext_systhread.h" 00025 00026 /*************************************************************************/ 00027 00028 // 3d simplex gradient directions 00029 static long jit_functor_noise_simplex_grad3count = 12; 00030 static long jit_functor_noise_simplex_grad3[12][3] = 00031 { 00032 {0,1,1},{0,1,-1},{0,-1,1},{0,-1,-1}, 00033 {1,0,1},{1,0,-1},{-1,0,1},{-1,0,-1}, 00034 {1,1,0},{1,-1,0},{-1,1,0},{-1,-1,0} 00035 }; 00036 00037 // 4d simplex gradient directions 00038 static long jit_functor_noise_simplex_grad4count = 32; 00039 static long jit_functor_noise_simplex_grad4[32][4] = 00040 { 00041 {0, 1, 1, 1}, {0, 1, 1, -1}, {0, 1, -1, 1}, {0, 1, -1, -1}, 00042 {0, -1, 1, 1}, {0, -1, 1, -1}, {0, -1, -1, 1}, {0, -1, -1, -1}, 00043 {1, 0, 1, 1}, {1, 0, 1, -1}, {1, 0, -1, 1}, {1, 0, -1, -1}, 00044 { -1, 0, 1, 1}, { -1, 0, 1, -1}, { -1, 0, -1, 1}, { -1, 0, -1, -1}, 00045 {1, 1, 0, 1}, {1, 1, 0, -1}, {1, -1, 0, 1}, {1, -1, 0, -1}, 00046 { -1, 1, 0, 1}, { -1, 1, 0, -1}, { -1, -1, 0, 1}, { -1, -1, 0, -1}, 00047 {1, 1, 1, 0}, {1, 1, -1, 0}, {1, -1, 1, 0}, {1, -1, -1, 0}, 00048 { -1, 1, 1, 0}, { -1, 1, -1, 0}, { -1, -1, 1, 0}, { -1, -1, -1, 0} 00049 }; 00050 00051 // 4d simplex index table 00052 static long jit_functor_noise_simplex_index[64][4] = 00053 { 00054 {0, 1, 2, 3}, {0, 1, 3, 2}, {0, 0, 0, 0}, {0, 2, 3, 1}, 00055 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 2, 3, 0}, 00056 {0, 2, 1, 3}, {0, 0, 0, 0}, {0, 3, 1, 2}, {0, 3, 2, 1}, 00057 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 3, 2, 0}, 00058 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00059 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00060 {1, 2, 0, 3}, {0, 0, 0, 0}, {1, 3, 0, 2}, {0, 0, 0, 0}, 00061 {0, 0, 0, 0}, {0, 0, 0, 0}, {2, 3, 0, 1}, {2, 3, 1, 0}, 00062 {1, 0, 2, 3}, {1, 0, 3, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00063 {0, 0, 0, 0}, {2, 0, 3, 1}, {0, 0, 0, 0}, {2, 1, 3, 0}, 00064 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00065 {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00066 {2, 0, 1, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00067 {3, 0, 1, 2}, {3, 0, 2, 1}, {0, 0, 0, 0}, {3, 1, 2, 0}, 00068 {2, 1, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, 00069 {3, 1, 0, 2}, {0, 0, 0, 0}, {3, 2, 0, 1}, {3, 2, 1, 0} 00070 }; 00071 00072 // skewing + unskewing factors 00073 #define F2 ((jit_math_sqrt(3.0)-1.0)/2.0) 00074 #define G2 ((3.0-jit_math_sqrt(3.0))/6.0) 00075 00076 #define F3 (1.0/3.0) 00077 #define G3 (1.0/6.0) 00078 00079 #define F4 ((jit_math_sqrt(5.0)-1.0)/4.0) 00080 #define G4 ((5.0-jit_math_sqrt(5.0))/20.0) 00081 00082 // safe permutation lookup 00083 #define P(i) (x->p[(i)&(x->tablemask)]) 00084 00085 /*************************************************************************/ 00086 00087 // the noise object 00088 typedef struct _jit_functor_noise_simplex 00089 { 00090 t_jit_object ob; 00091 00092 // pseudo random permutation table 00093 long *p; 00094 00095 // lattice of uniform values [0-1] 00096 t_jit_functor_combined_dynarray lattice; 00097 00098 // array of simplex directions 00099 t_jit_functor_combined_dimvalue *gradients; 00100 00101 // skew and unskew factors for determining simplex 00102 t_jit_functor_combined_value skew; 00103 t_jit_functor_combined_value unskew; 00104 00105 // current dimcount for simplex directions 00106 long dimcount; 00107 00108 // size and mask for tables 00109 long tablesize; 00110 long tablemask; 00111 00112 // seed for random number generation 00113 long seed; 00114 00115 // signed mode (0 = unsigned, 1 = signed) 00116 long sign; 00117 00118 // abs mode (0 = normal, 1 = absolute) 00119 long abs; 00120 00121 // lock on our dir init which can be called during parallel evaluation 00122 long spinwait; 00123 00124 } 00125 t_jit_functor_noise_simplex; 00126 00127 t_class *_jit_functor_noise_simplex_class; 00128 t_symbol *ps_jit_functor_noise_simplex_filter; 00129 00130 /*************************************************************************/ 00131 00132 t_jit_object *jit_functor_noise_simplex_new(void); 00133 t_jit_err jit_functor_noise_simplex_free(t_jit_functor_noise_simplex *x); 00134 00135 t_jit_err jit_functor_noise_simplex_abs(t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv); 00136 t_jit_err jit_functor_noise_simplex_sign(t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv); 00137 t_jit_err jit_functor_noise_simplex_seed(t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv); 00138 t_jit_err jit_functor_noise_simplex_tablesize(t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv); 00139 t_jit_err jit_functor_noise_simplex_recalc(t_jit_functor_noise_simplex *x); 00140 00141 double jit_functor_noise_simplex_eval_float64( 00142 t_jit_functor_noise_simplex *x, long dimcount, double *vals); 00143 00144 double jit_functor_noise_simplex_eval_float64_dim2( 00145 t_jit_functor_noise_simplex *x, long dimcount, double *vals); 00146 00147 double jit_functor_noise_simplex_eval_float64_dim3( 00148 t_jit_functor_noise_simplex *x, long dimcount, double *vals); 00149 00150 double jit_functor_noise_simplex_eval_float64_dim4( 00151 t_jit_functor_noise_simplex *x, long dimcount, double *vals); 00152 00153 float jit_functor_noise_simplex_eval_float32( 00154 t_jit_functor_noise_simplex *x, long dimcount, float *vals); 00155 00156 float jit_functor_noise_simplex_eval_float32_dim2( 00157 t_jit_functor_noise_simplex *x, long dimcount, float *vals); 00158 00159 float jit_functor_noise_simplex_eval_float32_dim3( 00160 t_jit_functor_noise_simplex *x, long dimcount, float *vals); 00161 00162 float jit_functor_noise_simplex_eval_float32_dim4( 00163 t_jit_functor_noise_simplex *x, long dimcount, float *vals); 00164 00165 long jit_functor_noise_simplex_eval_fixed( 00166 t_jit_functor_noise_simplex *x, long dimcount, long *vals); 00167 00168 t_jit_err jit_functor_noise_simplex_dir_init( 00169 t_jit_functor_noise_simplex *x); 00170 00171 /*************************************************************************/ 00172 00173 t_jit_err jit_functor_noise_simplex_init(void) 00174 { 00175 t_jit_object *attr; 00176 00177 // create class 00178 _jit_functor_noise_simplex_class = jit_class_new("jit_functor_noise_simplex", 00179 (method)jit_functor_noise_simplex_new, (method)jit_functor_noise_simplex_free, 00180 sizeof(t_jit_functor_noise_simplex), 0L); 00181 00182 // add attribute methods 00183 attr = jit_object_new(_jit_sym_jit_attr_offset, "abs", _jit_sym_long, 0, 00184 (method)0L, (method)jit_functor_noise_simplex_abs, 00185 calcoffset(t_jit_functor_noise_simplex, abs)); 00186 jit_class_addattr(_jit_functor_noise_simplex_class, attr); 00187 attr = jit_object_new(_jit_sym_jit_attr_offset, "sign", _jit_sym_long, 0, 00188 (method)0L, (method)jit_functor_noise_simplex_sign, 00189 calcoffset(t_jit_functor_noise_simplex, sign)); 00190 jit_class_addattr(_jit_functor_noise_simplex_class, attr); 00191 attr = jit_object_new(_jit_sym_jit_attr_offset, "seed", _jit_sym_long, 0, 00192 (method)0L, (method)jit_functor_noise_simplex_seed, 00193 calcoffset(t_jit_functor_noise_simplex, seed)); 00194 jit_class_addattr(_jit_functor_noise_simplex_class, attr); 00195 attr = jit_object_new(_jit_sym_jit_attr_offset, "tablesize", _jit_sym_long, 0, 00196 (method)0L, (method)jit_functor_noise_simplex_tablesize, 00197 calcoffset(t_jit_functor_noise_simplex, tablesize)); 00198 jit_class_addattr(_jit_functor_noise_simplex_class, attr); 00199 00200 // add evaluation methods 00201 jit_class_addmethod(_jit_functor_noise_simplex_class, 00202 (method)jit_functor_noise_simplex_eval_fixed, "evalfixed", A_CANT, 0L); 00203 jit_class_addmethod(_jit_functor_noise_simplex_class, 00204 (method)jit_functor_noise_simplex_eval_float32, "evalfloat32", A_CANT, 0L); 00205 jit_class_addmethod(_jit_functor_noise_simplex_class, 00206 (method)jit_functor_noise_simplex_eval_float64, "evalfloat64", A_CANT, 0L); 00207 00208 // register functor *then* class 00209 jit_functor_setup_class(_jit_functor_noise_simplex_class, "noise", "simplex"); 00210 jit_class_register(_jit_functor_noise_simplex_class); 00211 return JIT_ERR_NONE; 00212 } 00213 00214 t_jit_object *jit_functor_noise_simplex_new(void) 00215 { 00216 long i, j; 00217 t_jit_functor_noise_simplex *x; 00218 00219 if (x = (t_jit_functor_noise_simplex *)jit_object_alloc(_jit_functor_noise_simplex_class)) 00220 { 00221 // initialization 00222 x->p = NULL; 00223 x->lattice.fixed = NULL; 00224 x->lattice.float32 = NULL; 00225 x->lattice.float64 = NULL; 00226 00227 x->abs = FALSE; 00228 x->sign = FALSE; 00229 00230 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->skew, 0); 00231 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->unskew, 0); 00232 00233 x->seed = JIT_NOISE_DEFAULT_SEED; 00234 x->tablesize = jit_math_roundup_poweroftwo(JIT_NOISE_DEFAULT_TABLESIZE) * 2; 00235 x->tablemask = x->tablesize - 1; 00236 00237 x->dimcount = 0; 00238 x->gradients = NULL; 00239 x->spinwait = 0; 00240 00241 // allocate permutation and uniform tables 00242 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 00243 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 00244 jit_functor_noise_simplex_recalc(x); 00245 } 00246 00247 return (t_jit_object *)x; 00248 } 00249 00250 t_jit_err jit_functor_noise_simplex_free(t_jit_functor_noise_simplex *x) 00251 { 00252 // free any allocated memory 00253 jit_functor_combined_dynarray_destroy(&x->lattice); 00254 00255 if (x->p) 00256 jit_disposeptr((char*)x->p); 00257 x->p = NULL; 00258 00259 if (x->gradients) 00260 jit_disposeptr((char*)x->gradients); 00261 x->gradients = NULL; 00262 00263 00264 return JIT_ERR_NONE; 00265 } 00266 00267 t_jit_err jit_functor_noise_simplex_abs( 00268 t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv) 00269 { 00270 long v; 00271 00272 if (x) 00273 { 00274 v = jit_atom_getlong(argv); 00275 if (x->abs != v) 00276 { 00277 x->abs = (v > 0) ? TRUE : FALSE; 00278 jit_functor_noise_simplex_recalc(x); 00279 } 00280 return JIT_ERR_NONE; 00281 } 00282 return JIT_ERR_INVALID_PTR; 00283 } 00284 00285 t_jit_err jit_functor_noise_simplex_sign( 00286 t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv) 00287 { 00288 long v; 00289 00290 if (x) 00291 { 00292 v = jit_atom_getlong(argv); 00293 if (x->sign != v) 00294 { 00295 x->sign = (v > 0) ? TRUE : FALSE; 00296 jit_functor_noise_simplex_recalc(x); 00297 } 00298 return JIT_ERR_NONE; 00299 } 00300 return JIT_ERR_INVALID_PTR; 00301 } 00302 00303 t_jit_err jit_functor_noise_simplex_seed( 00304 t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv) 00305 { 00306 long v; 00307 00308 if (x) 00309 { 00310 v = jit_atom_getlong(argv); 00311 if (x->seed != v) 00312 { 00313 x->seed = v; 00314 00315 // allocate permutation and uniform tables 00316 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 00317 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 00318 jit_functor_noise_simplex_recalc(x); 00319 } 00320 return JIT_ERR_NONE; 00321 } 00322 return JIT_ERR_INVALID_PTR; 00323 } 00324 00325 t_jit_err jit_functor_noise_simplex_tablesize( 00326 t_jit_functor_noise_simplex *x, void *attr, long argc, t_atom *argv) 00327 { 00328 long v; 00329 00330 if (x) 00331 { 00332 v = jit_atom_getlong(argv); 00333 if (x->tablesize != v && v > 0) 00334 { 00335 00336 x->tablesize = jit_math_roundup_poweroftwo(v) * 2; 00337 x->tablemask = x->tablesize - 1; 00338 00339 // allocate permutation and uniform tables 00340 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 00341 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 00342 jit_functor_noise_simplex_recalc(x); 00343 } 00344 return JIT_ERR_NONE; 00345 } 00346 return JIT_ERR_INVALID_PTR; 00347 } 00348 00349 t_jit_err jit_functor_noise_simplex_recalc(t_jit_functor_noise_simplex *x) 00350 { 00351 // calculate intermediary values for efficiency 00352 return JIT_ERR_NONE; 00353 } 00354 00355 // -------------------------------------------------------------------------- 00356 00357 float jit_functor_noise_simplex_eval_float32_dim2( 00358 t_jit_functor_noise_simplex *x, long dimcount, float *vals) 00359 { 00360 long i, j; 00361 long i1, j1; 00362 long ii, jj; 00363 long gi0, gi1, gi2; 00364 00365 float s, t; 00366 float t0, t1, t2; 00367 float gx, gy; 00368 float n0, n1, n2; 00369 float X0, Y0, x0, y0; 00370 float x1, y1; 00371 float x2, y2; 00372 float xin, yin; 00373 float signal; 00374 00375 if (!x || !vals || dimcount < 2) 00376 return JIT_ERR_GENERIC; 00377 00378 // get input space 00379 xin = vals[0]; 00380 yin = vals[1]; 00381 00382 // skew input space to determine which simplex we are in 00383 s = (xin + yin) * x->skew.float32; 00384 i = JIT_MATH_F32_FLOOR(xin + s); 00385 j = JIT_MATH_F32_FLOOR(yin + s); 00386 t = (i + j) * x->unskew.float32; 00387 00388 // unskew cell origin back to 2d space 00389 X0 = i - t; 00390 Y0 = j - t; 00391 00392 // get distances from the cell origin 00393 x0 = xin - X0; 00394 y0 = yin - Y0; 00395 00396 // determine which simplex we are in. 00397 if (x0 > y0) 00398 { 00399 // lower triangle 00400 i1 = 1; 00401 j1 = 0; 00402 } 00403 else 00404 { 00405 // upper triangle 00406 i1 = 0; 00407 j1 = 1; 00408 } 00409 00410 // unskew offsets for middle corner 00411 x1 = x0 - i1 + x->unskew.float32; 00412 y1 = y0 - j1 + x->unskew.float32; 00413 00414 // unskew offsets for last corner 00415 x2 = x0 - 1.0f + 2.0f * x->unskew.float32; 00416 y2 = y0 - 1.0f + 2.0f * x->unskew.float32; 00417 00418 // get the hashed simplex indices of the three simplex corners 00419 ii = i & x->tablemask; 00420 jj = j & x->tablemask; 00421 00422 // get contribution for first corner 00423 t0 = 0.5f - (x0 * x0 + y0 * y0); 00424 if (t0 < 0.0f) 00425 { 00426 n0 = 0.0f; 00427 } 00428 else 00429 { 00430 // get gradient value via permutation table 00431 gi0 = P(ii + P(jj)) % 12; 00432 gx = x->gradients[gi0].float32[0]; 00433 gy = x->gradients[gi0].float32[1]; 00434 00435 // compute contribution 00436 t0 *= t0; 00437 n0 = t0 * t0 * (gx * x0 + gy * y0); 00438 } 00439 00440 // get contribution for middle corner 00441 t1 = 0.5f - (x1 * x1 + y1 * y1); 00442 if (t1 < 0.0f) 00443 { 00444 n1 = 0.0f; 00445 } 00446 else 00447 { 00448 // get gradient value via permutation table 00449 gi1 = P(ii + i1 + P(jj + j1)) % 12; 00450 gx = x->gradients[gi1].float32[0]; 00451 gy = x->gradients[gi1].float32[1]; 00452 00453 // compute contribution 00454 t1 *= t1; 00455 n1 = t1 * t1 * (gx * x1 + gy * y1); 00456 } 00457 00458 // get contribution for last corner 00459 t2 = 0.5f - (x2 * x2 + y2 * y2); 00460 if (t2 < 0.0f) 00461 { 00462 n2 = 0.0f; 00463 } 00464 else 00465 { 00466 // get gradient value via permutation table 00467 gi2 = P(ii + 1 + P(jj + 1)) % 12; 00468 gx = x->gradients[gi2].float32[0]; 00469 gy = x->gradients[gi2].float32[1]; 00470 00471 // compute contribution 00472 t2 *= t2; 00473 n2 = t2 * t2 * (gx * x2 + gy * y2); 00474 } 00475 00476 // add contributions from each corner, scale to [-1,1]. 00477 signal = 70.0f * (n0 + n1 + n2); 00478 return signal; 00479 } 00480 00481 float jit_functor_noise_simplex_eval_float32_dim3( 00482 t_jit_functor_noise_simplex *x, long dimcount, float *vals) 00483 { 00484 float xin, yin, zin; 00485 float x0, y0, z0; 00486 float X0, Y0, Z0; 00487 float x1, y1, z1; 00488 float x2, y2, z2; 00489 float x3, y3, z3; 00490 float gx, gy, gz; 00491 float n0, n1, n2, n3; 00492 float t0, t1, t2, t3, t4; 00493 float s, t; 00494 float signal; 00495 00496 long i, j, k; 00497 long i1, j1, k1; 00498 long i2, j2, k2; 00499 long ii, jj, kk; 00500 long gi0, gi1, gi2, gi3; 00501 00502 if (!x || !vals || dimcount < 3) 00503 return JIT_ERR_GENERIC; 00504 00505 // get the input space 00506 xin = vals[0]; 00507 yin = vals[1]; 00508 zin = vals[2]; 00509 00510 // skew the input space to determine which simplex cell we're in 00511 s = (xin + yin + zin) * x->skew.float32; 00512 i = JIT_MATH_F32_FLOOR(xin + s); 00513 j = JIT_MATH_F32_FLOOR(yin + s); 00514 k = JIT_MATH_F32_FLOOR(zin + s); 00515 t = (i + j + k) * x->unskew.float32; 00516 00517 // unskew the cell origin back to input space 00518 X0 = i - t; 00519 Y0 = j - t; 00520 Z0 = k - t; 00521 00522 // get the distances from the cell origin 00523 x0 = xin - X0; 00524 y0 = yin - Y0; 00525 z0 = zin - Z0; 00526 00527 // determine which simplex we are in. 00528 if (x0 >= y0) 00529 { 00530 if (y0 >= z0) 00531 { 00532 // X Y Z order 00533 i1 = 1; j1 = 0; k1 = 0; 00534 i2 = 1; j2 = 1; k2 = 0; 00535 } 00536 else if (x0 >= z0) 00537 { 00538 // X Z Y order 00539 i1 = 1; j1 = 0; k1 = 0; 00540 i2 = 1; j2 = 0; k2 = 1; 00541 } 00542 else 00543 { 00544 // Z X Y order 00545 i1 = 0; j1 = 0; k1 = 1; 00546 i2 = 1; j2 = 0; k2 = 1; 00547 } 00548 } 00549 else 00550 { 00551 if (y0 < z0) 00552 { 00553 // Z Y X order 00554 i1 = 0; j1 = 0; k1 = 1; 00555 i2 = 0; j2 = 1; k2 = 1; 00556 } 00557 else if (x0 < z0) 00558 { 00559 // Y Z X order 00560 i1 = 0; j1 = 1; k1 = 0; 00561 i2 = 0; j2 = 1; k2 = 1; 00562 } 00563 else 00564 { 00565 // Y X Z order 00566 i1 = 0; j1 = 1; k1 = 0; 00567 i2 = 1; j2 = 1; k2 = 0; 00568 } 00569 } 00570 00571 // get offsets for second corner 00572 x1 = x0 - i1 + x->unskew.float32; 00573 y1 = y0 - j1 + x->unskew.float32; 00574 z1 = z0 - k1 + x->unskew.float32; 00575 00576 // get offsets for third corner 00577 x2 = x0 - i2 + 2.0f * x->unskew.float32; 00578 y2 = y0 - j2 + 2.0f * x->unskew.float32; 00579 z2 = z0 - k2 + 2.0f * x->unskew.float32; 00580 00581 // get offsets for last corner 00582 x3 = x0 - 1.0f + 3.0f * x->unskew.float32; 00583 y3 = y0 - 1.0f + 3.0f * x->unskew.float32; 00584 z3 = z0 - 1.0f + 3.0f * x->unskew.float32; 00585 00586 // get the hashed simplex indices of the four simplex corners 00587 ii = i & x->tablemask; 00588 jj = j & x->tablemask; 00589 kk = k & x->tablemask; 00590 00591 // calculate the contribution from the four corners 00592 t0 = 0.6f - (x0 * x0 + y0 * y0 + z0 * z0); 00593 if (t0 < 0.0f) 00594 { 00595 n0 = 0.0f; 00596 } 00597 else 00598 { 00599 // get gradient value via permutation table 00600 gi0 = P(ii + P(jj + P(kk))) % 12; 00601 gx = x->gradients[gi0].float32[0]; 00602 gy = x->gradients[gi0].float32[1]; 00603 gz = x->gradients[gi0].float32[2]; 00604 00605 // compute contribution 00606 t0 *= t0; 00607 n0 = t0 * t0 * (gx * x0 + gy * y0 + gz * z0); 00608 } 00609 00610 t1 = 0.6f - (x1 * x1 + y1 * y1 + z1 * z1); 00611 if (t1 < 0.0f) 00612 { 00613 n1 = 0.0f; 00614 } 00615 else 00616 { 00617 // get gradient value via permutation table 00618 gi1 = P(ii + i1 + P(jj + j1 + P(kk + k1))) % 12; 00619 gx = x->gradients[gi1].float32[0]; 00620 gy = x->gradients[gi1].float32[1]; 00621 gz = x->gradients[gi1].float32[2]; 00622 00623 // compute contribution 00624 t1 *= t1; 00625 n1 = t1 * t1 * (gx * x1 + gy * y1 + gz * z1); 00626 } 00627 00628 t2 = 0.6f - (x2 * x2 + y2 * y2 + z2 * z2); 00629 if (t2 < 0.0f) 00630 { 00631 n2 = 0.0; 00632 } 00633 else 00634 { 00635 // get gradient value via permutation table 00636 gi2 = P(ii + i2 + P(jj + j2 + P(kk + k2))) % 12; 00637 gx = x->gradients[gi2].float32[0]; 00638 gy = x->gradients[gi2].float32[1]; 00639 gz = x->gradients[gi2].float32[2]; 00640 00641 // compute contribution 00642 t2 *= t2; 00643 n2 = t2 * t2 * (gx * x2 + gy * y2 + gz * z2); 00644 } 00645 00646 t3 = 0.6f - (x3 * x3 + y3 * y3 + z3 * z3); 00647 if (t3 < 0.0f) 00648 { 00649 n3 = 0.0f; 00650 } 00651 else 00652 { 00653 // get gradient value via permutation table 00654 gi3 = P(ii + 1 + P(jj + 1 + P(kk + 1))) % 12; 00655 gx = x->gradients[gi3].float32[0]; 00656 gy = x->gradients[gi3].float32[1]; 00657 gz = x->gradients[gi3].float32[2]; 00658 00659 // compute contribution 00660 t3 *= t3; 00661 n3 = t3 * t3 * (gx * x3 + gy * y3 + gz * z3); 00662 } 00663 00664 // add contributions from each corner to get the final noise value, scale to [-1,1] 00665 signal = 32.0f * (n0 + n1 + n2 + n3); 00666 return signal; 00667 00668 } 00669 00670 float jit_functor_noise_simplex_eval_float32_dim4( 00671 t_jit_functor_noise_simplex *x, long dimcount, float *vals) 00672 { 00673 long i, j, k, l; 00674 long ii, jj, kk, ll; 00675 long i1, j1, k1, l1; 00676 long i2, j2, k2, l2; 00677 long i3, j3, k3, l3; 00678 long c, c1, c2, c3, c4, c5, c6; 00679 long gi0, gi1, gi2, gi3, gi4; 00680 00681 float s, t; 00682 float signal; 00683 float xin, yin, zin, win; 00684 float X0, Y0, Z0, W0; 00685 float x0, y0, z0, w0; 00686 float x1, y1, z1, w1; 00687 float x2, y2, z2, w2; 00688 float x3, y3, z3, w3; 00689 float x4, y4, z4, w4; 00690 float gx, gy, gz, gw; 00691 float n0, n1, n2, n3, n4; 00692 float t0, t1, t2, t3, t4; 00693 00694 if (!x || !vals || dimcount < 4) 00695 return JIT_ERR_GENERIC; 00696 00697 xin = vals[0]; 00698 yin = vals[1]; 00699 zin = vals[2]; 00700 win = vals[3]; 00701 00702 // skew the input space to determine which cell of 24 simplices we're in 00703 s = (xin + yin + zin + win) * x->skew.float32; 00704 i = JIT_MATH_F32_FLOOR(xin + s); 00705 j = JIT_MATH_F32_FLOOR(yin + s); 00706 k = JIT_MATH_F32_FLOOR(zin + s); 00707 l = JIT_MATH_F32_FLOOR(win + s); 00708 t = (i + j + k + l) * x->unskew.float32; 00709 00710 // unskew the cell origin back to (x,y,z,w) space 00711 X0 = i - t; 00712 Y0 = j - t; 00713 Z0 = k - t; 00714 W0 = l - t; 00715 00716 // get the distances from the cell origin 00717 x0 = xin - X0; 00718 y0 = yin - Y0; 00719 z0 = zin - Z0; 00720 w0 = win - W0; 00721 00722 // To find out which of the 24 possible simplices we're in, we need to 00723 // determine the magnitude ordering of x0, y0, z0 and w0. 00724 // The method below is a good way of finding the ordering of x,y,z,w and 00725 // then find the correct traversal order for the simplex we�re in. 00726 // First, six pair-wise comparisons are performed between each possible pair 00727 // of the four coordinates, and the results are used to add up binary bits 00728 // for an integer index. 00729 c1 = (x0 > y0) ? 32 : 0; 00730 c2 = (x0 > z0) ? 16 : 0; 00731 c3 = (y0 > z0) ? 8 : 0; 00732 c4 = (x0 > w0) ? 4 : 0; 00733 c5 = (y0 > w0) ? 2 : 0; 00734 c6 = (z0 > w0) ? 1 : 0; 00735 00736 // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order. 00737 c = c1 + c2 + c3 + c4 + c5 + c6; 00738 00739 // the number 3 in the "simplex" array is at the position of the largest coordinate. 00740 i1 = jit_functor_noise_simplex_index[c][0] >= 3 ? 1 : 0; 00741 j1 = jit_functor_noise_simplex_index[c][1] >= 3 ? 1 : 0; 00742 k1 = jit_functor_noise_simplex_index[c][2] >= 3 ? 1 : 0; 00743 l1 = jit_functor_noise_simplex_index[c][3] >= 3 ? 1 : 0; 00744 00745 // the number 2 in the "simplex" array is at the second largest coordinate. 00746 i2 = jit_functor_noise_simplex_index[c][0] >= 2 ? 1 : 0; 00747 j2 = jit_functor_noise_simplex_index[c][1] >= 2 ? 1 : 0; 00748 k2 = jit_functor_noise_simplex_index[c][2] >= 2 ? 1 : 0; 00749 l2 = jit_functor_noise_simplex_index[c][3] >= 2 ? 1 : 0; 00750 00751 // the number 1 in the "simplex" array is at the second smallest coordinate. 00752 i3 = jit_functor_noise_simplex_index[c][0] >= 1 ? 1 : 0; 00753 j3 = jit_functor_noise_simplex_index[c][1] >= 1 ? 1 : 0; 00754 k3 = jit_functor_noise_simplex_index[c][2] >= 1 ? 1 : 0; 00755 l3 = jit_functor_noise_simplex_index[c][3] >= 1 ? 1 : 0; 00756 00757 // skip the fifth corner, it has all coordinate offsets = 1, so no need to look that up. 00758 00759 // get offsets for second corner 00760 x1 = x0 - i1 + x->unskew.float32; 00761 y1 = y0 - j1 + x->unskew.float32; 00762 z1 = z0 - k1 + x->unskew.float32; 00763 w1 = w0 - l1 + x->unskew.float32; 00764 00765 // get offsets for third corner 00766 x2 = x0 - i2 + 2.0f * x->unskew.float32; 00767 y2 = y0 - j2 + 2.0f * x->unskew.float32; 00768 z2 = z0 - k2 + 2.0f * x->unskew.float32; 00769 w2 = w0 - l2 + 2.0f * x->unskew.float32; 00770 00771 // get offsets for fourth corner 00772 x3 = x0 - i3 + 3.0f * x->unskew.float32; 00773 y3 = y0 - j3 + 3.0f * x->unskew.float32; 00774 z3 = z0 - k3 + 3.0f * x->unskew.float32; 00775 w3 = w0 - l3 + 3.0f * x->unskew.float32; 00776 00777 // get offsets for last corner 00778 x4 = x0 - 1.0f + 4.0f * x->unskew.float32; 00779 y4 = y0 - 1.0f + 4.0f * x->unskew.float32; 00780 z4 = z0 - 1.0f + 4.0f * x->unskew.float32; 00781 w4 = w0 - 1.0f + 4.0f * x->unskew.float32; 00782 00783 // get the hashed simplex indices of the five simplex corners 00784 ii = i & x->tablemask; 00785 jj = j & x->tablemask; 00786 kk = k & x->tablemask; 00787 ll = l & x->tablemask; 00788 00789 // calculate the contribution from the five corners 00790 t0 = 0.6f - (x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0); 00791 if (t0 < 0.0f) 00792 { 00793 n0 = 0.0f; 00794 } 00795 else 00796 { 00797 // get gradient value via permutation table 00798 gi0 = P(ii + P(jj + P(kk + P(ll)))) % 32; 00799 gx = x->gradients[gi0].float32[0]; 00800 gy = x->gradients[gi0].float32[1]; 00801 gz = x->gradients[gi0].float32[2]; 00802 gw = x->gradients[gi0].float32[3]; 00803 00804 // compute contribution 00805 t0 *= t0; 00806 n0 = t0 * t0 * (gx * x0 + gy * y0 + gz * z0 + gw * w0); 00807 } 00808 00809 t1 = 0.6 - (x1 * x1 + y1 * y1 + z1 * z1 + w1 * w1); 00810 if (t1 < 0.0f) 00811 { 00812 n1 = 0.0f; 00813 } 00814 else 00815 { 00816 // get gradient value via permutation table 00817 gi1 = P(ii + i1 + P(jj + j1 + P(kk + k1 + P(ll + l1)))) % 32; 00818 gx = x->gradients[gi1].float32[0]; 00819 gy = x->gradients[gi1].float32[1]; 00820 gz = x->gradients[gi1].float32[2]; 00821 gw = x->gradients[gi1].float32[3]; 00822 00823 // compute contribution 00824 t1 *= t1; 00825 n1 = t1 * t1 * (gx * x1 + gy * y1 + gz * z1 + gw * w1); 00826 } 00827 00828 t2 = 0.6 - (x2 * x2 + y2 * y2 + z2 * z2 + w2 * w2); 00829 if (t2 < 0.0f) 00830 { 00831 n2 = 0.0f; 00832 } 00833 else 00834 { 00835 // get gradient value via permutation table 00836 gi2 = P(ii + i2 + P(jj + j2 + P(kk + k2 + P(ll + l2)))) % 32; 00837 gx = x->gradients[gi2].float32[0]; 00838 gy = x->gradients[gi2].float32[1]; 00839 gz = x->gradients[gi2].float32[2]; 00840 gw = x->gradients[gi2].float32[3]; 00841 00842 // compute contribution 00843 t2 *= t2; 00844 n2 = t2 * t2 * (gx * x2 + gy * y2 + gz * z2 + gw * w2); 00845 } 00846 00847 t3 = 0.6f - (x3 * x3 + y3 * y3 + z3 * z3 + w3 * w3); 00848 if (t3 < 0.0f) 00849 { 00850 n3 = 0.0f; 00851 } 00852 else 00853 { 00854 // get gradient value via permutation table 00855 gi3 = P(ii + i3 + P(jj + j3 + P(kk + k3 + P(ll + l3)))) % 32; 00856 gx = x->gradients[gi3].float32[0]; 00857 gy = x->gradients[gi3].float32[1]; 00858 gz = x->gradients[gi3].float32[2]; 00859 gw = x->gradients[gi3].float32[3]; 00860 00861 // compute contribution 00862 t3 *= t3; 00863 n3 = t3 * t3 * (gx * x3 + gy * y3 + gz * z3 + gw * w3); 00864 } 00865 00866 t4 = 0.6f - (x4 * x4 + y4 * y4 + z4 * z4 + w4 * w4); 00867 if (t4 < 0.0f) 00868 { 00869 n4 = 0.0f; 00870 } 00871 else 00872 { 00873 // get gradient value via permutation table 00874 gi4 = P(ii + 1 + P(jj + 1 + P(kk + 1 + P(ll + 1)))) % 32; 00875 gx = x->gradients[gi4].float32[0]; 00876 gy = x->gradients[gi4].float32[1]; 00877 gz = x->gradients[gi4].float32[2]; 00878 gw = x->gradients[gi4].float32[3]; 00879 00880 // compute contribution 00881 t4 *= t4; 00882 n4 = t4 * t4 * (gx * x4 + gy * y4 + gz * z4 + gw * w4); 00883 } 00884 00885 // sum up and scale the result to cover the range [-1,1] 00886 signal = 27.0f * (n0 + n1 + n2 + n3 + n4); 00887 return signal; 00888 } 00889 00890 // -------------------------------------------------------------------------- 00891 00892 double jit_functor_noise_simplex_eval_float64_dim2( 00893 t_jit_functor_noise_simplex *x, long dimcount, double *vals) 00894 { 00895 long i, j; 00896 long i1, j1; 00897 long ii, jj; 00898 long gi0, gi1, gi2; 00899 00900 double s, t; 00901 double t0, t1, t2; 00902 double gx, gy; 00903 double n0, n1, n2; 00904 double X0, Y0, x0, y0; 00905 double x1, y1; 00906 double x2, y2; 00907 double xin, yin; 00908 double signal; 00909 00910 if (!x || !vals || dimcount < 2) 00911 return JIT_ERR_GENERIC; 00912 00913 // get input space 00914 xin = vals[0]; 00915 yin = vals[1]; 00916 00917 // skew input space to determine which simplex we are in 00918 s = (xin + yin) * x->skew.float64; 00919 i = JIT_MATH_F64_FLOOR(xin + s); 00920 j = JIT_MATH_F64_FLOOR(yin + s); 00921 t = (i + j) * x->unskew.float64; 00922 00923 // unskew cell origin back to 2d space 00924 X0 = i - t; 00925 Y0 = j - t; 00926 00927 // get distances from the cell origin 00928 x0 = xin - X0; 00929 y0 = yin - Y0; 00930 00931 // determine which simplex we are in. 00932 if (x0 > y0) 00933 { 00934 // lower triangle 00935 i1 = 1; 00936 j1 = 0; 00937 } 00938 else 00939 { 00940 // upper triangle 00941 i1 = 0; 00942 j1 = 1; 00943 } 00944 00945 // unskew offsets for middle corner 00946 x1 = x0 - i1 + x->unskew.float64; 00947 y1 = y0 - j1 + x->unskew.float64; 00948 00949 // unskew offsets for last corner 00950 x2 = x0 - 1.0 + 2.0 * x->unskew.float64; 00951 y2 = y0 - 1.0 + 2.0 * x->unskew.float64; 00952 00953 // get the hashed simplex indices of the three simplex corners 00954 ii = i & x->tablemask; 00955 jj = j & x->tablemask; 00956 00957 // get contribution for first corner 00958 t0 = 0.5f - (x0 * x0 + y0 * y0); 00959 if (t0 < 0.0) 00960 { 00961 n0 = 0.0; 00962 } 00963 else 00964 { 00965 // get gradient value via permutation table 00966 gi0 = P(ii + P(jj)) % 12; 00967 gx = x->gradients[gi0].float64[0]; 00968 gy = x->gradients[gi0].float64[1]; 00969 00970 // compute contribution 00971 t0 *= t0; 00972 n0 = t0 * t0 * (gx * x0 + gy * y0); 00973 } 00974 00975 // get contribution for middle corner 00976 t1 = 0.5f - (x1 * x1 + y1 * y1); 00977 if (t1 < 0.0) 00978 { 00979 n1 = 0.0; 00980 } 00981 else 00982 { 00983 // get gradient value via permutation table 00984 gi1 = P(ii + i1 + P(jj + j1)) % 12; 00985 gx = x->gradients[gi1].float64[0]; 00986 gy = x->gradients[gi1].float64[1]; 00987 00988 // compute contribution 00989 t1 *= t1; 00990 n1 = t1 * t1 * (gx * x1 + gy * y1); 00991 } 00992 00993 // get contribution for last corner 00994 t2 = 0.5f - (x2 * x2 + y2 * y2); 00995 if (t2 < 0.0) 00996 { 00997 n2 = 0.0; 00998 } 00999 else 01000 { 01001 // get gradient value via permutation table 01002 gi2 = P(ii + 1 + P(jj + 1)) % 12; 01003 gx = x->gradients[gi2].float64[0]; 01004 gy = x->gradients[gi2].float64[1]; 01005 01006 // compute contribution 01007 t2 *= t2; 01008 n2 = t2 * t2 * (gx * x2 + gy * y2); 01009 } 01010 01011 // add contributions from each corner, scale to [-1,1]. 01012 signal = 70.0 * (n0 + n1 + n2); 01013 01014 return signal; 01015 } 01016 01017 double jit_functor_noise_simplex_eval_float64_dim3( 01018 t_jit_functor_noise_simplex *x, long dimcount, double *vals) 01019 { 01020 double xin, yin, zin; 01021 double x0, y0, z0; 01022 double X0, Y0, Z0; 01023 double x1, y1, z1; 01024 double x2, y2, z2; 01025 double x3, y3, z3; 01026 double gx, gy, gz; 01027 double n0, n1, n2, n3; 01028 double t0, t1, t2, t3, t4; 01029 double s, t; 01030 double signal; 01031 01032 long i, j, k; 01033 long i1, j1, k1; 01034 long i2, j2, k2; 01035 long ii, jj, kk; 01036 long gi0, gi1, gi2, gi3; 01037 01038 if (!x || !vals || dimcount < 3) 01039 return JIT_ERR_GENERIC; 01040 01041 // get the input space 01042 xin = vals[0]; 01043 yin = vals[1]; 01044 zin = vals[2]; 01045 01046 // skew the input space to determine which simplex cell we're in 01047 s = (xin + yin + zin) * x->skew.float64; 01048 i = JIT_MATH_F64_FLOOR(xin + s); 01049 j = JIT_MATH_F64_FLOOR(yin + s); 01050 k = JIT_MATH_F64_FLOOR(zin + s); 01051 t = (i + j + k) * x->unskew.float64; 01052 01053 // unskew the cell origin back to input space 01054 X0 = i - t; 01055 Y0 = j - t; 01056 Z0 = k - t; 01057 01058 // get the distances from the cell origin 01059 x0 = xin - X0; 01060 y0 = yin - Y0; 01061 z0 = zin - Z0; 01062 01063 // determine which simplex we are in. 01064 if (x0 >= y0) 01065 { 01066 if (y0 >= z0) 01067 { 01068 // X Y Z order 01069 i1 = 1; j1 = 0; k1 = 0; 01070 i2 = 1; j2 = 1; k2 = 0; 01071 } 01072 else if (x0 >= z0) 01073 { 01074 // X Z Y order 01075 i1 = 1; j1 = 0; k1 = 0; 01076 i2 = 1; j2 = 0; k2 = 1; 01077 } 01078 else 01079 { 01080 // Z X Y order 01081 i1 = 0; j1 = 0; k1 = 1; 01082 i2 = 1; j2 = 0; k2 = 1; 01083 } 01084 } 01085 else 01086 { 01087 if (y0 < z0) 01088 { 01089 // Z Y X order 01090 i1 = 0; j1 = 0; k1 = 1; 01091 i2 = 0; j2 = 1; k2 = 1; 01092 } 01093 else if (x0 < z0) 01094 { 01095 // Y Z X order 01096 i1 = 0; j1 = 1; k1 = 0; 01097 i2 = 0; j2 = 1; k2 = 1; 01098 } 01099 else 01100 { 01101 // Y X Z order 01102 i1 = 0; j1 = 1; k1 = 0; 01103 i2 = 1; j2 = 1; k2 = 0; 01104 } 01105 } 01106 01107 // get offsets for second corner 01108 x1 = x0 - i1 + x->unskew.float64; 01109 y1 = y0 - j1 + x->unskew.float64; 01110 z1 = z0 - k1 + x->unskew.float64; 01111 01112 // get offsets for third corner 01113 x2 = x0 - i2 + 2.0 * x->unskew.float64; 01114 y2 = y0 - j2 + 2.0 * x->unskew.float64; 01115 z2 = z0 - k2 + 2.0 * x->unskew.float64; 01116 01117 // get offsets for last corner 01118 x3 = x0 - 1.0 + 3.0 * x->unskew.float64; 01119 y3 = y0 - 1.0 + 3.0 * x->unskew.float64; 01120 z3 = z0 - 1.0 + 3.0 * x->unskew.float64; 01121 01122 // get the hashed simplex indices of the four simplex corners 01123 ii = i & x->tablemask; 01124 jj = j & x->tablemask; 01125 kk = k & x->tablemask; 01126 01127 // calculate the contribution from the four corners 01128 t0 = 0.6 - (x0 * x0 + y0 * y0 + z0 * z0); 01129 if (t0 < 0) 01130 { 01131 n0 = 0.0; 01132 } 01133 else 01134 { 01135 // get gradient value via permutation table 01136 gi0 = P(ii + P(jj + P(kk))) % 12; 01137 gx = x->gradients[gi0].float64[0]; 01138 gy = x->gradients[gi0].float64[1]; 01139 gz = x->gradients[gi0].float64[2]; 01140 01141 // compute contribution 01142 t0 *= t0; 01143 n0 = t0 * t0 * (gx * x0 + gy * y0 + gz * z0); 01144 } 01145 01146 t1 = 0.6 - (x1 * x1 + y1 * y1 + z1 * z1); 01147 if (t1 < 0) 01148 { 01149 n1 = 0.0; 01150 } 01151 else 01152 { 01153 // get gradient value via permutation table 01154 gi1 = P(ii + i1 + P(jj + j1 + P(kk + k1))) % 12; 01155 gx = x->gradients[gi1].float64[0]; 01156 gy = x->gradients[gi1].float64[1]; 01157 gz = x->gradients[gi1].float64[2]; 01158 01159 // compute contribution 01160 t1 *= t1; 01161 n1 = t1 * t1 * (gx * x1 + gy * y1 + gz * z1); 01162 } 01163 01164 t2 = 0.6 - (x2 * x2 + y2 * y2 + z2 * z2); 01165 if (t2 < 0) 01166 { 01167 n2 = 0.0; 01168 } 01169 else 01170 { 01171 // get gradient value via permutation table 01172 gi2 = P(ii + i2 + P(jj + j2 + P(kk + k2))) % 12; 01173 gx = x->gradients[gi2].float64[0]; 01174 gy = x->gradients[gi2].float64[1]; 01175 gz = x->gradients[gi2].float64[2]; 01176 01177 // compute contribution 01178 t2 *= t2; 01179 n2 = t2 * t2 * (gx * x2 + gy * y2 + gz * z2); 01180 } 01181 01182 t3 = 0.6 - (x3 * x3 + y3 * y3 + z3 * z3); 01183 if (t3 < 0) 01184 { 01185 n3 = 0.0; 01186 } 01187 else 01188 { 01189 // get gradient value via permutation table 01190 gi3 = P(ii + 1 + P(jj + 1 + P(kk + 1))) % 12; 01191 gx = x->gradients[gi3].float64[0]; 01192 gy = x->gradients[gi3].float64[1]; 01193 gz = x->gradients[gi3].float64[2]; 01194 01195 // compute contribution 01196 t3 *= t3; 01197 n3 = t3 * t3 * (gx * x3 + gy * y3 + gz * z3); 01198 } 01199 01200 // add contributions from each corner to get the final noise value, scale to [-1,1] 01201 signal = 32.0 * (n0 + n1 + n2 + n3); 01202 01203 return signal; 01204 01205 } 01206 01207 double jit_functor_noise_simplex_eval_float64_dim4( 01208 t_jit_functor_noise_simplex *x, long dimcount, double *vals) 01209 { 01210 long i, j, k, l; 01211 long ii, jj, kk, ll; 01212 long i1, j1, k1, l1; 01213 long i2, j2, k2, l2; 01214 long i3, j3, k3, l3; 01215 long c, c1, c2, c3, c4, c5, c6; 01216 long gi0, gi1, gi2, gi3, gi4; 01217 01218 double s, t; 01219 double signal; 01220 double xin, yin, zin, win; 01221 double X0, Y0, Z0, W0; 01222 double x0, y0, z0, w0; 01223 double x1, y1, z1, w1; 01224 double x2, y2, z2, w2; 01225 double x3, y3, z3, w3; 01226 double x4, y4, z4, w4; 01227 double gx, gy, gz, gw; 01228 double n0, n1, n2, n3, n4; 01229 double t0, t1, t2, t3, t4; 01230 01231 if (!x || !vals || dimcount < 4) 01232 return JIT_ERR_GENERIC; 01233 01234 xin = vals[0]; 01235 yin = vals[1]; 01236 zin = vals[2]; 01237 win = vals[3]; 01238 01239 // skew the input space to determine which cell of 24 simplices we're in 01240 s = (xin + yin + zin + win) * x->skew.float64; 01241 i = JIT_MATH_F64_FLOOR(xin + s); 01242 j = JIT_MATH_F64_FLOOR(yin + s); 01243 k = JIT_MATH_F64_FLOOR(zin + s); 01244 l = JIT_MATH_F64_FLOOR(win + s); 01245 t = (i + j + k + l) * x->unskew.float64; 01246 01247 // unskew the cell origin back to (x,y,z,w) space 01248 X0 = i - t; 01249 Y0 = j - t; 01250 Z0 = k - t; 01251 W0 = l - t; 01252 01253 // get the distances from the cell origin 01254 x0 = xin - X0; 01255 y0 = yin - Y0; 01256 z0 = zin - Z0; 01257 w0 = win - W0; 01258 01259 // To find out which of the 24 possible simplices we're in, we need to 01260 // determine the magnitude ordering of x0, y0, z0 and w0. 01261 // The method below is a good way of finding the ordering of x,y,z,w and 01262 // then find the correct traversal order for the simplex we�re in. 01263 // First, six pair-wise comparisons are performed between each possible pair 01264 // of the four coordinates, and the results are used to add up binary bits 01265 // for an integer index. 01266 c1 = (x0 > y0) ? 32 : 0; 01267 c2 = (x0 > z0) ? 16 : 0; 01268 c3 = (y0 > z0) ? 8 : 0; 01269 c4 = (x0 > w0) ? 4 : 0; 01270 c5 = (y0 > w0) ? 2 : 0; 01271 c6 = (z0 > w0) ? 1 : 0; 01272 01273 // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order. 01274 c = c1 + c2 + c3 + c4 + c5 + c6; 01275 01276 // the number 3 in the "simplex" array is at the position of the largest coordinate. 01277 i1 = jit_functor_noise_simplex_index[c][0] >= 3 ? 1 : 0; 01278 j1 = jit_functor_noise_simplex_index[c][1] >= 3 ? 1 : 0; 01279 k1 = jit_functor_noise_simplex_index[c][2] >= 3 ? 1 : 0; 01280 l1 = jit_functor_noise_simplex_index[c][3] >= 3 ? 1 : 0; 01281 01282 // the number 2 in the "simplex" array is at the second largest coordinate. 01283 i2 = jit_functor_noise_simplex_index[c][0] >= 2 ? 1 : 0; 01284 j2 = jit_functor_noise_simplex_index[c][1] >= 2 ? 1 : 0; 01285 k2 = jit_functor_noise_simplex_index[c][2] >= 2 ? 1 : 0; 01286 l2 = jit_functor_noise_simplex_index[c][3] >= 2 ? 1 : 0; 01287 01288 // the number 1 in the "simplex" array is at the second smallest coordinate. 01289 i3 = jit_functor_noise_simplex_index[c][0] >= 1 ? 1 : 0; 01290 j3 = jit_functor_noise_simplex_index[c][1] >= 1 ? 1 : 0; 01291 k3 = jit_functor_noise_simplex_index[c][2] >= 1 ? 1 : 0; 01292 l3 = jit_functor_noise_simplex_index[c][3] >= 1 ? 1 : 0; 01293 01294 // skip the fifth corner, it has all coordinate offsets = 1, so no need to look that up. 01295 01296 // get offsets for second corner 01297 x1 = x0 - i1 + x->unskew.float64; 01298 y1 = y0 - j1 + x->unskew.float64; 01299 z1 = z0 - k1 + x->unskew.float64; 01300 w1 = w0 - l1 + x->unskew.float64; 01301 01302 // get offsets for third corner 01303 x2 = x0 - i2 + 2.0 * x->unskew.float64; 01304 y2 = y0 - j2 + 2.0 * x->unskew.float64; 01305 z2 = z0 - k2 + 2.0 * x->unskew.float64; 01306 w2 = w0 - l2 + 2.0 * x->unskew.float64; 01307 01308 // get offsets for fourth corner 01309 x3 = x0 - i3 + 3.0 * x->unskew.float64; 01310 y3 = y0 - j3 + 3.0 * x->unskew.float64; 01311 z3 = z0 - k3 + 3.0 * x->unskew.float64; 01312 w3 = w0 - l3 + 3.0 * x->unskew.float64; 01313 01314 // get offsets for last corner 01315 x4 = x0 - 1.0 + 4.0 * x->unskew.float64; 01316 y4 = y0 - 1.0 + 4.0 * x->unskew.float64; 01317 z4 = z0 - 1.0 + 4.0 * x->unskew.float64; 01318 w4 = w0 - 1.0 + 4.0 * x->unskew.float64; 01319 01320 // get the hashed simplex indices of the five simplex corners 01321 ii = i & x->tablemask; 01322 jj = j & x->tablemask; 01323 kk = k & x->tablemask; 01324 ll = l & x->tablemask; 01325 01326 // calculate the contribution from the five corners 01327 t0 = 0.6 - (x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0); 01328 if (t0 < 0) 01329 { 01330 n0 = 0.0; 01331 } 01332 else 01333 { 01334 // get gradient value via permutation table 01335 gi0 = P(ii + P(jj + P(kk + P(ll)))) % 32; 01336 gx = x->gradients[gi0].float64[0]; 01337 gy = x->gradients[gi0].float64[1]; 01338 gz = x->gradients[gi0].float64[2]; 01339 gw = x->gradients[gi0].float64[3]; 01340 01341 // compute contribution 01342 t0 *= t0; 01343 n0 = t0 * t0 * (gx * x0 + gy * y0 + gz * z0 + gw * w0); 01344 } 01345 01346 t1 = 0.6 - (x1 * x1 + y1 * y1 + z1 * z1 + w1 * w1); 01347 if (t1 < 0) 01348 { 01349 n1 = 0.0; 01350 } 01351 else 01352 { 01353 // get gradient value via permutation table 01354 gi1 = P(ii + i1 + P(jj + j1 + P(kk + k1 + P(ll + l1)))) % 32; 01355 gx = x->gradients[gi1].float64[0]; 01356 gy = x->gradients[gi1].float64[1]; 01357 gz = x->gradients[gi1].float64[2]; 01358 gw = x->gradients[gi1].float64[3]; 01359 01360 // compute contribution 01361 t1 *= t1; 01362 n1 = t1 * t1 * (gx * x1 + gy * y1 + gz * z1 + gw * w1); 01363 } 01364 01365 t2 = 0.6 - (x2 * x2 + y2 * y2 + z2 * z2 + w2 * w2); 01366 if (t2 < 0) 01367 { 01368 n2 = 0.0; 01369 } 01370 else 01371 { 01372 // get gradient value via permutation table 01373 gi2 = P(ii + i2 + P(jj + j2 + P(kk + k2 + P(ll + l2)))) % 32; 01374 gx = x->gradients[gi2].float64[0]; 01375 gy = x->gradients[gi2].float64[1]; 01376 gz = x->gradients[gi2].float64[2]; 01377 gw = x->gradients[gi2].float64[3]; 01378 01379 // compute contribution 01380 t2 *= t2; 01381 n2 = t2 * t2 * (gx * x2 + gy * y2 + gz * z2 + gw * w2); 01382 } 01383 01384 t3 = 0.6 - (x3 * x3 + y3 * y3 + z3 * z3 + w3 * w3); 01385 if (t3 < 0) 01386 { 01387 n3 = 0.0; 01388 } 01389 else 01390 { 01391 // get gradient value via permutation table 01392 gi3 = P(ii + i3 + P(jj + j3 + P(kk + k3 + P(ll + l3)))) % 32; 01393 gx = x->gradients[gi3].float64[0]; 01394 gy = x->gradients[gi3].float64[1]; 01395 gz = x->gradients[gi3].float64[2]; 01396 gw = x->gradients[gi3].float64[3]; 01397 01398 // compute contribution 01399 t3 *= t3; 01400 n3 = t3 * t3 * (gx * x3 + gy * y3 + gz * z3 + gw * w3); 01401 } 01402 01403 t4 = 0.6 - (x4 * x4 + y4 * y4 + z4 * z4 + w4 * w4); 01404 if (t4 < 0) 01405 { 01406 n4 = 0.0; 01407 } 01408 else 01409 { 01410 // get gradient value via permutation table 01411 gi4 = P(ii + 1 + P(jj + 1 + P(kk + 1 + P(ll + 1)))) % 32; 01412 gx = x->gradients[gi4].float64[0]; 01413 gy = x->gradients[gi4].float64[1]; 01414 gz = x->gradients[gi4].float64[2]; 01415 gw = x->gradients[gi4].float64[3]; 01416 01417 // compute contribution 01418 t4 *= t4; 01419 n4 = t4 * t4 * (gx * x4 + gy * y4 + gz * z4 + gw * w4); 01420 } 01421 01422 // sum up and scale the result to cover the range [-1,1] 01423 signal = 27.0 * (n0 + n1 + n2 + n3 + n4); 01424 01425 // remap 01426 return signal; 01427 01428 } 01429 01430 // -------------------------------------------------------------------------- 01431 01432 t_jit_err jit_functor_noise_simplex_dir_init( 01433 t_jit_functor_noise_simplex *x) 01434 { 01435 long i, j; 01436 long gradcount = 0; 01437 t_jit_err err = JIT_ERR_NONE; 01438 01439 if (x->gradients) 01440 jit_disposeptr((char*)x->gradients); 01441 x->gradients = NULL; 01442 01443 jit_rand_setseed(x->seed); 01444 switch (x->dimcount) 01445 { 01446 case 1: 01447 case 2: 01448 { 01449 gradcount = jit_functor_noise_simplex_grad3count; 01450 x->gradients = (t_jit_functor_combined_dimvalue*)jit_newptr(gradcount * sizeof(t_jit_functor_combined_dimvalue)); 01451 if (!x->gradients) 01452 { 01453 err = JIT_ERR_INVALID_PTR; 01454 goto out; 01455 } 01456 01457 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->skew, F2); 01458 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->unskew, G2); 01459 01460 // use 3d gradients 01461 for (i = 0; i < gradcount; i++) 01462 { 01463 for (j = 0; j < x->dimcount; j++) 01464 x->gradients[i].float64[j] = jit_functor_noise_simplex_grad3[i][j] * 4.0f - 1.0f; 01465 } 01466 break; 01467 } 01468 case 3: 01469 { 01470 gradcount = jit_functor_noise_simplex_grad3count; 01471 x->gradients = (t_jit_functor_combined_dimvalue*)jit_newptr(gradcount * sizeof(t_jit_functor_combined_dimvalue)); 01472 if (!x->gradients) 01473 { 01474 err = JIT_ERR_INVALID_PTR; 01475 goto out; 01476 } 01477 01478 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->skew, F3); 01479 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->unskew, G3); 01480 01481 // use 3d gradients 01482 for (i = 0; i < gradcount; i++) 01483 { 01484 for (j = 0; j < x->dimcount; j++) 01485 x->gradients[i].float64[j] = jit_functor_noise_simplex_grad3[i][j] * 4.0f - 1.0f; 01486 } 01487 break; 01488 } 01489 case 4: 01490 { 01491 gradcount = jit_functor_noise_simplex_grad4count; 01492 x->gradients = (t_jit_functor_combined_dimvalue*)jit_newptr(gradcount * sizeof(t_jit_functor_combined_dimvalue)); 01493 if (!x->gradients) 01494 { 01495 err = JIT_ERR_INVALID_PTR; 01496 goto out; 01497 } 01498 01499 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->skew, F4); 01500 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->unskew, G4); 01501 01502 // use 4d gradients 01503 for (i = 0; i < gradcount; i++) 01504 { 01505 for (j = 0; j < x->dimcount; j++) 01506 x->gradients[i].float64[j] = jit_functor_noise_simplex_grad4[i][j] * 4.0f - 1.0f; 01507 } 01508 break; 01509 } 01510 default: 01511 { 01512 // TODO! n-dim simplex gradient dir ? 01513 jit_object_error((t_object *)x,"jit.noise.simplex: dimcount too high -- max dimcount is 4"); 01514 } 01515 }; 01516 01517 // copy and convert 01518 for (i = 0; i < gradcount; i++) 01519 { 01520 for (j = 0; j < x->dimcount; j++) 01521 { 01522 x->gradients[i].float32[j] = (float)x->gradients[i].float64[j]; 01523 x->gradients[i].fixed[j] = DoubleToFixed(x->gradients[i].float64[j]); 01524 } 01525 } 01526 01527 out: 01528 return err; 01529 } 01530 01531 long jit_functor_noise_simplex_eval_fixed( 01532 t_jit_functor_noise_simplex *x, long dimcount, long *vals) 01533 { 01534 long i; 01535 float floatvals[JIT_MATRIX_MAX_DIMCOUNT]; 01536 01537 for(i = 0; i < dimcount; i++) 01538 floatvals[i] = FixedToFloat(vals[i]); 01539 01540 return FloatToFixed(jit_functor_noise_simplex_eval_float32(x, dimcount, floatvals)); 01541 } 01542 01543 float jit_functor_noise_simplex_eval_float32( 01544 t_jit_functor_noise_simplex *x, long dimcount, float *vals) 01545 { 01546 long i; 01547 float v2[2]; 01548 float signal = 0; 01549 01550 // safety 01551 if (!x->p) 01552 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 01553 01554 if (!x->lattice.float32) 01555 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 01556 01557 if (!x->gradients || dimcount != x->dimcount) 01558 { 01559 x->dimcount = dimcount; 01560 jit_functor_noise_simplex_dir_init(x); 01561 } 01562 01563 switch(dimcount) 01564 { 01565 case 1: 01566 v2[0] = vals[0]; 01567 v2[1] = 0; 01568 signal = jit_functor_noise_simplex_eval_float32_dim2(x, 2, v2); 01569 break; 01570 case 2: 01571 signal = jit_functor_noise_simplex_eval_float32_dim2(x, dimcount, vals); 01572 break; 01573 case 3: 01574 signal = jit_functor_noise_simplex_eval_float32_dim3(x, dimcount, vals); 01575 break; 01576 case 4: 01577 signal = jit_functor_noise_simplex_eval_float32_dim4(x, dimcount, vals); 01578 break; 01579 default: 01580 // iterate downward 01581 for (i = 0; i < dimcount;i++) 01582 { 01583 signal = jit_functor_noise_simplex_eval_float32_dim4(x, dimcount-1, vals); 01584 } 01585 }; 01586 01587 if (!x->sign) 01588 signal = 0.5f * signal + 0.5f; 01589 01590 if (x->abs) 01591 signal = JIT_MATH_F32_ABS(signal); 01592 01593 return signal; 01594 } 01595 01596 double jit_functor_noise_simplex_eval_float64( 01597 t_jit_functor_noise_simplex *x, long dimcount, double *vals) 01598 { 01599 long i; 01600 double v2[2]; 01601 double signal = 0; 01602 01603 // safety 01604 if (!x->p) 01605 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 01606 01607 if (!x->lattice.float64) 01608 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 01609 01610 if (!x->gradients || dimcount != x->dimcount) 01611 { 01612 x->dimcount = dimcount; 01613 jit_functor_noise_simplex_dir_init(x); 01614 } 01615 01616 switch(dimcount) 01617 { 01618 case 1: 01619 v2[0] = vals[0]; 01620 v2[1] = 0; 01621 signal = jit_functor_noise_simplex_eval_float64_dim2(x, 2, v2); 01622 break; 01623 case 2: 01624 signal = jit_functor_noise_simplex_eval_float64_dim2(x, dimcount, vals); 01625 break; 01626 case 3: 01627 signal = jit_functor_noise_simplex_eval_float64_dim3(x, dimcount, vals); 01628 break; 01629 case 4: 01630 signal = jit_functor_noise_simplex_eval_float64_dim4(x, dimcount, vals); 01631 break; 01632 default: 01633 // iterate downward 01634 for (i = 0; i < dimcount;i++) 01635 { 01636 signal = jit_functor_noise_simplex_eval_float64_dim4(x, dimcount-1, vals); 01637 } 01638 }; 01639 01640 if (!x->sign) 01641 signal = 0.5 * signal + 0.5; 01642 01643 if (x->abs) 01644 signal = JIT_MATH_F64_ABS(signal); 01645 01646 return signal; 01647 } 01648 01649 // -------------------------------------------------------------------------- 01650
Copyright © 2008, Cycling '74