aboutsummaryrefslogtreecommitdiff
path: root/msd2D/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'msd2D/main.cpp')
-rwxr-xr-xmsd2D/main.cpp167
1 files changed, 92 insertions, 75 deletions
diff --git a/msd2D/main.cpp b/msd2D/main.cpp
index 37a7510..4ab0dff 100755
--- a/msd2D/main.cpp
+++ b/msd2D/main.cpp
@@ -39,8 +39,8 @@
// define constants
#define MSD2D_VERSION 0.03
-#define nb_max_link 4000
-#define nb_max_mass 4000
+#define nb_max_link 4000
+#define nb_max_mass 4000
#define Id_length 20
// check for appropriate flext version
@@ -48,31 +48,40 @@
#error You need at least flext version 0.4.1
#endif
-#define max(a,b) ( ((a) > (b)) ? (a) : (b) )
+#define max(a,b) ( ((a) > (b)) ? (a) : (b) )
#define min(a,b) ( ((a) < (b)) ? (a) : (b) )
-typedef struct _mass {
+#ifdef _MSC_VER
+#define NEWARR(type,var,size) type *var = new type[size]
+#define DELARR(var) delete[] var
+#else
+#define NEWARR(type,var,size) type var[size]
+#define DELARR(var) ((void)0)
+#endif
+
+
+typedef struct _mass {
t_symbol *Id;
t_int nbr;
- t_int mobile;
- t_float invM;
- t_float speedX;
+ t_int mobile;
+ t_float invM;
+ t_float speedX;
t_float posX;
- t_float posX2;
+ t_float posX2;
t_float forceX;
t_float out_forceX;
- t_float speedY;
+ t_float speedY;
t_float posY;
- t_float posY2;
+ t_float posY2;
t_float forceY;
t_float out_forceY;
-} t_mass;
-
-typedef struct _link {
+} t_mass;
+
+typedef struct _link {
t_symbol *Id;
- t_int nbr;
- t_mass *mass1;
- t_mass *mass2;
+ t_int nbr;
+ t_mass *mass1;
+ t_mass *mass2;
t_float K1, D1, D2;
t_float longx, longy, longueur, long_min, long_max;
t_float distance_old;
@@ -106,7 +115,7 @@ protected:
void m_reset()
{
t_int i;
- t_atom sortie[0];
+// t_atom sortie;
for (i=0; i<nb_mass; i++) {
delete mass[i];
@@ -114,8 +123,8 @@ protected:
for (i=0; i<nb_link; i++) {
delete link[i];
}
- ToOutAnything(1,S_Reset,0,sortie);
- nb_link = 0;
+ ToOutAnything(1,S_Reset,0,NULL);
+ nb_link = 0;
nb_mass = 0;
id_mass = 0;
id_link = 0;
@@ -126,11 +135,11 @@ protected:
void m_bang()
{
- t_float F=0,Fx=0,Fy=0,distance,vitesse, X_new, Y_new;
+ t_float F=0,Fx=0,Fy=0,distance,vitesse, X_new, Y_new;
t_int i;
- struct _mass mass_1, mass_2;
+ struct _mass mass_1, mass_2;
- for (i=0; i<nb_link; i++) {
+ for (i=0; i<nb_link; i++) {
// compute link forces
distance = sqrt(pow(link[i]->mass1->posX-link[i]->mass2->posX,2) +
pow(link[i]->mass1->posY-link[i]->mass2->posY,2)); // L[n] = sqrt( (x1-x2)² +(y1-y2)²)
@@ -145,21 +154,21 @@ protected:
Fx = F * (link[i]->mass1->posX - link[i]->mass2->posX)/distance; // Fx = F * Lx[n]/L[n]
Fy = F * (link[i]->mass1->posY - link[i]->mass2->posY)/distance; // Fy = F * Ly[n]/L[n]
}
- }
+ }
link[i]->mass1->forceX -= Fx; // Fx1[n] = -Fx
- link[i]->mass1->forceX -= link[i]->D2*link[i]->mass1->speedX; // Fx1[n] = Fx1[n] - D2 * vx1[n-1]
+ link[i]->mass1->forceX -= link[i]->D2*link[i]->mass1->speedX; // Fx1[n] = Fx1[n] - D2 * vx1[n-1]
link[i]->mass2->forceX += Fx; // Fx2[n] = Fx
link[i]->mass2->forceX -= link[i]->D2*link[i]->mass2->speedX; // Fx2[n] = Fx2[n] - D2 * vx2[n-1]
link[i]->mass1->forceY -= Fy; // Fy1[n] = -Fy
- link[i]->mass1->forceY -= link[i]->D2*link[i]->mass1->speedY; // Fy1[n] = Fy1[n] - D2 * vy1[n-1]
+ link[i]->mass1->forceY -= link[i]->D2*link[i]->mass1->speedY; // Fy1[n] = Fy1[n] - D2 * vy1[n-1]
link[i]->mass2->forceY += Fy; // Fy2[n] = Fy
link[i]->mass2->forceY -= link[i]->D2*link[i]->mass2->speedY; // Fy1[n] = Fy1[n] - D2 * vy1[n-1]
link[i]->distance_old = distance; // L[n-1] = L[n]
}
-
- for (i=0; i<nb_mass; i++)
- // compute new masses position only if mobile = 1
- if (mass[i]->mobile == 1) {
+
+ for (i=0; i<nb_mass; i++)
+ // compute new masses position only if mobile = 1
+ if (mass[i]->mobile == 1) {
X_new = mass[i]->forceX * mass[i]->invM + 2*mass[i]->posX - mass[i]->posX2; // x[n] =Fx[n]/M+2x[n]-x[n-1]
mass[i]->posX2 = mass[i]->posX; // x[n-2] = x[n-1]
mass[i]->posX = max(min(X_new,Xmax),Xmin); // x[n-1] = x[n]
@@ -170,21 +179,21 @@ protected:
mass[i]->speedY = mass[i]->posY - mass[i]->posY2; // vx[n] = x[n] - x[n-1]
}
- for (i=0; i<nb_mass; i++) {
+ for (i=0; i<nb_mass; i++) {
// clear forces
- mass[i]->out_forceX = mass[i]->forceX;
+ mass[i]->out_forceX = mass[i]->forceX;
mass[i]->forceX = 0; // Fx[n] = 0
- mass[i]->out_forceY = mass[i]->forceY;
+ mass[i]->out_forceY = mass[i]->forceY;
mass[i]->forceY = 0; // Fy[n] = 0
- }
+ }
}
// -------------------------------------------------------------- MASSES
// ----------------------------------------------------------------------
- void m_mass(int argc,t_atom *argv)
- // add a mass
- // Id, nbr, mobile, invM, speedX Y, posX Y, forceX Y
+ void m_mass(int argc,t_atom *argv)
+ // add a mass
+ // Id, nbr, mobile, invM, speedX Y, posX Y, forceX Y
{
t_atom sortie[6], aux[2];
t_float M;
@@ -192,24 +201,24 @@ protected:
if (argc != 5)
error("mass : Id mobile mass X Y");
- mass[nb_mass] = new t_mass; // new mass
+ mass[nb_mass] = new t_mass; // new mass
mass[nb_mass]->Id = GetSymbol(argv[0]); // ID
mass[nb_mass]->mobile = GetInt(argv[1]); // mobile
if (GetFloat(argv[2])==0)
M=1;
else M = GetFloat(argv[2]);
mass[nb_mass]->invM = 1/(M); // invM
- mass[nb_mass]->speedX = 0; // vx[n]
+ mass[nb_mass]->speedX = 0; // vx[n]
mass[nb_mass]->posX = GetFloat(argv[3]); // x[n]
- mass[nb_mass]->posX2 = GetFloat(argv[3]); // x[n-1]
+ mass[nb_mass]->posX2 = GetFloat(argv[3]); // x[n-1]
mass[nb_mass]->forceX = 0; // Fx[n]
- mass[nb_mass]->speedY = 0; // vy[n]
+ mass[nb_mass]->speedY = 0; // vy[n]
mass[nb_mass]->posY = GetFloat(argv[4]); // y[n]
- mass[nb_mass]->posY2 = GetFloat(argv[4]); // y[n-1]
+ mass[nb_mass]->posY2 = GetFloat(argv[4]); // y[n-1]
mass[nb_mass]->forceY = 0; // Fy[n]
mass[nb_mass]->nbr = id_mass; // id number
nb_mass++ ;
- id_mass++;
+ id_mass++;
nb_mass = min ( nb_max_mass -1, nb_mass );
SetFloat((sortie[0]),id_mass-1);
SetSymbol((sortie[1]),GetSymbol(argv[0]));
@@ -220,8 +229,8 @@ protected:
ToOutAnything(1,S_Mass,6,sortie);
}
- void m_forceX(int argc,t_atom *argv)
- {
+ void m_forceX(int argc,t_atom *argv)
+ {
// add a force to mass(es) named Id
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -236,8 +245,8 @@ protected:
}
}
- void m_forceY(int argc,t_atom *argv)
- {
+ void m_forceY(int argc,t_atom *argv)
+ {
// add a force to mass(es) named Id
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -253,8 +262,8 @@ protected:
}
void m_posX(int argc,t_atom *argv)
- {
- // displace mass(es) named Id to a certain position
+ {
+ // displace mass(es) named Id to a certain position
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -270,8 +279,8 @@ protected:
}
void m_posY(int argc,t_atom *argv)
- {
- // displace mass(es) named Id to a certain position
+ {
+ // displace mass(es) named Id to a certain position
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -288,8 +297,8 @@ protected:
}
void m_set_mobile(int argc,t_atom *argv)
- {
- // set mass No to mobile
+ {
+ // set mass No to mobile
t_int i,aux;
if (argc != 1)
@@ -305,8 +314,8 @@ protected:
}
void m_set_fixe(int argc,t_atom *argv)
- {
- // set mass No to fixed
+ {
+ // set mass No to fixed
t_int i,aux;
if (argc != 1)
@@ -325,7 +334,8 @@ protected:
{
// Delete mass
t_int i,nb_link_delete=0;
- t_atom sortie[6], aux[nb_link];
+ t_atom sortie[6];
+ NEWARR(t_atom,aux,nb_link);
if (argc != 1)
error("deleteMass : Nomass");
@@ -356,11 +366,12 @@ protected:
ToOutAnything(1,S_Mass_deleted,6,sortie);
break;
}
+ DELARR(aux);
}
void m_Xmax(int argc,t_atom *argv)
- {
+ {
// set X max
if (argc != 1)
error("Xmax : Value");
@@ -368,14 +379,14 @@ protected:
}
void m_Xmin(int argc,t_atom *argv)
- {
+ {
// set X min
if (argc != 1)
error("Xmin : Value");
Xmin = GetFloat(argv[0]);
}
void m_Ymax(int argc,t_atom *argv)
- {
+ {
// set Y max
if (argc != 1)
error("Ymax : Value");
@@ -383,7 +394,7 @@ protected:
}
void m_Ymin(int argc,t_atom *argv)
- {
+ {
// set Y min
if (argc != 1)
error("Ymin : Value");
@@ -394,9 +405,9 @@ protected:
// ---------------------------------------------------------------------
void m_link(int argc,t_atom *argv)
- // add a link
- // Id, *mass1, *mass2, K1, D1, D2, (Lmin,Lmax)
- {
+ // add a link
+ // Id, *mass1, *mass2, K1, D1, D2, (Lmin,Lmax)
+ {
t_atom sortie[7], aux[2];
t_int i;
@@ -409,7 +420,7 @@ protected:
link[nb_link]->mass1 = mass[i];
else if(mass[i]->nbr==GetAInt(argv[2])) // pointer to mass2
link[nb_link]->mass2 = mass[i];
- link[nb_link]->K1 = GetFloat(argv[3]); // K1
+ link[nb_link]->K1 = GetFloat(argv[3]); // K1
link[nb_link]->D1 = GetFloat(argv[4]); // D1
link[nb_link]->D2 = GetFloat(argv[5]); // D2
link[nb_link]->longx = link[nb_link]->mass1->posX - link[nb_link]->mass2->posX; // Lx[0]
@@ -432,7 +443,7 @@ protected:
break;
}
nb_link++;
- id_link++;
+ id_link++;
nb_link = min ( nb_max_link -1, nb_link );
SetFloat((sortie[0]),id_link-1);
SetSymbol((sortie[1]),link[nb_link-1]->Id);
@@ -441,15 +452,17 @@ protected:
SetFloat((sortie[4]),link[nb_link-1]->K1);
SetFloat((sortie[5]),link[nb_link-1]->D1);
SetFloat((sortie[6]),link[nb_link-1]->D2);
- ToOutAnything(1,S_Link,7,sortie);
+ ToOutAnything(1,S_Link,7,sortie);
}
void m_ilink(int argc,t_atom *argv)
- // add interactor link
- // Id, Id masses1, Id masses2, K1, D1, D2, (Lmin, Lmax)
- {
+ // add interactor link
+ // Id, Id masses1, Id masses2, K1, D1, D2, (Lmin, Lmax)
+ {
t_atom aux[2], arglist[8];
- t_int i,j, imass1[nb_mass], nbmass1=0, imass2[nb_mass], nbmass2=0;
+ t_int i,j, nbmass1=0, nbmass2=0;
+ NEWARR(t_int,imass1,nb_mass);
+ NEWARR(t_int,imass2,nb_mass);
t_symbol *Id1, *Id2;
if (argc < 6 || argc > 8)
@@ -490,10 +503,12 @@ protected:
}
m_link(argc,arglist);
}
+ DELARR(imass1);
+ DELARR(imass2);
}
void m_setK(int argc,t_atom *argv)
- {
+ {
// set rigidity of link(s) named Id
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -509,7 +524,7 @@ protected:
}
void m_setD(int argc,t_atom *argv)
- {
+ {
// set damping of link(s) named Id
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -525,7 +540,7 @@ protected:
}
void m_setD2(int argc,t_atom *argv)
- {
+ {
// set damping of link(s) named Id
t_int i;
const t_symbol *sym = GetSymbol(argv[0]);
@@ -742,7 +757,7 @@ protected:
void m_mass_dumpl()
// List of masses positions on first outlet
{
- t_atom sortie[2*nb_mass];
+ NEWARR(t_atom,sortie,2*nb_mass);
t_int i;
for (i=0; i<nb_mass; i++) {
@@ -750,12 +765,13 @@ protected:
SetFloat((sortie[2*i+1]),mass[i]->posY);
}
ToOutAnything(0, S_massesPosL, 2*nb_mass, sortie);
+ DELARR(sortie);
}
void m_force_dumpl()
// List of masses forces on first outlet
{
- t_atom sortie[2*nb_mass];
+ NEWARR(t_atom,sortie,2*nb_mass);
t_int i;
for (i=0; i<nb_mass; i++) {
@@ -763,6 +779,7 @@ protected:
SetFloat((sortie[2*i+1]),mass[i]->out_forceY);
}
ToOutAnything(0, S_massesForcesL, 2*nb_mass, sortie);
+ DELARR(sortie);
}
void m_info_dumpl()
@@ -797,7 +814,7 @@ protected:
// -------------------------------------------------------------- GLOBAL VARIABLES
// --------------------------------------------------------------------------------
- t_link * link[nb_max_link];
+ t_link * link[nb_max_link];
t_mass * mass[nb_max_mass];
t_float Xmin, Xmax, Ymin, Ymax;
int nb_link, nb_mass, id_mass, id_link;