aboutsummaryrefslogtreecommitdiff
path: root/src/tLia3D.c
blob: 507d113839b51896eca32964ee65acc7808cc238 (plain)
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
#include "m_pd.h"
#include "math.h"

static t_class *tLia3D_class;

typedef struct _tLia3D {
  t_object  x_obj;
  t_float distance_old, position2Dx1, position2Dy1, position2Dz1, position2Dx2, position2Dy2,  position2Dz2;
  t_outlet *force1;
  t_outlet *force2;
  t_outlet *force3;
  t_outlet *force4;
  t_symbol *x_sym;  // receive
} t_tLia3D;

void tLia3D_reset(t_tLia3D *x)
{
}

void tLia3D_resetF(t_tLia3D *x)
{
}

void tLia3D_resetL(t_tLia3D *x)
{
}


void tLia3D_setK(t_tLia3D *x, t_float K)
{
}

void tLia3D_setL(t_tLia3D *x, t_float L)
{
}

void tLia3D_setD(t_tLia3D *x, t_float D)
{
}

void tLia3D_setD2(t_tLia3D *x, t_float D)
{
}

void tLia3D_Lmin(t_tLia3D *x, t_float Lmin)
{
}

void tLia3D_Lmax(t_tLia3D *x, t_float Lmax)
{
}


void tLia3D_position3D(t_tLia3D *x, t_floatarg f1, t_floatarg f2, t_floatarg f3)
{
  x->position2Dx1 = f1;
  x->position2Dy1 = f2;
  x->position2Dz1 = f3;
}

void tLia3D_position3D2(t_tLia3D *x, t_floatarg f1, t_floatarg f2, t_floatarg f3)
{
  x->position2Dx2 = f1;
  x->position2Dy2 = f2;
  x->position2Dz2 = f3;
}

void tLia3D_bang(t_tLia3D *x)
{
  t_float vitesse, distance, orientation;
  t_atom force1[3];

  distance = sqrt ( pow((x->position2Dx2-x->position2Dx1), 2) + pow((x->position2Dy2-x->position2Dy1), 2) + pow((x->position2Dz2-x->position2Dz1), 2) );

  vitesse = x->distance_old - distance;


  SETFLOAT(&(force1[0]), (x->position2Dx2 + x->position2Dx1)/ 2);
  SETFLOAT(&(force1[1]), (x->position2Dy2 + x->position2Dy1)/ 2);
  SETFLOAT(&(force1[2]), (x->position2Dz2 + x->position2Dz1)/ 2);
 
  outlet_anything(x->force4, gensym("position3D"), 3, force1);

  
  SETFLOAT(&(force1[0]), (x->position2Dx2 - x->position2Dx1)/ distance);
  SETFLOAT(&(force1[1]), (x->position2Dy2 - x->position2Dy1)/ distance);
  SETFLOAT(&(force1[2]), (x->position2Dz2 - x->position2Dz1)/ distance);
 
  outlet_anything(x->force3, gensym("vector3D"), 3, force1);

  outlet_float(x->force2, vitesse);

  outlet_float(x->force1, distance);

  x->distance_old = distance;

}


static void tLia3D_free(t_tLia3D *x)
{
    pd_unbind(&x->x_obj.ob_pd, x->x_sym);
}

void *tLia3D_new(t_symbol *s)
{
  
  t_tLia3D *x = (t_tLia3D *)pd_new(tLia3D_class);

  x->x_sym = s;
  pd_bind(&x->x_obj.ob_pd, s);

  inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("position3D"), gensym("position3D2"));
 
  x->force1=outlet_new(&x->x_obj, 0);
  x->force2=outlet_new(&x->x_obj, 0);
  x->force3=outlet_new(&x->x_obj, 0);
  x->force4=outlet_new(&x->x_obj, 0);

  x->distance_old = 0;

  return (x);
}

void tLia3D_setup(void) 
{

  tLia3D_class = class_new(gensym("tLia3D"),
        (t_newmethod)tLia3D_new,
        (t_method)tLia3D_free, 
		sizeof(t_tLia3D),
        CLASS_DEFAULT, A_DEFSYM, 0);

  class_addcreator((t_newmethod)tLia3D_new, gensym("tLink3D"), A_DEFSYM, 0);
  class_addcreator((t_newmethod)tLia3D_new, gensym("pmpd.tLink3D"), A_DEFSYM, 0);

  class_addbang(tLia3D_class, tLia3D_bang);

  class_addmethod(tLia3D_class, (t_method)tLia3D_position3D, gensym("position3D"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_position3D2, gensym("position3D2"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);

  // only for the object not to output erreor when having the same name as the link
  class_addmethod(tLia3D_class, (t_method)tLia3D_reset, gensym("reset"), 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_resetL, gensym("resetL"), 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_resetF, gensym("resetF"), 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_setD, gensym("setD"), A_DEFFLOAT, 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_setD2, gensym("setD2"), A_DEFFLOAT, 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_setK, gensym("setK"), A_DEFFLOAT, 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_setL, gensym("setL"), A_DEFFLOAT, 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_Lmin, gensym("setLmin"), A_DEFFLOAT, 0);
  class_addmethod(tLia3D_class, (t_method)tLia3D_Lmax, gensym("setLmax"), A_DEFFLOAT, 0);

}