volume.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*!
  2. \file volume.c
  3. \brief Volume subroutines
  4. (C) 2008, 2010 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author Martin Landa <landa.martin gmail.com> (Google SoC 2008/2010)
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/raster3d.h>
  13. #include <grass/glocale.h>
  14. #include "local_proto.h"
  15. /*!
  16. \brief Load 3d raster map layers -> volume
  17. \param params module parameters
  18. \param data nviz data
  19. \return number of loaded volumes
  20. */
  21. int load_rasters3d(const struct GParams *params, nv_data * data)
  22. {
  23. int i, nvol, id;
  24. float x, y, z;
  25. const char *mapset;
  26. nvol = opt_get_num_answers(params->volume);
  27. for (i = 0; i < nvol; i++) {
  28. mapset = G_find_raster3d(params->volume->answers[i], "");
  29. if (mapset == NULL) {
  30. G_fatal_error(_("3d raster map <%s> not found"),
  31. params->volume->answers[i]);
  32. }
  33. id = Nviz_new_map_obj(MAP_OBJ_VOL,
  34. G_fully_qualified_name(params->volume->
  35. answers[i], mapset), 0.0,
  36. data);
  37. /* set position */
  38. if (opt_get_num_answers(params->volume_pos) != 3 * nvol) {
  39. x = atof(params->volume_pos->answers[0]);
  40. y = atof(params->volume_pos->answers[1]);
  41. z = atof(params->volume_pos->answers[2]);
  42. }
  43. else {
  44. x = atof(params->volume_pos->answers[i * 3 + 0]);
  45. y = atof(params->volume_pos->answers[i * 3 + 1]);
  46. z = atof(params->volume_pos->answers[i * 3 + 2]);
  47. }
  48. GVL_set_trans(id, x, y, z);
  49. if (params->draw_volume_box->answer) {
  50. GVL_set_draw_wire(id, 1);
  51. }
  52. }
  53. return 1;
  54. }
  55. /*!
  56. \brief Add isosurfaces and set their attributes
  57. \param params module parameters
  58. \param data nviz data
  59. \return number of defined isosurfaces
  60. */
  61. int add_isosurfs(const struct GParams *params, nv_data * data)
  62. {
  63. int i;
  64. float level;
  65. int num, nvols, *vol_list, id, nisosurfs;
  66. int ncolor_map, ncolor_const, ntransp_map, ntransp_const, nshine_map,
  67. nshine_const;
  68. int res, draw_mode;
  69. char **tokens;
  70. const char *mapset, *style;
  71. vol_list = GVL_get_vol_list(&nvols);
  72. for (i = 0; params->isosurf_level->answers[i]; i++) {
  73. tokens = G_tokenize(params->isosurf_level->answers[i], ":");
  74. if (G_number_of_tokens(tokens) != 2)
  75. G_fatal_error(_("Error tokenize '%s'"),
  76. params->isosurf_level->answers[i]);
  77. num = atoi(tokens[0]);
  78. level = atof(tokens[1]);
  79. G_free_tokens(tokens);
  80. if (num > nvols) {
  81. G_fatal_error(_("Volume set number %d is not available"), num);
  82. }
  83. id = vol_list[num - 1];
  84. if (GVL_isosurf_add(id) < 0) {
  85. G_fatal_error(_("Unable to add isosurface (volume set %d)"), id);
  86. }
  87. nisosurfs = GVL_isosurf_num_isosurfs(id);
  88. if (params->isosurf_toggle_norm_dir->answer) {
  89. GVL_isosurf_set_flags(id, nisosurfs - 1, 1);
  90. }
  91. /* topography (level) */
  92. if (GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_TOPO, level) < 0) {
  93. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  94. nisosurfs - 1, ATT_TOPO, id);
  95. }
  96. /* color */
  97. ncolor_map = opt_get_num_answers(params->isosurf_color_map);
  98. ncolor_const = opt_get_num_answers(params->isosurf_color_const);
  99. if (i < ncolor_map &&
  100. strcmp(params->isosurf_color_map->answers[i], "")) {
  101. mapset =
  102. G_find_raster3d(params->isosurf_color_map->answers[i], "");
  103. if (mapset == NULL) {
  104. G_fatal_error(_("3d raster map <%s> not found"),
  105. params->isosurf_color_map->answers[i]);
  106. }
  107. if (GVL_isosurf_set_att_map(id, nisosurfs - 1, ATT_COLOR,
  108. params->isosurf_color_map->
  109. answers[i]) < 0)
  110. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  111. nisosurfs - 1, ATT_COLOR, id);
  112. }
  113. else if (i - ncolor_map < ncolor_const &&
  114. strcmp(params->isosurf_color_const->answers[i - ncolor_map],
  115. "")) {
  116. if (GVL_isosurf_set_att_const(id, nisosurfs - 1, ATT_COLOR,
  117. Nviz_color_from_str(params->
  118. isosurf_color_const->
  119. answers[i -
  120. ncolor_map]))
  121. < 0)
  122. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  123. nisosurfs - 1, ATT_COLOR, id);
  124. }
  125. else { /* use by default 3d raster map for coloring */
  126. GVL_isosurf_set_att_map(id, nisosurfs - 1, ATT_COLOR,
  127. params->volume->answers[num - 1]);
  128. G_verbose_message(_("Color attribute not defined, using default <%s>"),
  129. params->volume->answers[num - 1]);
  130. }
  131. /* transparency */
  132. ntransp_map = opt_get_num_answers(params->isosurf_transp_map);
  133. ntransp_const = opt_get_num_answers(params->isosurf_transp_const);
  134. if (i < ntransp_map &&
  135. strcmp(params->isosurf_transp_map->answers[i], "")) {
  136. if (GVL_isosurf_set_att_map
  137. (id, nisosurfs - 1, ATT_TRANSP,
  138. params->isosurf_transp_map->answers[i]) < 0)
  139. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  140. nisosurfs - 1, ATT_TRANSP, id);
  141. }
  142. else if (i - ntransp_map < ntransp_const &&
  143. strcmp(params->isosurf_transp_const->
  144. answers[i - ntransp_map], "")) {
  145. if (GVL_isosurf_set_att_const
  146. (id, nisosurfs - 1, ATT_TRANSP,
  147. atof(params->isosurf_transp_const->
  148. answers[i - ntransp_map])) < 0)
  149. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  150. nisosurfs - 1, ATT_TRANSP, id);
  151. }
  152. /* shine */
  153. nshine_map = opt_get_num_answers(params->isosurf_shine_map);
  154. nshine_const = opt_get_num_answers(params->isosurf_shine_const);
  155. if (i < nshine_map &&
  156. strcmp(params->isosurf_shine_map->answers[i], "")) {
  157. if (GVL_isosurf_set_att_map
  158. (id, nisosurfs - 1, ATT_SHINE,
  159. params->isosurf_shine_map->answers[i]) < 0)
  160. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  161. nisosurfs - 1, ATT_SHINE, id);
  162. }
  163. else if (i - nshine_map < nshine_const &&
  164. strcmp(params->isosurf_shine_const->answers[i - nshine_map],
  165. "")) {
  166. if (GVL_isosurf_set_att_const
  167. (id, nisosurfs - 1, ATT_SHINE,
  168. atof(params->isosurf_shine_const->answers[i - nshine_map])) <
  169. 0)
  170. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  171. nisosurfs - 1, ATT_SHINE, id);
  172. }
  173. }
  174. /* set draw resolution and shading after isosurfaces are added */
  175. for (i = 0; i < nvols; i++) {
  176. id = vol_list[i];
  177. /* set resolution */
  178. if (opt_get_num_answers(params->volume_res) != nvols)
  179. res = atof(params->volume_res->answers[0]);
  180. else
  181. res = atof(params->volume_res->answers[i]);
  182. GVL_isosurf_set_drawres(id, res, res, res);
  183. /* set shading */
  184. if (opt_get_num_answers(params->volume_shade) != nvols)
  185. style = params->volume_shade->answers[0];
  186. else
  187. style = params->volume_shade->answers[i];
  188. draw_mode = 0;
  189. if (strcmp(style, "flat") == 0) {
  190. draw_mode |= DM_FLAT;
  191. }
  192. else {
  193. draw_mode |= DM_GOURAUD;
  194. }
  195. GVL_isosurf_set_drawmode(id, draw_mode);
  196. }
  197. return 1;
  198. }
  199. int add_slices(const struct GParams *params, nv_data * data)
  200. {
  201. int i;
  202. int num, nvols, *vol_list, id, nslices, axis;
  203. int res, draw_mode;
  204. char **tokens;
  205. const char *style;
  206. vol_list = GVL_get_vol_list(&nvols);
  207. for (i = 0; params->slice->answers[i]; i++) {
  208. tokens = G_tokenize(params->slice->answers[i], ":");
  209. if (G_number_of_tokens(tokens) != 2)
  210. G_fatal_error(_("Error tokenize '%s'"),
  211. params->slice->answers[i]);
  212. num = atoi(tokens[0]);
  213. if (!strcmp(tokens[1], "x") || !strcmp(tokens[1], "X"))
  214. axis = 0;
  215. else if (!strcmp(tokens[1], "y") || !strcmp(tokens[1], "Y"))
  216. axis = 1;
  217. else if (!strcmp(tokens[1], "z") || !strcmp(tokens[1], "Z"))
  218. axis = 2;
  219. else
  220. G_fatal_error(_("Wrong name for axis: %s"), tokens[1]);
  221. G_free_tokens(tokens);
  222. if (num > nvols) {
  223. G_fatal_error(_("Volume set number %d is not available"), num);
  224. }
  225. id = vol_list[num - 1];
  226. if (GVL_slice_add(id) < 0) {
  227. G_fatal_error(_("Unable to add slice (volume set %d)"), id);
  228. }
  229. nslices = GVL_slice_num_slices(id);
  230. if (GVL_slice_set_pos
  231. (id, nslices - 1, atof(params->slice_pos->answers[i * 6 + 0]),
  232. atof(params->slice_pos->answers[i * 6 + 1]),
  233. atof(params->slice_pos->answers[i * 6 + 2]),
  234. atof(params->slice_pos->answers[i * 6 + 3]),
  235. atof(params->slice_pos->answers[i * 6 + 4]),
  236. atof(params->slice_pos->answers[i * 6 + 5]), axis) < 0)
  237. G_fatal_error(_("Unable to set slice (%d) position of volume %d"),
  238. nslices - 1, id);
  239. /* set transparency */
  240. if (GVL_slice_set_transp
  241. (id, nslices - 1, atoi(params->slice_transp->answers[i])) < 0)
  242. G_fatal_error(_("Unable to set slice (%d) transparency of volume %d"),
  243. nslices - 1, id);
  244. }
  245. /* set draw resolution and shading after slices are added */
  246. for (i = 0; i < nvols; i++) {
  247. id = vol_list[i];
  248. /* set resolution */
  249. if (opt_get_num_answers(params->volume_res) != nvols)
  250. res = atof(params->volume_res->answers[0]);
  251. else
  252. res = atof(params->volume_res->answers[i]);
  253. GVL_slice_set_drawres(id, res, res, res);
  254. /* set shading */
  255. if (opt_get_num_answers(params->volume_shade) != nvols)
  256. style = params->volume_shade->answers[0];
  257. else
  258. style = params->volume_shade->answers[i];
  259. draw_mode = 0;
  260. if (strcmp(style, "flat") == 0) {
  261. draw_mode |= DM_FLAT;
  262. }
  263. else {
  264. draw_mode |= DM_GOURAUD;
  265. }
  266. GVL_slice_set_drawmode(id, draw_mode);
  267. }
  268. return 1;
  269. }