[PD-cvs] externals/nusmuk msd.h,1.10,1.11
Nicolas Montgermont
nimon at users.sourceforge.net
Wed Jun 1 15:43:58 CEST 2005
Update of /cvsroot/pure-data/externals/nusmuk
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8732
Modified Files:
msd.h
Log Message:
Generalized for ND
Index: msd.h
===================================================================
RCS file: /cvsroot/pure-data/externals/nusmuk/msd.h,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** msd.h 1 Jun 2005 09:48:46 -0000 1.10
--- msd.h 1 Jun 2005 13:43:56 -0000 1.11
***************
*** 240,289 ****
t_float F;
Mass<N> *m1 = mass1,*m2 = mass2; // cache locally
! if (oriented == 0)
! distance = Mass<N>::dist(*m1,*m2);
! else if (oriented == 1) {
! for(int i = 0; i < N; ++i)
! distance += sqr((m1->pos[i]-m2->pos[i])*tdirection1[i]);
! distance = sqrt(distance);
! }
! else if (oriented == 2) {
! for(int i = 0; i < N; ++i)
! distance += sqr((m1->pos[i]-m2->pos[i])*(tdirection1[i] +tdirection2[i]));
! distance = sqrt(distance);
! }
!
! if (distance < long_min || distance > long_max || distance == 0) {
// for(int i = 0; i < N; ++i) {
// m1->force[i] -= D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
// m2->force[i] += D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
// }
}
- else { // Lmin < L < Lmax
- // F[n] = k1 (L[n] - L[0])/L[n] + D1 (L[n] - L[n-1])/L[n]
- if ((distance - longueur)>0)
- F = (K1 * pow(distance - longueur,puissance) + D1 * (distance - distance_old))/distance ;
- else
- F = (-K1 * pow(longueur - distance,puissance) + D1 * (distance - distance_old))/distance ;
- if (oriented == 0)
- for(int i = 0; i < N; ++i) {
- const t_float Fn = F * (m1->pos[i] - m2->pos[i]); // Fx = F * Lx[n]/L[n]
- m1->force[i] -= Fn + D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
- m2->force[i] += Fn - D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
- }
- else if (oriented == 1 || (oriented == 2 && N == 2))
- for(int i = 0; i < N; ++i) {
- const t_float Fn = F * (m1->pos[i] - m2->pos[i])*tdirection1[i]; // Fx = F * Lx[n]/L[n]
- m1->force[i] -= Fn + D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
- m2->force[i] += Fn - D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
- }
- else if (oriented == 2 && N == 3)
- for(int i = 0; i < N; ++i) {
- const t_float Fn = F * (m1->pos[i] - m2->pos[i])*(tdirection1[i] +tdirection2[i]); // Fx = F * Lx[n]/L[n]
- m1->force[i] -= Fn + D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
- m2->force[i] += Fn - D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
- }
- }
-
- distance_old = distance; // L[n-1] = L[n]
}
};
--- 240,291 ----
t_float F;
Mass<N> *m1 = mass1,*m2 = mass2; // cache locally
! if (m1->invM || m2->invM) {
! if (oriented == 0)
! distance = Mass<N>::dist(*m1,*m2);
! else if (oriented == 1) {
! for(int i = 0; i < N; ++i)
! distance += sqr((m1->pos[i]-m2->pos[i])*tdirection1[i]);
! distance = sqrt(distance);
! }
! else if (oriented == 2) {
! for(int i = 0; i < N; ++i)
! distance += sqr((m1->pos[i]-m2->pos[i])*(tdirection1[i] +tdirection2[i]));
! distance = sqrt(distance);
! }
!
! if (distance < long_min || distance > long_max || distance == 0) {
// for(int i = 0; i < N; ++i) {
// m1->force[i] -= D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
// m2->force[i] += D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
// }
+ }
+ else { // Lmin < L < Lmax
+ // F[n] = k1 (L[n] - L[0])/L[n] + D1 (L[n] - L[n-1])/L[n]
+ if ((distance - longueur)>0)
+ F = (K1 * pow(distance - longueur,puissance) + D1 * (distance - distance_old))/distance ;
+ else
+ F = (-K1 * pow(longueur - distance,puissance) + D1 * (distance - distance_old))/distance ;
+ if (oriented == 0)
+ for(int i = 0; i < N; ++i) {
+ const t_float Fn = F * (m1->pos[i] - m2->pos[i]); // Fx = F * Lx[n]/L[n]
+ m1->force[i] -= Fn + D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
+ m2->force[i] += Fn - D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
+ }
+ else if (oriented == 1 || (oriented == 2 && N == 2))
+ for(int i = 0; i < N; ++i) {
+ const t_float Fn = F * (m1->pos[i] - m2->pos[i])*tdirection1[i]; // Fx = F * Lx[n]/L[n]
+ m1->force[i] -= Fn + D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
+ m2->force[i] += Fn - D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
+ }
+ else if (oriented == 2 && N == 3)
+ for(int i = 0; i < N; ++i) {
+ const t_float Fn = F * (m1->pos[i] - m2->pos[i])*(tdirection1[i] +tdirection2[i]); // Fx = F * Lx[n]/L[n]
+ m1->force[i] -= Fn + D2 * m1->speed[i]; // Fx1[n] = -Fx, Fx1[n] = Fx1[n] - D2 * vx1[n-1]
+ m2->force[i] += Fn - D2 * m2->speed[i]; // Fx2[n] = Fx, Fx2[n] = Fx2[n] - D2 * vx2[n-1]
+ }
+ }
+
+ distance_old = distance; // L[n-1] = L[n]
}
}
};
***************
*** 555,559 ****
m_pos(argc-1,arglist,GetAInt(argv[0])-1);
}
! // set mass No to mobile
void m_set_mobile(int argc,t_atom *argv,bool mob = true)
{
--- 557,561 ----
m_pos(argc-1,arglist,GetAInt(argv[0])-1);
}
! // set mass to mobile
void m_set_mobile(int argc,t_atom *argv,bool mob = true)
{
***************
*** 647,650 ****
--- 649,676 ----
}
+ // set Id of link(s) named Id or number No
+ void m_setMassId(int argc,t_atom *argv)
+ {
+ if (argc != 2) {
+ error("%s - %s Syntax : OldId/NoMass NewId",thisName(),GetString(thisTag()));
+ return;
+ }
+
+ const t_symbol *id = GetSymbol(argv[1]);
+
+ if(IsSymbol(argv[0])) {
+ typename IDMap<t_mass *>::iterator it;
+ for(it = massids.find(GetSymbol(argv[0])); it; ++it)
+ it.data()->Id = id;
+ }
+ else {
+ t_mass *m = mass.find(GetAInt(argv[0]));
+ if(m)
+ m->Id = id;
+ else
+ error("%s - %s : Index not found",thisName(),GetString(thisTag()));
+ }
+ }
+
void m_grab_mass(int argc,t_atom *argv)
{
***************
*** 1046,1049 ****
--- 1072,1099 ----
}
+ // set Id of link(s) named Id or number No
+ void m_setLinkId(int argc,t_atom *argv)
+ {
+ if (argc != 2) {
+ error("%s - %s Syntax : OldId/NoLink NewId",thisName(),GetString(thisTag()));
+ return;
+ }
+
+ const t_symbol *id = GetSymbol(argv[1]);
+
+ if(IsSymbol(argv[0])) {
+ typename IDMap<t_link *>::iterator it;
+ for(it = linkids.find(GetSymbol(argv[0])); it; ++it)
+ it.data()->Id = id;
+ }
+ else {
+ t_link *l = link.find(GetAInt(argv[0]));
+ if(l)
+ l->Id = id;
+ else
+ error("%s - %s : Index not found",thisName(),GetString(thisTag()));
+ }
+ }
+
// set rigidity of link(s) named Id or number No
void m_setK(int argc,t_atom *argv)
***************
*** 1167,1172 ****
--- 1217,1224 ----
t_atom sortie[1+2*N];
+ t_float mean[N] ,std[N], nombre;
const t_symbol *auxtype = GetSymbol(argv[0]);
+
if (argc == 1) {
if (auxtype == S_massesPos) { // get all masses positions
***************
*** 1177,1180 ****
--- 1229,1262 ----
}
}
+ else if (auxtype == S_massesPosMean) { // get all masses positions mean
+ for(int i = 0; i<N; ++i)
+ mean[i] = 0;
+ nombre = 0;
+ for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
+ ++nombre;
+ for(int i = 0; i < N; ++i)
+ mean[i] += mit.data()->pos[i];
+ }
+ for(int i = 0; i < N; ++i)
+ SetFloat(sortie[0+i],mean[i]/nombre);
+ ToOutAnything(0,S_massesPosMean,0+N,sortie);
+ }
+ else if (auxtype == S_massesPosStd) { // get all masses positions std
+ for(int i = 0; i<N; ++i) {
+ mean[i] = 0;
+ std[i] = 0;
+ }
+ nombre = 0;
+ for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
+ ++nombre;
+ for(int i = 0; i < N; ++i) {
+ mean[i] += mit.data()->pos[i];
+ std[i] += sqr(mit.data()->pos[i]) ;
+ }
+ }
+ for(int i = 0; i < N; ++i)
+ SetFloat(sortie[0+i],sqrt(std[i]/nombre-sqr(mean[i]/nombre)));
+ ToOutAnything(0,S_massesPosStd,0+N,sortie);
+ }
else if (auxtype == S_massesForces) { // get all masses forces
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
***************
*** 1184,1187 ****
--- 1266,1299 ----
}
}
+ else if (auxtype == S_massesForcesMean) { // get all masses forces mean
+ for(int i = 0; i<N; ++i)
+ mean[i] = 0;
+ nombre = 0;
+ for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
+ ++nombre;
+ for(int i = 0; i < N; ++i)
+ mean[i] += mit.data()->out_force[i];
+ }
+ for(int i = 0; i < N; ++i)
+ SetFloat(sortie[0+i],mean[i]/nombre);
+ ToOutAnything(0,S_massesForcesMean,0+N,sortie);
+ }
+ else if (auxtype == S_massesForcesStd) { // get all masses forces std
+ for(int i = 0; i<N; ++i) {
+ mean[i] = 0;
+ std[i] = 0;
+ }
+ nombre = 0;
+ for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
+ ++nombre;
+ for(int i = 0; i < N; ++i) {
+ mean[i] += mit.data()->out_force[i];
+ std[i] += sqr(mit.data()->out_force[i]) ;
+ }
+ }
+ for(int i = 0; i < N; ++i)
+ SetFloat(sortie[0+i],sqrt(std[i]/nombre-sqr(mean[i]/nombre)));
+ ToOutAnything(0,S_massesForcesStd,0+N,sortie);
+ }
else if (auxtype == S_linksPos) { // get all links positions
for(typename IndexMap<t_link *>::iterator lit(link); lit; ++lit) {
***************
*** 1194,1198 ****
}
}
! else { // get all masses speeds
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
SetInt(sortie[0],mit.data()->nbr);
--- 1306,1344 ----
}
}
! else if (auxtype == S_linksLenghts) { // get all links lenghts
! for(typename IndexMap<t_link *>::iterator lit(link); lit; ++lit) {
! SetInt(sortie[0],lit.data()->nbr);
! SetFloat(sortie[1],lit.data()->distance_old);
! ToOutAnything(0,S_linksLenghts,2,sortie);
! }
! }
! else if (auxtype == S_linksLenghtsMean) { // get all links lenghts mean
! for(int i = 0; i<N; ++i)
! mean[i] = 0;
! nombre = 0;
! for(typename IndexMap<t_link *>::iterator lit(link); lit; ++lit) {
! ++nombre;
! mean[0] += lit.data()->distance_old;
! }
! for(int i = 0; i < N; ++i)
! SetFloat(sortie[0],mean[0]/nombre);
! ToOutAnything(0,S_linksLenghtsMean,1,sortie);
! }
! else if (auxtype == S_linksLenghtsStd) { // get all links lenghts std
! for(int i = 0; i<N; ++i) {
! mean[i] = 0;
! std[i] = 0;
! }
! nombre = 0;
! for(typename IndexMap<t_link *>::iterator lit(link); lit; ++lit) {
! ++nombre;
! mean[0] += lit.data()->distance_old;
! std[0] += sqr(lit.data()->distance_old) ;
! }
! for(int i = 0; i < N; ++i)
! SetFloat(sortie[0],sqrt(std[0]/nombre-sqr(mean[0]/nombre)));
! ToOutAnything(0,S_linksLenghtsStd,1,sortie);
! }
! else if (auxtype == S_massesSpeeds) { // get all masses speeds
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
SetInt(sortie[0],mit.data()->nbr);
***************
*** 1201,1204 ****
--- 1347,1382 ----
}
}
+ else if (auxtype == S_massesSpeedsMean) { // get all masses forces mean
+ for(int i = 0; i<N; ++i)
+ mean[i] = 0;
+ nombre = 0;
+ for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
+ ++nombre;
+ for(int i = 0; i < N; ++i)
+ mean[i] += mit.data()->speed[i];
+ }
+ for(int i = 0; i < N; ++i)
+ SetFloat(sortie[0+i],mean[i]/nombre);
+ ToOutAnything(0,S_massesSpeedsMean,0+N,sortie);
+ }
+ else if (auxtype == S_massesSpeedsStd) { // get all masses forces std
+ for(int i = 0; i<N; ++i) {
+ mean[i] = 0;
+ std[i] = 0;
+ }
+ nombre = 0;
+ for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
+ ++nombre;
+ for(int i = 0; i < N; ++i) {
+ mean[i] += mit.data()->speed[i];
+ std[i] += sqr(mit.data()->speed[i]) ;
+ }
+ }
+ for(int i = 0; i < N; ++i)
+ SetFloat(sortie[0+i],sqrt(std[i]/nombre-sqr(mean[i]/nombre)));
+ ToOutAnything(0,S_massesSpeedsStd,0+N,sortie);
+ }
+ else
+ error("%s - %s : Syntax error",thisName(),GetString(thisTag()));
return;
}
***************
*** 1280,1283 ****
--- 1458,1484 ----
}
}
+ else if (auxtype == S_linksLenghts) // get links lenghts
+ {
+ for(int j = 1; j<argc; j++) {
+ if(IsSymbol(argv[j])) {
+ typename IDMap<t_link *>::iterator lit;
+ for(lit = linkids.find(GetSymbol(argv[j])); lit; ++lit) {
+ SetSymbol(sortie[0],lit.data()->Id);
+ SetFloat(sortie[1],lit.data()->distance_old);
+ ToOutAnything(0,S_linksLenghtsId,2,sortie);
+ }
+ }
+ else {
+ t_link *l = link.find(GetAInt(argv[j]));
+ if(l) {
+ SetInt(sortie[0],l->nbr);
+ SetFloat(sortie[1],l->distance_old);
+ ToOutAnything(0,S_linksLenghtsNo,2,sortie);
+ }
+ // else
+ // error("%s - %s : Index not found",thisName(),GetString(thisTag()));
+ }
+ }
+ }
else // get mass speeds
{
***************
*** 1489,1492 ****
--- 1690,1695 ----
const static t_symbol *S_Link_deleted;
const static t_symbol *S_massesPos;
+ const static t_symbol *S_massesPosMean;
+ const static t_symbol *S_massesPosStd;
const static t_symbol *S_massesPosNo;
const static t_symbol *S_massesPosId;
***************
*** 1494,1501 ****
--- 1697,1713 ----
const static t_symbol *S_linksPosNo;
const static t_symbol *S_linksPosId;
+ const static t_symbol *S_linksLenghts;
+ const static t_symbol *S_linksLenghtsMean;
+ const static t_symbol *S_linksLenghtsStd;
+ const static t_symbol *S_linksLenghtsNo;
+ const static t_symbol *S_linksLenghtsId;
const static t_symbol *S_massesForces;
+ const static t_symbol *S_massesForcesMean;
+ const static t_symbol *S_massesForcesStd;
const static t_symbol *S_massesForcesNo;
const static t_symbol *S_massesForcesId;
const static t_symbol *S_massesSpeeds;
+ const static t_symbol *S_massesSpeedsMean;
+ const static t_symbol *S_massesSpeedsStd;
const static t_symbol *S_massesSpeedsNo;
const static t_symbol *S_massesSpeedsId;
***************
*** 1517,1520 ****
--- 1729,1734 ----
S_Link_deleted = MakeSymbol("Link deleted");
S_massesPos = MakeSymbol("massesPos");
+ S_massesPosMean = MakeSymbol("massesPosMean");
+ S_massesPosStd = MakeSymbol("massesPosStd");
S_massesPosNo = MakeSymbol("massesPosNo");
S_massesPosId = MakeSymbol("massesPosId");
***************
*** 1522,1529 ****
--- 1736,1752 ----
S_linksPosNo = MakeSymbol("linksPosNo");
S_linksPosId = MakeSymbol("linksPosId");
+ S_linksLenghts = MakeSymbol("linksLenghts");
+ S_linksLenghtsMean = MakeSymbol("linksLenghtsMean");
+ S_linksLenghtsStd = MakeSymbol("linksLenghtsStd");
+ S_linksLenghtsNo = MakeSymbol("linksLenghtsNo");
+ S_linksLenghtsId = MakeSymbol("linksLenghtsId");
S_massesForces = MakeSymbol("massesForces");
+ S_massesForcesMean = MakeSymbol("massesForcesMean");
+ S_massesForcesStd = MakeSymbol("massesForcesStd");
S_massesForcesNo = MakeSymbol("massesForcesNo");
S_massesForcesId = MakeSymbol("massesForcesId");
S_massesSpeeds = MakeSymbol("massesSpeeds");
+ S_massesSpeedsMean = MakeSymbol("massesSpeedsMean");
+ S_massesSpeedsStd = MakeSymbol("massesSpeedsStd");
S_massesSpeedsNo = MakeSymbol("massesSpeedsNo");
S_massesSpeedsId = MakeSymbol("massesSpeedsId");
***************
*** 1572,1575 ****
--- 1795,1800 ----
FLEXT_CADDMETHOD_(c,0,"setMobile",m_set_mobile);
FLEXT_CADDMETHOD_(c,0,"setFixed",m_set_fixe);
+ FLEXT_CADDMETHOD_(c,0,"setMassId",m_setMassId);
+ FLEXT_CADDMETHOD_(c,0,"setLinkId",m_setLinkId);
FLEXT_CADDMETHOD_(c,0,"setK",m_setK);
FLEXT_CADDMETHOD_(c,0,"setD",m_setD);
***************
*** 1620,1623 ****
--- 1845,1850 ----
FLEXT_CALLBACK_V(m_forceN)
FLEXT_CALLBACK_V(m_posN)
+ FLEXT_CALLBACK_V(m_setMassId)
+ FLEXT_CALLBACK_V(m_setLinkId)
FLEXT_CALLBACK_V(m_setK)
FLEXT_CALLBACK_V(m_setD)
***************
*** 1639,1646 ****
*msdN<N>::S_massesPos,*msdN<N>::S_massesPosNo,*msdN<N>::S_massesPosId, \
*msdN<N>::S_linksPos,*msdN<N>::S_linksPosNo,*msdN<N>::S_linksPosId, \
! *msdN<N>::S_massesForces,*msdN<N>::S_massesForcesNo,*msdN<N>::S_massesForcesId, \
! *msdN<N>::S_massesSpeeds,*msdN<N>::S_massesSpeedsNo,*msdN<N>::S_massesSpeedsId, \
*msdN<N>::S_massesPosL,*msdN<N>::S_massesPosXL,*msdN<N>::S_massesPosYL, \
! *msdN<N>::S_massesPosZL,*msdN<N>::S_massesForcesL; \
\
typedef msdN<N> CLASS; \
--- 1866,1878 ----
*msdN<N>::S_massesPos,*msdN<N>::S_massesPosNo,*msdN<N>::S_massesPosId, \
*msdN<N>::S_linksPos,*msdN<N>::S_linksPosNo,*msdN<N>::S_linksPosId, \
! *msdN<N>::S_linksLenghts,*msdN<N>::S_linksLenghtsMean,*msdN<N>::S_linksLenghtsStd, \
! *msdN<N>::S_linksLenghtsNo,*msdN<N>::S_linksLenghtsId, \
! *msdN<N>::S_massesForces,*msdN<N>::S_massesForcesMean,*msdN<N>::S_massesForcesStd, \
! *msdN<N>::S_massesForcesNo,*msdN<N>::S_massesForcesId, \
! *msdN<N>::S_massesSpeeds,*msdN<N>::S_massesSpeedsMean,*msdN<N>::S_massesSpeedsStd, \
! *msdN<N>::S_massesSpeedsNo,*msdN<N>::S_massesSpeedsId, \
*msdN<N>::S_massesPosL,*msdN<N>::S_massesPosXL,*msdN<N>::S_massesPosYL, \
! *msdN<N>::S_massesPosZL,*msdN<N>::S_massesPosStd,*msdN<N>::S_massesPosMean,\
! *msdN<N>::S_massesForcesL; \
\
typedef msdN<N> CLASS; \
More information about the Pd-cvs
mailing list