Max 5 API Reference
00001 /* 00002 * jit.curve.linear.c 00003 * 00004 * Copyright 2001-2005 - Cycling '74 00005 * Derek Gerstmann - derek@cycling74.com 00006 * 00007 * functor for a one-dimensional linearly interpolated curve 00008 * 00009 */ 00010 00011 // -------------------------------------------------------------------------- 00012 00013 #include "jit.common.h" 00014 #include "jit.functor.h" 00015 00016 #include <math.h> 00017 00018 // -------------------------------------------------------------------------- 00019 00020 typedef struct _jit_functor_curve_linear 00021 { 00022 t_jit_object ob; 00023 double *knots; 00024 long knotcount; 00025 } t_jit_functor_curve_linear; 00026 00027 // -------------------------------------------------------------------------- 00028 00029 t_jit_object *jit_functor_curve_linear_new(void); 00030 t_jit_err jit_functor_curve_linear_free(t_jit_functor_curve_linear *x); 00031 t_jit_err jit_functor_curve_linear_setattr_knots(t_jit_functor_curve_linear *x, void *attr, long argc, t_atom *argv); 00032 t_jit_err jit_functor_curve_linear_recalc(t_jit_functor_curve_linear *x); 00033 00034 long jit_functor_curve_linear_eval_fixed(t_jit_functor_curve_linear *x, long dimcount, long *vals); 00035 float jit_functor_curve_linear_eval_float32(t_jit_functor_curve_linear *x, long dimcount, float *vals); 00036 double jit_functor_curve_linear_eval_float64(t_jit_functor_curve_linear *x, long dimcount, double *vals); 00037 long jit_functor_curve_linear_eval_fixed_scalar(t_jit_functor_curve_linear *x, long val); 00038 float jit_functor_curve_linear_eval_float32_scalar(t_jit_functor_curve_linear *x, float val); 00039 double jit_functor_curve_linear_eval_float64_scalar(t_jit_functor_curve_linear *x, double val); 00040 00041 t_class * _jit_functor_curve_linear_class; 00042 00043 // -------------------------------------------------------------------------- 00044 00045 t_jit_err jit_functor_curve_linear_init(void) 00046 { 00047 t_jit_object *attr; 00048 00049 // create the class 00050 _jit_functor_curve_linear_class = jit_class_new("jit_functor_curve_linear", 00051 (method)jit_functor_curve_linear_new,(method)jit_functor_curve_linear_free, 00052 sizeof(t_jit_functor_curve_linear),0L); 00053 00054 // add attribute methods 00055 attr = (t_jit_object*)jit_object_new(_jit_sym_jit_attr_offset, "knots", 00056 _jit_sym_float64, 0, (method)0L,(method)jit_functor_curve_linear_setattr_knots, 00057 calcoffset(t_jit_functor_curve_linear, knots) ); 00058 jit_class_addattr(_jit_functor_curve_linear_class,attr); 00059 00060 // add functor evaluation methods 00061 jit_class_addmethod(_jit_functor_curve_linear_class, 00062 (method)jit_functor_curve_linear_eval_fixed, "evalfixed", A_CANT, 0L); 00063 jit_class_addmethod(_jit_functor_curve_linear_class, 00064 (method)jit_functor_curve_linear_eval_float32, "evalfloat32", A_CANT, 0L); 00065 jit_class_addmethod(_jit_functor_curve_linear_class, 00066 (method)jit_functor_curve_linear_eval_float64, "evalfloat64", A_CANT, 0L); 00067 00068 // add to functor registry and register class 00069 jit_functor_setup_class(_jit_functor_curve_linear_class,"curve","linear"); 00070 jit_class_register(_jit_functor_curve_linear_class); 00071 00072 return JIT_ERR_NONE; 00073 } 00074 00075 t_jit_object *jit_functor_curve_linear_new(void) 00076 { 00077 t_jit_functor_curve_linear *x; 00078 00079 if (x = (t_jit_functor_curve_linear *)jit_object_alloc(_jit_functor_curve_linear_class)) { 00080 00081 // initialization 00082 x->knots = NULL; 00083 x->knotcount = 0; 00084 jit_functor_curve_linear_recalc(x); 00085 } 00086 00087 return (t_jit_object *)x; 00088 } 00089 00090 t_jit_err jit_functor_curve_linear_free(t_jit_functor_curve_linear *x) 00091 { 00092 return JIT_ERR_NONE; 00093 } 00094 00095 t_jit_err jit_functor_curve_linear_setattr_knots( 00096 t_jit_functor_curve_linear *x, void *attr, long argc, t_atom *argv) 00097 { 00098 long i; 00099 double v; 00100 00101 if (x) { 00102 00103 // allocate mem for knot array 00104 if(x->knots) jit_disposeptr((char*)x->knots); 00105 x->knots = NULL; 00106 x->knots = (double*)jit_newptr(argc * sizeof(double)); 00107 if(!x->knots) return JIT_ERR_INVALID_PTR; 00108 00109 // copy all data values 00110 for(i = 0; i < argc; i++) 00111 x->knots[i] = jit_atom_getfloat(argv + i); 00112 00113 // recalc 00114 jit_functor_curve_linear_recalc(x); 00115 00116 return JIT_ERR_NONE; 00117 } 00118 return JIT_ERR_INVALID_PTR; 00119 } 00120 00121 t_jit_err jit_functor_curve_linear_recalc(t_jit_functor_curve_linear *x) 00122 { 00123 // calculate intermediary values for efficiency 00124 return JIT_ERR_NONE; 00125 } 00126 00127 // -------------------------------------------------------------------------- 00128 // vector evaluation functions (calls scalar functions and generates a product of scalar evaluation for each dimension) 00129 // -------------------------------------------------------------------------- 00130 long jit_functor_curve_linear_eval_fixed( 00131 t_jit_functor_curve_linear *x, long dimcount, long *vals) 00132 { 00133 return jit_functor_eval_fixed_with_float64((t_jit_object *)x,dimcount,vals, 00134 (t_jit_functor_float64_sig)jit_functor_curve_linear_eval_float64); 00135 } 00136 00137 float jit_functor_curve_linear_eval_float32( 00138 t_jit_functor_curve_linear *x, long dimcount, float *vals) 00139 { 00140 return jit_functor_eval_float32_with_scalar_product((t_jit_object *)x,dimcount,vals, 00141 (t_jit_functor_float32_scalar_sig)jit_functor_curve_linear_eval_float32_scalar); 00142 } 00143 00144 double jit_functor_curve_linear_eval_float64( 00145 t_jit_functor_curve_linear *x, long dimcount, double *vals) 00146 { 00147 return jit_functor_eval_float64_with_scalar_product((t_jit_object *)x,dimcount,vals, 00148 (t_jit_functor_float64_scalar_sig)jit_functor_curve_linear_eval_float64_scalar); 00149 } 00150 00151 // -------------------------------------------------------------------------- 00152 // scalar evaluation functions 00153 // -------------------------------------------------------------------------- 00154 long jit_functor_curve_linear_eval_fixed_scalar( 00155 t_jit_functor_curve_linear *x, long val) 00156 { 00157 return FloatToFixed(jit_functor_curve_linear_eval_float32_scalar(x,FixedToFloat(val))); 00158 } 00159 00160 float jit_functor_curve_linear_eval_float32_scalar( 00161 t_jit_functor_curve_linear *x, float val) 00162 { 00163 float fx, g[2]; 00164 long span; 00165 long index = 0; 00166 long spancount = x->knotcount - 1; 00167 00168 if (spancount < 1) 00169 return 0.0f; 00170 00171 // find the appropriate 1-point span 00172 fx = CLAMP(val, 0.0f, 1.0f) * spancount; 00173 span = (long)fx; 00174 00175 if (span >= x->knotcount - 1) 00176 span = x->knotcount - 1; 00177 00178 // determine the section to eval 00179 fx -= span; 00180 index += span; 00181 00182 // get the control points 00183 g[0] = x->knots[ 0 + index ]; 00184 g[1] = x->knots[ 1 + index ]; 00185 00186 // linearly interpolate from g0 to g1 00187 return ( g[0] + fx * (g[1] - g[0]) ); 00188 } 00189 00190 double jit_functor_curve_linear_eval_float64_scalar( 00191 t_jit_functor_curve_linear *x, double val) 00192 { 00193 double dx, g[2]; 00194 long span; 00195 long index = 0; 00196 long spancount = x->knotcount - 1; 00197 00198 if (spancount < 1) 00199 return 0.0; 00200 00201 // find the appropriate 1-point span 00202 dx = CLAMP(val, 0.0, 1.0) * spancount; 00203 span = (long)dx; 00204 00205 if (span >= x->knotcount - 1) 00206 span = x->knotcount - 1; 00207 00208 // determine the section to eval 00209 dx -= span; 00210 index += span; 00211 00212 // get the control points 00213 g[0] = x->knots[ 0 + index ]; 00214 g[1] = x->knots[ 1 + index ]; 00215 00216 // linearly interpolate from g0 to g1 00217 return ( g[0] + dx * (g[1] - g[0]) ); 00218 } 00219 // --------------------------------------------------------------------------
Copyright © 2008, Cycling '74