volume.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. 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->answers[i],
  35. mapset),
  36. 0.0, 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. }
  50. return 1;
  51. }
  52. /*!
  53. \brief Add isosurfaces and set their attributes
  54. \param params module parameters
  55. \param data nviz data
  56. \return number of defined isosurfaces
  57. */
  58. int add_isosurfs(const struct GParams *params, nv_data *data)
  59. {
  60. int i;
  61. float level;
  62. int num, nvols, *vol_list, id, nisosurfs;
  63. int ncolor_map, ncolor_const, ntransp_map, ntransp_const, nshine_map, nshine_const;
  64. int res, draw_mode;
  65. char **tokens;
  66. const char *mapset, *style;
  67. vol_list = GVL_get_vol_list(&nvols);
  68. for (i = 0; params->isosurf_level->answers[i]; i++) {
  69. tokens = G_tokenize(params->isosurf_level->answers[i], ":");
  70. if (G_number_of_tokens(tokens) != 2)
  71. G_fatal_error(_("Error tokenize '%s'"),
  72. params->isosurf_level->answers[i]);
  73. num = atoi(tokens[0]);
  74. level = atof(tokens[1]);
  75. G_free_tokens(tokens);
  76. if (num > nvols) {
  77. G_fatal_error(_("Volume set number %d is not available"),
  78. num);
  79. }
  80. id = vol_list[num-1];
  81. if (GVL_isosurf_add(id) < 0) {
  82. G_fatal_error(_("Unable to add isosurface (volume set %d)"),
  83. id);
  84. }
  85. nisosurfs = GVL_isosurf_num_isosurfs(id);
  86. if (params->isosurf_toggle_norm_dir->answer) {
  87. GVL_isosurf_set_flags(id, nisosurfs-1, 1);
  88. }
  89. /* topography (level) */
  90. if (GVL_isosurf_set_att_const(id, nisosurfs-1, ATT_TOPO, level) < 0) {
  91. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  92. nisosurfs-1, ATT_TOPO, id);
  93. }
  94. /* color */
  95. ncolor_map = opt_get_num_answers(params->isosurf_color_map);
  96. ncolor_const = opt_get_num_answers(params->isosurf_color_const);
  97. if (i < ncolor_map && strcmp(params->isosurf_color_map->answers[i], "")) {
  98. mapset = G_find_raster3d(params->isosurf_color_map->answers[i], "");
  99. if (mapset == NULL) {
  100. G_fatal_error(_("3d raster map <%s> not found"),
  101. params->isosurf_color_map->answers[i]);
  102. }
  103. if (GVL_isosurf_set_att_map(id, nisosurfs-1, ATT_COLOR,
  104. params->isosurf_color_map->answers[i]) < 0)
  105. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  106. nisosurfs-1, ATT_COLOR, id);
  107. }
  108. else if (i-ncolor_map < ncolor_const &&
  109. strcmp(params->isosurf_color_const->answers[i-ncolor_map], "")) {
  110. if (GVL_isosurf_set_att_const(id, nisosurfs-1, ATT_COLOR,
  111. Nviz_color_from_str(params->isosurf_color_const->answers[i-ncolor_map])) < 0)
  112. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  113. nisosurfs-1, ATT_COLOR, id);
  114. }
  115. else { /* use by default 3d raster map for coloring */
  116. GVL_isosurf_set_att_map(id, nisosurfs-1, ATT_COLOR, params->volume->answers[num-1]);
  117. G_verbose_message(_("Color attribute not defined, using default <%s>"),
  118. params->volume->answers[num-1]);
  119. }
  120. /* transparency */
  121. ntransp_map = opt_get_num_answers(params->isosurf_transp_map);
  122. ntransp_const = opt_get_num_answers(params->isosurf_transp_const);
  123. if (i < ntransp_map && strcmp(params->isosurf_transp_map->answers[i], "")) {
  124. if (GVL_isosurf_set_att_map(id, nisosurfs-1, ATT_TRANSP,
  125. params->isosurf_transp_map->answers[i]) < 0)
  126. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  127. nisosurfs-1, ATT_TRANSP, id);
  128. }
  129. else if (i-ntransp_map < ntransp_const &&
  130. strcmp(params->isosurf_transp_const->answers[i-ntransp_map], "")) {
  131. if (GVL_isosurf_set_att_const(id, nisosurfs-1, ATT_TRANSP,
  132. atof(params->isosurf_transp_const->answers[i-ntransp_map])) < 0)
  133. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  134. nisosurfs-1, ATT_TRANSP, id);
  135. }
  136. /* shine */
  137. nshine_map = opt_get_num_answers(params->isosurf_shine_map);
  138. nshine_const = opt_get_num_answers(params->isosurf_shine_const);
  139. if (i < nshine_map && strcmp(params->isosurf_shine_map->answers[i], "")) {
  140. if (GVL_isosurf_set_att_map(id, nisosurfs-1, ATT_SHINE,
  141. params->isosurf_shine_map->answers[i]) < 0)
  142. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  143. nisosurfs-1, ATT_SHINE, id);
  144. }
  145. else if (i-nshine_map < nshine_const &&
  146. strcmp(params->isosurf_shine_const->answers[i-nshine_map], "")) {
  147. if (GVL_isosurf_set_att_const(id, nisosurfs-1, ATT_SHINE,
  148. atof(params->isosurf_shine_const->answers[i-nshine_map])) < 0)
  149. G_fatal_error(_("Unable to set isosurface (%d) attribute (%d) of volume %d"),
  150. nisosurfs-1, ATT_SHINE, id);
  151. }
  152. }
  153. /* set draw resolution and shading after isosurfaces are added*/
  154. for (i = 0; i < nvols; i++) {
  155. id = vol_list[i];
  156. /* set resolution */
  157. if (opt_get_num_answers(params->volume_res) != nvols)
  158. res = atof(params->volume_res->answers[0]);
  159. else
  160. res = atof(params->volume_res->answers[i]);
  161. GVL_isosurf_set_drawres(id, res, res, res);
  162. /* set shading */
  163. if (opt_get_num_answers(params->volume_shade) != nvols)
  164. style = params->volume_shade->answers[0];
  165. else
  166. style = params->volume_shade->answers[i];
  167. draw_mode = 0;
  168. if (strcmp(style, "flat") == 0) {
  169. draw_mode |= DM_FLAT;
  170. }
  171. else {
  172. draw_mode |= DM_GOURAUD;
  173. }
  174. GVL_isosurf_set_drawmode(id, draw_mode);
  175. }
  176. return 1;
  177. }
  178. int add_slices(const struct GParams *params, nv_data *data)
  179. {
  180. int i;
  181. int num, nvols, *vol_list, id, nslices, axis;
  182. int res, draw_mode;
  183. char **tokens;
  184. const char* style;
  185. vol_list = GVL_get_vol_list(&nvols);
  186. for (i = 0; params->slice->answers[i]; i++) {
  187. tokens = G_tokenize(params->slice->answers[i], ":");
  188. if (G_number_of_tokens(tokens) != 2)
  189. G_fatal_error(_("Error tokenize '%s'"),
  190. params->slice->answers[i]);
  191. num = atoi(tokens[0]);
  192. if (!strcmp(tokens[1],"x") || !strcmp(tokens[1],"X"))
  193. axis = 0;
  194. else if (!strcmp(tokens[1],"y") || !strcmp(tokens[1],"Y"))
  195. axis = 1;
  196. else if (!strcmp(tokens[1],"z") || !strcmp(tokens[1],"Z"))
  197. axis = 2;
  198. else
  199. G_fatal_error(_("Wrong name for axis: %s"),
  200. tokens[1]);
  201. G_free_tokens(tokens);
  202. if (num > nvols) {
  203. G_fatal_error(_("Volume set number %d is not available"),
  204. num);
  205. }
  206. id = vol_list[num-1];
  207. if (GVL_slice_add(id) < 0) {
  208. G_fatal_error(_("Unable to add slice (volume set %d)"),
  209. id);
  210. }
  211. nslices = GVL_slice_num_slices(id);
  212. if (GVL_slice_set_pos(id, nslices-1, atof(params->slice_pos->answers[i*6+0]),
  213. atof(params->slice_pos->answers[i*6+1]),
  214. atof(params->slice_pos->answers[i*6+2]),
  215. atof(params->slice_pos->answers[i*6+3]),
  216. atof(params->slice_pos->answers[i*6+4]),
  217. atof(params->slice_pos->answers[i*6+5]),
  218. axis) < 0)
  219. G_fatal_error(_("Unable to set slice (%d) position of volume %d"),
  220. nslices-1, id);
  221. /* set transparency */
  222. if (GVL_slice_set_transp(id, nslices-1, atoi(params->slice_transp->answers[i])) < 0)
  223. G_fatal_error(_("Unable to set slice (%d) transparency of volume %d"),
  224. nslices-1, id);
  225. }
  226. /* set draw resolution and shading after slices are added*/
  227. for (i = 0; i < nvols; i++) {
  228. id = vol_list[i];
  229. /* set resolution */
  230. if (opt_get_num_answers(params->volume_res) != nvols)
  231. res = atof(params->volume_res->answers[0]);
  232. else
  233. res = atof(params->volume_res->answers[i]);
  234. GVL_slice_set_drawres(id, res, res, res);
  235. /* set shading */
  236. if (opt_get_num_answers(params->volume_shade) != nvols)
  237. style = params->volume_shade->answers[0];
  238. else
  239. style = params->volume_shade->answers[i];
  240. draw_mode = 0;
  241. if (strcmp(style, "flat") == 0) {
  242. draw_mode |= DM_FLAT;
  243. }
  244. else {
  245. draw_mode |= DM_GOURAUD;
  246. }
  247. GVL_slice_set_drawmode(id, draw_mode);
  248. }
  249. return 1;
  250. }