Index: Makefile =================================================================== --- Makefile (nonexistent) +++ Makefile (revision 7364) @@ -0,0 +1,3 @@ +CFLAGS = -Wall -g +LDFLAGS = -lm +cap: cap.o pcb_draw.o gp.o c24lib/image/draw.o c24lib/image/image.o c24lib/image/pnm.o \ No newline at end of file Index: cap.c =================================================================== --- cap.c (nonexistent) +++ cap.c (revision 7364) @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include "c24lib/image/image.h" +#include "c24lib/image/draw.h" +#include "c24lib/image/pnm.h" + +#include "pcb_draw.h" +#include "gp.h" + +#define MAX_STROKES 64 +int max_strokes = 16; +image_t *ref; + +typedef struct { + int x1, y1, x2, y2, th; +} stroke_t; + +typedef struct { + int len; + stroke_t str[MAX_STROKES]; +} pcb_t; + +int myrand(int from, int to) +{ + return from + rand() % (to - from); +} + +void *pcb_create(void) +{ + pcb_t *p = malloc(sizeof(pcb_t)); + int n; + + p->len = myrand(1, max_strokes); + for(n = 0; n < p->len; n++) { + p->str[n].x1 = myrand(0, ref->sx-1); + p->str[n].x2 = myrand(0, ref->sx-1); + p->str[n].y1 = myrand(0, ref->sy-1); + p->str[n].y2 = myrand(0, ref->sy-1); + p->str[n].th = myrand(1, 20); + } + + return p; +} + +double pcb_score(void *data) +{ + pcb_t *p = data; + return p->len; +} + + +void pcb_mutate(void *data) +{ + +} + +void pcb_clone(void *dst, void *src) +{ + memcpy(dst, src, sizeof(pcb_t)); +} + + +gp_pop_t pop = { + .len = 16, + .weak_kill = 5, + .clone_times = 2, + .create = pcb_create, + .score = pcb_score, + .mutate = pcb_mutate, + .clone = pcb_clone +}; + +int main() +{ + image_t *img; + + img = image_new(240, 240); + draw_color(pixel_red); + + draw_pcb_line(img, 30, 30, 200, 150, 10); + draw_pcb_line(img, 150, 150, 30, 30, 10); + + draw_pcb_line(img, 10, 100, 50, 100, 10); + + draw_pcb_line(img, 10, 100, 10, 150, 10); + + + pnm_save(img, "test.pnm"); +} Index: gp.c =================================================================== --- gp.c (nonexistent) +++ gp.c (revision 7364) @@ -0,0 +1,46 @@ +#include + +#include "gp.h" + +static int cmp_fitness(const void *d1, const void *d2) +{ + const gp_indiv_t *i1 = d1, *i2 = d2; + return i1->fitness > i2->fitness; +} + +static void gp_sort(gp_pop_t *pop) +{ + qsort(pop->indiv, pop->len, sizeof(gp_indiv_t), cmp_fitness); +} + +void gp_setup(gp_pop_t *pop) +{ + int n; + pop->indiv = malloc(sizeof(gp_indiv_t) * pop->len); + for(n = 0; n < pop->len; n++) { + pop->indiv[n].data = pop->create(); + pop->indiv[n].fitness = pop->score(pop->indiv[n].data); + } + gp_sort(pop); +} + +void gp_iterate(gp_pop_t *pop) +{ + int n, s, delay; + + /* kill the weakest few and replace them with the strongests */ + for(delay = s = 0, n = pop->len - pop->weak_kill - 1; n < pop->len; n++) { + pop->clone(pop->indiv[n].data, pop->indiv[s].data); + pop->mutate(pop->indiv[n].data); + if (delay > pop->clone_times) { + delay = 0; + s++; + } + } + + /* score and sort */ + for(n = s; n < pop->len; n++) + pop->indiv[n].fitness = pop->score(pop->indiv[n].data); + gp_sort(pop); +} + Index: gp.h =================================================================== --- gp.h (nonexistent) +++ gp.h (revision 7364) @@ -0,0 +1,22 @@ +typedef struct { + void *data; + double fitness; +} gp_indiv_t; + +typedef struct { + /* config */ + int len; + int weak_kill; + int clone_times; + + void *(*create)(void); + void (*mutate)(void *data); + void (*clone)(void *dst, void *src); + double (*score)(void *data); + + /* internal */ + gp_indiv_t *indiv; +} gp_pop_t; + +void gp_setup(gp_pop_t *pop); +void gp_iterate(gp_pop_t *pop); Index: pcb_draw.c =================================================================== --- pcb_draw.c (nonexistent) +++ pcb_draw.c (revision 7364) @@ -0,0 +1,62 @@ +#include +#include "pcb_draw.h" +#include "c24lib/image/image.h" + +static void draw_thick_line(image_t * dst, int x1, int y1, int x2, int y2, double vx, double vy, double nx, double ny, int th) +{ + double x, y; + x = x1; + y = y1; + for(;;) { + int n; + double xx, yy; + xx = x - nx * th / 2.0; + yy = y - ny * th / 2.0; + for(n = 0; n < th; n++) { + if ((xx >= 0) && (xx <= dst->sx) && (yy >= 0) && (yy <= dst->sy)) { + pixel_t *pixel = (dst->pixmap) + (int) xx + dst->sx * (int) yy; + *pixel = pixel_green; + } + xx += nx; + yy += ny; + } + + x += vx/2.0; + y += vy/2.0; + if ((x > x2) || (y > y2)) + break; + } + + return; +} + + +void draw_pcb_line(image_t *dst, int x1, int y1, int x2, int y2, double th) +{ + double nx, ny, vx, vy; + double l; + int tmp; + + if (x1 > x2) { + tmp = x1; + x1 = x2; + x2 = tmp; + tmp = y1; + y1 = y2; + y2 = tmp; + } + + vx = x2 - x1; + vy = y2 - y1; + l = hypot(vx, vy); + vx /= l; + vy /= l; + nx = -vy; + ny = vx; + + draw_thick_line(dst, x1, y1, x2, y2, vx, vy, nx, ny, th); + +// draw_color(pixel_red); +// draw_line(dst, x1+nx*th/2.0, y1+ny*th, x2+nx*th, y2+ny*th/2.0); +// draw_line(dst, x1-nx*th/2.0, y1-ny*th, x2-nx*th, y2-ny*th/2.0); +} Index: pcb_draw.h =================================================================== --- pcb_draw.h (nonexistent) +++ pcb_draw.h (revision 7364) @@ -0,0 +1,4 @@ +#include "c24lib/image/image.h" + +void draw_pcb_line(image_t *dst, int x1, int y1, int x2, int y2, double th); +