[GRASS-SVN] r31981 - grass/trunk/visualization/nviz2/cmd

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jul 3 06:35:45 EDT 2008


Author: martinl
Date: 2008-07-03 06:35:45 -0400 (Thu, 03 Jul 2008)
New Revision: 31981

Modified:
   grass/trunk/visualization/nviz2/cmd/args.c
   grass/trunk/visualization/nviz2/cmd/local_proto.h
   grass/trunk/visualization/nviz2/cmd/surface.c
Log:
nviz2/cmd: checking parameters (consistency)

Modified: grass/trunk/visualization/nviz2/cmd/args.c
===================================================================
--- grass/trunk/visualization/nviz2/cmd/args.c	2008-07-03 08:53:45 UTC (rev 31980)
+++ grass/trunk/visualization/nviz2/cmd/args.c	2008-07-03 10:35:45 UTC (rev 31981)
@@ -22,6 +22,10 @@
 
 #include "local_proto.h"
 
+static void print_error(int, int, int,
+			const char *, const char *,
+			const char *, const char *);
+
 /*!
    \brief Parse command
 
@@ -316,23 +320,31 @@
   \brief Get number of answers of given option
 
   \param pointer to option
-  
-  \return number
+  \param[out] number of non-zero length items (or NULL)
+  \param[out] number of all items (or NULL)
 */
-int opt_get_num_answers(const struct Option *opt)
+void opt_get_num_answers(const struct Option *opt, int *non_zero, int *all)
 {
-    int i, num;
-    i = num = 0;
+    int i;
+
+    i = 0;
+    if (non_zero)
+	*non_zero = 0;
+    if (all) 
+	*all = 0;
     if (opt->answer) {
 	while (opt->answers[i]) {
+	    if (all)
+		(*all)++;
 	    if (strcmp(opt->answers[i], "")) {
-		num++; /* skip empty values */
+		if (non_zero)
+		    (*non_zero)++; /* skip empty values */
 	    }
 	    i++;
 	}
     }
 
-    return i;
+    return;
 }
 
 /*!
@@ -342,33 +354,117 @@
 */
 void check_parameters(const struct GParams * params)
 {
-    int nelevs;
-    int ncolor_map, ncolor_const, nmasks, ntransps;
-    int nshines, nemits;
+    int nelev_map, nelev_const, nelev_map0, nelev_const0, nelevs0;
+    int nmaps0, nconsts0;
 
-    nelevs = opt_get_num_answers(params->elev_map);
-    nelevs += opt_get_num_answers(params->elev_const);
+    /* topography */
+    opt_get_num_answers(params->elev_map, &nelev_map, &nelev_map0); 
+    opt_get_num_answers(params->elev_const, &nelev_const, &nelev_const0);
 
-    if (nelevs < 1)
+    if (nelev_map + nelev_const < 1)
 	G_fatal_error(_("At least one <%s> or <%s> required"),
 		      params->elev_map->key, params->elev_const->key);
 
-    ncolor_map = opt_get_num_answers(params->color_map);
-    ncolor_const = opt_get_num_answers(params->color_const);
+    if ((nelev_map > 0 && nelev_const > 0) &&
+	(nelev_map0 != nelev_const0))
+	G_fatal_error (_("Inconsistent number of attributes (<%s> %d, <%s> %d)"),
+		       params->elev_map->key, nelev_map0,
+		       params->elev_const->key, nelev_const0);
+    
+    if (nelev_map0 > 0)
+	nelevs0 = nelev_map0;
+    else
+	nelevs0 = nelev_const0;
 
-    if (nelevs != ncolor_map + ncolor_const)
-	G_fatal_error(_("Invalid number of color attributes (<%s> %d, <%s> %d"),
-		      params->color_map->key, ncolor_map,
-		      params->color_const->key, ncolor_const);
+    /* color */
+    opt_get_num_answers(params->color_map, NULL, &nmaps0);
+    opt_get_num_answers(params->color_const, NULL, &nconsts0);
 
-    nmasks = opt_get_num_answers(params->mask_map);
-    ntransps = opt_get_num_answers(params->transp_map);
-    ntransps += opt_get_num_answers(params->transp_const);
-    nshines = opt_get_num_answers(params->shine_map);
-    nshines += opt_get_num_answers(params->shine_const);
-    nemits = opt_get_num_answers(params->emit_map);
-    nemits += opt_get_num_answers(params->emit_const);
+    print_error(nmaps0, nconsts0, nelevs0,
+		params->elev_map->key, params->elev_const->key,
+		params->color_map->key, params->color_const->key);
 
+    /* mask */
+    opt_get_num_answers(params->mask_map, NULL, &nmaps0);
+    if (nmaps0 > 0 && nelevs0 != nmaps0)
+	G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+		      params->elev_map->key, params->elev_const->key, nelevs0,
+		      params->mask_map->key, nmaps0);
 
+
+    /* transparency */
+    opt_get_num_answers(params->transp_map, NULL, &nmaps0);
+    opt_get_num_answers(params->transp_const, NULL, &nconsts0);
+    print_error(nmaps0, nconsts0, nelevs0,
+		params->elev_map->key, params->elev_const->key,
+		params->transp_map->key, params->transp_const->key);
+
+    /* shininess */
+    opt_get_num_answers(params->shine_map, NULL, &nmaps0);
+    opt_get_num_answers(params->shine_const, NULL, &nconsts0);
+    print_error(nmaps0, nconsts0, nelevs0,
+		params->elev_map->key, params->elev_const->key,
+		params->shine_map->key, params->shine_const->key);
+
+    /* emit */
+    opt_get_num_answers(params->emit_map, NULL, &nmaps0);
+    opt_get_num_answers(params->emit_const, NULL, &nconsts0);
+    print_error(nmaps0, nconsts0, nelevs0,
+		params->elev_map->key, params->elev_const->key,
+		params->emit_map->key, params->emit_const->key);
+
+    /* draw mode */
+    if (!params->mode_all->answer) { /* use one mode for all surfaces */
+	opt_get_num_answers(params->mode, NULL, &nconsts0);
+	if (nconsts0 > 0 && nconsts0 != nelevs0)
+	    G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+			  params->elev_map->key, params->elev_const->key, nelevs0,
+			  params->mode->key, nconsts0);
+
+	opt_get_num_answers(params->res_fine, NULL, &nconsts0);
+	if (nconsts0 > 0 && nconsts0 != nelevs0)
+	    G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+			  params->elev_map->key, params->elev_const->key, nelevs0,
+			  params->res_fine->key, nconsts0);
+
+	opt_get_num_answers(params->res_coarse, NULL, &nconsts0);
+	if (nconsts0 > 0 && nconsts0 != nelevs0)
+	    G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+			  params->elev_map->key, params->elev_const->key, nelevs0,
+			  params->res_coarse->key, nconsts0);
+
+	opt_get_num_answers(params->style, NULL, &nconsts0);
+	if (nconsts0 > 0 && nconsts0 != nelevs0)
+	    G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+			  params->elev_map->key, params->elev_const->key, nelevs0,
+			  params->style->key, nconsts0);
+
+	opt_get_num_answers(params->shade, NULL, &nconsts0);
+	if (nconsts0 > 0 && nconsts0 != nelevs0)
+	    G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+			  params->elev_map->key, params->elev_const->key, nelevs0,
+			  params->shade->key, nconsts0);
+
+	opt_get_num_answers(params->wire_color, NULL, &nconsts0);
+	if (nconsts0 > 0 && nconsts0 != nelevs0)
+	    G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d"),
+			  params->elev_map->key, params->elev_const->key, nelevs0,
+			  params->wire_color->key, nconsts0);
+    }
+
     return;
 }
+
+void print_error(int nmaps, int nconsts, int nelevs,
+		 const char *elev_map, const char *elev_const,
+		 const char *map_name, const char *const_name)
+{
+    if ((nmaps > 0 && nelevs != nmaps) ||
+	(nconsts > 0 && nelevs != nconsts))
+	G_fatal_error(_("Inconsistent number of attributes (<%s/%s> %d: <%s> %d, <%s> %d"),
+		      elev_map, elev_const, nelevs,
+		      map_name, nmaps,
+		      const_name, nconsts);
+
+    return;
+}

Modified: grass/trunk/visualization/nviz2/cmd/local_proto.h
===================================================================
--- grass/trunk/visualization/nviz2/cmd/local_proto.h	2008-07-03 08:53:45 UTC (rev 31980)
+++ grass/trunk/visualization/nviz2/cmd/local_proto.h	2008-07-03 10:35:45 UTC (rev 31981)
@@ -25,7 +25,7 @@
 /* args.c */
 void parse_command(int, char**, struct GParams *);
 int color_from_cmd(const char *);
-int opt_get_num_answers(const struct Option *);
+void opt_get_num_answers(const struct Option *, int *, int *);
 void check_parameters(const struct GParams *);
 
 /* surface.c */

Modified: grass/trunk/visualization/nviz2/cmd/surface.c
===================================================================
--- grass/trunk/visualization/nviz2/cmd/surface.c	2008-07-03 08:53:45 UTC (rev 31980)
+++ grass/trunk/visualization/nviz2/cmd/surface.c	2008-07-03 10:35:45 UTC (rev 31981)
@@ -1,7 +1,7 @@
 /*!
   \file surface.c
  
-  \brief Surface procedures
+  \brief Surface subroutines
   
   COPYRIGHT: (C) 2008 by the GRASS Development Team
 
@@ -22,7 +22,7 @@
 #include "local_proto.h"
 
 /*!
-  \brief Load raster maps/constants and set their attributes
+  \brief Load raster maps/constants and set surface attributes
   
   \param params module parameters
   \param data nviz data
@@ -32,16 +32,24 @@
 {
     char *mapset;
     int i;
-    int ncolors, nmask_map;
-    int ntransp_map, ntransp_const, nshine_map, nshine_const;
-    int nemit_map, nemit_const;
+    int nelevs, nelev_map0, nelev_const0, ncolor_map0, ncolor_const0, nmask_map0;
+    int ntransp_map0, ntransp_const0, nshine_map0, nshine_const0;
+    int nemit_map0, nemit_const0;
     int *surf_list, nsurfs;
     int id;
 
-    /* topography */    
-    if (params->elev_map->answer) {
-	/* maps */
-	for (i = 0; params->elev_map->answers[i]; i++) {
+    opt_get_num_answers(params->elev_map, NULL, &nelev_map0);
+    opt_get_num_answers(params->elev_const, NULL, &nelev_const0);
+
+    if (nelev_map0 > 0)
+	nelevs = nelev_map0;
+    else
+	nelevs = nelev_const0;
+
+    /* topography (required) */    
+    for (i = 0; i < nelevs; i++) {
+	/* check maps */
+	if (i < nelev_map0 && strcmp(params->elev_map->answers[i], "")) {
 	    mapset = G_find_cell2 (params->elev_map->answers[i], "");
 	    if (mapset == NULL) {
 		G_fatal_error(_("Raster map <%s> not found"),
@@ -52,43 +60,37 @@
 				  G_fully_qualified_name(params->elev_map->answers[i], mapset), 0.0,
 				  data);
 	}
-    }
-    
-    /* color */
-    ncolors = opt_get_num_answers(params->color_map);
-    if (params->elev_const->answer) {
-	/* constants */
-	float value;
-	surf_list = GS_get_surf_list(&nsurfs);
-	for (i = 0; i < nsurfs; i++) {
-	    if (i < ncolors) { /* check map first */
-		value = atof(params->elev_const->answers[i]);
-	    }
-	    /* check for color */
-	    if (i + nsurfs >= ncolors) {
-		G_fatal_error (_("Missing color settings for elevation value %f"),
-			       value);
-		/* topography */
+	else {
+	    if (i < nelev_const0 && strcmp(params->elev_const->answers[i], "")) {
 		id = Nviz_new_map_obj(MAP_OBJ_SURF,
-				      NULL, value,
+				      NULL, atof(params->elev_const->answers[i]),
 				      data);
 	    }
+	    else {
+		G_fatal_error(_("Missing topography attribute for surface %d"),
+			      i + 1);
+	    }
 	}
     }
-
+    
     /* set surface attributes */
     surf_list = GS_get_surf_list(&nsurfs);
-    nmask_map = opt_get_num_answers(params->mask_map);
-    ntransp_map = opt_get_num_answers(params->transp_map);
-    ntransp_const = opt_get_num_answers(params->transp_const);
-    nshine_map = opt_get_num_answers(params->shine_map);
-    nshine_const = opt_get_num_answers(params->shine_const);
-    nemit_map = opt_get_num_answers(params->emit_map);
-    nemit_const = opt_get_num_answers(params->emit_const);
+
+    opt_get_num_answers(params->color_map, NULL, &ncolor_map0);
+    opt_get_num_answers(params->color_const, NULL, &ncolor_const0);
+    opt_get_num_answers(params->mask_map, NULL, &nmask_map0);
+    opt_get_num_answers(params->transp_map, NULL, &ntransp_map0);
+    opt_get_num_answers(params->transp_const, NULL, &ntransp_const0);
+    opt_get_num_answers(params->shine_map, NULL, &nshine_map0);
+    opt_get_num_answers(params->shine_const, NULL, &nshine_const0);
+    opt_get_num_answers(params->emit_map, NULL, &nemit_map0);
+    opt_get_num_answers(params->emit_const, NULL, &nemit_const0);
+
     for (i = 0; i < nsurfs; i++) {
 	id = surf_list[i];
 	/* color */
-	if (i < ncolors) {
+	/* check for color map */
+	if (i < ncolor_map0 && strcmp(params->color_map->answers[i], "")) {
 	    mapset = G_find_cell2 (params->color_map->answers[i], "");
 	    if (mapset == NULL) {
 		G_fatal_error(_("Raster map <%s> not found"),
@@ -99,7 +101,8 @@
 			  G_fully_qualified_name(params->color_map->answers[i], mapset), -1.0,
 			  data);
 	}
-	else if (i < ncolors) { /* check for color value */
+	/* check for color value */
+	else if (i < ncolor_const0 && strcmp(params->color_const->answers[i], "")) { 
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
 			  NULL, Nviz_color_from_str(params->color_const->answers[i]),
 			  data);
@@ -108,47 +111,49 @@
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
 			  G_fully_qualified_name(params->elev_map->answers[i], mapset), -1.0,
 			  data);
+	    G_verbose_message(_("Color attribute not defined, using default <%s>"),
+			      G_fully_qualified_name(params->elev_map->answers[i], mapset));
 	}
 	/* mask */
-	if (i < nmask_map) {
+	if (i < nmask_map0 && strcmp(params->mask_map->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_MASK, MAP_ATT,
 			  G_fully_qualified_name(params->mask_map->answers[i], mapset), -1.0,
 			  data);
 	}
 
 	/* transparency */
-	if (i < ntransp_map) {
+	if (i < ntransp_map0 && strcmp(params->transp_map->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_TRANSP, MAP_ATT,
 			  G_fully_qualified_name(params->transp_map->answers[i], mapset), -1.0,
 			  data);
 	}
-	else if (i < ntransp_map + ntransp_const) {
+	else if (i < ntransp_const0 && strcmp(params->transp_const->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_TRANSP, CONST_ATT,
-			  NULL, atof(params->transp_const->answers[i-ntransp_map]),
+			  NULL, atof(params->transp_const->answers[i]),
 			  data);
 	}
 
 	/* shininess */
-	if (i < nshine_map) {
+	if (i < nshine_map0 && strcmp(params->shine_map->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_SHINE, MAP_ATT,
 			  G_fully_qualified_name(params->shine_map->answers[i], mapset), -1.0,
 			  data);
 	}
-	else if (i < nshine_map + nshine_const) {
+	else if (i < nshine_const0 && strcmp(params->shine_const->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_SHINE, CONST_ATT,
-			  NULL, atof(params->shine_const->answers[i-nshine_map]),
+			  NULL, atof(params->shine_const->answers[i]),
 			  data);
 	}
 
 	/* emission */
-	if (i < nemit_map) {
+	if (i < nemit_map0 && strcmp(params->emit_map->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_EMIT, MAP_ATT,
 			  G_fully_qualified_name(params->emit_map->answers[i], mapset), -1.0,
 			  data);
 	}
-	else if (i < nemit_map + nemit_const) {
+	else if (i < nemit_const0 && strcmp(params->emit_const->answers[i], "")) {
 	    Nviz_set_attr(id, MAP_OBJ_SURF, ATT_EMIT, CONST_ATT,
-			  NULL, atof(params->emit_const->answers[i-nemit_map]),
+			  NULL, atof(params->emit_const->answers[i]),
 			  data);
 	}
 



More information about the grass-commit mailing list