]> mj.ucw.cz Git - leo.git/blobdiff - labeller.c
Labelling: Let's truly support map parts and links to them
[leo.git] / labeller.c
index ff4e4a0abd749a65e33ad5a612649e4e68884923..5e82680e6648342ed180ca389ba834d2c924f16e 100644 (file)
@@ -90,6 +90,12 @@ int move_max = 1;
 
 int num_requests = 0;
 
+int conf_map_part_width = 5;
+int conf_map_part_height = 5;
+int num_map_parts_row;
+int num_map_parts_col;
+int num_map_parts;
+
 void make_graph(void);
 void label_graph(void);
 void join_edge(struct graph_edge *e, int dir);
@@ -131,6 +137,9 @@ void gen_coords_point(struct placement *p);
 void gen_coords_segment(struct placement *p);
 void gen_coords_area(struct placement *p);
 
+struct map_part **get_map_parts(struct placement *p);
+void update_map_parts(struct placement *p);
+
 void make_segments_old(void);
 
 void labeller_cleanup(void);
@@ -139,7 +148,7 @@ struct individual **perform_crossover(struct individual *parent1, struct individ
 void perform_mutation(struct individual *individual);
 
 void hide_segment_labels(struct individual *individual);
-void init_placement(struct placement *p, struct request *r);
+void init_placement(struct placement *p, struct individual *individual, struct request *r);
 void init_individual(struct individual *i);
 struct map_part **get_parts(struct placement *symbol, struct individual *individual);
 
@@ -165,6 +174,8 @@ int min2(int a, int b);
 int max4(int a, int b, int c, int d);
 int min4(int a, int b, int c, int d);
 
+struct placement dummy_placement;
+
 int max2(int a, int b)
 {
   return (a > b ? a : b);
@@ -209,6 +220,10 @@ void labeller_init(void)
 
   page_width_int = floor(page_width);
   page_height_int = floor(page_height);
+
+  num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
+  num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
+  num_map_parts = num_map_parts_row * num_map_parts_col;
 }
 
 void make_bitmap(struct point_variant *v, struct symbol *sym)
@@ -247,28 +262,13 @@ void make_bitmap_point(struct point_variant *v, struct sym_point *sp)
 
 void make_bitmap_label(struct point_variant *v, struct sym_text *text)
 {
-  int x_ld = 0;
-  int y_ld = 0;
-  int x_lu = 0;
-  int y_lu = 0;
-  int x_rd = 0;
-  int y_rd = 0;
-  int x_ru = 0;
-  int y_ru = 0;
-
-  v->width = max4(x_ld, x_lu, x_rd, x_ru) - min4(x_ld, x_lu, x_rd, x_ru);
-  v->height = max4(y_ld, y_lu, y_rd, y_ru) - min4(y_ld, y_lu, y_rd, y_ru);
-  //v->bitmap = malloc((int) (ceil(v->width) * ceil(v->height) * sizeof(bool)));
-
   v->width = ceil(text->tw);
   v->height = ceil(text->th);
   v->bitmap = malloc(v->width * v->height * sizeof(bool));
-//  printf("Allocated bitmap of %d bools for %d x %d label\n", v->width * v->height, v->width, v->height);
   for (int i=0; i<v->height; i++)
     for (int j=0; j<v->width; j++)
     {
       v->bitmap[i*v->width + j] = 1;
-//      printf("Writing at %d\n", i*v->width + j);
     }
 }
 
@@ -1062,27 +1062,27 @@ void make_population(void)
     int p = 0;
     for (uns j=0; j<GARY_SIZE(requests_point); j++)
     {
-      init_placement(&(individual->placements[p++]), (struct request *) &requests_point[j]);
+      init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_point[j]);
     }
 
     for (uns j=0; j<GARY_SIZE(requests_line); j++)
     {
-      init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j]);
+      init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j]);
 
       for (uns k=0; k<GARY_SIZE(requests_line[j].sections); k++)
       {
-        init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].sections[k]);
+        init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k]);
 
         for (uns l=0; l<GARY_SIZE(requests_line[j].sections[k].segments); l++)
         {
-          init_placement(&(individual->placements[p++]), (struct request *) &requests_line[j].sections[k].segments[l]);
+          init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_line[j].sections[k].segments[l]);
         }
       }
     }
 
     for (uns j=0; j<GARY_SIZE(requests_area); j++)
     {
-      init_placement(&(individual->placements[p++]), (struct request *) &requests_area[j]);
+      init_placement(&(individual->placements[p++]), individual, (struct request *) &requests_area[j]);
     }
 
     hide_segment_labels(individual);
@@ -1250,6 +1250,89 @@ void rank_population(void)
   // FIXME
 }
 
+struct map_part **get_map_parts(struct placement *p)
+{
+  if (p->variant_used < 0) return NULL;
+
+  struct map_part **buffer;
+  GARY_INIT(buffer, 0);
+
+  if (dbg_map_parts)
+    printf("Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
+
+  struct point_variant v;
+  switch (p->request->type)
+  {
+    case REQUEST_POINT:
+      v = ((struct request_point *) p->request)->variants[p->variant_used];
+      break;
+    case REQUEST_SEGMENT:
+      v = ((struct request_segment *) p->request)->variant[p->variant_used];
+      break;
+    case REQUEST_AREA:
+      v = ((struct request_area *) p->request)->variants[p->variant_used];
+      break;
+    default:
+      return NULL;
+  }
+
+  int x_min = max2(0, p->x) / conf_map_part_width;
+  int x_max = min2(page_width_int, (p->x + v.width + conf_map_part_width - 1)) / conf_map_part_width;
+  int y_min = max2(0, p->y) / conf_map_part_height;
+  int y_max = min2(page_height_int, (p->y + v.height + conf_map_part_height - 1)) / conf_map_part_height;
+
+  if (dbg_map_parts)
+    printf("Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
+
+  for (int y=y_min; y<=y_max; y++)
+    for (int x=x_min; x<=x_max; x++)
+    {
+      struct map_part **m = GARY_PUSH(buffer);
+      if (dbg_map_parts)
+        printf("Asking for %d of %u\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
+      *m = p->individual->map[y * num_map_parts_row + x];
+    }
+
+  return buffer;
+}
+
+void update_map_parts(struct placement *p)
+{
+  struct placement_link *ml = p->map_links;
+  while (ml)
+  {
+    struct map_placement *mp = ml->mp;
+    mp->prev = mp->next;
+    if (mp->next)
+      mp->next->prev = mp->prev;
+    free(mp);
+
+    struct placement_link *tmp = ml;
+    ml = ml->next;
+    free(tmp);
+  }
+
+  struct map_part **parts = get_map_parts(p);
+  if (parts == NULL) return;
+
+  for (uns i=0; i<GARY_SIZE(parts); i++)
+  {
+    struct map_placement *mp = malloc(sizeof(struct map_placement));
+    mp->placement = p;
+
+    mp->next = parts[i]->placement->next;
+    parts[i]->placement->next = mp;
+    if (mp->next) mp->next->prev = mp;
+
+    struct placement_link *ml = malloc(sizeof(struct placement_link));
+    ml->mp = mp;
+    ml->next = p->map_links;
+    p->map_links = ml;
+  }
+
+  GARY_FREE(parts);
+}
+
 void gen_coords(struct placement *p)
 {
   switch(p->request->type)
@@ -1270,6 +1353,8 @@ void gen_coords(struct placement *p)
       printf("Testing request type\n");
       ASSERT(p->request->type != REQUEST_INVALID);
   }
+
+  update_map_parts(p);
 }
 
 double gen_movement(void)
@@ -1418,13 +1503,15 @@ void hide_segment_labels(struct individual *individual)
   }
 }
 
-void init_placement(struct placement *p, struct request *r)
+void init_placement(struct placement *p, struct individual *individual, struct request *r)
 {
   // FIXME
   p->request = r;
   p->processed = 0;
   p->x = p->y = 0; // To prevent valgrind from complaining
   p->variant_used = 0;
+  p->map_links = NULL;
+  p->individual = individual;
   switch (r->type)
   {
     case REQUEST_POINT: ;
@@ -1463,6 +1550,14 @@ void init_individual(struct individual *i)
 //printf("Initing individual\n");
   GARY_INIT(i->placements, num_requests);
   GARY_INIT(i->map, 0);
+  for (uns j=0; j<num_map_parts; j++)
+  {
+    struct map_part *part = GARY_PUSH(i->map);
+    GARY_INIT(part->placement, 0);
+    struct map_placement *mp = GARY_PUSH(part->placement);
+    mp->placement = &dummy_placement;
+    mp->next = mp->prev = NULL;
+  }
   i->penalty = 0; // FIXME
 }