aboutsummaryrefslogtreecommitdiff
path: root/msd
diff options
context:
space:
mode:
authorThomas Grill <xovo@users.sourceforge.net>2005-04-26 11:39:49 +0000
committerThomas Grill <xovo@users.sourceforge.net>2005-04-26 11:39:49 +0000
commit3d7f8e6f9f4c708c6130ea0a63c2eb139f34327a (patch)
tree3feaf8b197c932b421dffce6cc5d24a8c41695df /msd
parent8ea100baa14f1f3f663b7a72a79f67aff2609a09 (diff)
some small changes for Windows compilation
svn path=/trunk/externals/nusmuk/; revision=2825
Diffstat (limited to 'msd')
-rwxr-xr-xmsd/main.cpp147
1 files changed, 82 insertions, 65 deletions
diff --git a/msd/main.cpp b/msd/main.cpp
index 2e4e5c2..69dfbd5 100755
--- a/msd/main.cpp
+++ b/msd/main.cpp
@@ -39,8 +39,8 @@
// define constants
#define MSD_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,33 +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_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, longueur, long_min, long_max;
t_float distance_old;
} t_link;
-
class msd:
public flext_base
{
@@ -105,7 +112,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];
@@ -113,8 +120,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;
@@ -125,11 +132,11 @@ protected:
void m_bang()
{
- t_float F=0,Fx=0,distance,vitesse, X_new;
+ t_float F=0,Fx=0,distance,vitesse, X_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 = link[i]->mass1->posX-link[i]->mass2->posX; // L[n] = x1 - x2
if (distance < 0)
@@ -141,37 +148,37 @@ protected:
F += link[i]->D1 * (distance - link[i]->distance_old) ; // F[n] = F[n] + D1 (L[n] - L[n-1])
if (distance != 0)
Fx = F * (link[i]->mass1->posX - link[i]->mass2->posX)/distance; // Fx = F * Lx[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]->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]
mass[i]->speedX = mass[i]->posX - mass[i]->posX2; // 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
- }
+ }
}
// -------------------------------------------------------------- MASSES
// ----------------------------------------------------------------------
- void m_mass(int argc,t_atom *argv)
- // add a mass
- // Id, nbr, mobile, invM, speedX, posX, forceX
+ void m_mass(int argc,t_atom *argv)
+ // add a mass
+ // Id, nbr, mobile, invM, speedX, posX, forceX
{
t_atom sortie[5], aux[2];
t_float M;
@@ -179,20 +186,20 @@ protected:
if (argc != 4)
error("mass : Id mobile mass X");
- 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); // 1/M
- 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]->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]));
@@ -202,8 +209,8 @@ protected:
ToOutAnything(1,S_Mass,5,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]);
@@ -219,8 +226,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 = GetASymbol(argv[0]);
@@ -245,8 +252,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)
@@ -262,8 +269,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)
@@ -282,7 +289,8 @@ protected:
{
// Delete mass
t_int i,nb_link_delete=0;
- t_atom sortie[5], aux[nb_link];
+ t_atom sortie[5];
+ NEWARR(t_atom,aux,nb_link);
if (argc != 1)
error("deleteMass : Nomass");
@@ -312,11 +320,13 @@ protected:
ToOutAnything(1,S_Mass_deleted,5,sortie);
break;
}
+
+ DELARR(aux);
}
void m_Xmax(int argc,t_atom *argv)
- {
+ {
// set X max
if (argc != 1)
error("Xmax : Value");
@@ -324,7 +334,7 @@ protected:
}
void m_Xmin(int argc,t_atom *argv)
- {
+ {
// set X min
if (argc != 1)
error("Xmin : Value");
@@ -335,9 +345,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;
@@ -350,7 +360,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]
@@ -375,7 +385,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);
@@ -384,15 +394,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)
@@ -433,10 +445,13 @@ 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]);
@@ -452,7 +467,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]);
@@ -468,7 +483,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]);
@@ -656,7 +671,7 @@ protected:
{
if (auxarg2==mass[i]->Id)
{
- SETFLOAT(&sortie[0],mass[i]->speedX);
+ SetFloat(sortie[0],mass[i]->speedX);
ToOutAnything(0,S_massesSpeedsId,1,sortie);
}
}
@@ -670,25 +685,27 @@ protected:
void m_mass_dumpl()
// List of masses positions on first outlet
{
- t_atom sortie[nb_mass];
+ NEWARR(t_atom,sortie,nb_mass);
t_int i;
for (i=0; i<nb_mass; i++) {
SetFloat((sortie[i]),mass[i]->posX);
}
ToOutAnything(0, S_massesPosL, nb_mass, sortie);
+ DELARR(sortie);
}
void m_force_dumpl()
// List of masses forces on first outlet
{
- t_atom sortie[nb_mass];
+ NEWARR(t_atom,sortie,nb_mass);
t_int i;
for (i=0; i<nb_mass; i++) {
SetFloat((sortie[i]),mass[i]->out_forceX);
}
ToOutAnything(0, S_massesForcesL, nb_mass, sortie);
+ DELARR(sortie);
}
void m_info_dumpl()
@@ -723,7 +740,7 @@ protected:
// -------------------------------------------------------------- PROTECTED VARIABLES
// -----------------------------------------------------------------------------------
- t_link * link[nb_max_link]; // Pointer table on links
+ t_link * link[nb_max_link]; // Pointer table on links
t_mass * mass[nb_max_mass]; // Pointer table on masses
t_float Xmin, Xmax; // Limit values
int nb_link, nb_mass, id_mass, id_link;