[GRASS-SVN] r31102 - grass/trunk/vector/v.label.sa

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Apr 24 12:18:02 EDT 2008


Author: wolf
Date: 2008-04-24 12:18:02 -0400 (Thu, 24 Apr 2008)
New Revision: 31102

Modified:
   grass/trunk/vector/v.label.sa/font.c
   grass/trunk/vector/v.label.sa/labels.c
   grass/trunk/vector/v.label.sa/labels.h
   grass/trunk/vector/v.label.sa/main.c
Log:
Fixes to the algorithm

Modified: grass/trunk/vector/v.label.sa/font.c
===================================================================
--- grass/trunk/vector/v.label.sa/font.c	2008-04-24 16:10:51 UTC (rev 31101)
+++ grass/trunk/vector/v.label.sa/font.c	2008-04-24 16:18:02 UTC (rev 31102)
@@ -1,6 +1,8 @@
 /*
- * These functions are copied from the diplay driver lib, so that we don't need
- * to have an active display driver open, to run this module.
+ * from lib/driver/parse_ftcap.c
+ * These functions are copied from the diplay driver lib,
+ * so that we don't need to have an active display driver open,
+ * to run this module.
  */
 
 #include <stdio.h>

Modified: grass/trunk/vector/v.label.sa/labels.c
===================================================================
--- grass/trunk/vector/v.label.sa/labels.c	2008-04-24 16:10:51 UTC (rev 31101)
+++ grass/trunk/vector/v.label.sa/labels.c	2008-04-24 16:18:02 UTC (rev 31102)
@@ -8,7 +8,6 @@
 static void label_point_candidates(label_t * label);
 static void label_line_candidates(label_t * label);
 static int candidate_compare(const void *a, const void *b);
-static struct line_pnts *skyline_trans_rot(struct line_pnts *skyline, label_point_t * p, double angle);
 static double label_avedist(label_t * label, label_candidate_t * candidate);
 static double label_flatness(label_t * label, label_candidate_t * candidate);
 static double label_pointover(label_t * label, label_candidate_t * candidate);
@@ -178,7 +177,7 @@
 
 	G_debug(3, "Label: %s", db_get_string(&value));
 
-	/* ignor empty strings */
+	/* ignore empty strings */
 	if (strlen(db_get_string(&value)) == 0)
 	    continue;
 
@@ -517,6 +516,7 @@
 	struct line_pnts *above_skyline, *below_skyline, *baseline;
 	double above_distance = 0.0, below_distance = 0.0,
 	    minimum_above_distance = 0.0, minimum_below_distance = 0.0, angle;
+	double flatness, centerdness;
 
 	seg1 =
 	    Vect_point_on_line(label->shape, pos, &p1.x, &p1.y, NULL, NULL,
@@ -550,7 +550,7 @@
 
 	/* find the maximum above_distance and below_distance from the swath
 	 * "diagonal" to determine maximum deviation from a straight line
-	 * create the swath lines at the same time
+	 * create the swath lines at the same time.
 	 */
 	above_candidates[i].swathline = Vect_new_line_struct();
 	below_candidates[i].swathline = Vect_new_line_struct();
@@ -647,33 +647,48 @@
 	above_candidates[i].rotation = angle;
 	below_candidates[i].rotation = angle;
 
+	/* AveDist */
 	above_candidates[i].score += label_avedist(label, &above_candidates[i]);
 	below_candidates[i].score += label_avedist(label, &below_candidates[i]);
 
-	above_candidates[i].score +=
-	    label_flatness(label, &above_candidates[i]);
-	below_candidates[i].score +=
-	    label_flatness(label, &below_candidates[i]);
+	/* flatness */
+	flatness = label_flatness(label, &above_candidates[i]);
+	above_candidates[i].score += flatness;
+	below_candidates[i].score += flatness;
 
 	/* centerdness */
-	above_candidates[i].score += 3.0 * fabs(2.0 * pos / length - 1.0);
-	below_candidates[i].score += 3.0 * fabs(2.0 * pos / length - 1.0);
+	centerdness = 3.0 * fabs(2.0 * pos / length - 1.0);
+	above_candidates[i].score += centerdness;
+	below_candidates[i].score += centerdness;
 
+	/* PointOver */
 	above_candidates[i].score += 10.0 *
 	    label_pointover(label, &above_candidates[i]);
 	below_candidates[i].score += 10.0 *
 	    label_pointover(label, &below_candidates[i]);
+
+	/* LineOver */
 	above_candidates[i].lineover = 15.0 *
 	    label_lineover(label, &above_candidates[i], GV_LINE);
 	above_candidates[i].score += above_candidates[i].lineover;
+if(label->cat == 1) {
+	printf("%s: above candidate %d: lineover=%lf score=%lf\n",label->text, i, above_candidates[i].lineover, above_candidates[i].score);
+}
+
 	below_candidates[i].lineover = 15.0 *
 	    label_lineover(label, &below_candidates[i], GV_LINE);
 	below_candidates[i].score += below_candidates[i].lineover;
+if(label->cat == 1) {
+	printf("%s: below candidate %d: lineover=%lf score=%lf\n",label->text, i, below_candidates[i].lineover, below_candidates[i].score);
+}
+
+	/* AreaOver */
 	above_candidates[i].score += 10.0 *
 	    label_lineover(label, &above_candidates[i], GV_BOUNDARY);
 	below_candidates[i].score += 10.0 *
 	    label_lineover(label, &below_candidates[i], GV_BOUNDARY);
 
+	/* aboveness */
 	below_candidates[i].score += 0.25;
 
 	i++;
@@ -766,17 +781,8 @@
     }
 }
 
-/**
- * This function rotates the label skyline and then translates it to the
- * given point.
- * @param skyline The skyline to translate
- * @param p The point to translate the skyline to
- * @param angle The angle (in radians) to rotate the label counter-clockwise
- * @return A lint_pnts structure containing the rotated and translated
- * skyline.
- */
-static struct line_pnts *skyline_trans_rot(struct line_pnts *skyline,
-					   label_point_t * p, double angle)
+struct line_pnts *skyline_trans_rot(struct line_pnts *skyline,
+				    label_point_t * p, double angle)
 {
     int i;
     struct line_pnts *Points;
@@ -879,29 +885,81 @@
 static double label_flatness(label_t * label, label_candidate_t * candidate)
 {
     struct line_pnts *line;
-    double flatness = 0.0, x, y;
+    double flatness = 0.0, x0, y0, x1, y1, x2, y2;
     int i;
 
+/* first generate a line which is parallel to the baseline, 
+   but the ideal distance away from it.*/
     line = Vect_new_line_struct();
-    Vect_append_point(line, candidate->point.x, candidate->point.y, 0);
-    x = candidate->point.x +
-	(label->bb.E - label->bb.W) * cos(candidate->rotation);
-    y = candidate->point.y +
-	(label->bb.E - label->bb.W) * sin(candidate->rotation);
-    Vect_append_point(line, x, y, 0);
+    x0 = x1 = candidate->point.x + ideal_distance * cos(candidate->rotation);
+    y0 = y1 = candidate->point.y - ideal_distance * sin(candidate->rotation);
+    Vect_append_point(line, x1, y1, 0);
+    x2 = x1 + (label->bb.E - label->bb.W) * cos(candidate->rotation);
+    y2 = y1 + (label->bb.E - label->bb.W) * sin(candidate->rotation);
+    Vect_append_point(line, x2, y2, 0);
 
-    for (i = 0; i < candidate->swathline->n_points; i++) {
-	double d;
-	Vect_line_distance(line, candidate->swathline->x[i],
-			   candidate->swathline->y[i], 0, 0,
-			   NULL, NULL, NULL, &d, NULL, NULL);
-	flatness += d;
+    /* now calculate the are between candidate->swathline and line */
+
+    for (i = 1; i < candidate->swathline->n_points; i++) {
+	int r;
+	double b,h;
+	double px1, py1, pz1, px2, py2, pz2;
+	r = Vect_segment_intersection(x1, y1, 0, x2, y2, 0,
+				      candidate->swathline->x[i-1],
+				      candidate->swathline->y[i-1],
+				      0,
+				      candidate->swathline->x[i],
+				      candidate->swathline->y[i],
+				      0,
+				      &px1, &py1, &pz1, &px2, &py2, &pz2, 0);
+	/* Now calculate the area between the swath and the line */
+	switch(r) {
+	    case 0: /* no intersection */
+		dig_distance2_point_to_line(candidate->swathline->x[i],
+					    candidate->swathline->y[i], 0,
+					    x1, y1, 0, x2, y2, 0, 0,
+					    &px1, &py1, &pz1, &h, NULL);
+		h = (sqrt(pow(x1 - candidate->swathline->x[i-1], 2.0) + 
+			 pow(y1 - candidate->swathline->y[i-1], 2.0)) +
+		    h) / 2.0;
+		b = sqrt(pow(px1-x1, 2) + pow(py1 - y1, 2));
+		flatness += b * h;
+		x1 = px1;
+		y1 = py1;
+		break;
+	    case 1:
+		h = sqrt(pow(x1 - candidate->swathline->x[i-1], 2.0) + 
+			 pow(y1 - candidate->swathline->y[i-1], 2.0));
+		b = sqrt(pow(px1-x1, 2) + pow(py1 - y1, 2));
+		flatness += b * h * 0.5; /* the first triangle */
+		x1 = px1;
+		y1 = py1;
+		dig_distance2_point_to_line(candidate->swathline->x[i],
+					    candidate->swathline->y[i], 0,
+					    x1, y1, 0, x2, y2, 0, 0,
+					    &px1, &py1, &pz1, &h, NULL);
+		b = sqrt(pow(px1-x1, 2) + pow(py1 - y1, 2));
+		flatness += b * h * 0.5; /* the second triangle */
+		x1 = px1;
+		y1 = py1;
+		break;
+	    case 3:
+		x1 = px2;
+		y1 = py2;
+		break;
+	    case 5:
+		x1 = px2;
+		y1 = py2;
+		break;
+	    default:
+		G_fatal_error("Programming error!!\n");
+		break;
+	}
     }
 
-    flatness /= (candidate->swathline->n_points);
-    Vect_destroy_line_struct(line);
-
-    return (flatness * flatness) / (ideal_distance * ideal_distance);
+    flatness /= sqrt((x2-x0)*(x2-x0) + (y2-y0)*(y2-y0)); /* this is d'' */
+    flatness = (flatness * flatness) / (ideal_distance * ideal_distance);
+    return flatness;
 }
 
 /**
@@ -960,13 +1018,13 @@
     b.x = (label->bb.E - label->bb.W) * cos(candidate->rotation);
     b.y = (label->bb.E - label->bb.W) * sin(candidate->rotation);
 
-    n = Vect_select_lines_by_polygon(&Map, trsk, 0, NULL, linetype, il);
+    trbb = box_trans_rot(&label->bb, &candidate->point, candidate->rotation);
+    n = Vect_select_lines_by_polygon(&Map, trbb, 0, NULL, linetype, il);
 
     if(n == 0) {
 	return 0.0;
     }
 
-    trbb = box_trans_rot(&label->bb, &candidate->point, candidate->rotation);
 
     for (i = 0; i < il->n_values; i++) {
 	int j, found=0;

Modified: grass/trunk/vector/v.label.sa/labels.h
===================================================================
--- grass/trunk/vector/v.label.sa/labels.h	2008-04-24 16:10:51 UTC (rev 31101)
+++ grass/trunk/vector/v.label.sa/labels.h	2008-04-24 16:18:02 UTC (rev 31102)
@@ -149,6 +149,18 @@
  */
 void print_label(FILE * labelf, label_t * label, struct params *p);
 
+/**
+ * This function rotates the label skyline and then translates it to the
+ * given point.
+ * @param skyline The skyline to translate
+ * @param p The point to translate the skyline to
+ * @param angle The angle (in radians) to rotate the label counter-clockwise
+ * @return A lint_pnts structure containing the rotated and translated
+ * skyline.
+ */
+struct line_pnts *skyline_trans_rot(struct line_pnts *skyline,
+				    label_point_t * p, double angle);
+
 void free_freetypecap(struct GFONT_CAP *ftcap);
 struct GFONT_CAP *find_font_from_freetypecap(const char *font);
 

Modified: grass/trunk/vector/v.label.sa/main.c
===================================================================
--- grass/trunk/vector/v.label.sa/main.c	2008-04-24 16:10:51 UTC (rev 31101)
+++ grass/trunk/vector/v.label.sa/main.c	2008-04-24 16:18:02 UTC (rev 31102)
@@ -68,20 +68,33 @@
 	G_percent(i, (n_labels - 1), 1);
     }
     fclose(labelf);
-/*
-    {
+
+/* dumping the skyline of the labels */
+/*    {
 	char *f;
+	FILE *skyf;
 	f = G_tempfile();
-	labelf = fopen(f, "w");
-	printf("Writing all labels to file %s", f);
+	skyf = fopen(f, "w");
+	printf("Writing label skylines to file %s", f);
+	fprintf(skyf, "VERTI:\n");
 	for (i = 0; i < n_labels; i++) {
-		if (labels[i].n_candidates > 0) {
-			print_labels(labelf, &labels[i], &p);
+	    int j;
+	    if (labels[i].n_candidates > 0) {
+		label_t *l = &labels[i];
+		label_candidate_t *cc = &l->candidates[l->current_candidate];
+		struct line_pnts *sky = skyline_trans_rot(l->skyline,
+							  &cc->point,
+							  cc->rotation);
+		fprintf(skyf, "L %d 1\n", sky->n_points);
+		for(j=0;j < sky->n_points; j++) {
+		    fprintf(skyf, " %.12f %.12f\n", sky->x[j], sky->y[j]);
 		}
-		G_percent(i, (n_labels - 1), 1);
+		fprintf(skyf, " %-5d %-10d\n", 1, i+1);
+	    }
+	    G_percent(i, (n_labels - 1), 1);
     	}
 	free(f);
-    	fclose(labelf);
+    	fclose(skyf);
     }
 */
     return EXIT_SUCCESS;
@@ -215,14 +228,14 @@
     fprintf(labelf, "north: %lf\n", label->candidates[cc].point.y);
     fprintf(labelf, "xoffset: %lf\n", 0.0); // * (size));
     fprintf(labelf, "yoffset: %lf\n", 0.0); // * (size));
-    fprintf(labelf, "ref: %s\n", "none none");
+    fprintf(labelf, "ref: %s\n", "center left");
 
     fprintf(labelf, "font: %s\n", p->font->answer);
     fprintf(labelf, "color: %s\n", p->color->answer);
 
     fprintf(labelf, "size: %lf\n", size);
 
-    fprintf(labelf, "width: %d\n", p->bowidth->answer);
+    fprintf(labelf, "width: %s\n", p->bowidth->answer);
     fprintf(labelf, "hcolor: %s\n", p->hlcolor->answer);
     fprintf(labelf, "hwidth: %d\n", hlwidth);
     fprintf(labelf, "background: %s\n", p->bgcolor->answer);



More information about the grass-commit mailing list