aboutsummaryrefslogtreecommitdiff
path: root/tSphere3D.c
blob: 6ef9cfa44f3466869acbc31f3a4c093f978d2c92 (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
#include "m_pd.h"
#include "math.h"

static t_class *tSphere3D_class;

typedef struct _tSphere3D {
  t_object  x_obj;
  t_float  X, Y, Z, Rmin, Rmax, position_old;
  t_outlet *force_new, *distance_out, *vitesse;// outlet
} t_tSphere3D;

void tSphere3D_position3D(t_tSphere3D *x, t_float X,  t_float Y, t_float Z)
{
t_float  distance, vitesse;
		distance = sqrt ( (X - x->X)*(X - x->X) + (Y - x->Y)*(Y - x->Y) + (Z - x->Z)*(Z - x->Z));

		vitesse = distance - x->position_old;
		x->position_old = distance;

		outlet_float(x->vitesse, vitesse);

		outlet_float(x->distance_out, distance);

		if ( (distance < x->Rmax) & (distance > x->Rmin) )
		{
			outlet_float(x->force_new, 1);
		}
		else
		{
			outlet_float(x->force_new, 0);
		}

}

void tSphere3D_setXYZ(t_tSphere3D *x, t_float X, t_float Y, t_float Z)
{
  x->X= X;
  x->Y= Y;
  x->Z= Z;
}

void tSphere3D_setX(t_tSphere3D *x, t_float X)
{
  x->X= X;
}

void tSphere3D_setY(t_tSphere3D *x, t_float Y)
{
  x->Y= Y;
}

void tSphere3D_setZ(t_tSphere3D *x, t_float Z)
{
  x->Z= Z;
}

void tSphere3D_setRmax(t_tSphere3D *x, t_float X)
{
  x->Rmax= X;
}

void tSphere3D_setRmin(t_tSphere3D *x, t_float X)
{
  x->Rmin= X;
}



void *tSphere3D_new(t_symbol *s, int argc, t_atom *argv)
{
  t_tSphere3D *x = (t_tSphere3D *)pd_new(tSphere3D_class);

  x->force_new=outlet_new(&x->x_obj, 0);
  x->distance_out=outlet_new(&x->x_obj, 0);
  x->vitesse=outlet_new(&x->x_obj, 0);
    
  x->position_old = 0;


    if (argc>=5)
    x->Rmax= atom_getfloatarg(4, argc, argv);
  else
    x->Rmax= 1;

  if (argc>=4)
    x->Rmin= atom_getfloatarg(3, argc, argv);
  else
    x->Rmin= 0;

  if (argc>=3)
    x->Z= atom_getfloatarg(2, argc, argv);
  else
	x->Z= 0;

  if (argc>=2)
    x->Y= atom_getfloatarg(1, argc, argv);
  else
	x->Y= 0;

  if (argc>=1)
	x->X= atom_getfloatarg(0, argc, argv);
  else
	x->X= 0;

  return (x);
}

void tSphere3D_setup(void) 
{

  tSphere3D_class = class_new(gensym("tSphere3D"),
        (t_newmethod)tSphere3D_new,
        0, sizeof(t_tSphere3D),
        CLASS_DEFAULT, A_GIMME, 0);

  class_addcreator((t_newmethod)tSphere3D_new, gensym("pmpd.tSphere3D"),  A_GIMME, 0);
 
  class_addmethod(tSphere3D_class, (t_method)tSphere3D_position3D, gensym("position3D"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);

  class_addmethod(tSphere3D_class, (t_method)tSphere3D_setX, gensym("setX"), A_DEFFLOAT, 0);
  class_addmethod(tSphere3D_class, (t_method)tSphere3D_setY, gensym("setY"), A_DEFFLOAT, 0);
  class_addmethod(tSphere3D_class, (t_method)tSphere3D_setZ, gensym("setZ"), A_DEFFLOAT, 0);
  class_addmethod(tSphere3D_class, (t_method)tSphere3D_setXYZ, gensym("setXYZ"), A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
  class_addmethod(tSphere3D_class, (t_method)tSphere3D_setRmax, gensym("setRmax"), A_DEFFLOAT, 0);
  class_addmethod(tSphere3D_class, (t_method)tSphere3D_setRmin, gensym("setRmin"), A_DEFFLOAT, 0);


}