Skip to content

Commit

Permalink
todo : cinematic moment, fork gene instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
julien.rodriguez-tao committed Oct 22, 2017
1 parent 3ffd3a1 commit 64aff7e
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 57 deletions.
19 changes: 12 additions & 7 deletions cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@
#include <vector>
using namespace std;

class Cell;
class Cell; //Cell is a linked bearing

class LinkCell
{
public:
int typ;//0 rigid 1 free 2 actif
Cell *c;
LinkCell(Cell *_c, int _typ) :c(_c),typ(_typ) {}
int typ; //0 rigid 1 free 2 actif
float cl; //size link consign
float phi,dphi; //dynamic : angle, d/dt angle
float l,dl; //dynamic : sizelink, d/dt sizelink

Cell *c; //Cell *dst;
LinkCell(Cell *_c, int _typ, float _cl, float _phi) :c(_c),typ(_typ),cl(_cl),phi(_phi) {l=cl;dl=dphi=0;}
};

class Cell
{
public:

int id;

vector<LinkCell*> stator;
vector<LinkCell*> rotor;

int id;
float px,py,diameter;
float px,py,vx,vy,diameter,mass; //dynamic parameters

Cell(int _id,float _px,float _py,float _diameter) : id(_id),px(_px),py(_py),diameter(_diameter) {;}
Cell(int _id,float _px,float _py,float _diameter) : id(_id),px(_px),py(_py),diameter(_diameter) {vx=vy=0;mass=diameter*diameter;}
};

#endif // CELL_H
4 changes: 0 additions & 4 deletions celldrawer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
#include "celldrawer.h"

CellDrawer::CellDrawer()
{

}
24 changes: 21 additions & 3 deletions celldrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class CellDrawer
glVertex3f(d->px-px*(2),d->py-py*(2),0.0f);



glVertex3f(d->px-px,d->py-py,0.0f); glVertex3f(d->px+py*c3-px*c2,d->py-px*c3-py*c2,0.0f);

glVertex3f(d->px-px,d->py-py,0.0f); glVertex3f(d->px-py*c3-px*c2,d->py+px*c3-py*c2,0.0f);
Expand All @@ -60,11 +59,29 @@ class CellDrawer

}

void draw(CellManager *cm)
void draw(CellManager *cm, int cgf)
{
int i,j;
float px,py,pd;
float cgx=0,cgy=0,sm=0;
Cell *c;

glPushMatrix();
if (cgf)
{
for (i=0; i<cm->nodes.size(); i++)
{
c = cm->nodes[i];
cgx+=c->px*c->mass;
cgy+=c->py*c->mass;
sm+=c->mass;
}
cgx/=sm;
cgy/=sm;
glTranslatef(-cgx,-cgy,0);
}


for (i=0; i<cm->nodes.size(); i++)
{
c = cm->nodes[i];
Expand All @@ -91,9 +108,10 @@ class CellDrawer

}
}
glPopMatrix();
}

CellDrawer();
CellDrawer() {}
};

#endif // CELLDRAWER_H
140 changes: 115 additions & 25 deletions cellmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ CellManager::CellManager() : GeneticLowlevelGrammar()
{
initLLcmds ();
cmdnm.push_back("cellgen ");
cmdsz.push_back(3);
cmdsz.push_back(1);
cmdnm.push_back("celllink ");
cmdsz.push_back(2);
cmdsz.push_back(4);
cmdnm.push_back("cellmv ");
cmdsz.push_back(1);

nodes.reserve(16);
nodes.push_back(new Cell(0,0.1,0.1,24));
nodes.push_back(new Cell(0,0,0,24));

}

void CellManager::makeLink(int idcell, int mld1, int mld2)
void CellManager::makeLink(int idcell, int mld1, int mld2, int mld3, int mld4)
{

if (mld2<0) return;
Expand All @@ -26,19 +26,19 @@ void CellManager::makeLink(int idcell, int mld1, int mld2)
{
case 0:
{
nodes[idcell]->stator.push_back(new LinkCell(nodes[mld2],mld1) );
nodes[idcell]->stator.push_back(new LinkCell(nodes[mld2],mld1,mld3*2,mld4) );

} break;

case 1:
{
nodes[idcell]->rotor.push_back(new LinkCell(nodes[mld2],mld1) );
nodes[idcell]->rotor.push_back(new LinkCell(nodes[mld2],mld1,mld3*2,mld4) );

} break;

case 2:
{
nodes[idcell]->rotor.push_back(new LinkCell(nodes[mld2],mld1) );
nodes[idcell]->rotor.push_back(new LinkCell(nodes[mld2],mld1,mld3*2,mld4) );

} break;

Expand All @@ -56,26 +56,28 @@ void CellManager::makeLink(int idcell, int mld1, int mld2)

int CellManager::TranslateHLCode(GeneticBase *gb, int idcell)
{
static double ang = 0;


switch (gb->data[0])
{
case 2://generate new cell linked with active cell
case 2://generate new cell
{
float apx=nodes[idcell]->px;
float apy=nodes[idcell]->py;
float ad=nodes[idcell]->diameter;
double ang = gb->data[2]/64.0f*M_PI;
float dia = gb->data[3]+ad+12;
Cell *nc=new Cell(nodes.size(),apx+cos(ang)*(dia),apy+sin(ang)*(dia),12);
float dia = (gb->data[1]+ad)*1.5;
ang+=0.1;
Cell *nc=new Cell(nodes.size(),apx+cos(ang)*dia,apy+sin(ang)*dia,gb->data[1]);
nodes.push_back(nc);
//makeLink(idcell,gb->data[1],nodes.size()-1);
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]);

makeLink(idcell,gb->data[1],idcell+gb->data[2]);
return 0;
}
break;
Expand All @@ -95,30 +97,27 @@ int CellManager::TranslateHLCode(GeneticBase *gb, int idcell)

void CellManager::geneticDataFromRawCode(GeneticData *gd, vector<int8_t> rawcode)
{
printf ("\nasm development code\n\n");
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;


//std::cout << cmdnm[cmd] << " ";
printf("%s ",cmdnm[cmd]);

db = new int8_t[cms];
printf("%s ",cmdnm[cmd]);
for (j=0; j<cms; j++)
{
db[j]=rawcode[i++];
//std::cout << db[j] << ", ";
if (j) printf("%i, ",(uint8_t)db[j]);

}
//std::cout << std::endl;
printf("\n");
gd->data.push_back(new GeneticBase(db,cms));

}
}

Expand All @@ -130,10 +129,9 @@ int CellManager::generateBody(GeneticData *cg)
int idcell=0;
GeneticBase *gb=NULL;


deque<GeneticBase*>::iterator it = cg->data.begin();

int ilim=0,limitinstruction=64;
int ilim=0,limitinstruction=256;
while (mvcmd<255 && ilim++<limitinstruction)
{
it+=mvcmd;
Expand All @@ -143,9 +141,101 @@ int CellManager::generateBody(GeneticData *cg)
idcmd+=mvcmd;
idcell+=mvcell;

printf ("%s mvcmd %i mvcell %i\n", cmdnm[gb->data[0]], mvcmd,mvcell);
printf ("%i %s mvcmd %i mvcell %i\n", ilim, cmdnm[gb->data[0]], mvcmd,mvcell);
//if (mvcmd!=1)for (int i=0; i<100; i++) dynamicCompute(0.01,0.0001);

}

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)/4;
if (lc->typ==2) spring/=4;

//spring = spring<0 ? -spring*spring : spring*spring;

if (fabs(spring)>2) spring=spring<0 ? -2 : 2;

tree->vx+=spring*dsx/dsd*dt/tree->mass;
tree->vy+=spring*dsy/dsd*dt/tree->mass;

lc->c->vx+=-spring*dsx/dsd*dt/lc->c->mass;
lc->c->vy+=-spring*dsy/dsd*dt/lc->c->mass;

if (lc->typ<2)
{
springtan = -(atan2(dsy,dsx)*180/M_PI-lc->phi)/8;
if (lc->typ==1) springtan/=64;

springtan = springtan<0 ? -springtan*springtan : springtan*springtan;

if (fabs(springtan)>1) springtan=springtan<0 ? -1 : 1;


tree->vx+=-springtan*dsy/dsd*dt/tree->mass;
tree->vy+=springtan*dsx/dsd*dt/tree->mass;

lc->c->vx+=springtan*dsy/dsd*dt/lc->c->mass;
lc->c->vy+=-springtan*dsx/dsd*dt/lc->c->mass;
}
}
}

int CellManager::dynamicCompute(float dt, float f)
{
dynamicComputeRec(nodes[0],dt);
float fv;
f*=dt;
Cell *c;
for (int i=0; i<nodes.size(); i++)
{
c=nodes[i];
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;
}
}



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->stator.size(); j++)
{
lc = tree->stator[j];
forceCompute(tree, lc, dt);
dynamicComputeRec(tree->stator[j]->c,dt);
}

for (j=0; j<tree->rotor.size(); j++)
{
lc = tree->rotor[j];
forceCompute(tree, lc, dt);
dynamicComputeRec(tree->rotor[j]->c,dt);
}
}
13 changes: 8 additions & 5 deletions cellmanager.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CELLMANAGER_H
#define CELLMANAGER_H
#include "stdio.h"
#include "math.h"

#include "cell.h"
#include "geneticdata.h"
#include "stdio.h"
#include "math.h"

class CellManager : public GeneticLowlevelGrammar
{
Expand All @@ -13,12 +13,15 @@ class CellManager : public GeneticLowlevelGrammar
vector<Cell*> nodes;

CellManager();

int TranslateHLCode(GeneticBase *gb, int idcell);

void geneticDataFromRawCode(GeneticData *gd, vector<int8_t> rawcode);
void makeLink(int idcell, int mld1, int mld2);
void makeLink(int idcell, int mld1, int mld2, int mld3, int mld4);
int generateBody (GeneticData *cg);

void forceCompute(Cell *tree, LinkCell *lc, float dt);
int dynamicCompute (float dt, float f);
private:
int dynamicComputeRec (Cell *tree, float dt);
};

#endif // CELLMANAGER_H
3 changes: 3 additions & 0 deletions geneticdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ void GeneticLowlevelGrammar::initLLcmds ()
cmdnm.push_back("cmdifmv ");
cmdsz.push_back(2);

//cmdnm.push_back("cmdfork ");


}

int GeneticLowlevelGrammar::TranslateLLCode(GeneticBase *gb, int idcmd, int idcell)
Expand Down
Loading

0 comments on commit 64aff7e

Please sign in to comment.