Max 5 API Reference
00001 /* 00002 * Copyright 2001-2005 - Cycling '74 00003 * Derek Gerstmann - derek@cycling74.com 00004 * 00005 * Coherent cell/blocky noise functor object 00006 * 00007 */ 00008 00009 /*************************************************************************/ 00010 00011 #include "jit.noise.h" 00012 #include "jit.common.h" 00013 #include "jit.functor.h" 00014 00015 /*************************************************************************/ 00016 00017 // the cell noise object 00018 typedef struct _jit_functor_noise_cell 00019 { 00020 t_jit_object ob; 00021 00022 // pseudo random permutation table 00023 long *p; 00024 00025 // lattice of uniform values [0-1] 00026 t_jit_functor_combined_dynarray lattice; 00027 00028 // size and mask for tables 00029 long tablesize; 00030 long tablemask; 00031 00032 // seed for random number generation 00033 long seed; 00034 00035 // signed mode (0 = unsigned, 1 = signed) 00036 long sign; 00037 00038 // abs mode (0 = normal, 1 = absolute) 00039 long abs; 00040 00041 } t_jit_functor_noise_cell; 00042 00043 t_class *_jit_functor_noise_cell_class; 00044 00045 /*************************************************************************/ 00046 00047 t_jit_object *jit_functor_noise_cell_new(void); 00048 t_jit_err jit_functor_noise_cell_free(t_jit_functor_noise_cell *x); 00049 00050 t_jit_err jit_functor_noise_cell_abs(t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv); 00051 t_jit_err jit_functor_noise_cell_sign(t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv); 00052 t_jit_err jit_functor_noise_cell_seed(t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv); 00053 t_jit_err jit_functor_noise_cell_tablesize(t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv); 00054 t_jit_err jit_functor_noise_cell_recalc(t_jit_functor_noise_cell *x); 00055 00056 long jit_functor_noise_cell_eval_fixed(t_jit_functor_noise_cell *x, long dimcount, long *vals); 00057 float jit_functor_noise_cell_eval_float32(t_jit_functor_noise_cell *x, long dimcount, float *vals); 00058 double jit_functor_noise_cell_eval_float64(t_jit_functor_noise_cell *x, long dimcount, double *vals); 00059 00060 /*************************************************************************/ 00061 00062 t_jit_err jit_functor_noise_cell_init(void) 00063 { 00064 t_jit_object *attr; 00065 00066 // create class 00067 _jit_functor_noise_cell_class = jit_class_new("jit_functor_noise_cell", 00068 (method)jit_functor_noise_cell_new,(method)jit_functor_noise_cell_free, 00069 sizeof(t_jit_functor_noise_cell), A_GIMME, 0L); 00070 00071 // add attributes 00072 attr = jit_object_new(_jit_sym_jit_attr_offset,"abs",_jit_sym_long,0, 00073 (method)0L,(method)jit_functor_noise_cell_abs,calcoffset(t_jit_functor_noise_cell,abs)); 00074 jit_class_addattr(_jit_functor_noise_cell_class,attr); 00075 attr = jit_object_new(_jit_sym_jit_attr_offset,"sign",_jit_sym_long,0, 00076 (method)0L,(method)jit_functor_noise_cell_sign,calcoffset(t_jit_functor_noise_cell,sign)); 00077 jit_class_addattr(_jit_functor_noise_cell_class,attr); 00078 attr = jit_object_new(_jit_sym_jit_attr_offset,"seed",_jit_sym_long,0, 00079 (method)0L,(method)jit_functor_noise_cell_seed,calcoffset(t_jit_functor_noise_cell,seed)); 00080 jit_class_addattr(_jit_functor_noise_cell_class,attr); 00081 00082 // add methods 00083 jit_class_addmethod(_jit_functor_noise_cell_class, 00084 (method)jit_functor_noise_cell_eval_fixed, "evalfixed", A_CANT, 0L); 00085 jit_class_addmethod(_jit_functor_noise_cell_class, 00086 (method)jit_functor_noise_cell_eval_float32, "evalfloat32", A_CANT, 0L); 00087 jit_class_addmethod(_jit_functor_noise_cell_class, 00088 (method)jit_functor_noise_cell_eval_float64, "evalfloat64", A_CANT, 0L); 00089 00090 // register functor *then* class 00091 jit_functor_setup_class(_jit_functor_noise_cell_class,"noise","cell"); 00092 jit_class_register(_jit_functor_noise_cell_class); 00093 00094 return JIT_ERR_NONE; 00095 } 00096 00097 t_jit_object *jit_functor_noise_cell_new(void) 00098 { 00099 t_jit_functor_noise_cell *x; 00100 00101 if (x = (t_jit_functor_noise_cell *)jit_object_alloc(_jit_functor_noise_cell_class)) { 00102 00103 // initialization 00104 x->p = NULL; 00105 x->lattice.fixed = NULL; 00106 x->lattice.float32 = NULL; 00107 x->lattice.float64 = NULL; 00108 00109 x->seed = JIT_NOISE_DEFAULT_SEED; 00110 x->tablesize = jit_math_roundup_poweroftwo(JIT_NOISE_DEFAULT_TABLESIZE); 00111 x->tablemask = x->tablesize - 1; 00112 00113 x->sign = FALSE; 00114 x->abs = FALSE; 00115 00116 // allocate permutation and uniform tables 00117 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 00118 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 00119 jit_functor_noise_cell_recalc(x); 00120 } 00121 00122 return (t_jit_object *)x; 00123 } 00124 00125 t_jit_err jit_functor_noise_cell_free(t_jit_functor_noise_cell *x) 00126 { 00127 if(x->p) 00128 jit_disposeptr((char*)x->p); 00129 x->p = NULL; 00130 00131 jit_functor_combined_dynarray_destroy(&x->lattice); 00132 return JIT_ERR_NONE; 00133 } 00134 00135 t_jit_err jit_functor_noise_cell_abs( 00136 t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv) 00137 { 00138 long v; 00139 00140 if (x) { 00141 v = jit_atom_getlong(argv); 00142 if (x->abs != v) { 00143 x->abs = (v > 0) ? TRUE : FALSE; 00144 jit_functor_noise_cell_recalc(x); 00145 } 00146 return JIT_ERR_NONE; 00147 } 00148 return JIT_ERR_INVALID_PTR; 00149 } 00150 00151 t_jit_err jit_functor_noise_cell_sign( 00152 t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv) 00153 { 00154 long v; 00155 00156 if (x) { 00157 v = jit_atom_getlong(argv); 00158 if (x->sign != v) { 00159 x->sign = (v > 0) ? TRUE : FALSE; 00160 jit_functor_noise_cell_recalc(x); 00161 } 00162 return JIT_ERR_NONE; 00163 } 00164 return JIT_ERR_INVALID_PTR; 00165 } 00166 00167 t_jit_err jit_functor_noise_cell_seed( 00168 t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv) 00169 { 00170 long v; 00171 00172 if (x) { 00173 v = jit_atom_getlong(argv); 00174 if (x->seed != v) { 00175 x->seed = v; 00176 00177 // allocate permutation and uniform tables 00178 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 00179 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 00180 jit_functor_noise_cell_recalc(x); 00181 } 00182 return JIT_ERR_NONE; 00183 } 00184 return JIT_ERR_INVALID_PTR; 00185 } 00186 00187 t_jit_err jit_functor_noise_cell_tablesize( 00188 t_jit_functor_noise_cell *x, void *attr, long argc, t_atom *argv) 00189 { 00190 long v; 00191 00192 if (x) { 00193 v = jit_atom_getlong(argv); 00194 if (x->tablesize != v && v > 0) { 00195 00196 x->tablesize = jit_math_roundup_poweroftwo(v); 00197 x->tablemask = x->tablesize - 1; 00198 00199 // allocate permutation and uniform tables 00200 jit_functor_noise_permutations_init(&x->p, x->tablesize, x->seed); 00201 jit_functor_noise_lattice_init(&x->lattice, x->tablesize, x->seed); 00202 jit_functor_noise_cell_recalc(x); 00203 } 00204 return JIT_ERR_NONE; 00205 } 00206 return JIT_ERR_INVALID_PTR; 00207 } 00208 00209 t_jit_err jit_functor_noise_cell_recalc(t_jit_functor_noise_cell *x) 00210 { 00211 // calculate intermediary values for efficiency 00212 return JIT_ERR_NONE; 00213 } 00214 00215 long jit_functor_noise_cell_eval_fixed( 00216 t_jit_functor_noise_cell *x, long dimcount, long *vals) 00217 { 00218 long i; 00219 long hash; 00220 long val; 00221 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00222 00223 // convert to long 00224 for(i = 0; i < dimcount; i++) 00225 cell[i] = FixedFloor(vals[i] + JIT_NOISE_FIXED_DISALIGN); 00226 00227 // hash the dimensional value 00228 hash = cell[0]; 00229 for (i = 1; i < dimcount; i++) { 00230 hash += x->p[hash & x->tablemask] + cell[i]; 00231 } 00232 00233 // get value via lut 00234 val = x->lattice.fixed[hash & x->tablemask]; 00235 00236 if(x->sign) 00237 val = FixMul(FloatToFixed(2.0f), val) - FloatToFixed(1.0f); 00238 00239 if(x->abs) 00240 val = JIT_MATH_FIXED_ABS(val); 00241 00242 return val; 00243 } 00244 00245 float jit_functor_noise_cell_eval_float32( 00246 t_jit_functor_noise_cell *x, long dimcount, float *vals) 00247 { 00248 long i; 00249 long hash; 00250 float val; 00251 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00252 00253 // convert to long 00254 for(i = 0; i < dimcount; i++) 00255 cell[i] = JIT_MATH_F32_FLOOR(vals[i] + JIT_NOISE_F32_DISALIGN); 00256 00257 // hash the dimensional value 00258 hash = cell[0]; 00259 for (i = 1; i < dimcount; i++) { 00260 hash += x->p[hash & x->tablemask] + cell[i]; 00261 } 00262 00263 // get value via lut 00264 val = x->lattice.float32[hash & x->tablemask]; 00265 00266 if(x->sign) 00267 val = 2.0f * val - 1.0f; 00268 00269 if(x->abs) 00270 val = JIT_MATH_F32_ABS(val); 00271 00272 return val; 00273 } 00274 00275 double jit_functor_noise_cell_eval_float64( 00276 t_jit_functor_noise_cell *x, long dimcount, double *vals) 00277 { 00278 long i; 00279 long hash; 00280 double val; 00281 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00282 00283 // convert to long 00284 for(i = 0; i < dimcount; i++) 00285 cell[i] = JIT_MATH_F64_FLOOR(vals[i] + JIT_NOISE_F64_DISALIGN); 00286 00287 // hash the dimensional value 00288 hash = cell[0]; 00289 for (i = 1; i < dimcount; i++) { 00290 hash += x->p[hash & x->tablemask] + cell[i]; 00291 } 00292 00293 // get value via lut 00294 val = x->lattice.float64[hash & x->tablemask]; 00295 00296 if(x->sign) 00297 val = 2.0 * val - 1.0; 00298 00299 if(x->abs) 00300 val = JIT_MATH_F64_ABS(val); 00301 00302 return val; 00303 }
Copyright © 2008, Cycling '74