map_obj.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*!
  2. \file lib/nviz/map_obj.c
  3. \brief Nviz library -- Define creation and interface functions for map objects.
  4. Map objects are considered to be surfaces, vector plots, or site
  5. files.
  6. Based on visualization/nviz/src/map_obj.c
  7. (C) 2008, 2010 by the GRASS Development Team
  8. This program is free software under the GNU General Public License
  9. (>=v2). Read the file COPYING that comes with GRASS for details.
  10. \author Updated/modified by Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  11. */
  12. #include <stdlib.h>
  13. #include <time.h>
  14. #include <grass/glocale.h>
  15. #include <grass/nviz.h>
  16. /*!
  17. \brief Create a new map object which can be one of surf, vect, vol or site.
  18. This routine creates the object internally in the gsf libraryb.
  19. Optionally, a logical name may be specified for the new map object.
  20. If no name is specified, a logical name is assigned to the new
  21. object automatically. Note that maintaining unique logical names is
  22. not the responsibility of the library (currently).
  23. Initially map objects contain no data, use the attribute commands to
  24. set attributes such as topology, color, etc.
  25. \param type map object type
  26. \param name map name (NULL for constant)
  27. \param value constant (used if <i>name</i> is NULL)
  28. \param data nviz data
  29. \return map object id
  30. \return -1 on error
  31. */
  32. int Nviz_new_map_obj(int type, const char *name, double value, nv_data * data)
  33. {
  34. int new_id, i;
  35. int num_surfs, *surf_list;
  36. /*
  37. * For each type of map obj do the following --
  38. * 1) Verify we haven't maxed out the number of
  39. * allowed objects.
  40. * 2) Call the internal library to generate a new
  41. * map object of the specified type.
  42. */
  43. /* raster -> surface */
  44. if (type == MAP_OBJ_SURF) {
  45. if (GS_num_surfs() >= MAX_SURFS) {
  46. G_warning(_("Maximum surfaces loaded!"));
  47. return -1;
  48. }
  49. new_id = GS_new_surface();
  50. if (new_id < 0) {
  51. return -1;
  52. }
  53. if (name) {
  54. /* map */
  55. if (!Nviz_set_attr(new_id, MAP_OBJ_SURF, ATT_TOPO,
  56. MAP_ATT, name, -1.0, data)) {
  57. return -1;
  58. }
  59. }
  60. else {
  61. /* constant */
  62. if (!Nviz_set_attr(new_id, MAP_OBJ_SURF, ATT_TOPO,
  63. CONST_ATT, NULL, value,
  64. data)) {
  65. return -1;
  66. }
  67. }
  68. }
  69. /* vector overlay */
  70. else if (type == MAP_OBJ_VECT) {
  71. if (GV_num_vects() >= MAX_VECTS) {
  72. G_warning(_("Maximum vector line maps loaded!"));
  73. return -1;
  74. }
  75. new_id = GV_new_vector();
  76. if (name) {
  77. if (GV_load_vector(new_id, name) < 0) {
  78. GV_delete_vector(new_id);
  79. G_warning(_("Error loading vector map <%s>"), name);
  80. return -1;
  81. }
  82. }
  83. /* initialize display parameters
  84. automatically select all surfaces to draw vector */
  85. GV_set_style(new_id, 1, 0x000000, 2, 0);
  86. surf_list = GS_get_surf_list(&num_surfs);
  87. if (num_surfs) {
  88. for (i = 0; i < num_surfs; i++) {
  89. GV_select_surf(new_id, surf_list[i]);
  90. }
  91. }
  92. G_free(surf_list);
  93. }
  94. /* vector points overlay */
  95. else if (type == MAP_OBJ_SITE) {
  96. if (GP_num_sites() >= MAX_SITES) {
  97. G_warning(_("Maximum vector point maps loaded!"));
  98. return -1;
  99. }
  100. new_id = GP_new_site();
  101. /* initizalize site attributes */
  102. Nviz_set_vpoint_attr_default(new_id);
  103. /* load vector points */
  104. if (0 > GP_load_site(new_id, name)) {
  105. GP_delete_site(new_id);
  106. G_warning(_("Error loading vector map <%s>"), name);
  107. return -1;
  108. }
  109. /* initialize display parameters */
  110. GP_set_style(new_id, 0x000000, 2, 100, ST_X);
  111. surf_list = GS_get_surf_list(&num_surfs);
  112. for (i = 0; i < num_surfs; i++) {
  113. GP_select_surf(new_id, surf_list[i]);
  114. }
  115. G_free(surf_list);
  116. }
  117. /* 3d raster map -> volume */
  118. else if (type == MAP_OBJ_VOL) {
  119. if (GVL_num_vols() >= MAX_VOLS) {
  120. G_warning(_("Maximum volumes loaded!"));
  121. return -1;
  122. }
  123. new_id = GVL_new_vol();
  124. /* load volume */
  125. if (0 > GVL_load_vol(new_id, name)) {
  126. GVL_delete_vol(new_id);
  127. G_warning(_("Error loading 3d raster map <%s>"), name);
  128. return -1;
  129. }
  130. /* initilaze volume attributes */
  131. Nviz_set_volume_attr_default(new_id);
  132. }
  133. else {
  134. G_warning(_("Nviz_new_map_obj(): unsupported data type"));
  135. return -1;
  136. }
  137. return new_id;
  138. }
  139. /*!
  140. Set map object attribute
  141. \param id map object id
  142. \param type map object type (MAP_OBJ_SURF, MAP_OBJ_VECT, ...)
  143. \param desc attribute descriptor
  144. \param src attribute source
  145. \param str_value attribute value as string (if NULL, check for <i>num_value</i>)
  146. \param num_value attribute value as double
  147. \return 1 on success
  148. \return 0 on failure
  149. */
  150. int Nviz_set_attr(int id, int type, int desc, int src,
  151. const char *str_value, double num_value, nv_data * data)
  152. {
  153. int ret;
  154. double value;
  155. switch (type) {
  156. case (MAP_OBJ_SURF):{
  157. /* Basically two cases, either we are setting to a constant field, or
  158. * we are loading an actual file. Setting a constant is the easy part
  159. * so we try and do that first.
  160. */
  161. if (src == CONST_ATT) {
  162. /* Get the value for the constant
  163. * Note that we require the constant to be an integer
  164. */
  165. if (str_value)
  166. value = (double)atof(str_value);
  167. else
  168. value = num_value;
  169. /* Only special case is setting constant color.
  170. * In this case we have to decode the constant Tcl
  171. * returns so that the gsf library understands it.
  172. */
  173. if (desc == ATT_COLOR) {
  174. /* TODO check this - sometimes gets reversed when save state
  175. saves a surface with constant color
  176. int r, g, b;
  177. r = (((int) value) & RED_MASK) >> 16;
  178. g = (((int) value) & GRN_MASK) >> 8;
  179. b = (((int) value) & BLU_MASK);
  180. value = r + (g << 8) + (b << 16);
  181. */
  182. }
  183. /* Once the value is parsed, set it */
  184. ret = GS_set_att_const(id, desc, value);
  185. }
  186. else if (src == MAP_ATT) {
  187. ret = GS_load_att_map(id, str_value, desc);
  188. }
  189. else
  190. ret = -1;
  191. /* After we've loaded a constant map or a file,
  192. * may need to adjust resolution if we are resetting
  193. * topology (for example)
  194. */
  195. if (0 <= ret) {
  196. if (desc == ATT_TOPO) {
  197. int rows, cols, max;
  198. int max2;
  199. /* If topology attribute is being set then need to set
  200. * resolution of incoming map to some sensible value so we
  201. * don't wait all day for drawing.
  202. */
  203. GS_get_dims(id, &rows, &cols);
  204. max = (rows > cols) ? rows : cols;
  205. max = max / 50;
  206. if (max < 1)
  207. max = 1;
  208. max2 = max / 5;
  209. if (max2 < 1)
  210. max2 = 1;
  211. /* reset max to finer for coarse surf drawing */
  212. max = max2 + max2 / 2;
  213. if (max < 1)
  214. max = 1;
  215. GS_set_drawres(id, max2, max2, max, max);
  216. GS_set_drawmode(id, DM_GOURAUD | DM_POLY | DM_GRID_SURF);
  217. }
  218. /* Not sure about this next line, should probably just
  219. * create separate routines to figure the Z range as well
  220. * as the XYrange
  221. */
  222. Nviz_update_ranges(data);
  223. break;
  224. }
  225. default:{
  226. return 0;
  227. }
  228. }
  229. }
  230. return 1;
  231. }
  232. /*!
  233. \brief Set default surface attributes
  234. */
  235. void Nviz_set_surface_attr_default()
  236. {
  237. float defs[MAX_ATTS];
  238. defs[ATT_TOPO] = 0;
  239. defs[ATT_COLOR] = DEFAULT_SURF_COLOR;
  240. defs[ATT_MASK] = 0;
  241. defs[ATT_TRANSP] = 0;
  242. defs[ATT_SHINE] = 60;
  243. defs[ATT_EMIT] = 0;
  244. GS_set_att_defaults(defs, defs);
  245. return;
  246. }
  247. /*!
  248. \brief Set default vector point attributes
  249. \param id vector point set id
  250. \return 1 on success
  251. \return 0 on failure
  252. */
  253. int Nviz_set_vpoint_attr_default(int id)
  254. {
  255. geosite *gp;
  256. gp = gp_get_site(id);
  257. if (!gp)
  258. return 0;
  259. return 1;
  260. }
  261. /*!
  262. \brief Set default volume attributes
  263. \param id volume set id
  264. \return 1 on success
  265. \return 0 on failure
  266. */
  267. int Nviz_set_volume_attr_default(int id)
  268. {
  269. int rows, cols, depths;
  270. int max;
  271. GVL_get_dims(id, &rows, &cols, &depths);
  272. max = (rows > cols) ? rows : cols;
  273. max = (depths > max) ? depths : max;
  274. max = max / 35;
  275. if (max < 1)
  276. max = 1;
  277. if (max > cols)
  278. max = cols / 2;
  279. if (max > rows)
  280. max = rows / 2;
  281. if (max > depths)
  282. max = depths / 2;
  283. /* set default drawres and drawmode for isosurfaces */
  284. GVL_isosurf_set_drawres(id, max, max, max);
  285. GVL_isosurf_set_drawmode(id, DM_GOURAUD);
  286. /* set default drawres and drawmode for slices */
  287. GVL_slice_set_drawres(id, 1, 1, 1);
  288. GVL_slice_set_drawmode(id, DM_GOURAUD | DM_POLY);
  289. return 1;
  290. }
  291. /*!
  292. Unset map object attribute
  293. \param id map object id
  294. \param type map object type (MAP_OBJ_SURF, MAP_OBJ_VECT, ...)
  295. \param desc attribute descriptor
  296. \return 1 on success
  297. \return 0 on failure
  298. */
  299. int Nviz_unset_attr(int id, int type, int desc)
  300. {
  301. if (type == MAP_OBJ_SURF) {
  302. return GS_unset_att(id, desc);
  303. }
  304. return 0;
  305. }