[mapserver-commits] r12989 - sandbox/sdlime/rfc-77

svn at osgeo.org svn at osgeo.org
Mon Jan 16 22:55:33 EST 2012


Author: sdlime
Date: 2012-01-16 19:55:33 -0800 (Mon, 16 Jan 2012)
New Revision: 12989

Modified:
   sandbox/sdlime/rfc-77/mapdraw.c
Log:
Have within label group collision detection working.

Modified: sandbox/sdlime/rfc-77/mapdraw.c
===================================================================
--- sandbox/sdlime/rfc-77/mapdraw.c	2012-01-16 18:04:54 UTC (rev 12988)
+++ sandbox/sdlime/rfc-77/mapdraw.c	2012-01-17 03:55:33 UTC (rev 12989)
@@ -2240,8 +2240,7 @@
   int nReturnVal = MS_SUCCESS;
 
   if(image) {
-    if(MS_RENDERER_PLUGIN(image->format)) {          
-      pointObj p;
+    if(MS_RENDERER_PLUGIN(image->format)) {
       int i, l, ll, priority;
       rectObj r;
 
@@ -2255,9 +2254,16 @@
       layerObj *layerPtr=NULL;
       labelObj *labelPtr=NULL;
 
-      int marker_width, marker_height;
-      int marker_offset_x, marker_offset_y, label_offset_x, label_offset_y;
+      int marker_width, marker_height; /* marker is defined in POINT or ANNOTATION layer class styles */
+      int marker_offset_x, marker_offset_y;
       rectObj marker_rect = {0,0,0,0};
+ 
+      int label_offset_x, label_offset_y;
+ 
+      int label_marker_width, label_marker_height; /* label_marker is defined in label styles */
+      int label_marker_offset_x, label_marker_offset_y;
+      rectObj label_marker_rect = {0,0,0,0};
+
       int label_mindistance=-1,
       label_buffer=0, map_edge_buffer=0;
 
@@ -2347,7 +2353,38 @@
 
           } else { /* point-based label */
             int drawLabelText=MS_TRUE; // not used yet
+            shapeObj *poly=NULL; /* hold label polygon for one label, cachePtr->poly holds label polygon for the whole group */
 
+            poly = (shapeObj *) msSmallMalloc(sizeof(shapeObj));
+            msInitShape(poly);
+
+            marker_offset_x = marker_offset_y = 0; /* assume no marker */
+            marker_width = marker_height = 0;
+            if(layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0) {
+              /* TODO: at the moment only checks the bottom (first) marker style since it *should* be the
+                 largest, perhaps we should check all of them and build a composite size */
+              if(msGetMarkerSize(&map->symbolset, &(cachePtr->styles[0]), &marker_width, &marker_height, layerPtr->scalefactor) != MS_SUCCESS)
+                return MS_FAILURE;
+
+              marker_offset_x = MS_NINT(marker_width/2.0);
+              marker_offset_y = MS_NINT(marker_height/2.0);
+   
+              marker_rect.minx = MS_NINT(cachePtr->point.x - .5 * marker_width);
+              marker_rect.miny = MS_NINT(cachePtr->point.y - .5 * marker_height);
+              marker_rect.maxx = marker_rect.minx + (marker_width-1);
+              marker_rect.maxy = marker_rect.miny + (marker_height-1); 
+              msRectToPolygon(marker_rect, cachePtr->poly);
+            } else if (layerPtr->type == MS_LAYER_POINT) { /* there is a marker already in the image */
+              marker_rect = markerPtr->poly->bounds;
+              marker_width = marker_rect.maxx - marker_rect.minx;
+              marker_height = marker_rect.maxy - marker_rect.miny;
+              marker_offset_x = MS_NINT(marker_width/2.0);
+              marker_offset_y = MS_NINT(marker_height/2.0);
+              msRectToPolygon(marker_rect, cachePtr->poly);
+            }
+
+            fprintf(stderr, "      got marker size (%d,%d)\n", marker_width, marker_height);
+
             /* all other cases *could* have multiple labels defined */
             for(ll=(cachePtr->numlabels-1); ll>=0; ll--) {
               labelPtr = &(cachePtr->labels[ll]);
@@ -2356,7 +2393,6 @@
               if(!labelPtr->annotext || strlen(labelPtr->annotext) == 0) 
                 continue; /* skip this label (RFC 77 TODO: ok to bail here?) */
 
-	      // RFC 77 TODO: account for raster fonts
               if(labelPtr->type == MS_TRUETYPE) {
                 size = labelPtr->size * layerPtr->scalefactor;
                 size = MS_MAX(size, labelPtr->minsize*image->resolutionfactor);
@@ -2396,55 +2432,30 @@
 
               fprintf(stderr, "      got label size\n");
 
-              marker_offset_x = marker_offset_y = 0; /* assume no marker */
-              marker_width = marker_height = 0; 
-              if(layerPtr->type == MS_LAYER_ANNOTATION && cachePtr->numstyles > 0) {
-                /* TODO: at the moment only checks the bottom (first) marker style since it *should* be the
-                   largest, perhaps we should check all of them and build a composite size */
-                if(msGetMarkerSize(&map->symbolset, &(cachePtr->styles[0]), &marker_width, &marker_height, layerPtr->scalefactor) != MS_SUCCESS)
-                  return MS_FAILURE;
-   
-                marker_offset_x = MS_NINT(marker_width/2.0);
-                marker_offset_y = MS_NINT(marker_height/2.0);
-   
-                marker_rect.minx = MS_NINT(cachePtr->point.x - .5 * marker_width);
-                marker_rect.miny = MS_NINT(cachePtr->point.y - .5 * marker_height);
-                marker_rect.maxx = marker_rect.minx + (marker_width-1);
-                marker_rect.maxy = marker_rect.miny + (marker_height-1); 
-              } else if (layerPtr->type == MS_LAYER_POINT) { /* there is a marker already in the image */
-                marker_rect = markerPtr->poly->bounds;
-                marker_width = marker_rect.maxx - marker_rect.minx;
-                marker_height = marker_rect.maxy - marker_rect.miny;
-                marker_offset_x = MS_NINT(marker_width/2.0);
-                marker_offset_y = MS_NINT(marker_height/2.0);
-              }
-
+              label_marker_offset_x = label_marker_offset_y = 0; /* assume no label marker */
+              label_marker_width = label_marker_height = 0;
               if(labelPtr->numstyles > 0) { 
-                int temp_marker_width, temp_marker_height; 
 
                 for(i=0; i<labelPtr->numstyles; i++) { 
                   /* TODO: at the moment only checks the bottom (first) label style, perhaps should check all of them */ 
                   if(labelPtr->styles[i]->_geomtransform.type == MS_GEOMTRANSFORM_LABELPOINT) { 
-                    if(msGetMarkerSize(&map->symbolset, labelPtr->styles[i], &temp_marker_width, &temp_marker_height, layerPtr->scalefactor) != MS_SUCCESS) 
+                    if(msGetMarkerSize(&map->symbolset, labelPtr->styles[i], &label_marker_width, &label_marker_height, layerPtr->scalefactor) != MS_SUCCESS) 
                       return MS_FAILURE;
                      break; 
                   } 
                 }
 
-                if(temp_marker_width > marker_width || temp_marker_height > marker_height) { 
-                  marker_width = temp_marker_width; 
-                  marker_height = temp_marker_height; 
-
-                  marker_offset_x = MS_NINT(marker_width/2.0); 
-                  marker_offset_y = MS_NINT(marker_height/2.0); 
+                label_marker_offset_x = MS_NINT(label_marker_width/2.0); 
+                label_marker_offset_y = MS_NINT(label_marker_height/2.0); 
  
-                  marker_rect.minx = MS_NINT(cachePtr->point.x - .5 * marker_width) - labelPtr->buffer; 
-                  marker_rect.miny = MS_NINT(cachePtr->point.y - .5 * marker_height) - labelPtr->buffer; 
-                  marker_rect.maxx = MS_NINT(cachePtr->point.x + .5 * marker_width) + labelPtr->buffer; // marker_rect.minx + (marker_width-1) + labelPtr->buffer; 
-                  marker_rect.maxy = MS_NINT(cachePtr->point.y + .5 * marker_height) + labelPtr->buffer; // marker_rect.miny + (marker_height-1) + labelPtr->buffer; 
-                } 
-	      } 
+                label_marker_rect.minx = MS_NINT(cachePtr->point.x - .5 * label_marker_width) - labelPtr->buffer; 
+                label_marker_rect.miny = MS_NINT(cachePtr->point.y - .5 * label_marker_height) - labelPtr->buffer; 
+                label_marker_rect.maxx = MS_NINT(cachePtr->point.x + .5 * label_marker_width) + labelPtr->buffer; // marker_rect.minx + (marker_width-1) + labelPtr->buffer; 
+                label_marker_rect.maxy = MS_NINT(cachePtr->point.y + .5 * label_marker_height) + labelPtr->buffer; // marker_rect.miny + (marker_height-1) + labelPtr->buffer; 
+	      }
 
+              fprintf(stderr, "      got label marker size (%d,%d)\n", label_marker_width, label_marker_height); // RFC 77 TODO: take these values into account
+
               if(drawLabelText && labelPtr->position == MS_AUTO) {
                 int positions[MS_POSITIONS_LENGTH], npositions=0;
    
@@ -2464,19 +2475,25 @@
                 }
    
                 for(i=0; i<npositions; i++) {
-                  msFreeShape(cachePtr->poly);
+                  msFreeShape(poly);
                   cachePtr->status = MS_TRUE; /* assume label *can* be drawn */
    
-                  labelPtr->annopoint = get_metrics(&(cachePtr->point), positions[i], r, (marker_offset_x + label_offset_x), (marker_offset_y + label_offset_y), labelPtr->angle, label_buffer, cachePtr->poly);
-                  if(marker_width > 0 && marker_height > 0)
-                    msRectToPolygon(marker_rect, cachePtr->poly); /* save marker bounding polygon (TODO: could do this once!) */
+                  labelPtr->annopoint = get_metrics(&(cachePtr->point), positions[i], r, (marker_offset_x + label_offset_x), (marker_offset_y + label_offset_y), labelPtr->angle, label_buffer, poly);
+                  // if(label_marker_width > 0 && label_marker_height > 0)
+                  //   msRectToPolygon(label_marker_rect, poly); /* save marker bounding polygon (TODO: could do conversion once) */
 
-                  /* Compare against image bounds, rendered labels and markers (sets cachePtr->status) */
+                  /* first, compare poly against cachePtr->poly (group poly) if not the first label in the group */
+                  if((ll < cachePtr->numlabels-1) && intersectLabelPolygons(poly, cachePtr->poly) == MS_TRUE) continue; /* next position */
+
+                  /* second, add poly to cachePtr->poly */
+                  msCopyShape(poly, cachePtr->poly);
+
+                  /* finally, compare against image bounds, rendered labels and markers (sets cachePtr->status) */
                   msTestLabelCacheCollisions(&(map->labelcache), labelPtr, image->width, image->height, (map_edge_buffer-label_buffer), cachePtr, priority, l, label_mindistance, (r.maxx-r.minx));
 
                   /* found a suitable place for this label */
                   if(cachePtr->status) {
-                    if(drawLabelPoly) 
+                    if(drawLabelPoly)
                       get_metrics_line(&(cachePtr->point), positions[i], r, (marker_offset_x + label_offset_x), (marker_offset_y + label_offset_y), labelPtr->angle, 1, labelPoly.line);
                     break; /* ...out of position loop */
                   }
@@ -2491,25 +2508,36 @@
    
               } else { /* explicit position */
                 cachePtr->status = MS_TRUE; /* assume label *can* be drawn */
+                msFreeShape(poly);
 
                 fprintf(stderr, "    explicit position case (ll=%d)\n", ll);
 
                 if(labelPtr->position == MS_CC) { /* don't need the marker_offset */
-                  labelPtr->annopoint = get_metrics(&(cachePtr->point), labelPtr->position, r, label_offset_x, label_offset_y, labelPtr->angle, label_buffer, cachePtr->poly);
+                  labelPtr->annopoint = get_metrics(&(cachePtr->point), labelPtr->position, r, label_offset_x, label_offset_y, labelPtr->angle, label_buffer, poly);
                   if(drawLabelPoly) get_metrics_line(&(cachePtr->point), labelPtr->position, r, label_offset_x, label_offset_y, labelPtr->angle, 1, labelPoly.line);
                 } else {
-                  labelPtr->annopoint = get_metrics(&(cachePtr->point), labelPtr->position, r, (marker_offset_x + label_offset_x), (marker_offset_y + label_offset_y), labelPtr->angle, label_buffer, cachePtr->poly);
+                  labelPtr->annopoint = get_metrics(&(cachePtr->point), labelPtr->position, r, (marker_offset_x + label_offset_x), (marker_offset_y + label_offset_y), labelPtr->angle, label_buffer, poly);
                   if(drawLabelPoly) get_metrics_line(&(cachePtr->point), labelPtr->position, r, (marker_offset_x + label_offset_x), (marker_offset_y + label_offset_y), labelPtr->angle, 1, labelPoly.line);
                 }
 
-                if(marker_width > 0 && marker_height > 0)
-                  msRectToPolygon(marker_rect, cachePtr->poly); /* save marker bounding polygon, part of overlap tests */
+                // if(label_marker_width > 0 && label_marker_height > 0)
+                //   msRectToPolygon(label_marker_rect, poly); /* save marker bounding polygon, part of overlap tests */
 
-                /* Compare against image bounds, rendered labels and markers (sets cachePtr->status), if FORCE=TRUE then skip the test */
-                if(!labelPtr->force)
-                  msTestLabelCacheCollisions(&(map->labelcache), labelPtr, image->width, image->height, (map_edge_buffer-label_buffer), cachePtr, priority, l, label_mindistance, (r.maxx-r.minx));  
+                fprintf(stderr, "    %d, %d\n", ll, cachePtr->numlabels-1);
+                if(ll < cachePtr->numlabels-1 && !labelPtr->force && intersectLabelPolygons(poly, cachePtr->poly) == MS_TRUE) {
+                  fprintf(stderr, "    [IF]\n");
+                  cachePtr->status = MS_FALSE;
+                  break; /* out of labels loop */
+                } else {
+                  fprintf(stderr, "    [ELSE]\n");
+                  msCopyShape(poly, cachePtr->poly);
+
+                  if(!labelPtr->force)
+                    msTestLabelCacheCollisions(&(map->labelcache), labelPtr, image->width, image->height, (map_edge_buffer-label_buffer), cachePtr, priority, l, label_mindistance, (r.maxx-r.minx));  
+                }
               }
-            }
+
+            } /* next label in the group */
    
             if(!cachePtr->status)
               continue; /* next label */
@@ -2521,7 +2549,8 @@
                 msDrawMarkerSymbol(&map->symbolset, image, &(cachePtr->point), &(cachePtr->styles[i]), layerPtr->scalefactor);
             }
 
-            for(ll=(cachePtr->numlabels-1); ll>=0; ll--) {
+            // for(ll=(cachePtr->numlabels-1); ll>=0; ll--) {
+            for(ll=0; ll<cachePtr->numlabels; ll++) {
               labelPtr = &(cachePtr->labels[ll]);
 
               /* here's where we draw the label styles */
@@ -2540,6 +2569,9 @@
    
               msDrawText(image, labelPtr->annopoint, labelPtr->annotext, labelPtr, &(map->fontset), layerPtr->scalefactor); /* actually draw the label */
             }
+
+            msFreeShape(poly); /* clean up label polygon */
+            msFree(poly);
           }
 	} /* next label */
       } /* next priority */



More information about the mapserver-commits mailing list