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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
|
/*
msd - mass spring damper model for Pure Data or Max/MSP
Copyright (C) 2005 Nicolas Montgermont
Written by Nicolas Montgermont for a Master's train in Acoustic,
Signal processing and Computing Applied to Music (ATIAM, Paris 6)
at La Kitchen supervised by Cyrille Henry.
Optimized by Thomas Grill for Flext
Based on Pure Data by Miller Puckette and others
Based on pmpd by Cyrille Henry
Contact : Nicolas Montgermont, montgermont@la-kitchen.fr
Cyrille Henry, Cyrille.Henry@la-kitchen.fr
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Version 0.05 -- 28.04.2005
*/
// include flext header
#include <flext.h>
#include <flmap.h>
#include <math.h>
#include <string.h>
#include <vector>
// define constants
#define MSD_VERSION 0.05
// check for appropriate flext version
#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 500)
#error You need at least flext version 0.5.0
#endif
#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
inline t_float sqr(t_float x) { return x*x; }
template<int N> class Link;
template<int N>
class LinkList
: public std::vector<Link<N> *>
{
public:
void insert(Link<N> *l)
{
for(typename LinkList::iterator it = begin(); it != end(); ++it)
if(*it == l) return;
// not found -> add
push_back(l);
}
void erase(Link<N> *l)
{
for(typename LinkList::iterator it = begin(); it != end(); ++it)
if(*it == l) {
// found
std::vector<Link<N> *>::erase(it);
return;
}
}
};
template<int N>
class Mass {
public:
t_int nbr;
const t_symbol *Id;
t_float M,invM;
t_float speed[N];
t_float pos[N];
t_float pos2[N];
t_float force[N];
t_float out_force[N];
LinkList<N> links;
Mass(t_int n,const t_symbol *id,bool mob,t_float m,t_float p[N])
: nbr(n),Id(id)
, M(m)
{
if(mob) setMobile(); else setFixed();
for(int i = 0; i < N; ++i) {
pos[i] = pos2[i] = p[i];
force[i] = speed[i] = 0;
}
}
inline void setForce(int n,t_float f) { force[n] = f; }
inline void setForce(t_float f[N])
{
for(int i = 0; i < N; ++i) setForce(i,f[i]);
}
inline void setPos(int n,t_float p) { pos[n] = pos2[n] = p; }
inline void setPos(t_float p[N])
{
for(int i = 0; i < N; ++i) setPos(i,p[i]);
}
inline bool getMobile() const { return invM != 0; }
inline void setMobile() { invM = M?1.f/M:1.; }
inline void setFixed() { invM = 0; }
inline void compute(t_float limit[N][2])
{
for(int i = 0; i < N; ++i) {
t_float pold = pos[i];
t_float pnew = force[i] * invM + 2*pold - pos2[i]; // x[n] =Fx[n]/M+2x[n]-x[n-1]
if(pnew < limit[i][0]) pnew = limit[i][0]; else if(pnew > limit[i][1]) pnew = limit[i][1];
speed[i] = (pos[i] = pnew) - (pos2[i] = pold); // x[n-2] = x[n-1], x[n-1] = x[n],vx[n] = x[n] - x[n-1]
// clear forces
out_force[i] = force[i];
force[i] = 0; // Fx[n] = 0
}
}
static inline t_float dist(const Mass &m1,const Mass &m2)
{
if(N == 1)
return fabs(m1.pos[0]-m2.pos[0]); // L[n] = |x1 - x2|
else {
t_float distance = 0;
for(int i = 0; i < N; ++i) distance += sqr(m1.pos[i]-m2.pos[i]);
return sqrt(distance);
}
}
};
template<int N>
class Link {
public:
t_int nbr;
const t_symbol *Id;
Mass<N> *mass1,*mass2;
t_float K1, D1, D2;
t_float longueur, long_min, long_max;
t_float distance_old;
Link(t_int n,const t_symbol *id,Mass<N> *m1,Mass<N> *m2,t_float k1,t_float d1,t_float d2,t_float lmin,t_float lmax)
: nbr(n),Id(id)
, mass1(m1),mass2(m2)
, K1(k1),D1(d1),D2(d2)
, long_min(lmin),long_max(lmax)
{
distance_old = longueur = Mass<N>::dist(*mass1,*mass2); // L[n-1]
mass1->links.insert(this);
mass2->links.insert(this);
}
~Link()
{
mass1->links.erase(this);
mass2->links.erase(this);
}
// compute link forces
inline void compute()
{
Mass<N> *m1 = mass1,*m2 = mass2; // cache locally
t_float distance = Mass<N>::dist(*m1,*m2);
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
const t_float F = (K1 * (distance - longueur) + D1 * (distance - distance_old))/distance ; // F[n] = k1 (L[n] - L[0])/L[n] + D1 (L[n] - L[n-1])/L[n]
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]
}
}
distance_old = distance; // L[n-1] = L[n]
}
};
template <typename T>
inline T bitrev(T k)
{
T r = 0;
for(int i = 0; i < sizeof(k)*8; ++i) r = (r<<1)|(k&1),k >>= 1;
return r;
}
// use bit-reversed key to pseudo-balance the map tree
template <typename T>
class IndexMap
: TablePtrMap<unsigned int,T,64>
{
public:
typedef TablePtrMap<unsigned int,T,64> Parent;
virtual ~IndexMap() { reset(); }
void reset()
{
// delete all associated items
for(typename Parent::iterator it(*this); it; ++it) delete it.data();
Parent::clear();
}
inline int size() const { return Parent::size(); }
inline T insert(unsigned int k,T v) { return Parent::insert(bitrev(k),v); }
inline T find(unsigned int k) { return Parent::find(bitrev(k)); }
inline T remove(unsigned int k) { return Parent::remove(bitrev(k)); }
class iterator
: public Parent::iterator
{
public:
iterator() {}
iterator(IndexMap &m): Parent::iterator(m) {}
inline unsigned int key() const { return bitrev(Parent::key()); }
};
};
template <typename T>
class IDMap
: TablePtrMap<const t_symbol *,TablePtrMap<T,T,4> *,4>
{
public:
// that's the container holding the data items (masses, links) of one ID
typedef TablePtrMap<T,T,4> Container;
// that's the map for the key ID (symbol,int) relating to the data items
typedef TablePtrMap<const t_symbol *,Container *,4> Parent;
typedef typename Container::iterator iterator;
IDMap() {}
virtual ~IDMap() { reset(); }
void reset()
{
typename Parent::iterator it(*this);
for(; it; ++it) delete it.data();
Parent::clear();
}
void insert(T item)
{
Container *c = Parent::find(item->Id);
if(!c)
Parent::insert(item->Id,c = new Container);
c->insert(item,item);
}
iterator find(const t_symbol *key)
{
Container *c = Parent::find(key);
if(c)
return iterator(*c);
else
return iterator();
}
void erase(T item)
{
Container *c = Parent::find(item->Id);
if(c) c->remove(item);
}
};
template<int N>
class msdN:
public flext_base
{
FLEXT_HEADER_S(msdN,flext_base,setup) //class with setup
public:
// constructor with no arguments
msdN(int argc,t_atom *argv)
: id_mass(0),id_link(0)
{
for(int i = 0; i < N; ++i) limit[i][0] = -1.e10,limit[i][1] = 1.e10;
// --- define inlets and outlets ---
AddInAnything("bang, reset, etc."); // default inlet
AddOutAnything("infos on masses"); // outlet for integer count
AddOutAnything("control"); // outlet for bang
}
virtual ~msdN() { clear(); }
protected:
// -------------------------------------------------------------- PROTECTED VARIABLES
// -----------------------------------------------------------------------------------
typedef Mass<N> t_mass;
typedef Link<N> t_link;
IndexMap<t_link *> link; // links
IDMap<t_link *> linkids; // links by name
IndexMap<t_mass *> mass; // masses
IDMap<t_mass *> massids; // masses by name
t_float limit[N][2]; // Limit values
unsigned int id_mass, id_link, mouse_grab, nearest_mass, link_deleted, mass_deleted;
// --------------------------------------------------------------- RESET
// ----------------------------------------------------------------------
void m_reset()
{
clear();
ToOutAnything(1,S_Reset,0,NULL);
}
// -------------------------------------------------------------- COMPUTE
// -----------------------------------------------------------------------
void m_bang()
{
// update all links
for (typename IndexMap<t_link *>::iterator lit(link); lit; ++lit) lit.data()->compute();
// update all masses
for (typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) mit.data()->compute(limit);
}
// -------------------------------------------------------------- MASSES
// ----------------------------------------------------------------------
// add a mass
// Id, nbr, mobile, invM, speedX, posX, forceX
void m_mass(int argc,t_atom *argv)
{
if(argc != 3+N) {
error("mass : Id mobile mass X%s%s",N >= 2?" Y":"",N >= 3?" Z":"");
return;
}
t_float pos[N];
for(int i = 0; i < N; ++i) pos[i] = GetAFloat(argv[3+i]);
t_mass *m = new t_mass(
id_mass, // index
GetSymbol(argv[0]), // ID
GetABool(argv[1]), // mobile
GetAFloat(argv[2]), // mass
pos // pos
);
outmass(S_Mass,m);
massids.insert(m);
mass.insert(id_mass++,m);
}
// add a force to mass(es) named Id or No
void m_force(int argc,t_atom *argv,int n)
{
if(argc != 2) {
error("%s - %s Syntax : Id/Nomass value",thisName(),GetString(thisTag()));
return;
}
const t_float f = GetAFloat(argv[1]);
if(IsSymbol(argv[0])) {
typename IDMap<t_mass *>::iterator it;
for(it = massids.find(GetSymbol(argv[0])); it; ++it) {
t_mass *m = it.data();
m->setForce(n,f);
}
}
else {
t_mass *m = mass.find(GetAInt(argv[0]));
if(m)
m->setForce(n,f);
else
error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
}
inline void m_forceX(int argc,t_atom *argv) { m_force(argc,argv,0); }
inline void m_forceY(int argc,t_atom *argv) { m_force(argc,argv,1); }
inline void m_forceZ(int argc,t_atom *argv) { m_force(argc,argv,2); }
// displace mass(es) named Id or No to a certain position
void m_pos(int argc,t_atom *argv,int n)
{
if(argc != 2) {
error("%s - %s Syntax : Id/Nomass value",thisName(),GetString(thisTag()));
return;
}
const t_float p = GetAFloat(argv[1]);
if(p > limit[n][1] || p < limit[n][0]) return;
if(IsSymbol(argv[0])) {
typename IDMap<t_mass *>::iterator it;
for(it = massids.find(GetSymbol(argv[0])); it; ++it)
it.data()->setPos(n,p);
}
else {
t_mass *m = mass.find(GetAInt(argv[0]));
if(m)
m->setPos(n,p);
else
error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
}
inline void m_posX(int argc,t_atom *argv) { m_pos(argc,argv,0); }
inline void m_posY(int argc,t_atom *argv) { m_pos(argc,argv,1); }
inline void m_posZ(int argc,t_atom *argv) { m_pos(argc,argv,2); }
// set mass No to mobile
void m_set_mobile(int argc,t_atom *argv,bool mob = true)
{
if (argc != 1) {
error("%s - %s Syntax : Idmass",thisName(),GetString(thisTag()));
return;
}
t_mass *m = mass.find(GetAInt(argv[0]));
if(m)
if(mob) m->setMobile();
else m->setFixed();
else
error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
// set mass No to fixed
inline void m_set_fixe(int argc,t_atom *argv) { m_set_mobile(argc,argv,false); }
// Delete mass
void m_delete_mass(int argc,t_atom *argv)
{
if (argc != 1) {
error("%s - %s Syntax : Nomass",thisName(),GetString(thisTag()));
return;
}
t_mass *m = mass.find(GetAInt(argv[0]));
if(m) {
// Delete all associated links
for(typename std::vector<t_link *>::iterator it = m->links.begin(); it < m->links.end(); ++it)
deletelink(*it);
outmass(S_Mass_deleted,m);
massids.erase(m);
mass.remove(m->nbr);
delete m;
mass_deleted = 1;
}
else
error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
// set X,Y,Z min/max
void m_limit(int argc,t_atom *argv,int n,int i)
{
if (argc != 1)
error("%s - %s Syntax : Value",thisName(),GetString(thisTag()));
else
limit[n][i] = GetAFloat(argv[0]);
}
inline void m_Xmin(int argc,t_atom *argv) { m_limit(argc,argv,0,0); }
inline void m_Ymin(int argc,t_atom *argv) { m_limit(argc,argv,1,0); }
inline void m_Zmin(int argc,t_atom *argv) { m_limit(argc,argv,2,0); }
inline void m_Xmax(int argc,t_atom *argv) { m_limit(argc,argv,0,1); }
inline void m_Ymax(int argc,t_atom *argv) { m_limit(argc,argv,1,1); }
inline void m_Zmax(int argc,t_atom *argv) { m_limit(argc,argv,2,1); }
void m_grab_mass(int argc,t_atom *argv)
{
// grab nearest mass X Y
t_mass **mi;
t_float aux, distance;
t_atom aux2[2];
bool mobil;
// if click
if (GetInt(argv[2])==1 && mass.size()>0) {
if (argc != 3)
error("grabMass : X Y click");
// first time we grab this mass?Find nearest mass
if (mouse_grab == 0) {
t_mass *m = mass.find(0);
aux = sqr(m->pos[0]-GetFloat(argv[0])) + sqr(m->pos[1]-GetFloat(argv[1]));
nearest_mass = 0;
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
distance = sqr(mit.data()->pos[0]-GetFloat(argv[0])) + sqr(mit.data()->pos[1]-GetFloat(argv[1]));
if (distance<aux) {
aux = distance;
nearest_mass = mit.data()->nbr;
}
}
}
// Set fixed if mobile
mobil = mass.find(nearest_mass)->M;
SetInt(aux2[0],nearest_mass);
if (mobil != 0)
m_set_fixe(1,aux2);
// Set XY
SetFloat(aux2[1],GetFloat(argv[0]));
m_posX(2,aux2);
SetFloat(aux2[1],GetFloat(argv[1]));
m_posY(2,aux2);
// Set mobile
if(mobil != 0)
m_set_mobile(1,aux2);
// Current grabbing on
mouse_grab = 1;
}
else
// Grabing off
mouse_grab = 0;
}
// -------------------------------------------------------------- LINKS
// ---------------------------------------------------------------------
// add a link
// Id, *mass1, *mass2, K1, D1, D2, (Lmin,Lmax)
void m_link(int argc,t_atom *argv)
{
if (argc < 6 || argc > 8) {
error("%s - %s Syntax : Id Nomass1 Nomass2 K D1 D2 (Lmin Lmax)",thisName(),GetString(thisTag()));
return;
}
t_mass *mass1 = mass.find(GetAInt(argv[1]));
t_mass *mass2 = mass.find(GetAInt(argv[2]));
if(!mass1 || !mass2) {
error("%s - %s : Index not found",thisName(),GetString(thisTag()));
return;
}
t_link *l = new t_link(
id_link,
GetSymbol(argv[0]), // ID
mass1,mass2, // pointer to mass1, mass2
GetAFloat(argv[3]), // K1
GetAFloat(argv[4]), // D1
GetAFloat(argv[5]), // D2
argc >= 7?GetFloat(argv[6]):0,
argc >= 8?GetFloat(argv[7]):32768
);
linkids.insert(l);
link.insert(id_link++,l);
outlink(S_Link,l);
}
// add interactor link
// Id, Id masses1, Id masses2, K1, D1, D2, (Lmin, Lmax)
void m_ilink(int argc,t_atom *argv)
{
if (argc < 6 || argc > 8) {
error("%s - %s Syntax : Id Idmass1 Idmass2 K D1 D2 (Lmin Lmax)",thisName(),GetString(thisTag()));
return;
}
typename IDMap<t_mass *>::iterator it1,it2,it;
it1 = massids.find(GetSymbol(argv[1]));
it2 = massids.find(GetSymbol(argv[2]));
for(; it1; ++it1) {
for(it = it2; it; ++it) {
t_link *l = new t_link(
id_link,
GetSymbol(argv[0]), // ID
it1.data(),it.data(), // pointer to mass1, mass2
GetAFloat(argv[3]), // K1
GetAFloat(argv[4]), // D1
GetAFloat(argv[5]), // D2
argc >= 7?GetFloat(argv[6]):0,
argc >= 8?GetFloat(argv[7]):32768
);
linkids.insert(l);
link.insert(id_link++,l);
outlink(S_Link,l);
}
}
}
// set rigidity of link(s) named Id
void m_setK(int argc,t_atom *argv)
{
if (argc != 2) {
error("%s - %s Syntax : IdLink Value",thisName(),GetString(thisTag()));
return;
}
t_float k1 = GetAFloat(argv[1]);
typename IDMap<t_link *>::iterator it;
for(it = linkids.find(GetSymbol(argv[0])); it; ++it)
it.data()->K1 = k1;
}
// set damping of link(s) named Id
void m_setD(int argc,t_atom *argv)
{
if (argc != 2) {
error("%s - %s Syntax : IdLink Value",thisName(),GetString(thisTag()));
return;
}
t_float d1 = GetAFloat(argv[1]);
typename IDMap<t_link *>::iterator it;
for(it = linkids.find(GetSymbol(argv[0])); it; ++it)
it.data()->D1 = d1;
}
// set damping of link(s) named Id
void m_setD2(int argc,t_atom *argv)
{
if (argc != 2) {
error("%s - %s Syntax : IdLink Value",thisName(),GetString(thisTag()));
return;
}
t_float d2 = GetAFloat(argv[1]);
typename IDMap<t_link *>::iterator it;
for(it = linkids.find(GetSymbol(argv[0])); it; ++it)
it.data()->D2 = d2;
}
// Delete link
void m_delete_link(int argc,t_atom *argv)
{
if (argc != 1) {
error("%s - %s Syntax : NoLink",thisName(),GetString(thisTag()));
return;
}
t_link *l = link.find(GetAInt(argv[0]));
if(l) {
deletelink(l);
link_deleted = 1;
}
else {
error("%s - %s : Index not found",thisName(),GetString(thisTag()));
return;
}
}
// -------------------------------------------------------------- GET
// -------------------------------------------------------------------
// get attributes
void m_get(int argc,t_atom *argv)
{
if(argc == 0) {
return;
}
t_atom sortie[1+2*N];
const t_symbol *auxtype = GetSymbol(argv[0]);
if (argc == 1) {
if (auxtype == S_massesPos) { // get all masses positions
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
SetInt(sortie[0],mit.data()->nbr);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],mit.data()->pos[i]);
ToOutAnything(0,S_massesPos,1+N,sortie);
}
}
else if (auxtype == S_massesForces) { // get all masses forces
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
SetInt(sortie[0],mit.data()->nbr);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],mit.data()->out_force[i]);
ToOutAnything(0,S_massesForces,1+N,sortie);
}
}
else if (auxtype == S_linksPos) { // get all links positions
for(typename IndexMap<t_link *>::iterator lit(link); lit; ++lit) {
SetInt(sortie[0],lit.data()->nbr);
for(int i = 0; i < N; ++i) {
SetFloat(sortie[1+i],lit.data()->mass1->pos[i]);
SetFloat(sortie[1+N+i],lit.data()->mass2->pos[i]);
}
ToOutAnything(0,S_linksPos,1+2*N,sortie);
}
}
else { // get all masses speeds
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit) {
SetInt(sortie[0],mit.data()->nbr);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],mit.data()->speed[i]);
ToOutAnything(0,S_massesSpeeds,1+N,sortie);
}
}
return;
}
// more than 1 args
if (auxtype == S_massesPos) // get mass positions
{
for(int j = 1; j<argc; j++) {
if(IsSymbol(argv[j])) {
typename IDMap<t_mass *>::iterator mit;
for(mit = massids.find(GetSymbol(argv[j])); mit; ++mit) {
SetSymbol(sortie[0],mit.data()->Id);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],mit.data()->pos[i]);
ToOutAnything(0,S_massesPosId,1+N,sortie);
}
}
else {
t_mass *m = mass.find(GetAInt(argv[j]));
if(m) {
SetInt(sortie[0],m->nbr);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],m->pos[i]);
ToOutAnything(0,S_massesPosNo,1+N,sortie);
}
// else
// error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
}
}
else if (auxtype == S_massesForces) // get mass forces
{
for(int j = 1; j<argc; j++) {
if(IsSymbol(argv[j])) {
typename IDMap<t_mass *>::iterator mit;
for(mit = massids.find(GetSymbol(argv[j])); mit; ++mit) {
SetSymbol(sortie[0],mit.data()->Id);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],mit.data()->out_force[i]);
ToOutAnything(0,S_massesForcesId,1+N,sortie);
}
}
else {
t_mass *m = mass.find(GetAInt(argv[j]));
if(m) {
SetInt(sortie[0],m->nbr);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],m->out_force[i]);
ToOutAnything(0,S_massesForcesNo,1+N,sortie);
}
// else
// error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
}
}
else if (auxtype == S_linksPos) // get links positions
{
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);
for(int i = 0; i < N; ++i) {
SetFloat(sortie[1+i],lit.data()->mass1->pos[i]);
SetFloat(sortie[1+N+i],lit.data()->mass2->pos[i]);
}
ToOutAnything(0,S_linksPosId,1+2*N,sortie);
}
}
else {
t_link *l = link.find(GetAInt(argv[j]));
if(l) {
SetInt(sortie[0],l->nbr);
for(int i = 0; i < N; ++i) {
SetFloat(sortie[1+i],l->mass1->pos[i]);
SetFloat(sortie[1+N+i],l->mass2->pos[i]);
}
ToOutAnything(0,S_linksPosNo,1+2*N,sortie);
}
// else
// error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
}
}
else // get mass speeds
{
for(int j = 1; j<argc; j++) {
if(IsSymbol(argv[j])) {
typename IDMap<t_mass *>::iterator mit;
for(mit = massids.find(GetSymbol(argv[j])); mit; ++mit) {
SetSymbol(sortie[0],mit.data()->Id);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],mit.data()->speed[i]);
ToOutAnything(0,S_massesSpeedsId,1+N,sortie);
}
}
else {
t_mass *m = mass.find(GetAInt(argv[j]));
if(m) {
SetInt(sortie[0],m->nbr);
for(int i = 0; i < N; ++i) SetFloat(sortie[1+i],m->speed[i]);
ToOutAnything(0,S_massesSpeedsNo,1+N,sortie);
}
// else
// error("%s - %s : Index not found",thisName(),GetString(thisTag()));
}
}
}
}
// List of masses positions on first outlet
void m_mass_dumpl()
{
if (mass_deleted ==0) {
int sz = mass.size();
NEWARR(t_atom,sortie,sz*N);
t_atom *s = sortie;
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit)
for(int i = 0; i < N; ++i) SetFloat(s[mit.data()->nbr*N+i],mit.data()->pos[i]);
ToOutAnything(0, S_massesPosL, sz*N, sortie);
DELARR(sortie);
}
else
error("%s - %s : Message Forbidden when deletion is used",thisName(),GetString(thisTag()));
}
// List of masses x positions on first outlet
void m_mass_dump_xl()
{
if (mass_deleted ==0) {
int sz = mass.size();
NEWARR(t_atom,sortie,sz);
t_atom *s = sortie;
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit)
SetFloat(s[mit.data()->nbr],mit.data()->pos[0]);
ToOutAnything(0, S_massesPosXL, sz, sortie);
DELARR(sortie);
}
else
error("%s - %s : Message Forbidden when deletion is used",thisName(),GetString(thisTag()));
}
// List of masses y positions on first outlet
void m_mass_dump_yl()
{
if (mass_deleted ==0) {
int sz = mass.size();
NEWARR(t_atom,sortie,sz);
t_atom *s = sortie;
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit)
SetFloat(s[mit.data()->nbr],mit.data()->pos[1]);
ToOutAnything(0, S_massesPosYL, sz, sortie);
DELARR(sortie);
}
else
error("%s - %s : Message Forbidden when deletion is used",thisName(),GetString(thisTag()));
}
// List of masses z positions on first outlet
void m_mass_dump_zl()
{
if (mass_deleted ==0) {
int sz = mass.size();
NEWARR(t_atom,sortie,sz);
t_atom *s = sortie;
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit)
SetFloat(s[mit.data()->nbr],mit.data()->pos[2]);
ToOutAnything(0, S_massesPosZL, sz, sortie);
DELARR(sortie);
}
else
error("%s - %s : Message Forbidden when deletion is used",thisName(),GetString(thisTag()));
}
// List of masses forces on first outlet
void m_force_dumpl()
{
if (mass_deleted ==0) {
int sz = mass.size();
NEWARR(t_atom,sortie,sz*N);
t_atom *s = sortie;
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit)
for(int i = 0; i < N; ++i) SetFloat(s[mit.data()->nbr*N+i],mit.data()->out_force[i]);
ToOutAnything(0, S_massesForcesL, sz*N, sortie);
DELARR(sortie);
}
else
error("%s - %s : Message Forbidden when deletion is used",thisName(),GetString(thisTag()));
}
// List of masses and links infos on second outlet
void m_info_dumpl()
{
for(typename IndexMap<t_mass *>::iterator mit(mass); mit; ++mit)
outmass(S_Mass,mit.data());
for(typename IndexMap<t_link *>::iterator lit(link); lit; ++lit)
outlink(S_Link,lit.data());
}
// -------------------------------------------------------------- SETUP
// ---------------------------------------------------------------------
private:
void clear()
{
linkids.reset();
link.reset();
massids.reset();
mass.reset();
id_mass = id_link = mouse_grab = mass_deleted = link_deleted = 0;
}
void deletelink(t_link *l)
{
outlink(S_Link_deleted,l);
linkids.erase(l);
link.remove(l->nbr);
delete l;
}
void outmass(const t_symbol *s,const t_mass *m)
{
t_atom sortie[4+N];
SetInt((sortie[0]),m->nbr);
SetSymbol((sortie[1]),m->Id);
SetBool((sortie[2]),m->getMobile());
SetFloat((sortie[3]),m->M);
for(int i = 0; i < N; ++i) SetFloat((sortie[4+i]),m->pos[i]);
ToOutAnything(1,s,4+N,sortie);
}
void outlink(const t_symbol *s,const t_link *l)
{
t_atom sortie[7];
SetInt((sortie[0]),l->nbr);
SetSymbol((sortie[1]),l->Id);
SetInt((sortie[2]),l->mass1->nbr);
SetInt((sortie[3]),l->mass2->nbr);
SetFloat((sortie[4]),l->K1);
SetFloat((sortie[5]),l->D1);
SetFloat((sortie[6]),l->D2);
ToOutAnything(1,s,7,sortie);
}
// Static symbols
const static t_symbol *S_Reset;
const static t_symbol *S_Mass;
const static t_symbol *S_Link;
const static t_symbol *S_iLink;
const static t_symbol *S_Mass_deleted;
const static t_symbol *S_Link_deleted;
const static t_symbol *S_massesPos;
const static t_symbol *S_massesPosNo;
const static t_symbol *S_massesPosId;
const static t_symbol *S_linksPos;
const static t_symbol *S_linksPosNo;
const static t_symbol *S_linksPosId;
const static t_symbol *S_massesForces;
const static t_symbol *S_massesForcesNo;
const static t_symbol *S_massesForcesId;
const static t_symbol *S_massesSpeeds;
const static t_symbol *S_massesSpeedsNo;
const static t_symbol *S_massesSpeedsId;
const static t_symbol *S_massesPosL;
const static t_symbol *S_massesPosXL;
const static t_symbol *S_massesPosYL;
const static t_symbol *S_massesPosZL;
const static t_symbol *S_massesForcesL;
static void setup(t_classid c)
{
S_Reset = MakeSymbol("Reset");
S_Mass = MakeSymbol("Mass");
S_Link = MakeSymbol("Link");
S_iLink = MakeSymbol("iLink");
S_Mass_deleted = MakeSymbol("Mass deleted");
S_Link_deleted = MakeSymbol("Link deleted");
S_massesPos = MakeSymbol("massesPos");
S_massesPosNo = MakeSymbol("massesPosNo");
S_massesPosId = MakeSymbol("massesPosId");
S_linksPos = MakeSymbol("linksPos");
S_linksPosNo = MakeSymbol("linksPosNo");
S_linksPosId = MakeSymbol("linksPosId");
S_massesForces = MakeSymbol("massesForces");
S_massesForcesNo = MakeSymbol("massesForcesNo");
S_massesForcesId = MakeSymbol("massesForcesId");
S_massesSpeeds = MakeSymbol("massesSpeeds");
S_massesSpeedsNo = MakeSymbol("massesSpeedsNo");
S_massesSpeedsId = MakeSymbol("massesSpeedsId");
S_massesPosL = MakeSymbol("massesPosL");
S_massesPosXL = MakeSymbol("massesPosXL");
S_massesPosYL = MakeSymbol("massesPosYL");
S_massesPosZL = MakeSymbol("massesPosZL");
S_massesForcesL = MakeSymbol("massesForcesL");
// --- set up methods (class scope) ---
// register a bang method to the default inlet (0)
FLEXT_CADDBANG(c,0,m_bang);
// set up tagged methods for the default inlet (0)
// the underscore _ after CADDMETHOD indicates that a message tag is used
// no, variable list or anything and all single arguments are recognized automatically, ...
FLEXT_CADDMETHOD_(c,0,"reset",m_reset);
FLEXT_CADDMETHOD_(c,0,"forceX",m_forceX);
FLEXT_CADDMETHOD_(c,0,"posX",m_posX);
FLEXT_CADDMETHOD_(c,0,"Xmax",m_Xmax);
FLEXT_CADDMETHOD_(c,0,"Xmin",m_Xmin);
FLEXT_CADDMETHOD_(c,0,"massesPosL",m_mass_dumpl);
FLEXT_CADDMETHOD_(c,0,"massesPosXL",m_mass_dump_xl);
if(N >= 2) {
FLEXT_CADDMETHOD_(c,0,"forceY",m_forceY);
FLEXT_CADDMETHOD_(c,0,"posY",m_posY);
FLEXT_CADDMETHOD_(c,0,"Ymax",m_Ymax);
FLEXT_CADDMETHOD_(c,0,"Ymin",m_Ymin);
FLEXT_CADDMETHOD_(c,0,"massesPosYL",m_mass_dump_yl);
FLEXT_CADDMETHOD_(c,0,"grabMass",m_grab_mass);
}
if(N >= 3) {
FLEXT_CADDMETHOD_(c,0,"forceZ",m_forceZ);
FLEXT_CADDMETHOD_(c,0,"posZ",m_posZ);
FLEXT_CADDMETHOD_(c,0,"Zmax",m_Zmax);
FLEXT_CADDMETHOD_(c,0,"Zmin",m_Zmin);
FLEXT_CADDMETHOD_(c,0,"massesPosZL",m_mass_dump_zl);
}
FLEXT_CADDMETHOD_(c,0,"setMobile",m_set_mobile);
FLEXT_CADDMETHOD_(c,0,"setFixed",m_set_fixe);
FLEXT_CADDMETHOD_(c,0,"setK",m_setK);
FLEXT_CADDMETHOD_(c,0,"setD",m_setD);
FLEXT_CADDMETHOD_(c,0,"setD2",m_setD2);
FLEXT_CADDMETHOD_(c,0,"mass",m_mass);
FLEXT_CADDMETHOD_(c,0,"link",m_link);
FLEXT_CADDMETHOD_(c,0,"iLink",m_ilink);
FLEXT_CADDMETHOD_(c,0,"get",m_get);
FLEXT_CADDMETHOD_(c,0,"deleteLink",m_delete_link);
FLEXT_CADDMETHOD_(c,0,"deleteMass",m_delete_mass);
FLEXT_CADDMETHOD_(c,0,"infosL",m_info_dumpl);
FLEXT_CADDMETHOD_(c,0,"massesForcesL",m_force_dumpl);
}
// for every registered method a callback has to be declared
FLEXT_CALLBACK(m_bang)
FLEXT_CALLBACK(m_mass_dumpl)
FLEXT_CALLBACK(m_mass_dump_xl)
FLEXT_CALLBACK(m_mass_dump_yl)
FLEXT_CALLBACK(m_mass_dump_zl)
FLEXT_CALLBACK(m_info_dumpl)
FLEXT_CALLBACK(m_force_dumpl)
FLEXT_CALLBACK(m_reset)
FLEXT_CALLBACK_V(m_set_mobile)
FLEXT_CALLBACK_V(m_set_fixe)
FLEXT_CALLBACK_V(m_mass)
FLEXT_CALLBACK_V(m_link)
FLEXT_CALLBACK_V(m_ilink)
FLEXT_CALLBACK_V(m_Xmax)
FLEXT_CALLBACK_V(m_Xmin)
FLEXT_CALLBACK_V(m_forceX)
FLEXT_CALLBACK_V(m_posX)
FLEXT_CALLBACK_V(m_Ymax)
FLEXT_CALLBACK_V(m_Ymin)
FLEXT_CALLBACK_V(m_forceY)
FLEXT_CALLBACK_V(m_posY)
FLEXT_CALLBACK_V(m_Zmax)
FLEXT_CALLBACK_V(m_Zmin)
FLEXT_CALLBACK_V(m_forceZ)
FLEXT_CALLBACK_V(m_posZ)
FLEXT_CALLBACK_V(m_setK)
FLEXT_CALLBACK_V(m_setD)
FLEXT_CALLBACK_V(m_setD2)
FLEXT_CALLBACK_V(m_get)
FLEXT_CALLBACK_V(m_delete_link)
FLEXT_CALLBACK_V(m_delete_mass)
FLEXT_CALLBACK_V(m_grab_mass)
};
// -------------------------------------------------------------- STATIC VARIABLES
// -------------------------------------------------------------------------------
#define MSD(NAME,CLASS,N) \
const t_symbol \
*msdN<N>::S_Reset,*msdN<N>::S_Mass, \
*msdN<N>::S_Link,*msdN<N>::S_iLink, \
*msdN<N>::S_Mass_deleted,*msdN<N>::S_Link_deleted, \
*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; \
FLEXT_NEW_V(NAME,CLASS)
|