Max 5 API Reference
00001 /* 00002 * jit.fractal.multi.ridged.c 00003 * 00004 * Copyright 2001-2005 - Cycling '74 00005 * Derek Gerstmann - derek@cycling74.com 00006 * 00007 * Functor for evaluating a ridged multiplicative multifractal 00008 * using an arbitrary *signed* basis function. The multi in multifractal 00009 * refering to varying/multiple statistical featuristics of the evaluation. 00010 * The ridged refers to the sharp crease formed by taking the absolute value 00011 * of the basis function. 00012 * 00013 */ 00014 00015 // -------------------------------------------------------------------------- 00016 00017 #include "jit.common.h" 00018 #include "jit.functor.h" 00019 #include "jit.fractal.h" 00020 00021 // -------------------------------------------------------------------------- 00022 00023 typedef struct _jit_functor_fractal_turbulence 00024 { 00025 t_jit_object ob; 00026 t_symbol *basis; 00027 long seed; // seed for random rotation matrix 00028 long dimcount; // coord dimension of last evaluation 00029 long rotate; // flag for rotations per octave 00030 t_jit_functor_combined_dimvalue scale; // position scale factor for basis 00031 t_jit_functor_combined_dimvalue offset; // position offset for basis 00032 t_jit_functor_combined_value roughness; // determines the fractal dimension 00033 t_jit_functor_combined_value lacunarity; // spacing or gaps between detail 00034 t_jit_functor_combined_value detail; // determines number of octaves 00035 t_jit_functor_combined_value level; // adjusts the zero offset 00036 t_jit_functor_combined_value result; // scaling factor for result 00037 t_jit_functor_combined_dynarray exponents; // precalc-ed exponents 00038 t_jit_functor_fractal_combined_matrix_dynarray rotations; // precalc-ed rotation matrices per octave 00039 t_jit_functor_wrapper evaluator; // basis functor 00040 00041 } t_jit_functor_fractal_turbulence; 00042 00043 // -------------------------------------------------------------------------- 00044 00045 t_jit_object *jit_functor_fractal_turbulence_new(void); 00046 t_jit_err jit_functor_fractal_turbulence_free(t_jit_functor_fractal_turbulence *x); 00047 00048 t_jit_err jit_functor_fractal_turbulence_basis(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00049 t_jit_err jit_functor_fractal_turbulence_seed(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00050 t_jit_err jit_functor_fractal_turbulence_rotate(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00051 t_jit_err jit_functor_fractal_turbulence_scale(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00052 t_jit_err jit_functor_fractal_turbulence_offset(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00053 t_jit_err jit_functor_fractal_turbulence_roughness(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00054 t_jit_err jit_functor_fractal_turbulence_lacunarity(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00055 t_jit_err jit_functor_fractal_turbulence_detail(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00056 t_jit_err jit_functor_fractal_turbulence_level(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00057 t_jit_err jit_functor_fractal_turbulence_result(t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv); 00058 t_jit_err jit_functor_fractal_turbulence_recalc(t_jit_functor_fractal_turbulence *x); 00059 00060 long jit_functor_fractal_turbulence_eval_fixed(t_jit_functor_fractal_turbulence *x, long dimcount, long *vals); 00061 float jit_functor_fractal_turbulence_eval_float32(t_jit_functor_fractal_turbulence *x, long dimcount, float *vals); 00062 double jit_functor_fractal_turbulence_eval_float64(t_jit_functor_fractal_turbulence *x, long dimcount, double *vals); 00063 t_jit_object *jit_functor_fractal_turbulence_subfunctor(t_jit_functor_fractal_turbulence *x, t_symbol *name); 00064 00065 t_class * _jit_functor_fractal_turbulence_class; 00066 t_symbol * ps_jit_functor_fractal_turbulence_basis; 00067 // -------------------------------------------------------------------------- 00068 00069 t_jit_err jit_functor_fractal_turbulence_init(void) 00070 { 00071 t_jit_object *attr; 00072 00073 // create functor class 00074 _jit_functor_fractal_turbulence_class = jit_class_new("jit_functor_fractal_turbulence", 00075 (method)jit_functor_fractal_turbulence_new,(method)jit_functor_fractal_turbulence_free, 00076 sizeof(t_jit_functor_fractal_turbulence),0L); 00077 00078 // add attribute methods 00079 attr = jit_object_new(_jit_sym_jit_attr_offset,"type",_jit_sym_symbol,0, 00080 (method)0L,(method)jit_functor_fractal_turbulence_basis, 00081 calcoffset(t_jit_functor_fractal_turbulence,basis)); 00082 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00083 attr = jit_object_new(_jit_sym_jit_attr_offset,"basis",_jit_sym_symbol,0, 00084 (method)0L,(method)jit_functor_fractal_turbulence_basis, 00085 calcoffset(t_jit_functor_fractal_turbulence,basis)); 00086 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00087 attr = jit_object_new(_jit_sym_jit_attr_offset,"seed",_jit_sym_long,0, 00088 (method)0L,(method)jit_functor_fractal_turbulence_seed, 00089 calcoffset(t_jit_functor_fractal_turbulence,seed)); 00090 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00091 attr = jit_object_new(_jit_sym_jit_attr_offset,"rotate",_jit_sym_long,0, 00092 (method)0L,(method)jit_functor_fractal_turbulence_rotate, 00093 calcoffset(t_jit_functor_fractal_turbulence,rotate)); 00094 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00095 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"scale",_jit_sym_float64,JIT_MATRIX_MAX_DIMCOUNT,0, 00096 (method)0L,(method)jit_functor_fractal_turbulence_scale, 00097 calcoffset(t_jit_functor_fractal_turbulence,scale.dimcount), 00098 calcoffset(t_jit_functor_fractal_turbulence,scale.float64)); 00099 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00100 attr = jit_object_new(_jit_sym_jit_attr_offset_array,"offset",_jit_sym_float64,JIT_MATRIX_MAX_DIMCOUNT,0, 00101 (method)0L,(method)jit_functor_fractal_turbulence_offset, 00102 calcoffset(t_jit_functor_fractal_turbulence,offset.dimcount), 00103 calcoffset(t_jit_functor_fractal_turbulence,offset.float64)); 00104 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00105 attr = jit_object_new(_jit_sym_jit_attr_offset,"roughness",_jit_sym_float64,0, 00106 (method)0L,(method)jit_functor_fractal_turbulence_roughness, 00107 calcoffset(t_jit_functor_fractal_turbulence,roughness.float64)); 00108 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00109 attr = jit_object_new(_jit_sym_jit_attr_offset,"lacunarity",_jit_sym_float64,0, 00110 (method)0L,(method)jit_functor_fractal_turbulence_lacunarity, 00111 calcoffset(t_jit_functor_fractal_turbulence,lacunarity.float64)); 00112 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00113 attr = jit_object_new(_jit_sym_jit_attr_offset,"detail",_jit_sym_float64,0, 00114 (method)0L,(method)jit_functor_fractal_turbulence_detail, 00115 calcoffset(t_jit_functor_fractal_turbulence,detail.float64)); 00116 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00117 attr = jit_object_new(_jit_sym_jit_attr_offset,"level",_jit_sym_float64,0, 00118 (method)0L,(method)jit_functor_fractal_turbulence_level, 00119 calcoffset(t_jit_functor_fractal_turbulence,level.float64)); 00120 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00121 attr = jit_object_new(_jit_sym_jit_attr_offset,"result",_jit_sym_float64,0, 00122 (method)0L,(method)jit_functor_fractal_turbulence_result, 00123 calcoffset(t_jit_functor_fractal_turbulence,result.float64)); 00124 jit_class_addattr(_jit_functor_fractal_turbulence_class,attr); 00125 00126 // add evaluation methods 00127 jit_class_addmethod(_jit_functor_fractal_turbulence_class, 00128 (method)jit_functor_fractal_turbulence_eval_fixed, "evalfixed", A_CANT, 0L); 00129 jit_class_addmethod(_jit_functor_fractal_turbulence_class, 00130 (method)jit_functor_fractal_turbulence_eval_float32, "evalfloat32", A_CANT, 0L); 00131 jit_class_addmethod(_jit_functor_fractal_turbulence_class, 00132 (method)jit_functor_fractal_turbulence_eval_float64, "evalfloat64", A_CANT, 0L); 00133 00134 // exposing subfunctor to set/getattr interface 00135 jit_class_addmethod(_jit_functor_fractal_turbulence_class, 00136 (method)jit_functor_fractal_turbulence_subfunctor, "subfunctor", A_CANT, 0L); 00137 00138 // important to add last for subclassing methods 00139 jit_functor_setup_class(_jit_functor_fractal_turbulence_class,"fractal","turbulence"); 00140 jit_class_register(_jit_functor_fractal_turbulence_class); 00141 00142 // generate symbols 00143 ps_jit_functor_fractal_turbulence_basis = gensym("basis"); 00144 return JIT_ERR_NONE; 00145 } 00146 00147 t_jit_object *jit_functor_fractal_turbulence_new(void) 00148 { 00149 long i; 00150 t_jit_functor_fractal_turbulence *x; 00151 t_atom a; 00152 00153 if (x = (t_jit_functor_fractal_turbulence *)jit_object_alloc(_jit_functor_fractal_turbulence_class)) { 00154 00155 // initialization 00156 x->basis = gensym("noise.gradient"); 00157 x->seed = JIT_FRACTAL_DEFAULT_SEED; 00158 x->dimcount = 2; 00159 x->rotate = FALSE; 00160 00161 // set all data members 00162 for(i = 0; i < JIT_MATRIX_MAX_DIMCOUNT; i++) 00163 { 00164 x->scale.float64[i] = JIT_FRACTAL_DEFAULT_SCALE; 00165 x->scale.float32[i] = JIT_FRACTAL_DEFAULT_SCALE; 00166 x->scale.fixed[i] = FloatToFixed(x->scale.float32[i]); 00167 00168 x->offset.float64[i] = JIT_FRACTAL_DEFAULT_OFFSET; 00169 x->offset.float32[i] = JIT_FRACTAL_DEFAULT_OFFSET; 00170 x->offset.fixed[i] = FloatToFixed(x->offset.float32[i]); 00171 } 00172 00173 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->roughness, JIT_FRACTAL_DEFAULT_ROUGHNESS); 00174 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->lacunarity, JIT_FRACTAL_DEFAULT_LACUNARITY); 00175 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->detail, JIT_FRACTAL_DEFAULT_DETAIL); 00176 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->level, JIT_FRACTAL_DEFAULT_LEVEL); 00177 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->result, JIT_FRACTAL_DEFAULT_RESULT); 00178 00179 x->exponents.float64 = NULL; 00180 x->exponents.float32 = NULL; 00181 x->exponents.fixed = NULL; 00182 x->exponents.count = 0; 00183 00184 x->rotations.m = NULL; 00185 x->rotations.count = 0; 00186 00187 x->evaluator.ob = NULL; 00188 x->evaluator.fm = NULL; 00189 00190 jit_functor_wrapper_init(&x->evaluator, NULL, x->basis); 00191 00192 // disable signed evaluation 00193 jit_atom_setlong(&a, 1); 00194 jit_object_method(x->evaluator.ob, gensym("sign"), 1, &a); 00195 00196 // init exponents 00197 jit_functor_fractal_exponents_init( 00198 &x->exponents, x->detail.float64, 00199 x->roughness.float64, x->lacunarity.float64); 00200 00201 jit_functor_fractal_turbulence_recalc(x); 00202 } 00203 00204 return (t_jit_object *)x; 00205 } 00206 00207 t_jit_err jit_functor_fractal_turbulence_free(t_jit_functor_fractal_turbulence *x) 00208 { 00209 // dispose allocated mem 00210 jit_functor_combined_dynarray_destroy(&x->exponents); 00211 jit_functor_fractal_rotations_destroy(&x->rotations); 00212 00213 if(x->evaluator.ob) 00214 jit_object_free(x->evaluator.ob); 00215 x->evaluator.ob = NULL; 00216 x->evaluator.fm = NULL; 00217 00218 return JIT_ERR_NONE; 00219 } 00220 00221 t_jit_err jit_functor_fractal_turbulence_basis( 00222 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00223 { 00224 t_jit_err err; 00225 t_symbol *v; 00226 t_atom a; 00227 00228 if (x) { 00229 v = jit_atom_getsym(argv); 00230 if (x->basis != v) { 00231 x->basis = v; 00232 00233 err = jit_functor_wrapper_init(&x->evaluator, NULL, x->basis); 00234 if(err) return err; 00235 00236 jit_atom_setlong(&a, 1); 00237 err = (t_jit_err) jit_object_method(x->evaluator.ob, gensym("sign"), 1, &a); 00238 if(err) 00239 jit_object_post((t_object *)x,"jit.fractal.multi: selected evaluator may not produce an appropriate output range"); 00240 00241 jit_functor_fractal_turbulence_recalc(x); 00242 } 00243 return JIT_ERR_NONE; 00244 } 00245 return JIT_ERR_INVALID_PTR; 00246 } 00247 00248 t_jit_err jit_functor_fractal_turbulence_seed( 00249 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00250 { 00251 long v; 00252 00253 if (x) { 00254 v = jit_atom_getlong(argv); 00255 if (x->seed != v) { 00256 x->seed = v; 00257 jit_functor_fractal_rotations_init(&x->rotations, x->detail.float64, x->dimcount, x->seed); 00258 jit_functor_fractal_turbulence_recalc(x); 00259 } 00260 return JIT_ERR_NONE; 00261 } 00262 return JIT_ERR_INVALID_PTR; 00263 } 00264 00265 t_jit_err jit_functor_fractal_turbulence_rotate( 00266 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00267 { 00268 long v; 00269 00270 if (x) { 00271 v = jit_atom_getlong(argv); 00272 if (x->rotate != v) { 00273 x->rotate = (v > 0) ? TRUE : FALSE; 00274 jit_functor_fractal_rotations_init(&x->rotations, x->detail.float64, x->dimcount, x->seed); 00275 jit_functor_fractal_turbulence_recalc(x); 00276 } 00277 return JIT_ERR_NONE; 00278 } 00279 return JIT_ERR_INVALID_PTR; 00280 } 00281 00282 t_jit_err jit_functor_fractal_turbulence_offset( 00283 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00284 { 00285 long i; 00286 double v; 00287 char changed = FALSE; // changed flag 00288 00289 if (x) { 00290 for(i = 0; i < argc && i < JIT_MATRIX_MAX_DIMCOUNT; i++) 00291 { 00292 v = jit_atom_getfloat(argv+i); 00293 if(x->offset.float64[i] != v) 00294 { 00295 // copy and convert 00296 JIT_FUNCTOR_COMBINED_ARRAY_SETALL(i, x->offset, v); 00297 changed = TRUE; 00298 } 00299 } 00300 if(changed) { 00301 x->offset.dimcount = argc; 00302 jit_functor_fractal_turbulence_recalc(x); 00303 } 00304 return JIT_ERR_NONE; 00305 } 00306 return JIT_ERR_INVALID_PTR; 00307 } 00308 00309 t_jit_err jit_functor_fractal_turbulence_scale( 00310 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00311 { 00312 long i; 00313 double v; 00314 char changed = FALSE; // changed flag 00315 00316 if (x) { 00317 for(i = 0; i < argc && i < JIT_MATRIX_MAX_DIMCOUNT; i++) 00318 { 00319 v = jit_atom_getfloat(argv+i); 00320 if(x->scale.float64[i] != v) 00321 { 00322 // copy and convert 00323 JIT_FUNCTOR_COMBINED_ARRAY_SETALL(i, x->scale, v); 00324 changed = TRUE; 00325 } 00326 } 00327 if(changed) { 00328 x->scale.dimcount = argc; 00329 jit_functor_fractal_turbulence_recalc(x); 00330 } 00331 return JIT_ERR_NONE; 00332 } 00333 return JIT_ERR_INVALID_PTR; 00334 } 00335 00336 t_jit_err jit_functor_fractal_turbulence_roughness( 00337 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00338 { 00339 double v; 00340 00341 if (x) { 00342 v = jit_atom_getfloat(argv); 00343 if (x->roughness.float64 != v) { 00344 00345 // copy and convert 00346 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->roughness, v); 00347 00348 // init exponents 00349 jit_functor_fractal_exponents_init( 00350 &x->exponents, x->detail.float64, 00351 x->roughness.float64, x->lacunarity.float64); 00352 00353 jit_functor_fractal_turbulence_recalc(x); 00354 } 00355 return JIT_ERR_NONE; 00356 } 00357 return JIT_ERR_INVALID_PTR; 00358 } 00359 00360 t_jit_err jit_functor_fractal_turbulence_lacunarity( 00361 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00362 { 00363 double v; 00364 00365 if (x) { 00366 v = jit_atom_getfloat(argv); 00367 if (x->lacunarity.float64 != v && v > 0.0) { 00368 00369 // copy and convert 00370 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->lacunarity, v); 00371 00372 // init exponents 00373 jit_functor_fractal_exponents_init( 00374 &x->exponents, x->detail.float64, 00375 x->roughness.float64, x->lacunarity.float64); 00376 00377 jit_functor_fractal_turbulence_recalc(x); 00378 } 00379 return JIT_ERR_NONE; 00380 } 00381 return JIT_ERR_INVALID_PTR; 00382 } 00383 00384 t_jit_err jit_functor_fractal_turbulence_detail( 00385 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00386 { 00387 double v; 00388 00389 if (x) { 00390 v = jit_atom_getfloat(argv); 00391 if (x->detail.float64 != v && v > 0.0 && v < JIT_FRACTAL_MAX_OCTAVES) { 00392 00393 // copy and convert 00394 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->detail, v); 00395 00396 // init exponents 00397 jit_functor_fractal_exponents_init( 00398 &x->exponents, x->detail.float64, 00399 x->roughness.float64, x->lacunarity.float64); 00400 00401 // init rotation matrices 00402 if(x->rotate) 00403 jit_functor_fractal_rotations_init( 00404 &x->rotations, x->detail.float64, x->dimcount, x->seed); 00405 00406 jit_functor_fractal_turbulence_recalc(x); 00407 } 00408 return JIT_ERR_NONE; 00409 } 00410 return JIT_ERR_INVALID_PTR; 00411 } 00412 00413 t_jit_err jit_functor_fractal_turbulence_level( 00414 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00415 { 00416 double v; 00417 00418 if (x) { 00419 v = jit_atom_getfloat(argv); 00420 if (x->level.float64 != v) { 00421 00422 // copy and convert 00423 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->level, v); 00424 jit_functor_fractal_turbulence_recalc(x); 00425 } 00426 return JIT_ERR_NONE; 00427 } 00428 return JIT_ERR_INVALID_PTR; 00429 } 00430 00431 t_jit_err jit_functor_fractal_turbulence_result( 00432 t_jit_functor_fractal_turbulence *x, void *attr, long argc, t_atom *argv) 00433 { 00434 double v; 00435 00436 if (x) { 00437 v = jit_atom_getfloat(argv); 00438 if (x->result.float64 != v) { 00439 00440 // copy and convert 00441 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->result, v); 00442 jit_functor_fractal_turbulence_recalc(x); 00443 } 00444 return JIT_ERR_NONE; 00445 } 00446 return JIT_ERR_INVALID_PTR; 00447 } 00448 00449 t_jit_err jit_functor_fractal_turbulence_recalc( 00450 t_jit_functor_fractal_turbulence *x) 00451 { 00452 // calculate intermediary values for efficiency 00453 return JIT_ERR_NONE; 00454 } 00455 00456 // -------------------------------------------------------------------------- 00457 // vector evaluation functions 00458 // -------------------------------------------------------------------------- 00459 long jit_functor_fractal_turbulence_eval_fixed( 00460 t_jit_functor_fractal_turbulence *x, long dimcount, long *vals) 00461 { 00462 /* TODO! */ 00463 return jit_functor_eval_fixed_with_float32((t_jit_object *)x,dimcount,vals, 00464 (t_jit_functor_float32_sig)jit_functor_fractal_turbulence_eval_float32); 00465 } 00466 00467 float jit_functor_fractal_turbulence_eval_float32( 00468 t_jit_functor_fractal_turbulence *x, long dimcount, float *vals) 00469 { 00470 long i, j, k; 00471 long count = (long)x->detail.float32; 00472 float pos[JIT_MATRIX_MAX_DIMCOUNT]; 00473 float rot[JIT_MATRIX_MAX_DIMCOUNT]; 00474 00475 float signal; 00476 float weight; 00477 float basis; 00478 float value; 00479 float rem; 00480 00481 // safety 00482 if(!x->evaluator.ob || !x->evaluator.fm || !x->exponents.float64) 00483 return 0.0f; 00484 00485 // reinit rotations if dimcount has changed 00486 if(dimcount != x->dimcount && x->rotate) { 00487 x->dimcount = dimcount; 00488 jit_functor_fractal_rotations_init(&x->rotations, x->detail.float64, x->dimcount, x->seed); 00489 } 00490 00491 // scale and offset position 00492 for( i = 0; i < dimcount; i++) 00493 pos[i] = (vals[i] + x->offset.float32[i]) * x->scale.float32[i]; 00494 00495 // process each integer octave 00496 value = 0.0f; 00497 for ( i = 0; i < count; i++ ) 00498 { 00499 // evaluate the basis function to create the spectrum 00500 basis = x->evaluator.fm->evalfloat32(x->evaluator.ob, dimcount, pos ); 00501 00502 // determine the signal contribution 00503 value += (JIT_MATH_F64_ABS(basis) + x->level.float32) * x->exponents.float32[i]; 00504 00505 // multipy in the lacunarity to create the gaps between octaves 00506 for( j = 0; j < dimcount; j++ ) 00507 pos[j] *= x->lacunarity.float32; 00508 00509 // apply rotation to reduce alignment artifacts 00510 if(i > 0 && x->rotate) { 00511 for( j = 0; j < dimcount; j++ ) { 00512 rot[j] = 0; 00513 for( k = 0; k < dimcount; k++ ) { 00514 rot[j] += x->rotations.m[i-1].float32[j][k] * pos[k]; 00515 } 00516 } 00517 for( j = 0; j < dimcount; j++ ) 00518 pos[j] = rot[j]; 00519 } 00520 } 00521 00522 // take care of remainder in detail using i from above 00523 rem = x->detail.float32 - count; 00524 if ( rem ) 00525 { 00526 basis = x->evaluator.fm->evalfloat32(x->evaluator.ob, dimcount, pos ); 00527 value += rem * (JIT_MATH_F32_ABS(basis) + x->level.float32) * x->exponents.float32[i]; 00528 } 00529 00530 // weight the result 00531 return value * x->result.float32; 00532 } 00533 00534 double jit_functor_fractal_turbulence_eval_float64( 00535 t_jit_functor_fractal_turbulence *x, long dimcount, double *vals) 00536 { 00537 long i, j, k; 00538 long count = (long)x->detail.float64; 00539 double pos[JIT_MATRIX_MAX_DIMCOUNT]; 00540 double rot[JIT_MATRIX_MAX_DIMCOUNT]; 00541 00542 double signal; 00543 double weight; 00544 double basis; 00545 double value; 00546 double rem; 00547 00548 // safety 00549 if(!x->evaluator.ob || !x->evaluator.fm || !x->exponents.float64) 00550 return 0.0; 00551 00552 // reinit rotations if dimcount has changed 00553 if(dimcount != x->dimcount && x->rotate) { 00554 x->dimcount = dimcount; 00555 jit_functor_fractal_rotations_init(&x->rotations, x->detail.float64, x->dimcount, x->seed); 00556 } 00557 00558 // scale and offset position 00559 for( i = 0; i < dimcount; i++) 00560 pos[i] = (vals[i] + x->offset.float64[i]) * x->scale.float64[i]; 00561 00562 // process each integer octave 00563 value = 0.0; 00564 for ( i = 0; i < count; i++ ) 00565 { 00566 // evaluate the basis function to create the spectrum 00567 basis = x->evaluator.fm->evalfloat64(x->evaluator.ob, dimcount, pos ); 00568 00569 // determine the signal contribution 00570 value += (JIT_MATH_F64_ABS(basis) + x->level.float64) * x->exponents.float64[i]; 00571 00572 // multipy in the lacunarity to create the gaps between octaves 00573 for( j = 0; j < dimcount; j++ ) 00574 pos[j] *= x->lacunarity.float64; 00575 00576 // apply rotation to reduce alignment artifacts 00577 if(i > 0 && x->rotate) { 00578 for( j = 0; j < dimcount; j++ ) { 00579 rot[j] = 0; 00580 for( k = 0; k < dimcount; k++ ) { 00581 rot[j] += x->rotations.m[i-1].float64[j][k] * pos[k]; 00582 } 00583 } 00584 for( j = 0; j < dimcount; j++ ) 00585 pos[j] = rot[j]; 00586 } 00587 } 00588 00589 // take care of remainder in detail using i from above 00590 rem = x->detail.float64 - count; 00591 if ( rem ) 00592 { 00593 basis = x->evaluator.fm->evalfloat64(x->evaluator.ob, dimcount, pos ); 00594 value += rem * (JIT_MATH_F64_ABS(basis) + x->level.float64) * x->exponents.float64[i]; 00595 } 00596 00597 // weight the result 00598 return value * x->result.float64; 00599 } 00600 00601 // -------------------------------------------------------------------------- 00602 00603 t_jit_object *jit_functor_fractal_turbulence_subfunctor( 00604 t_jit_functor_fractal_turbulence *x, t_symbol *name) 00605 { 00606 if (name == ps_jit_functor_fractal_turbulence_basis) 00607 return (t_jit_object *)x->evaluator.ob; 00608 else 00609 return NULL; 00610 } 00611 00612 // --------------------------------------------------------------------------
Copyright © 2008, Cycling '74