]> mj.ucw.cz Git - leo.git/blobdiff - lab-bitmaps.c
Labelling: Bugfixes in get_closure
[leo.git] / lab-bitmaps.c
index 8845eac42ae0c4e80815df7a236b92ad826c7562..51d1da847623d12f17944dae19e0b098ccfa5ddf 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "leo.h"
 #include "sym.h"
+#include "map.h"
 #include "labeller.h"
 #include "lab-bitmaps.h"
 #include "lab-utils.h"
@@ -20,7 +21,11 @@ static void make_bitmap_label(struct variant *v, struct sym_text *text);
 static int overlaps(struct placement *p1, struct placement *p2);
 
 static struct map_part **get_map_parts(struct placement *p);
-static struct placement **get_overlapping(struct placement *p);
+static struct placement **get_overlapping_in(struct placement *p, struct individual *individual);
+
+double bitmap_granularity = 1;
+
+int page_width_gran, page_height_gran;
 
 
 void make_bitmap(struct variant *v, struct symbol *sym)
@@ -104,11 +109,11 @@ static void make_bitmap_label(struct variant *v, struct sym_text *text)
 
 void dump_bitmaps(struct individual *individual)
 {
-  bool *bitmap = xmalloc(page_width_int * page_height_int * sizeof(bool));
-  printf("Bitmap size is %d\n", page_width_int * page_height_int);
-  for (int i=0; i<page_height_int; i++)
-    for (int j=0; j<page_width_int; j++)
-      bitmap[i*page_width_int + j] = 0;
+  bool *bitmap = xmalloc(page_width_gran * page_height_gran * sizeof(bool));
+  printf("Bitmap size is %d\n", page_width_gran * page_height_gran);
+  for (int i=0; i<page_height_gran; i++)
+    for (int j=0; j<page_width_gran; j++)
+      bitmap[i*page_width_gran + j] = 0;
 
   int total = 0;
   for (uns i=0; i<GARY_SIZE(individual->placements); i++)
@@ -130,10 +135,18 @@ void dump_bitmaps(struct individual *individual)
         continue;
     }
 
-    int base_x = p->x; int base_y = p->y;
-    for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
+    // FIXME:
+    // if e.g. granularity is 2, p->x = 2.4 and v->width = 4
+    // then <2, 2.5> will be marked as occupied while <4, 4.5> won't be,
+    // though the variant occupies <2.4, 4,4> and therefore occupies much
+    // more of <4, 4.5> than of <2, 2.5>
+    int base_x = p->x * bitmap_granularity;
+    int base_y = p->y * bitmap_granularity;
+    int max_dr = min2(v->height - 1, (page_height - p->y) * bitmap_granularity);
+    int max_dc = min2(v->width - 1, (page_width - p->x) * bitmap_granularity);
+    for (int dr = max2(0, 0-p->y); dr < max_dr; dr++)
     {
-      for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
+      for (int dc = max2(0, 0-p->x); dc < max_dc; dc++)
       {
         if (v->bitmap[dr * v->width + dc])
         {
@@ -147,12 +160,12 @@ void dump_bitmaps(struct individual *individual)
 
   FILE *fd_dump = fopen("dump.pbm", "w");
   fprintf(fd_dump, "P1\n");
-  fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
-  for (int i=0; i<page_height_int; i++)
+  fprintf(fd_dump, "%d %d\n", page_width_gran, page_height_gran);
+  for (int i=0; i<page_height_gran; i++)
   {
-    for (int j=0; j<page_width_int; j++)
+    for (int j=0; j<page_width_gran; j++)
     {
-      fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
+      fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int*bitmap_granularity + j)] ? 1 : 0);
     }
     fprintf(fd_dump, "\n");
   }
@@ -187,10 +200,10 @@ static struct map_part **get_map_parts(struct placement *p)
 
   int x_min = max2(0, p->x) / conf_map_part_width;
   // CHECK ME: Is rounding needed?
-  int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
+  int x_max = min2(page_width_int, (p->x + v.width / bitmap_granularity)) / conf_map_part_width;
   int y_min = max2(0, p->y) / conf_map_part_height;
   // CHECK ME: Is rounding needed?
-  int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
+  int y_max = min2(page_height_int, (p->y + v.height / bitmap_granularity)) / conf_map_part_height;
 
   DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
 
@@ -253,7 +266,7 @@ void update_map_parts(struct placement *p)
   update_map_parts_create(p);
 }
 
-struct placement **get_closure(struct placement *placement)
+struct placement **get_closure(struct placement *placement, struct individual *other)
 {
   struct placement **closure;
   GARY_INIT(closure, 0);
@@ -267,7 +280,7 @@ struct placement **get_closure(struct placement *placement)
   while (first < GARY_SIZE(closure))
   {
     DEBUG(dbg_breeding, VERBOSITY_ALL, "Iterating, first is %u of current %zu\n", first, GARY_SIZE(closure));
-    struct placement **overlapping = get_overlapping(placement);
+    struct placement **overlapping = get_overlapping_in(closure[first], other);
     if (! overlapping) { first++; continue; }
 
     struct placement **filtered = filter(overlapping, &chosen);
@@ -280,7 +293,8 @@ struct placement **get_closure(struct placement *placement)
       {
         if (overlaps(*p, overlapping[j]))
         {
-         p = GARY_PUSH(closure); *p = overlapping[j];
+         p = GARY_PUSH(closure);
+         *p = &(placement->individual->placements[overlapping[j]->request->ind]);
          DEBUG(dbg_breeding, VERBOSITY_ALL, "Adding placement of request %d (in fact at [%.2f; %.2f] of size %d x %d)\n", overlapping[j]->request->ind, overlapping[j]->x, overlapping[j]->y, overlapping[j]->request->variants[overlapping[j]->variant_used].width, overlapping[j]->request->variants[overlapping[j]->variant_used].height);
          chosen[overlapping[j]->request->ind] = 1;
        }
@@ -295,7 +309,7 @@ struct placement **get_closure(struct placement *placement)
   return closure;
 }
 
-static struct placement **get_overlapping(struct placement *p)
+static struct placement **get_overlapping_in(struct placement *p, struct individual *individual)
 {
   struct placement **buffer;
   GARY_INIT(buffer, 0);
@@ -305,7 +319,7 @@ static struct placement **get_overlapping(struct placement *p)
 
   for (uns i=0; i<GARY_SIZE(parts); i++)
   {
-    struct map_placement *mp = parts[i]->placement->next_in_map;
+    struct map_placement *mp = individual->map[parts[i]->ind]->placement->next_in_map;
     while (mp)
     {
       if (p->variant_used >= 0)
@@ -343,18 +357,42 @@ static int overlaps(struct placement *p1, struct placement *p2)
   v1 = &(p1->request->variants[p1->variant_used]);
   v2 = &(p2->request->variants[p2->variant_used]);
 
-  // FIXME: This doesn't fully respect offset which it probably should
-  int p1x = p1->x; int p1y = p1->y;
-  int p2x = p2->x; int p2y = p2->y;
+  int x1, x2;
+  // FIXME: CHECK ME: Is rounding working all right here?
+  if (p1->x > p2->x)
+  {
+    x1 = 0;
+    x2 = (p1->x - p2->y) * bitmap_granularity;
+  }
+  else
+  {
+    x1 = (p2->x - p1->x) * bitmap_granularity;
+    x2 = 0;
+  }
+
+  int max_iter_x = min2(v1->width-1-x1, v2->width-1-x2);
+
+  int y1, y2;
+  if (p1->y > p2->y)
+  {
+    y1 = 0;
+    y2 = (p1->y - p2->y) * bitmap_granularity;
+  }
+  else
+  {
+    y1 = (p2->y - p1->y) * bitmap_granularity;
+    y2 = 0;
+  }
+
+  int max_iter_y = min2(v1->width-1-y1, v2->width-1-y2);
 
   int overlap = 0;
-  for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
-    for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
-    {
-      if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
-          v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
+  for (int j=0; j<max_iter_y; j++, y1++, y2++) {
+    for (int i=0; i<max_iter_x; i++, x1++, x2++) {
+      if (v1->bitmap[y1*v1->width + x1] && v2->bitmap[y2*v2->width + x2])
         overlap++;
     }
+  }
 
   return overlap;
 }
@@ -410,13 +448,13 @@ int get_overlap(struct placement *p, int **planned_ptr, int iteration)
     //dump_placement_links(p);
   }
 
-  if (p->x < 0) overlap += 0 - p->x;
-  if (p->x + p->request->variants[p->variant_used].width > page_width_int)
-    overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
+  if (p->x < 0) overlap += (0 - p->x) * bitmap_granularity;
+  if (p->x + p->request->variants[p->variant_used].width * bitmap_granularity > page_width)
+    overlap += p->x + p->request->variants[p->variant_used].width * bitmap_granularity - page_width;
 
-  if (p->y < 0) overlap += 0 - p->y;
-  if (p->y + p->request->variants[p->variant_used].height > page_height_int)
-    overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
+  if (p->y < 0) overlap += (0 - p->y) * bitmap_granularity;
+  if (p->y + p->request->variants[p->variant_used].height * bitmap_granularity > page_height)
+    overlap += p->y + p->request->variants[p->variant_used].height * bitmap_granularity - page_height;
 
   return overlap;
 }