diff options
author | Cyrille Henry <nusmuk@users.sourceforge.net> | 2014-10-15 17:38:13 +0000 |
---|---|---|
committer | Cyrille Henry <nusmuk@users.sourceforge.net> | 2014-10-15 17:38:13 +0000 |
commit | 2b5ae7744de11430fe34b8d399dd836aa80dc1e1 (patch) | |
tree | da2d5e8123eaa6cf18495b07e8a090722551415d | |
parent | ddcb70ce901a80a36e0f028655573b4f520f0334 (diff) |
add new message : closestMassN to get a list of the N closest mass to a point
svn path=/trunk/externals/pmpd/; revision=17359
-rw-r--r-- | pmpd.c | 1 | ||||
-rw-r--r-- | pmpd2d.c | 1 | ||||
-rw-r--r-- | pmpd2d_various.c | 81 | ||||
-rw-r--r-- | pmpd3d.c | 1 | ||||
-rw-r--r-- | pmpd3d_various.c | 88 | ||||
-rw-r--r-- | pmpd_various.c | 77 |
6 files changed, 249 insertions, 0 deletions
@@ -235,6 +235,7 @@ void pmpd_setup(void) class_addmethod(pmpd_class, (t_method)pmpd_grabMass, gensym("grabMass"), A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(pmpd_class, (t_method)pmpd_closestMass, gensym("closestMass"), A_GIMME, 0);
+ class_addmethod(pmpd_class, (t_method)pmpd_closestMassN, gensym("closestMassN"), A_GIMME, 0);
class_addmethod(pmpd_class, (t_method)pmpd_massDistances, gensym("massDistance"), A_GIMME, 0);
// class_addmethod(pmpd_class, (t_method)pmpd_forcesXT, gensym("forceXT"), A_GIMME, 0);
@@ -316,6 +316,7 @@ void pmpd2d_setup(void) class_addmethod(pmpd2d_class, (t_method)pmpd2d_grabMass, gensym("grabMass"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(pmpd2d_class, (t_method)pmpd2d_closestMass, gensym("closestMass"), A_GIMME, 0);
+ class_addmethod(pmpd2d_class, (t_method)pmpd2d_closestMassN, gensym("closestMassN"), A_GIMME, 0);
class_addmethod(pmpd2d_class, (t_method)pmpd2d_massDistances, gensym("massDistance"), A_GIMME, 0);
// class_addmethod(pmpd2d_class, (t_method)pmpd2d_forcesXT, gensym("forceXT"), A_GIMME, 0);
diff --git a/pmpd2d_various.c b/pmpd2d_various.c index 7a31c4f..64631d5 100644 --- a/pmpd2d_various.c +++ b/pmpd2d_various.c @@ -462,6 +462,87 @@ void pmpd2d_massDistances(t_pmpd2d *x, t_symbol *s, int argc, t_atom *argv) } } +int bulle_order(t_int *listIndex, t_float *listDistance, t_int index) +{ + t_int tmpI; + t_float tmpD; + + if ( listDistance[index] < listDistance[index+1] ) + { + tmpD = listDistance[index]; + tmpI = listIndex[index]; + listDistance[index] = listDistance[index+1]; + listIndex[index] = listIndex[index+1]; + listDistance[index+1] = tmpD; + listIndex[index+1]= tmpI; + return(1); + } + return(0); +} + +void pmpd2d_closestMassN(t_pmpd2d *x, t_symbol *s, int argc, t_atom *argv) +{ + t_float dist; + t_int i, j, nbout, list_index[x->nb_mass]; + t_float list_distance[x->nb_mass]; + t_atom std_out[x->nb_mass * 2]; + t_float posX, posY, posZ; + + if ( (argc >= 1) && (argv[0].a_type == A_FLOAT) ) + nbout = atom_getfloatarg(0, argc, argv); + else + nbout = 1; + nbout = max(nbout,0); + nbout = min(nbout,x->nb_mass); + + if ( (argc >= 2) && (argv[1].a_type == A_FLOAT) ) + posX = atom_getfloatarg(1, argc, argv); + else + posX = 0; + + if ( (argc >= 3) && (argv[2].a_type == A_FLOAT) ) + posY = atom_getfloatarg(2, argc, argv); + else + posY = 0; + + for (i=0; i < nbout; i++) // on remplie avec les premiere données disponible + { + list_index[i] = i; + list_distance[i] = sqr(x->mass[i].posX - posX) + sqr(x->mass[i].posY - posY) ; + } + + for (i=1; i < nbout; i++) // trie a bulle pour ordoner cela + { + for (j=0; j < nbout-i; j++) + { + bulle_order(list_index, list_distance, j); + } + } + + for (i = nbout; i< x->nb_mass; i++) // on test le reste des masses + { + dist = sqr(x->mass[i].posX - posX) + sqr(x->mass[i].posY - posY); + if (dist < list_distance[0]) // cette mass doit rentrer dans la liste + { + list_index[0] = i; + list_distance[0] = dist; + j = 0; + while ( (j < nbout-1) && bulle_order(list_index, list_distance, j) ) // on reordone la liste + { + j++; + } + } + } + + for (i=0; i<nbout; i++) + { + SETFLOAT(&(std_out[2*i]), list_index[nbout-1-i]); + SETFLOAT(&(std_out[2*i+1]), sqrt(list_distance[nbout-1-i])); + } + + outlet_anything(x->main_outlet, gensym("closestMassN"),2*nbout,std_out); +} + /* void pmpd2d_forcesXT(t_pmpd2d *x, t_symbol *s, int argc, t_atom *argv) { @@ -347,6 +347,7 @@ void pmpd3d_setup(void) class_addmethod(pmpd3d_class, (t_method)pmpd3d_addPosZ, gensym("addPosZ"), A_GIMME, 0);
class_addmethod(pmpd3d_class, (t_method)pmpd3d_grabMass, gensym("grabMass"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
class_addmethod(pmpd3d_class, (t_method)pmpd3d_closestMass, gensym("closestMass"), A_GIMME, 0);
+ class_addmethod(pmpd3d_class, (t_method)pmpd3d_closestMassN, gensym("closestMassN"), A_GIMME, 0);
class_addmethod(pmpd3d_class, (t_method)pmpd3d_massDistances, gensym("massDistance"), A_GIMME, 0);
// class_addmethod(pmpd3d_class, (t_method)pmpd3d_forcesXT, gensym("forceXT"), A_GIMME, 0);
diff --git a/pmpd3d_various.c b/pmpd3d_various.c index a3e823b..d3e4297 100644 --- a/pmpd3d_various.c +++ b/pmpd3d_various.c @@ -558,6 +558,93 @@ void pmpd3d_massDistances(t_pmpd3d *x, t_symbol *s, int argc, t_atom *argv) } } +int bulle_order(t_int *listIndex, t_float *listDistance, t_int index) +{ + t_int tmpI; + t_float tmpD; + + if ( listDistance[index] < listDistance[index+1] ) + { + tmpD = listDistance[index]; + tmpI = listIndex[index]; + listDistance[index] = listDistance[index+1]; + listIndex[index] = listIndex[index+1]; + listDistance[index+1] = tmpD; + listIndex[index+1]= tmpI; + return(1); + } + return(0); +} + +void pmpd3d_closestMassN(t_pmpd3d *x, t_symbol *s, int argc, t_atom *argv) +{ + t_float dist; + t_int i, j, nbout, list_index[x->nb_mass]; + t_float list_distance[x->nb_mass]; + t_atom std_out[x->nb_mass * 2]; + t_float posX, posY, posZ; + + if ( (argc >= 1) && (argv[0].a_type == A_FLOAT) ) + nbout = atom_getfloatarg(0, argc, argv); + else + nbout = 1; + nbout = max(nbout,0); + nbout = min(nbout,x->nb_mass); + + if ( (argc >= 2) && (argv[1].a_type == A_FLOAT) ) + posX = atom_getfloatarg(1, argc, argv); + else + posX = 0; + + if ( (argc >= 3) && (argv[2].a_type == A_FLOAT) ) + posY = atom_getfloatarg(2, argc, argv); + else + posY = 0; + + if ( (argc >= 4) && (argv[3].a_type == A_FLOAT) ) + posZ = atom_getfloatarg(3, argc, argv); + else + posZ = 0; + + for (i=0; i < nbout; i++) // on remplie avec les premiere données disponible + { + list_index[i] = i; + list_distance[i] = sqr(x->mass[i].posX - posX) + sqr(x->mass[i].posY - posY) + sqr(x->mass[i].posZ - posZ); + } + + for (i=1; i < nbout; i++) //trie a bulle pour ordoner cela + { + for (j=0; j < nbout-i; j++) + { + bulle_order(list_index, list_distance, j); + } + } + + for (i = nbout; i< x->nb_mass; i++) // on test le reste des masses + { + dist = sqr(x->mass[i].posX - posX) + sqr(x->mass[i].posY - posY) + sqr(x->mass[i].posZ - posZ); + if (dist < list_distance[0]) // cette mass doit rentrer dans la liste + { + list_index[0] = i; + list_distance[0] = dist; + j = 0; + while ( (j<nbout-1) && bulle_order(list_index, list_distance, j) ) // on reordone la liste + { + j++; + } + } + } + + for (i=0; i<nbout; i++) + { + SETFLOAT(&(std_out[2*i]), list_index[nbout-1-i]); + SETFLOAT(&(std_out[2*i+1]), sqrt(list_distance[nbout-1-i])); + } + + outlet_anything(x->main_outlet, gensym("closestMassN"),2*nbout,std_out); +} + + /* void pmpd3d_forcesXT(t_pmpd3d *x, t_symbol *s, int argc, t_atom *argv) { @@ -697,3 +784,4 @@ void pmpd3d_forcesZT(t_pmpd3d *x, t_symbol *s, int argc, t_atom *argv) } } */ + diff --git a/pmpd_various.c b/pmpd_various.c index 9233800..9b6a6ab 100644 --- a/pmpd_various.c +++ b/pmpd_various.c @@ -274,6 +274,83 @@ void pmpd_massDistances(t_pmpd *x, t_symbol *s, int argc, t_atom *argv) } } +int bulle_order(t_int *listIndex, t_float *listDistance, t_int index) +{ + t_int tmpI; + t_float tmpD; + + if ( listDistance[index] < listDistance[index+1] ) + { + tmpD = listDistance[index]; + tmpI = listIndex[index]; + listDistance[index] = listDistance[index+1]; + listIndex[index] = listIndex[index+1]; + listDistance[index+1] = tmpD; + listIndex[index+1]= tmpI; + return(1); + } + return(0); +} + +void pmpd_closestMassN(t_pmpd *x, t_symbol *s, int argc, t_atom *argv) +{ + t_float dist; + t_int i, j, nbout, list_index[x->nb_mass]; + t_float list_distance[x->nb_mass]; + t_atom std_out[x->nb_mass * 2]; + t_float posX; + + if ( (argc >= 1) && (argv[0].a_type == A_FLOAT) ) + nbout = atom_getfloatarg(0, argc, argv); + else + nbout = 1; + nbout = max(nbout,0); + nbout = min(nbout,x->nb_mass); + + if ( (argc >= 2) && (argv[1].a_type == A_FLOAT) ) + posX = atom_getfloatarg(1, argc, argv); + else + posX = 0; + + for (i=0; i < nbout; i++) // on remplie avec les premiere données disponible + { + list_index[i] = i; + list_distance[i] = x->mass[i].posX - posX ; + } + + for (i=1; i < nbout; i++) //trie a bulle pour ordoner cela + { + for (j=0; j < nbout-i; j++) + { + bulle_order(list_index, list_distance, j); + } + } + + for (i = nbout; i< x->nb_mass; i++) // on test le reste des masses + { + dist = x->mass[i].posX - posX; + if (dist < list_distance[0]) // cette mass doit rentrer dans la liste + { + list_index[0] = i; + list_distance[0] = dist; + j = 0; + while ( (j<nbout-1) && bulle_order(list_index, list_distance, j) ) // on reordone la liste + { + j++; + } + } + } + + for (i=0; i<nbout; i++) + { + SETFLOAT(&(std_out[2*i]), list_index[nbout-1-i]); + SETFLOAT(&(std_out[2*i+1]), list_distance[nbout-1-i]); + } + + outlet_anything(x->main_outlet, gensym("closestMassN"),2*nbout,std_out); +} + + /* void pmpd_forcesXT(t_pmpd *x, t_symbol *s, int argc, t_atom *argv) { |