Max 5 API Reference
00001 00002 /* 00003 * Copyright 2001-2005 - Cycling '74 00004 * Derek Gerstmann - derek@cycling74.com 00005 * 00006 * Common utilities for the coherent noise functor objects 00007 * 00008 */ 00009 00010 #include "jit.noise.h" 00011 00012 /*************************************************************************/ 00013 00014 t_jit_err jit_functor_noise_lattice_fixed_init( 00015 double *src, long **dest, long count) 00016 { 00017 long i; 00018 00019 // safety 00020 if(!dest) 00021 return JIT_ERR_INVALID_PTR; 00022 00023 // dispose of any allocated mem 00024 if(*dest) 00025 jit_disposeptr((char*)(*dest)); 00026 (*dest) = NULL; 00027 00028 // allocate 00029 (*dest) = (long*)jit_newptr(count * sizeof(long)); 00030 if(!(*dest)) 00031 return JIT_ERR_INVALID_PTR; 00032 00033 // copy and convert 00034 for(i = 0; i < count; i++) 00035 (*dest)[i] = DoubleToFixed(src[i]); 00036 00037 return JIT_ERR_NONE; 00038 } 00039 00040 t_jit_err jit_functor_noise_lattice_float32_init( 00041 double *src, float **dest, long count) 00042 { 00043 long i; 00044 00045 // safety 00046 if(!dest) 00047 return JIT_ERR_INVALID_PTR; 00048 00049 // dispose of any allocated mem 00050 if((*dest)) 00051 jit_disposeptr((char*)(*dest)); 00052 (*dest) = NULL; 00053 00054 // allocate 00055 (*dest) = (float*)jit_newptr(count * sizeof(float)); 00056 if(!(*dest)) 00057 return JIT_ERR_INVALID_PTR; 00058 00059 // copy and convert 00060 for(i = 0; i < count; i++) 00061 (*dest)[i] = (float)src[i]; 00062 00063 return JIT_ERR_NONE; 00064 } 00065 00066 t_jit_err jit_functor_noise_lattice_float64_init( 00067 double *src, double **dest, long count) 00068 { 00069 long i; 00070 00071 // safety 00072 if(!dest) 00073 return JIT_ERR_INVALID_PTR; 00074 00075 // dispose of any allocated mem 00076 if((*dest)) 00077 jit_disposeptr((char*)(*dest)); 00078 (*dest) = NULL; 00079 00080 // allocate 00081 (*dest) = (double*)jit_newptr(count * sizeof(double)); 00082 if(!(*dest)) 00083 return JIT_ERR_INVALID_PTR; 00084 00085 // copy and convert 00086 for(i = 0; i < count; i++) 00087 (*dest)[i] = (double)src[i]; 00088 00089 return JIT_ERR_NONE; 00090 } 00091 00092 // -------------------------------------------------------------------------- 00093 00094 t_jit_err jit_functor_noise_permutations_init( 00095 long **perm, long count, long seed) 00096 { 00097 long i, j, t, n; 00098 00099 // safety 00100 if(!perm) 00101 return JIT_ERR_INVALID_PTR; 00102 00103 // dispose of allocated mem 00104 if((*perm)) 00105 jit_disposeptr((char*)(*perm)); 00106 (*perm) = NULL; 00107 00108 // allocate 00109 (*perm) = (long*)jit_newptr(count * sizeof(long)); 00110 if(!(*perm)) 00111 return JIT_ERR_INVALID_PTR; 00112 00113 // set the seed 00114 jit_rand_setseed(seed); 00115 00116 // fill the tables with pseudo random integers 00117 for (i = 0; i < count; i++){ 00118 (*perm)[i] = jit_rand() % count; 00119 } 00120 00121 // mess up the order by randomly swapping values 00122 for (n = 0; n < 11; n++) { 00123 for(i = 0; i < count; i++){ 00124 j = jit_rand() % count; 00125 t = (*perm)[j]; 00126 (*perm)[j] = (*perm)[i]; 00127 (*perm)[i] = t; 00128 } 00129 } 00130 00131 return JIT_ERR_NONE; 00132 } 00133 00134 t_jit_err jit_functor_noise_lattice_init( 00135 t_jit_functor_combined_dynarray *lattice, long count, long seed) 00136 { 00137 long i; 00138 t_jit_err err; 00139 double *uniform = NULL; 00140 00141 // safety 00142 if(!lattice) 00143 return JIT_ERR_INVALID_PTR; 00144 00145 // allocate 00146 uniform = (double*)jit_newptr(count * sizeof(double)); 00147 if(!uniform) 00148 return JIT_ERR_INVALID_PTR; 00149 00150 // set the seed 00151 jit_rand_setseed(seed); 00152 00153 // fill table with uniform random values [0-1] 00154 for (i = 0; i < count; i++){ 00155 uniform[i] = (jit_rand() % count) / (double)count; 00156 } 00157 00158 // init all datatypes 00159 err = jit_functor_noise_lattice_fixed_init(uniform, &lattice->fixed, count); 00160 if(err) return err; 00161 00162 err = jit_functor_noise_lattice_float32_init(uniform, &lattice->float32, count); 00163 if(err) return err; 00164 00165 err = jit_functor_noise_lattice_float64_init(uniform, &lattice->float64, count); 00166 if(err) return err; 00167 00168 jit_disposeptr((char *)uniform); 00169 00170 return JIT_ERR_NONE; 00171 } 00172 00173 // -------------------------------------------------------------------------- 00174 00175 t_jit_err jit_functor_noise_evaluator_init( 00176 t_jit_functor_wrapper *x, t_symbol *category, t_symbol *name ) 00177 { 00178 t_jit_err err = JIT_ERR_NONE; 00179 00180 if(x && category && name) 00181 { 00182 // create the new object and get its interface 00183 err = jit_functor_wrapper_init(x, category, name); 00184 } 00185 else 00186 { 00187 err = JIT_ERR_INVALID_PTR; 00188 } 00189 00190 if(err && category && category->s_name && name && name->s_name) 00191 jit_object_error((t_object *)x,"jit.functor.noise: unable to create evaluator object '%s.%s'.", 00192 category->s_name, name->s_name); 00193 00194 return err; 00195 } 00196 00197 // -------------------------------------------------------------------------- 00198 00199 long jit_functor_noise_hash_fixed_eval( 00200 long dimcount, long *vals, long *perm, long mask) 00201 { 00202 long i; 00203 long hash; 00204 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00205 00206 // convert to long 00207 for(i = 0; i < dimcount; i++) 00208 cell[i] = FixedFloor(vals[i]); 00209 00210 // hash the dimensional value 00211 hash = cell[0]; 00212 for (i = 1; i < dimcount; i++) { 00213 hash += perm[hash & mask] + cell[i]; 00214 } 00215 return (hash & mask); 00216 } 00217 00218 long jit_functor_noise_hash_float32_eval( 00219 long dimcount, float *vals, long *perm, long mask) 00220 { 00221 long i; 00222 long hash; 00223 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00224 00225 // convert to long 00226 for(i = 0; i < dimcount; i++) 00227 cell[i] = (long)JIT_MATH_F32_FLOOR(vals[i]); 00228 00229 // hash the dimensional value 00230 hash = cell[0]; 00231 for (i = 1; i < dimcount; i++) { 00232 hash += perm[hash & mask] + cell[i]; 00233 } 00234 return (hash & mask); 00235 } 00236 00237 long jit_functor_noise_hash_float64_eval( 00238 long dimcount, double *vals, long *perm, long mask) 00239 { 00240 long i; 00241 long hash; 00242 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00243 00244 // convert to long 00245 for(i = 0; i < dimcount; i++) 00246 cell[i] = JIT_MATH_F64_FLOOR(vals[i]); 00247 00248 // hash the dimensional value 00249 hash = cell[0]; 00250 for (i = 1; i < dimcount; i++) { 00251 hash += perm[hash & mask] + cell[i]; 00252 } 00253 return (hash & mask); 00254 } 00255 00256 00257 long jit_functor_noise_lattice_fixed_eval( 00258 long dimcount, long *vals, long *perm, 00259 long *lattice, long mask) 00260 { 00261 long i; 00262 long hash; 00263 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00264 00265 // convert to long 00266 for(i = 0; i < dimcount; i++) 00267 cell[i] = FixedFloor(vals[i]); 00268 00269 // hash the dimensional value 00270 hash = cell[0]; 00271 for (i = 1; i < dimcount; i++) { 00272 hash += perm[hash & mask] + cell[i]; 00273 } 00274 00275 // return value via lut 00276 return lattice[hash & mask]; 00277 } 00278 00279 float jit_functor_noise_lattice_float32_eval( 00280 long dimcount, float *vals, long *perm, 00281 float *lattice, long mask) 00282 { 00283 long i; 00284 long hash; 00285 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00286 00287 // convert to long 00288 for(i = 0; i < dimcount; i++) 00289 cell[i] = JIT_MATH_F32_FLOOR(vals[i]); 00290 00291 // hash the dimensional value 00292 hash = cell[0]; 00293 for (i = 1; i < dimcount; i++) { 00294 hash += perm[hash & mask] + cell[i]; 00295 } 00296 00297 // return value via lut 00298 return lattice[hash & mask]; 00299 } 00300 00301 double jit_functor_noise_lattice_float64_eval( 00302 long dimcount, double *vals, long *perm, 00303 double *lattice, long mask) 00304 { 00305 long i; 00306 long hash; 00307 long cell[JIT_MATRIX_MAX_DIMCOUNT]; 00308 00309 // convert to long 00310 for(i = 0; i < dimcount; i++) 00311 cell[i] = JIT_MATH_F64_FLOOR(vals[i]); 00312 00313 // hash the dimensional value 00314 hash = cell[0]; 00315 for (i = 1; i < dimcount; i++) { 00316 hash += perm[hash & mask] + cell[i]; 00317 } 00318 00319 // return value via lut 00320 return lattice[hash & mask]; 00321 }
Copyright © 2008, Cycling '74