-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcellmanager.cpp
301 lines (237 loc) · 7 KB
/
cellmanager.cpp
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
#include "cellmanager.h"
CellManager::CellManager() : GeneticLowlevelGrammar()
{
initLLcmds ();
cmdnm.push_back("cellgen ");
cmdsz.push_back(1);
cmdnm.push_back("celllink ");
cmdsz.push_back(4);
cmdnm.push_back("cellmv ");
cmdsz.push_back(1);
nodes.reserve(16);
nodes.push_back(new Cell(0,0,0,24));
}
void CellManager::makeLink(int idcell, int mld1, int mld2, int mld3, int mld4)
{
if (mld2<0) return;
if (mld2>=nodes.size()) return;
switch (mld1)
{
case 0:
{
nodes[idcell]->links.push_back(new LinkCell(nodes[mld2],mld1,mld3*2,mld4) );
nodes[mld2]->invLinks.push_back(LinkCellInv(nodes[idcell],nodes[idcell]->links.back()));
} break;
case 1:
{
nodes[idcell]->links.push_back(new LinkCell(nodes[mld2],mld1,mld3*2,mld4) );
nodes[mld2]->invLinks.push_back(LinkCellInv(nodes[idcell],nodes[idcell]->links.back()));
} break;
case 2:
{
nodes[mld2]->links.push_back(new LinkCell(nodes[idcell],mld1,mld3*2,mld4) );
nodes[idcell]->invLinks.push_back(LinkCellInv(nodes[mld2],nodes[mld2]->links.back()));
} break;
/*case 3:
{
} break;*/
default://error
//return 0;
break;
}
//return 1;
}
int CellManager::TranslateHLCode(GeneticBase *gb, int idcell)
{
static double ang = 0;
switch (gb->data[0])
{
case 2://generate new cell
{
float apx=nodes[idcell]->px;
float apy=nodes[idcell]->py;
float ad=nodes[idcell]->diameter;
float dia = (gb->data[1]+ad)*1.5;
ang+=0.025*0;
Cell *nc=new Cell(nodes.size(),apx+cos(ang)*dia,apy+sin(ang)*dia,gb->data[1]);
nodes.push_back(nc);
return 0;
}
break;
case 3://generate new link from active cell to cell (uint)*(data+2)
{
makeLink(idcell,gb->data[1],idcell+gb->data[2],gb->data[3],gb->data[4]);
return 0;
}
break;
case 4://move to cell at idcell+uint8_t(data+1)
{
return (int)(*(gb->data+1));
}
break;
default: //low level command or error
return 0;
break;
}
}
void CellManager::geneticDataFromRawCode(GeneticData *gd, vector<int8_t> rawcode)
{
printf ("\nraw development src\n\n");
for (auto i = rawcode.begin(); i != rawcode.end(); ++i)
printf("%i ",*i);
printf("\n\n");
int i=0,j,cmd,cms=1,szc=rawcode.size();
int8_t *db;
while (i<szc && cms>0)
{
cmd=rawcode[i];
cms=cmdsz[cmd]+1;
db = new int8_t[cms];
printf("%s ",cmdnm[cmd]);
for (j=0; j<cms; j++)
{
db[j]=rawcode[i++];
if (j) printf("%i, ",(uint8_t)db[j]);
}
printf("\n");
gd->data.push_back(new GeneticBase(db,cms));
}
}
int CellManager::generateBody(GeneticData *cg)
{
printf ("\nexecute development code\n\n");
static int idcmd=0,mvcmd=0,mvcell;
static int idcell=0;
GeneticBase *gb=NULL;
static deque<GeneticBase*>::iterator it = cg->data.begin();
static int ilim=0,limitinstruction=1024;
while (mvcmd<255 && ilim++<limitinstruction)
{
it+=mvcmd;
gb = *it;
mvcmd = TranslateLLCode(gb,idcmd, idcell);
mvcell = TranslateHLCode(gb, idcell);
idcmd+=mvcmd;
idcell+=mvcell;
printf ("%i %s mvcmd %i mvcell %i\n", ilim, cmdnm[gb->data[0]], mvcmd,mvcell);
if (mvcmd!=1) break;
}
return 1;
}
void CellManager::forceCompute(Cell *tree, LinkCell *lc, float dt)
{
int j;
float cl,l,dl,psx,psy,pdx,pdy,dsd2,dsd,dsx,dsy;
float spring,springtan;
psx=tree->px;
psy=tree->py;
if (lc->typ<3)
{
pdx=lc->c->px; pdy=lc->c->py;
dsx=psx-pdx; dsy=psy-pdy;
dsd2=dsx*dsx+dsy*dsy; dsd=sqrt(dsd2);
spring = (lc->cl - dsd)/2;
if (lc->typ==2) spring/=16;
//spring = spring<0 ? -spring*spring : spring*spring;
//if (fabs(spring)>2) spring=spring<0 ? -2 : 2;
tree->fx+=spring*dsx/dsd*dt/tree->mass;
tree->fy+=spring*dsy/dsd*dt/tree->mass;
lc->c->fx+=-spring*dsx/dsd*dt/lc->c->mass;
lc->c->fy+=-spring*dsy/dsd*dt/lc->c->mass;
if (lc->typ==0)
{
springtan = -(atan2(dsy,dsx)-lc->phi/80-(-tree->phi+0*lc->c->phi))/20;
/*springtan = springtan<0 ? -springtan*springtan : springtan*springtan;
if (fabs(springtan)>1) springtan=springtan<0 ? -1 : 1;*/
tree->fx+=-springtan*dsy/dsd*dt/tree->mass;
tree->fy+=springtan*dsx/dsd*dt/tree->mass;
lc->c->fx+=springtan*dsy/dsd*dt/lc->c->mass;
lc->c->fy+=-springtan*dsx/dsd*dt/lc->c->mass;
}
}
}
int CellManager::dynamicCompute(float dt, float f)
{
//result of forces initialisation
Cell *c,*d;
LinkCell *lc;
LinkCellInv *ilc;
float dist,ux,uy;
for (int i=0; i<nodes.size(); i++)
{
c=nodes[i];
c->fx=0;
c->fy=0;
c->cpl=0;
//c->fn=0;
}
//result of force recursive computation
dynamicComputeRec(nodes[0],dt);
//result of torque computation
for (int i=0; i<nodes.size(); i++)
{
c=nodes[i];
for (int j=0; j<c->links.size(); j++)
{
lc = c->links[j];
if (lc->typ==0)
{
d=lc->c;
ux=d->px-c->px;
uy=d->py-c->py;
c->cpl+=d->fx*uy-d->fy*ux;
}
}
for (int j=0; j<c->invLinks.size(); j++)
{
ilc=&c->invLinks[j];
lc=ilc->lc;
if (lc->typ==0)
{
d=ilc->c;
ux=d->px-c->px;
uy=d->py-c->py;
c->cpl+=d->fx*uy-d->fy*ux;
}
}
}
//update velocity and torque
float fv;
f*=dt;
for (int i=0; i<nodes.size(); i++)
{
c=nodes[i];
c->vx+=c->fx;
c->vy+=c->fy;
fv = sqrt(c->vx*c->vx+c->vy*c->vy)*f;
c->vx-=c->vx*fv;c->vy-=c->vy*fv;
c->px+=c->vx*dt;
c->py+=c->vy*dt;
c->dphi+=c->cpl/c->mass/10;
c->phi+=d->dphi*dt;
if (c->phi>M_PI)
c->phi-=M_PI;
if (c->phi<-M_PI)
c->phi+=M_PI;
}
}
int CellManager::dynamicComputeRec(Cell *tree, float dt)
{
int j;
float cl,l,dl,psx,psy,pdx,pdy,dsd2,dsd,dsx,dsy;
float spring,springtan;
LinkCell *lc;
psx=tree->px;
psy=tree->py;
for (j=0; j<tree->links.size(); j++)
{
lc = tree->links[j];
forceCompute(tree, lc, dt);
//if (tree->ok) //shit multiple pass.. use monte carlo mecanic... or heap of computecell order by magnitude of pertubation...
if (lc->typ<2) //ok :) constraint skeleton to be a tree use grow and fork for develelopment
{
dynamicComputeRec(tree->links[j]->c,dt);
//tree->ok=0;
}
}
}