1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
/*
* pmpd_core.c
*/
t_float sign_ch(t_float v)
{
return v > 0 ? 1 : -1;
}
t_float pow_ch(t_float x, t_float y)
{
return x > 0 ? pow(x,y) : -pow(-x,y);
}
t_float sqr(t_float x)
{
return x*x ;
}
t_float mix(t_float X, t_float Y, t_float mix)
{
return (1-mix)*X + mix*Y ;
}
t_float tabread2(t_pmpd *x, t_float pos, t_symbol *array)
{
t_garray *a;
int npoints;
t_word *vec;
t_float posx;
if (!(a = (t_garray *)pd_findbyclass(array, garray_class)))
pd_error(x, "%s: no such array", array->s_name);
else if (!garray_getfloatwords(a, &npoints, &vec))
pd_error(x, "%s: bad template for tabLink", array->s_name);
else
{
posx = fabs(pos)*npoints;
int n=posx;
if (n >= npoints - 1)
return (sign_ch(pos)*vec[npoints-1].w_float);
float fract = posx-n;
return (sign_ch(pos) * ( fract*vec[n+1].w_float+(1-fract)*vec[n].w_float));
}
return( pos); // si il y a un pb sur le tableau, on renvoie l'identité
}
void pmpd_reset(t_pmpd *x)
{
x->nb_link = 0;
x->nb_mass = 0;
x->minX = -1000000;
x->maxX = 1000000;
x->grab = 0;
}
void *pmpd_new()
{
t_pmpd *x = (t_pmpd *)pd_new(pmpd_class);
pmpd_reset(x);
x->main_outlet=outlet_new(&x->x_obj, 0);
// x->info_outlet=outlet_new(&x->x_obj, 0); // TODO
return (void *)x;
}
void pmpd_bang(t_pmpd *x)
{ // this part is doing all the PM
t_float F, L, absL, tmpX;
t_int i;
// post("bang");
for (i=0; i<x->nb_mass; i++)
// compute new masses position
if (x->mass[i].mobile > 0) // only if mobile
{
// amplify force that opose to movement
if (x->mass[i].overdamp != 0)
{
tmpX = x->mass[i].speedX * x->mass[i].forceX;
tmpX = min(0,tmpX); // overdamped only if force opose movment
tmpX *= -x->mass[i].overdamp;
tmpX += 1;
x->mass[i].forceX *= tmpX;
}
x->mass[i].speedX += x->mass[i].forceX * x->mass[i].invM;
// x->mass[i].forceX = 0;
x->mass[i].posX += x->mass[i].speedX ;
// x->mass[i].posX = min(x->maxX,max(x->minX,x->mass[i].posX));
if ( (x->mass[i].posX < x->minX) || (x->mass[i].posX > x->maxX) )
{
tmpX = min(x->maxX,max(x->minX,x->mass[i].posX));
x->mass[i].speedX -= x->mass[i].posX - tmpX;
x->mass[i].posX = tmpX;
}
x->mass[i].forceX = -x->mass[i].D2 * x->mass[i].speedX;
}
for (i=0; i<x->nb_link; i++)
// compute link forces
{
if (x->link[i].active == 1)
{
L = x->link[i].mass1->posX - x->link[i].mass2->posX;
absL = fabs(L);
if ( (absL >= x->link[i].Lmin) && (absL < x->link[i].Lmax) && (L !=0))
{
if (x->link[i].lType == 2)
{ // K et D viennent d'une table
F = x->link[i].D * tabread2(x, (L - x->link[i].distance) / x->link[i].D_L, x->link[i].arrayD);
F += x->link[i].K * tabread2(x, L / x->link[i].K_L, x->link[i].arrayK);
}
else
{
F = x->link[i].D * (L - x->link[i].distance) ;
F += x->link[i].K * sign_ch(L) * pow_ch( absL - x->link[i].L, x->link[i].Pow);
}
x->link[i].mass1->forceX -= F;
x->link[i].mass2->forceX += F;
x->link[i].forceX = F; // save for latter use
}
x->link[i].distance=L;
}
}
}
void pmpd_mass(t_pmpd *x, t_symbol *s, int argc, t_atom *argv)
// t_symbol *Id, t_float mobile, t_float M, t_float posX)
{ // add a mass
x->mass[x->nb_mass].Id = gensym("mass");
if ((argc >= 1) && (argv[0].a_type == A_SYMBOL))
x->mass[x->nb_mass].Id = atom_getsymbolarg(0,argc,argv);
x->mass[x->nb_mass].mobile = 1;
if ((argc >= 2) && (argv[1].a_type == A_FLOAT))
x->mass[x->nb_mass].mobile = (int) atom_getfloatarg(1, argc, argv);
t_float M = 1;
if ((argc >= 3) && (argv[2].a_type == A_FLOAT))
M = atom_getfloatarg(2, argc, argv);
if (M<=0) M=1;
x->mass[x->nb_mass].invM = 1/M;
x->mass[x->nb_mass].speedX = 0;
x->mass[x->nb_mass].posX = 0;
if ((argc >= 4) && (argv[3].a_type == A_FLOAT))
x->mass[x->nb_mass].posX = atom_getfloatarg(3, argc, argv);
x->mass[x->nb_mass].forceX = 0;
x->mass[x->nb_mass].num = x->nb_mass;
x->mass[x->nb_mass].D2 = 0;
x->mass[x->nb_mass].overdamp = 0;
x->nb_mass++ ;
x->nb_mass = min ( nb_max_mass -1, x->nb_mass );
}
void pmpd_create_link(t_pmpd *x, t_symbol *Id, int mass1, int mass2, t_float K, t_float D, t_float Pow, t_float Lmin, t_float Lmax, t_int type)
{ // create a link based on mass number
if ((x->nb_mass>0) && (mass1 != mass2) && (mass1 >= 0) && (mass2 >= 0) && (mass1 < x->nb_mass) && (mass2 < x->nb_mass) )
{
x->link[x->nb_link].lType = type;
x->link[x->nb_link].Id = Id;
x->link[x->nb_link].active = 1;
x->link[x->nb_link].mass1 = &x->mass[mass1];
x->link[x->nb_link].mass2 = &x->mass[mass2];
x->link[x->nb_link].K = K;
x->link[x->nb_link].D = D;
x->link[x->nb_link].L = x->mass[mass1].posX - x->mass[mass2].posX;
x->link[x->nb_link].Pow = Pow;
x->link[x->nb_link].Lmin = Lmin;
x->link[x->nb_link].Lmax = Lmax;
x->link[x->nb_link].distance = x->link[x->nb_link].L;
x->link[x->nb_link].forceX = 0 ;
x->nb_link++ ;
x->nb_link = min ( nb_max_link -1, x->nb_link );
}
}
void pmpd_link(t_pmpd *x, t_symbol *s, int argc, t_atom *argv)
{ // add a link : Id, *mass1, *mass2, K, D, Pow, Lmin, Lmax;
int i, j;
t_symbol *Id = gensym("link");
if ((argc >= 1) && (argv[0].a_type == A_SYMBOL))
Id = atom_getsymbolarg(0,argc,argv);
t_float K = 0;
if ((argc >= 4) && (argv[3].a_type == A_FLOAT))
K = atom_getfloatarg(3, argc, argv);
t_float D = 0;
if ((argc >= 5) && (argv[4].a_type == A_FLOAT))
D = atom_getfloatarg(4, argc, argv);
t_float Pow = 1;
if (argc > 5) Pow = atom_getfloatarg(5, argc, argv);
t_float Lmin = -1000000;
if (argc > 6) Lmin = atom_getfloatarg(6, argc, argv);
t_float Lmax = 1000000;
if (argc > 7) Lmax = atom_getfloatarg(7, argc, argv);
// post("%d,%d, %f,%f", mass1, mass2, K, D);
if ( ( argc > 2 ) && ( argv[1].a_type == A_FLOAT ) && ( argv[2].a_type == A_FLOAT ) )
{
pmpd_create_link(x, Id, atom_getfloatarg(1, argc, argv), atom_getfloatarg(2, argc, argv), K, D, Pow, Lmin, Lmax, 0);
}
else if ( ( argc > 2 ) && ( argv[1].a_type == A_SYMBOL ) && ( argv[2].a_type == A_FLOAT ) )
{
for (i=0; i< x->nb_mass; i++)
{
if ( atom_getsymbolarg(1,argc,argv) == x->mass[i].Id)
{
pmpd_create_link(x, Id, i, atom_getfloatarg(2, argc, argv), K, D, Pow, Lmin, Lmax, 0);
}
}
}
else if ( ( argc > 2 ) && ( argv[1].a_type == A_FLOAT ) && ( argv[2].a_type == A_SYMBOL ) )
{
for (i=0; i< x->nb_mass; i++)
{
if ( atom_getsymbolarg(2,argc,argv) == x->mass[i].Id)
{
pmpd_create_link(x, Id, atom_getfloatarg(1, argc, argv), i, K, D, Pow, Lmin, Lmax, 0);
}
}
}
else if ( ( argc > 2 ) && ( argv[1].a_type == A_SYMBOL ) && ( argv[2].a_type == A_SYMBOL ) )
{
for (i=0; i< x->nb_mass; i++)
{
for (j=0; j< x->nb_mass; j++)
{
if ( (atom_getsymbolarg(1,argc,argv) == x->mass[i].Id)&(atom_getsymbolarg(2,argc,argv) == x->mass[j].Id))
{
if (!( (x->mass[i].Id == x->mass[j].Id) && (i>j) ))
// si lien entre 2 serie de masses identique entres elle, alors on ne creer qu'un lien sur 2, pour evider les redondances
pmpd_create_link(x, Id, i, j, K, D, Pow, Lmin, Lmax, 0);
}
}
}
}
else
pmpd_create_link(x, Id, 0, 1, K, D, Pow, Lmin, Lmax, 0);
}
void pmpd_tabLink(t_pmpd *x, t_symbol *s, int argc, t_atom *argv)
{
int i, j;
t_symbol *Id = atom_getsymbolarg(0,argc,argv);
int mass1 = atom_getfloatarg(1, argc, argv);
int mass2 = atom_getfloatarg(2, argc, argv);
t_symbol *arrayK = atom_getsymbolarg(3,argc,argv);
t_float Kl = atom_getfloatarg(4, argc, argv);
if (Kl <= 0) Kl = 1;
t_symbol *arrayD = atom_getsymbolarg(5,argc,argv);
t_float Dl = atom_getfloatarg(6, argc, argv);
if (Dl <= 0) Dl = 1;
if ( ( argv[1].a_type == A_FLOAT ) && ( argv[2].a_type == A_FLOAT ) )
{
pmpd_create_link(x, Id, mass1, mass2, 1, 1, 1, -1000000, 1000000, 2);
x->link[x->nb_link-1].arrayK = arrayK;
x->link[x->nb_link-1].arrayD = arrayD;
x->link[x->nb_link-1].K_L = Kl;
x->link[x->nb_link-1].D_L = Dl;
}
else
if ( ( argv[1].a_type == A_SYMBOL ) && ( argv[2].a_type == A_FLOAT ) )
{
for (i=0; i< x->nb_mass; i++)
{
if ( atom_getsymbolarg(1,argc,argv) == x->mass[i].Id)
{
pmpd_create_link(x, Id, i, mass2, 1, 1, 1, -1000000, 1000000, 2);
x->link[x->nb_link-1].arrayK = arrayK;
x->link[x->nb_link-1].arrayD = arrayD;
x->link[x->nb_link-1].K_L = Kl;
x->link[x->nb_link-1].D_L = Dl;
}
}
}
else
if ( ( argv[1].a_type == A_FLOAT ) && ( argv[2].a_type == A_SYMBOL ) )
{
for (i=0; i< x->nb_mass; i++)
{
if ( atom_getsymbolarg(2,argc,argv) == x->mass[i].Id)
{
pmpd_create_link(x, Id, mass1, i, 1, 1, 1, -1000000, 1000000, 2);
x->link[x->nb_link-1].arrayK = arrayK;
x->link[x->nb_link-1].arrayD = arrayD;
x->link[x->nb_link-1].K_L = Kl;
x->link[x->nb_link-1].D_L = Dl;
}
}
}
else
if ( ( argv[1].a_type == A_SYMBOL ) && ( argv[2].a_type == A_SYMBOL ) )
{
for (i=0; i< x->nb_mass; i++)
{
for (j=0; j< x->nb_mass; j++)
{
if ( (atom_getsymbolarg(1,argc,argv) == x->mass[i].Id)&(atom_getsymbolarg(2,argc,argv) == x->mass[j].Id))
{
if (!( (x->mass[i].Id == x->mass[j].Id) && (i>j) ))
// si lien entre 2 serie de masses identique entres elle, alors on ne creer qu'un lien sur 2, pour evider les redondances
{
pmpd_create_link(x, Id, i, j, 1, 1, 1, -1000000, 1000000, 2);
x->link[x->nb_link-1].arrayK = arrayK;
x->link[x->nb_link-1].arrayD = arrayD;
x->link[x->nb_link-1].K_L = Kl;
x->link[x->nb_link-1].D_L = Dl;
}
}
}
}
}
}
void pmpd_delLink_int(t_pmpd *x, int dellink)
{
int i;
if ( ( dellink < x->nb_link ) && ( dellink >= 0) )
{
x->nb_link--;
for (i=dellink; i < x->nb_link; i++)
x->link[i]=x->link[i+1];
}
}
void pmpd_delLink(t_pmpd *x, t_symbol *s, int argc, t_atom *argv)
{
int i,nb_toremove;
if ( (argc > 0) && ( argv[0].a_type == A_FLOAT ) )
pmpd_delLink_int(x, atom_getfloatarg(0, argc, argv));
if ( (argc > 0) && ( argv[0].a_type == A_SYMBOL ) )
/* for (i=0; i<x->nb_link; )
if ( atom_getsymbolarg(0,argc,argv) == x->link[i].Id )
pmpd_delLink_int(x, i);
else i++;
*/
{
nb_toremove=0;
for (i=0; i < x->nb_link; i++)
{
if ( atom_getsymbolarg(0,argc,argv) == x->link[i].Id )
{
nb_toremove++;
}
else
{
if (nb_toremove > 0)
{
x->link[i-nb_toremove]=x->link[i];
}
}
}
x->nb_link -= nb_toremove;
}
}
void pmpd_delMass_int(t_pmpd *x, int delmass)
{
int i,nb_toremove;
if ( ( delmass < x->nb_mass ) && ( delmass >= 0) )
{
/* for (i=0; i < x->nb_link; ) // delete link connected to the mass to delete
{
if ( (x->link[i].mass1->num == delmass) || (x->link[i].mass2->num == delmass) )
pmpd_delLink_int(x, i);
else i++;
// post("loop %d sur %d", i, x->nb_link);
}*/
nb_toremove=0;
for (i=0; i < x->nb_link; i++)
{
if ( (x->link[i].mass1->num == delmass) || (x->link[i].mass2->num == delmass) )
{
nb_toremove++;
}
else
{
if (nb_toremove > 0)
{
x->link[i-nb_toremove]=x->link[i];
}
}
}
x->nb_link -= nb_toremove;
for (i=0; i < x->nb_link; i++) // change pointer to mass that index moved
{
if (x->link[i].mass1->num > delmass )
{ x->link[i].mass1 = &x->mass[x->link[i].mass1->num-1]; }
else if (x->link[i].mass2->num > delmass )
{ x->link[i].mass2 = &x->mass[x->link[i].mass2->num-1]; }
}
x->nb_mass--;
for (i=delmass; i < x->nb_mass; i++)
{
x->mass[i]=x->mass[i+1];
x->mass[i].num=i;
}
}
}
void pmpd_delMass(t_pmpd *x, t_symbol *s, int argc, t_atom *argv)
{
int i, j, delmass, nb_toremove;
if ( (argc > 0) && ( argv[0].a_type == A_FLOAT ) )
pmpd_delMass_int(x, atom_getfloatarg(0, argc, argv));
if ( (argc > 0) && ( argv[0].a_type == A_SYMBOL ) )
/*
for (i=0; i<x->nb_mass; )
if ( atom_getsymbolarg(0,argc,argv) == x->mass[i].Id )
pmpd_delMass_int(x, i);
else i++;
*/
{
nb_toremove=0;
for (i=0; i < x->nb_link; i++) // revove link associated with this mass Id
{
if ( (x->link[i].mass1->Id == atom_getsymbolarg(0, argc, argv)) || (x->link[i].mass2->Id == atom_getsymbolarg(0, argc, argv)) )
{
nb_toremove++;
}
else
{
if (nb_toremove > 0)
{
x->link[i-nb_toremove]=x->link[i];
}
}
}
x->nb_link -= nb_toremove;
nb_toremove=0;
for (i=0; i < x->nb_mass; i++) // remove mass
{
if ( atom_getsymbolarg(0,argc,argv) == x->mass[i].Id )
{
nb_toremove++;
// post("remove mass %d", i);
}
else
{
if (nb_toremove > 0)
{
x->mass[i-nb_toremove]=x->mass[i];
x->mass[i-nb_toremove].num = i-nb_toremove;
for (j=0; j < x->nb_link; j++) // for every link replace mass with the new pointer
{
if (x->link[j].mass1->num == i )
{
// post("mass %d : relocate link %d to mass %d",i, j, i-nb_toremove);
x->link[j].mass1 = &x->mass[i-nb_toremove];
}
else if (x->link[j].mass2->num == i )
{
// post("mass %d : relocate link2 %d to mass %d",i, j, i-nb_toremove);
x->link[j].mass2 = &x->mass[i-nb_toremove];
}
}
}
}
}
x->nb_mass -= nb_toremove;
}
}
|