Max 5 API Reference
00001 /* 00002 * jit.transfer.ease.c 00003 * 00004 * Copyright 2001-2005 - Cycling '74 00005 * Derek Gerstmann - derek@cycling74.com 00006 * 00007 * Base on Richard Parent's ease-in/ease-out curve presented in 00008 * his textbook on Computer Animation, 2nd Ed. 00009 * 00010 * This method uses parabolic blending at the end points * 00011 * First leg has constant acceleration from 0 to v during time 0 to t1 00012 * Second leg has constant velocity of v during time t1 to t2 00013 * Third leg has constant deceleration from v to 0 during time t2 to 1 00014 * These are integrated to get the 'distance' traveled at any time 00015 */ 00016 00017 // -------------------------------------------------------------------------- 00018 00019 #include "jit.common.h" 00020 #include "jit.functor.h" 00021 00022 // -------------------------------------------------------------------------- 00023 00024 typedef struct _jit_functor_transfer_ease 00025 { 00026 t_jit_object ob; 00027 t_jit_functor_combined_value start; 00028 t_jit_functor_combined_value end; 00029 } t_jit_functor_transfer_ease; 00030 00031 // -------------------------------------------------------------------------- 00032 00033 t_jit_object *jit_functor_transfer_ease_new(void); 00034 t_jit_err jit_functor_transfer_ease_free(t_jit_functor_transfer_ease *x); 00035 t_jit_err jit_functor_transfer_ease_setattr_beta(t_jit_functor_transfer_ease *x, void *attr, long argc, t_atom *argv); 00036 t_jit_err jit_functor_transfer_ease_recalc(t_jit_functor_transfer_ease *x); 00037 00038 long jit_functor_transfer_ease_eval_fixed(t_jit_functor_transfer_ease *x, long dimcount, long *vals); 00039 float jit_functor_transfer_ease_eval_float32(t_jit_functor_transfer_ease *x, long dimcount, float *vals); 00040 double jit_functor_transfer_ease_eval_float64(t_jit_functor_transfer_ease *x, long dimcount, double *vals); 00041 long jit_functor_transfer_ease_eval_fixed_scalar(t_jit_functor_transfer_ease *x, long val); 00042 float jit_functor_transfer_ease_eval_float32_scalar(t_jit_functor_transfer_ease *x, float val); 00043 double jit_functor_transfer_ease_eval_float64_scalar(t_jit_functor_transfer_ease *x, double val); 00044 00045 t_class * _jit_functor_transfer_ease_class; 00046 00047 // -------------------------------------------------------------------------- 00048 00049 t_jit_err jit_functor_transfer_ease_init(void) 00050 { 00051 t_jit_object *attr; 00052 00053 // create the functor class 00054 _jit_functor_transfer_ease_class = jit_class_new("jit_functor_transfer_ease", 00055 (method)jit_functor_transfer_ease_new,(method)jit_functor_transfer_ease_free, 00056 sizeof(t_jit_functor_transfer_ease),0L); 00057 00058 // add attribute methods 00059 attr = jit_object_new(_jit_sym_jit_attr_offset,"start",_jit_sym_float64,0, 00060 (method)0L,(method)jit_functor_transfer_ease_setattr_start, 00061 calcoffset(t_jit_functor_transfer_ease,start.float64)); 00062 jit_class_addattr(_jit_functor_transfer_ease_class,attr); 00063 attr = jit_object_new(_jit_sym_jit_attr_offset,"end",_jit_sym_float64,0, 00064 (method)0L,(method)jit_functor_transfer_ease_setattr_end, 00065 calcoffset(t_jit_functor_transfer_ease,end.float64)); 00066 jit_class_addattr(_jit_functor_transfer_ease_class,attr); 00067 00068 // add evaluation methods 00069 jit_class_addmethod(_jit_functor_transfer_ease_class, 00070 (method)jit_functor_transfer_ease_eval_fixed, "evalfixed", A_CANT, 0L); 00071 jit_class_addmethod(_jit_functor_transfer_ease_class, 00072 (method)jit_functor_transfer_ease_eval_float32, "evalfloat32", A_CANT, 0L); 00073 jit_class_addmethod(_jit_functor_transfer_ease_class, 00074 (method)jit_functor_transfer_ease_eval_float64, "evalfloat64", A_CANT, 0L); 00075 00076 // important to add last for subclassing methods 00077 jit_functor_setup_class(_jit_functor_transfer_ease_class,"transfer","ease"); 00078 jit_class_register(_jit_functor_transfer_ease_class); 00079 00080 return JIT_ERR_NONE; 00081 } 00082 00083 t_jit_object *jit_functor_transfer_ease_new(void) 00084 { 00085 t_jit_functor_transfer_ease *x; 00086 00087 if (x = (t_jit_functor_transfer_ease *)jit_object_alloc(_jit_functor_transfer_ease_class)) { 00088 00089 // initialization 00090 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->start, 0.0); 00091 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->end, 1.0); 00092 jit_functor_transfer_ease_recalc(x); 00093 } 00094 00095 return (t_jit_object *)x; 00096 } 00097 00098 t_jit_err jit_functor_transfer_ease_free(t_jit_functor_transfer_ease *x) 00099 { 00100 return JIT_ERR_NONE; 00101 } 00102 00103 t_jit_err jit_functor_transfer_ease_setattr_start( 00104 t_jit_functor_transfer_ease *x, void *attr, long argc, t_atom *argv) 00105 { 00106 double v; 00107 00108 if (x) 00109 { 00110 v = jit_atom_getfloat(argv); 00111 if (x->start.float64 != v) 00112 { 00113 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->start, v); 00114 jit_functor_transfer_ease_recalc(x); 00115 } 00116 return JIT_ERR_NONE; 00117 } 00118 return JIT_ERR_INVALID_PTR; 00119 } 00120 00121 t_jit_err jit_functor_transfer_ease_setattr_end( 00122 t_jit_functor_transfer_ease *x, void *attr, long argc, t_atom *argv) 00123 { 00124 double v; 00125 00126 if (x) 00127 { 00128 v = jit_atom_getfloat(argv); 00129 if (x->end.float64 != v) 00130 { 00131 JIT_FUNCTOR_COMBINED_VALUE_SETALL(x->end, v); 00132 jit_functor_transfer_ease_recalc(x); 00133 } 00134 return JIT_ERR_NONE; 00135 } 00136 return JIT_ERR_INVALID_PTR; 00137 } 00138 00139 t_jit_err jit_functor_transfer_ease_recalc(t_jit_functor_transfer_ease *x) 00140 { 00141 // calculate intermediary values for efficiency 00142 return JIT_ERR_NONE; 00143 } 00144 00145 // -------------------------------------------------------------------------- 00146 // vector evaluation functions (calls scalar functions and generates a product of scalar evaluation for each dimension) 00147 // -------------------------------------------------------------------------- 00148 long jit_functor_transfer_ease_eval_fixed(t_jit_functor_transfer_ease *x, long dimcount, long *vals) 00149 { 00150 return jit_functor_eval_fixed_with_float64((t_jit_object *)x,dimcount,vals, 00151 (t_jit_functor_float64_sig)jit_functor_transfer_ease_eval_float64); 00152 } 00153 00154 float jit_functor_transfer_ease_eval_float32(t_jit_functor_transfer_ease *x, long dimcount, float *vals) 00155 { 00156 return jit_functor_eval_float32_with_scalar_product((t_jit_object *)x,dimcount,vals, 00157 (t_jit_functor_float32_scalar_sig)jit_functor_transfer_ease_eval_float32_scalar); 00158 } 00159 00160 double jit_functor_transfer_ease_eval_float64(t_jit_functor_transfer_ease *x, long dimcount, double *vals) 00161 { 00162 return jit_functor_eval_float64_with_scalar_product((t_jit_object *)x,dimcount,vals, 00163 (t_jit_functor_float64_scalar_sig)jit_functor_transfer_ease_eval_float64_scalar); 00164 } 00165 00166 // -------------------------------------------------------------------------- 00167 // scalar evaluation functions 00168 // -------------------------------------------------------------------------- 00169 long jit_functor_transfer_ease_eval_fixed_scalar(t_jit_functor_transfer_ease *x, long val) 00170 { 00171 return FloatToFixed(jit_functor_transfer_ease_eval_float32_scalar(x,FixedToFloat(val))); 00172 } 00173 00174 float jit_functor_transfer_ease_eval_float32_scalar(t_jit_functor_transfer_ease *x, float val) 00175 { 00176 float ans,s,a,b,c,nt,rt; 00177 float v,a1,a2; 00178 float t = val; 00179 float t1 = x->start.float32; 00180 float t2 = x->end.float32; 00181 00182 // constant velocity attained 00183 v = 2/(1+t2-t1); 00184 00185 // acceleration of first leg 00186 a1 = v/t1; 00187 00188 // deceleration of last leg 00189 a2 = -v/(1-t2); 00190 if (t<t1) 00191 { 00192 // pos = 1/2 * acc * t*t 00193 rt = 0.5*a1*t*t; 00194 } 00195 else if (t<t2) 00196 { 00197 // get distance from start 00198 a = 0.5*a1*t1*t1; 00199 00200 // distance = vel * time of end 00201 b = v*(t-t1); 00202 rt = a + b; 00203 } 00204 else 00205 { 00206 // get distance from start 00207 a = 0.5*a1*t1*t1; 00208 00209 // get distance from end 00210 b = v*(t2-t1); 00211 00212 // distance = ave vel. * time 00213 c = ((v + v + (t-t2)*a2)/2) * (t-t2); 00214 rt = a + b + c; 00215 } 00216 return(rt); 00217 } 00218 00219 double jit_functor_transfer_ease_eval_float64_scalar(t_jit_functor_transfer_ease *x, double val) 00220 { 00221 double ans,s,a,b,c,nt,rt; 00222 double v,a1,a2; 00223 double t = val; 00224 double t1 = x->start.float64; 00225 double t2 = x->end.float64; 00226 00227 // constant velocity attained 00228 v = 2/(1+t2-t1); 00229 00230 // acceleration of first leg 00231 a1 = v/t1; 00232 00233 // deceleration of last leg 00234 a2 = -v/(1-t2); 00235 if (t<t1) 00236 { 00237 // pos = 1/2 * acc * t*t 00238 rt = 0.5*a1*t*t; 00239 } 00240 else if (t<t2) 00241 { 00242 // get distance from start 00243 a = 0.5*a1*t1*t1; 00244 00245 // distance = vel * time of end 00246 b = v*(t-t1); 00247 rt = a + b; 00248 } 00249 else 00250 { 00251 // get distance from start 00252 a = 0.5*a1*t1*t1; 00253 00254 // get distance from end 00255 b = v*(t2-t1); 00256 00257 // distance = ave vel. * time 00258 c = ((v + v + (t-t2)*a2)/2) * (t-t2); 00259 rt = a + b + c; 00260 } 00261 return(rt); 00262 } 00263 // --------------------------------------------------------------------------
Copyright © 2008, Cycling '74