#include #include #include "gene.h" double drand48(); extern double CellSize; cell_step(ind,c) Individual *ind; Cell *c; { Gene *action; double ax[4][4]; if (c->state & 0xf8) return 0; action = ind->gene + (c->state & 0x07); daughter_pose(c,action,ax); if (new_cell(ind,ax,(action->state>>4)&0x0f,c)) c->state = action->state & 0x0f; else c->state |= 0x10; } daughter_pose(m,gene,a) Cell *m; /* mother cell */ Gene *gene; /* spawning direction */ double a[4][4]; /* daughter's matrix */ { int i, j, k; double r, d[3][3]; double x, ca, sa, cb, sb, cc, sc; bzero(a,sizeof(double)*16); x = gene->alpha * M_PI / 127.5; ca = cos(x); sa = sin(x); x = gene->beta * M_PI / 127.5; cb = cos(x); sb = sin(x); x = gene->gamma * M_PI / 127.5; cc = cos(x); sc = sin(x); d[0][0] = cb * cc; d[0][1] = cb * sc; d[0][2] = 0; d[1][0] = sa * sb * cc - ca * sc; d[1][1] = sa * sb * sc + ca * cc; d[1][2] = sa * cb; d[2][0] = ca * sb * cc + sa * sc; d[2][1] = ca * sb * sc - sa * cc; d[2][2] = ca * cb; for (i = 0; i < 3; i ++) for (j = 0; j < 3; j ++) for (k = 0; k < 3; k ++) a[i][j] += m->afm[i][k] * d[k][j]; for (i = 0; i < 3; i ++) a[3][i] = m->afm[3][i]; for (i = 0; i < 4; i ++) a[i][3] = m->afm[i][3] + CellSize * 2 * a[i][1]; } new_cell(ind,ax,st,mother) Individual *ind; double ax[4][4]; int st; Cell *mother; { Cell *c, *alloc_cell(); double x = ax[0][3], y = ax[1][3], z = ax[2][3]; extern unsigned long Step; extern int MaxCells; if (ind->size >= MaxCells) return 1; if (mother) { double r = CellSize * 2 / sqrt(pow(x-mother->x,2.0)+pow(y-mother->y,2.0) +pow(z-mother->z,2.0)); x = mother->x + (x - mother->x) * r; y = mother->y + (y - mother->y) * r; z = mother->z + (z - mother->z) * r; } if (if_conflict(ind,x,y,z)) return 0; c = alloc_cell(ind); c->x = x; c->y = y; c->z = z; bcopy(ax,c->afm,sizeof(double)*16); c->state = st; c->step = Step; c->mother = mother; ind->size ++; return 1; }