#include "m_pd.h" /*-------------------------------------------------------*/ /*sphere~ */ /* A tool to control volumes based off of 3 dimentions */ /* using linier decay. Inlets are (from left to right): */ /* the center of the sphere x, y, z, the radius of the */ /* sphere and the location of the control point x, y, z. */ /* The outlet is a float from 0 to 1. The outlet returns */ /* 0 if the control point is outside the sphere and the */ /* output is linier from 0 to 1, from the edge to the */ /* center of the sphere if the control point lies within */ /* the sphere */ /* */ /* Major help recieved from: */ /* Thomas Grill, Chad Wood and Olaf Matthes */ /* Thanks for the Help! */ /*-------------------------------------------------------*/ static t_class *sphere_tilde_class; typedef struct _sphere_tilde { t_object x_obj; t_sample dummy; } t_sphere_tilde; void *sphere_tilde_new(t_floatarg f) { t_symbol *p_signal = gensym("signal"); t_sphere_tilde *ref = (t_sphere_tilde *)pd_new(sphere_tilde_class); ref->dummy=f; inlet_new(&ref->x_obj, &ref->x_obj.ob_pd, p_signal, p_signal); inlet_new(&ref->x_obj, &ref->x_obj.ob_pd, p_signal, p_signal); inlet_new(&ref->x_obj, &ref->x_obj.ob_pd, p_signal, p_signal); inlet_new(&ref->x_obj, &ref->x_obj.ob_pd, p_signal, p_signal); inlet_new(&ref->x_obj, &ref->x_obj.ob_pd, p_signal, p_signal); inlet_new(&ref->x_obj, &ref->x_obj.ob_pd, p_signal, p_signal); outlet_new(&ref->x_obj, p_signal); return (void *)ref; } t_int *sphere_tilde_perform(t_int *w) { t_sphere_tilde *ref = (t_sphere_tilde *)(w[1]);//data structure t_sample *in1 = (t_sample *)(w[2]); //centerx t_sample *in2 = (t_sample *)(w[3]); //centery t_sample *in3 = (t_sample *)(w[4]); //centerz t_sample *in4 = (t_sample *)(w[5]); //radius t_sample *in5 = (t_sample *)(w[6]); //controlx t_sample *in6 = (t_sample *)(w[7]); //controly t_sample *in7 = (t_sample *)(w[8]); //controlz t_sample *out = (t_sample *)(w[9]); //outlet int n = (int)(w[10]); //number of samples in block while (n--) { t_sample cent_x=*in1++; t_sample cent_y=*in2++; t_sample cent_z=*in3++; t_sample rad=*in4++; t_sample cont_x=*in5++; t_sample cont_y=*in6++; t_sample cont_z=*in7++; t_sample vol; if ((rad>0.001) || (rad<-0.001)) { vol=1-((((cont_x-cent_x)*(cont_x-cent_x))+((cont_y-cent_y)*(cont_y-cent_y))+((cont_z-cent_z)*(cont_z-cent_z)))/(rad*rad)); if (vol<0) *out++ = 0; else *out++ = vol; } else { vol=1-((((cont_x-cent_x)*(cont_x-cent_x))+((cont_y-cent_y)*(cont_y-cent_y))+((cont_z-cent_z)*(cont_z-cent_z)))/(0.001*0.001)); if (vol<0) *out++ = 0; else *out++ = 1; } } return (w+11); } void sphere_tilde_dsp(t_sphere_tilde *ref, t_signal **sp) { dsp_add(sphere_tilde_perform, 10, ref, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec, sp[4]->s_vec, sp[5]->s_vec, sp[6]->s_vec, sp[7]->s_vec, sp[0]->s_n); } void sphere_tilde_setup(void) { sphere_tilde_class = class_new(gensym("sphere~"), (t_newmethod)sphere_tilde_new, 0, sizeof(t_sphere_tilde), CLASS_DEFAULT, A_DEFFLOAT, 0); class_addmethod(sphere_tilde_class, (t_method)sphere_tilde_dsp, gensym("dsp"), 0); CLASS_MAINSIGNALIN(sphere_tilde_class, t_sphere_tilde, dummy); post("sphere~: written by wade with help from the PD list"); }