load.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /**
  2. \file nviz/load.cpp
  3. \brief wxNviz extension (3D view mode) - load data layers
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. (C) 2008-2009 by Martin Landa, and the GRASS development team
  8. \author Martin Landa <landa.martin gmail.com> (Google SoC 2008)
  9. */
  10. #include "nviz.h"
  11. extern "C" {
  12. #include <grass/G3d.h>
  13. #include <grass/glocale.h>
  14. }
  15. /*!
  16. \brief Load raster map (surface)
  17. \param name raster map name
  18. \param color_name raster map for color (NULL for color_value)
  19. \param color_value color string (named color or RGB triptet)
  20. \return object id
  21. \return -1 on failure
  22. */
  23. int Nviz::LoadSurface(const char* name, const char *color_name, const char *color_value)
  24. {
  25. const char *mapset;
  26. int id;
  27. mapset = G_find_raster2 (name, "");
  28. if (mapset == NULL) {
  29. G_warning(_("Raster map <%s> not found"),
  30. name);
  31. return -1;
  32. }
  33. /* topography */
  34. id = Nviz_new_map_obj(MAP_OBJ_SURF,
  35. G_fully_qualified_name(name, mapset), 0.0,
  36. data);
  37. if (color_name) { /* check for color map */
  38. mapset = G_find_raster2 (color_name, "");
  39. if (mapset == NULL) {
  40. G_warning(_("Raster map <%s> not found"),
  41. color_name);
  42. GS_delete_surface(id);
  43. return -1;
  44. }
  45. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  46. G_fully_qualified_name(color_name, mapset), -1.0,
  47. data);
  48. }
  49. else if (color_value) { /* check for color value */
  50. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, CONST_ATT,
  51. NULL, Nviz_color_from_str(color_value),
  52. data);
  53. }
  54. else { /* use by default elevation map for coloring */
  55. Nviz_set_attr(id, MAP_OBJ_SURF, ATT_COLOR, MAP_ATT,
  56. G_fully_qualified_name(name, mapset), -1.0,
  57. data);
  58. }
  59. /*
  60. if (i > 1)
  61. set_default_wirecolors(data, i);
  62. */
  63. /* focus on loaded data */
  64. Nviz_set_focus_map(MAP_OBJ_UNDEFINED, -1);
  65. G_debug(1, "Nviz::LoadRaster(): name=%s -> id=%d", name, id);
  66. return id;
  67. }
  68. /*!
  69. \brief Unload surface
  70. \param id surface id
  71. \return 1 on success
  72. \return 0 on failure
  73. */
  74. int Nviz::UnloadSurface(int id)
  75. {
  76. if (!GS_surf_exists(id)) {
  77. return 0;
  78. }
  79. G_debug(1, "Nviz::UnloadSurface(): id=%d", id);
  80. if (GS_delete_surface(id) < 0)
  81. return 0;
  82. return 1;
  83. }
  84. /*!
  85. \brief Load vector map overlay
  86. \param name vector map name
  87. \param points if true load 2d points rather then 2d lines
  88. \return object id
  89. \return -1 on failure
  90. */
  91. int Nviz::LoadVector(const char *name, bool points)
  92. {
  93. int id;
  94. const char *mapset;
  95. if (GS_num_surfs() == 0) { /* load base surface if no loaded */
  96. int *surf_list, nsurf;
  97. Nviz_new_map_obj(MAP_OBJ_SURF, NULL, 0.0, data);
  98. surf_list = GS_get_surf_list(&nsurf);
  99. GS_set_att_const(surf_list[0], ATT_TRANSP, 255);
  100. }
  101. mapset = G_find_vector2 (name, "");
  102. if (mapset == NULL) {
  103. G_warning(_("Vector map <%s> not found"),
  104. name);
  105. }
  106. if (points) {
  107. id = Nviz_new_map_obj(MAP_OBJ_SITE,
  108. G_fully_qualified_name(name, mapset), 0.0,
  109. data);
  110. }
  111. else {
  112. id = Nviz_new_map_obj(MAP_OBJ_VECT,
  113. G_fully_qualified_name(name, mapset), 0.0,
  114. data);
  115. }
  116. G_debug(1, "Nviz::LoadVector(): name=%s -> id=%d", name, id);
  117. return id;
  118. }
  119. /*!
  120. \brief Unload vector set
  121. \param id vector set id
  122. \param points vector points or lines set
  123. \return 1 on success
  124. \return 0 on failure
  125. */
  126. int Nviz::UnloadVector(int id, bool points)
  127. {
  128. G_debug(1, "Nviz::UnloadVector(): id=%d", id);
  129. if (points) {
  130. if (!GP_site_exists(id)) {
  131. return 0;
  132. }
  133. if (GP_delete_site(id) < 0)
  134. return 0;
  135. }
  136. else {
  137. if (!GV_vect_exists(id)) {
  138. return 0;
  139. }
  140. if (GV_delete_vector(id) < 0)
  141. return 0;
  142. }
  143. return 1;
  144. }
  145. /*!
  146. \brief Load 3d raster map (volume)
  147. \param name 3d raster map name
  148. \param color_name 3d raster map for color (NULL for color_value)
  149. \param color_value color string (named color or RGB triptet)
  150. \return object id
  151. \return -1 on failure
  152. */
  153. int Nviz::LoadVolume(const char* name, const char *color_name, const char *color_value)
  154. {
  155. char *mapset;
  156. int id;
  157. mapset = G_find_grid3(name, "");
  158. if (mapset == NULL) {
  159. G_warning(_("3d raster map <%s> not found"),
  160. name);
  161. return -1;
  162. }
  163. /* topography */
  164. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  165. G_fully_qualified_name(name, mapset), 0.0,
  166. data);
  167. if (color_name) { /* check for color map */
  168. mapset = G_find_grid3(color_name, "");
  169. if (mapset == NULL) {
  170. G_warning(_("3d raster map <%s> not found"),
  171. color_name);
  172. GVL_delete_vol(id);
  173. return -1;
  174. }
  175. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  176. G_fully_qualified_name(color_name, mapset), -1.0,
  177. data);
  178. }
  179. else if (color_value) { /* check for color value */
  180. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, CONST_ATT,
  181. NULL, Nviz_color_from_str(color_value),
  182. data);
  183. }
  184. else { /* use by default elevation map for coloring */
  185. Nviz_set_attr(id, MAP_OBJ_VOL, ATT_COLOR, MAP_ATT,
  186. G_fully_qualified_name(name, mapset), -1.0,
  187. data);
  188. }
  189. G_debug(1, "Nviz::LoadVolume(): name=%s -> id=%d", name, id);
  190. return id;
  191. }
  192. /*!
  193. \brief Unload volume
  194. \param id volume id
  195. \return 1 on success
  196. \return 0 on failure
  197. */
  198. int Nviz::UnloadVolume(int id)
  199. {
  200. if (!GVL_vol_exists(id)) {
  201. return 0;
  202. }
  203. G_debug(1, "Nviz::UnloadVolume(): id=%d", id);
  204. if (GVL_delete_vol(id) < 0)
  205. return 0;
  206. return 1;
  207. }