surface.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. \file nviz/surface.cpp
  3. \brief wxNviz extension (3D view mode) - surface attributes
  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. /*!
  12. \brief Set surface topography
  13. \param id surface id
  14. \param map if true use map otherwise constant
  15. \param value map name of value
  16. \return 1 on success
  17. \return -1 surface not found
  18. \return -2 setting attributes failed
  19. */
  20. int Nviz::SetSurfaceTopo(int id, bool map, const char *value)
  21. {
  22. return SetSurfaceAttr(id, ATT_TOPO, map, value);
  23. }
  24. /*!
  25. \brief Set surface color
  26. \param id surface id
  27. \param map if true use map otherwise constant
  28. \param value map name of value
  29. \return 1 on success
  30. \return -1 surface not found
  31. \return -2 setting attributes failed
  32. */
  33. int Nviz::SetSurfaceColor(int id, bool map, const char *value)
  34. {
  35. return SetSurfaceAttr(id, ATT_COLOR, map, value);
  36. }
  37. /*!
  38. \brief Set surface mask
  39. @todo invert
  40. \param id surface id
  41. \param invert if true invert mask
  42. \param value map name of value
  43. \return 1 on success
  44. \return -1 surface not found
  45. \return -2 setting attributes failed
  46. */
  47. int Nviz::SetSurfaceMask(int id, bool invert, const char *value)
  48. {
  49. return SetSurfaceAttr(id, ATT_MASK, true, value);
  50. }
  51. /*!
  52. \brief Set surface mask
  53. @todo invert
  54. \param id surface id
  55. \param map if true use map otherwise constant
  56. \param value map name of value
  57. \return 1 on success
  58. \return -1 surface not found
  59. \return -2 setting attributes failed
  60. */
  61. int Nviz::SetSurfaceTransp(int id, bool map, const char *value)
  62. {
  63. return SetSurfaceAttr(id, ATT_TRANSP, map, value);
  64. }
  65. /*!
  66. \brief Set surface shininess
  67. \param id surface id
  68. \param map if true use map otherwise constant
  69. \param value map name of value
  70. \return 1 on success
  71. \return -1 surface not found
  72. \return -2 setting attributes failed
  73. */
  74. int Nviz::SetSurfaceShine(int id, bool map, const char *value)
  75. {
  76. return SetSurfaceAttr(id, ATT_SHINE, map, value);
  77. }
  78. /*!
  79. \brief Set surface emission
  80. \param id surface id
  81. \param map if true use map otherwise constant
  82. \param value map name of value
  83. \return 1 on success
  84. \return -1 surface not found
  85. \return -2 setting attributes failed
  86. */
  87. int Nviz::SetSurfaceEmit(int id, bool map, const char *value)
  88. {
  89. return SetSurfaceAttr(id, ATT_EMIT, map, value);
  90. }
  91. /*!
  92. \brief Set surface attribute
  93. \param id surface id
  94. \param attr attribute desc
  95. \param map if true use map otherwise constant
  96. \param value map name of value
  97. \return 1 on success
  98. \return -1 surface not found
  99. \return -2 setting attributes failed
  100. */
  101. int Nviz::SetSurfaceAttr(int id, int attr, bool map, const char *value)
  102. {
  103. int ret;
  104. if (!GS_surf_exists(id)) {
  105. return -1;
  106. }
  107. if (map) {
  108. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, MAP_ATT,
  109. value, -1.0,
  110. data);
  111. }
  112. else {
  113. float val;
  114. if (attr == ATT_COLOR) {
  115. val = Nviz_color_from_str(value);
  116. }
  117. else {
  118. val = atof(value);
  119. }
  120. ret = Nviz_set_attr(id, MAP_OBJ_SURF, attr, CONST_ATT,
  121. NULL, val,
  122. data);
  123. }
  124. G_debug(1, "Nviz::SetSurfaceAttr(): id=%d, attr=%d, map=%d, value=%s",
  125. id, attr, map, value);
  126. return ret ? 1 : -2;
  127. }
  128. /*!
  129. \brief Unset surface mask
  130. \param id surface id
  131. \return 1 on success
  132. \return -1 surface not found
  133. \return -2 setting attributes failed
  134. \return -1 on failure
  135. */
  136. int Nviz::UnsetSurfaceMask(int id)
  137. {
  138. return UnsetSurfaceAttr(id, ATT_MASK);
  139. }
  140. /*!
  141. \brief Unset surface transparency
  142. \param id surface id
  143. \return 1 on success
  144. \return -1 surface not found
  145. \return -2 setting attributes failed
  146. */
  147. int Nviz::UnsetSurfaceTransp(int id)
  148. {
  149. return UnsetSurfaceAttr(id, ATT_TRANSP);
  150. }
  151. /*!
  152. \brief Unset surface emission
  153. \param id surface id
  154. \return 1 on success
  155. \return -1 surface not found
  156. \return -2 setting attributes failed
  157. */
  158. int Nviz::UnsetSurfaceEmit(int id)
  159. {
  160. return UnsetSurfaceAttr(id, ATT_EMIT);
  161. }
  162. /*!
  163. \brief Unset surface attribute
  164. \param id surface id
  165. \param attr attribute descriptor
  166. \return 1 on success
  167. \return -1 surface not found
  168. \return -2 setting attributes failed
  169. */
  170. int Nviz::UnsetSurfaceAttr(int id, int attr)
  171. {
  172. int ret;
  173. if (!GS_surf_exists(id)) {
  174. return -1;
  175. }
  176. G_debug(1, "Nviz::UnsetSurfaceAttr(): id=%d, attr=%d",
  177. id, attr);
  178. ret = Nviz_unset_attr(id, MAP_OBJ_SURF, attr);
  179. return ret ? 1 : -2;
  180. }
  181. /*!
  182. \brief Set surface resolution
  183. \param id surface id
  184. \param fine x/y fine resolution
  185. \param coarse x/y coarse resolution
  186. \return 1 on success
  187. \return -1 surface not found
  188. \return -2 setting attributes failed
  189. */
  190. int Nviz::SetSurfaceRes(int id, int fine, int coarse)
  191. {
  192. G_debug(1, "Nviz::SetSurfaceRes(): id=%d, fine=%d, coarse=%d",
  193. id, fine, coarse);
  194. if (id > 0) {
  195. if (!GS_surf_exists(id)) {
  196. return -1;
  197. }
  198. if (GS_set_drawres(id, fine, fine, coarse, coarse) < 0) {
  199. return -2;
  200. }
  201. }
  202. else {
  203. GS_setall_drawres(fine, fine, coarse, coarse);
  204. }
  205. return 1;
  206. }
  207. /*!
  208. \brief Set draw style
  209. Draw styles:
  210. - DM_GOURAUD
  211. - DM_FLAT
  212. - DM_FRINGE
  213. - DM_WIRE
  214. - DM_COL_WIRE
  215. - DM_POLY
  216. - DM_WIRE_POLY
  217. - DM_GRID_WIRE
  218. - DM_GRID_SURF
  219. \param id surface id (<= 0 for all)
  220. \param style draw style
  221. \return 1 on success
  222. \return -1 surface not found
  223. \return -2 setting attributes failed
  224. */
  225. int Nviz::SetSurfaceStyle(int id, int style)
  226. {
  227. G_debug(1, "Nviz::SetSurfaceStyle(): id=%d, style=%d",
  228. id, style);
  229. if (id > 0) {
  230. if (!GS_surf_exists(id)) {
  231. return -1;
  232. }
  233. if (GS_set_drawmode(id, style) < 0) {
  234. return -2;
  235. }
  236. return 1;
  237. }
  238. if (GS_setall_drawmode(style) < 0) {
  239. return -2;
  240. }
  241. return 1;
  242. }
  243. /*!
  244. \brief Set color of wire
  245. \todo all
  246. \param surface id (< 0 for all)
  247. \param color color string (R:G:B)
  248. \return 1 on success
  249. \return -1 surface not found
  250. \return -2 setting attributes failed
  251. \return 1 on success
  252. \return 0 on failure
  253. */
  254. int Nviz::SetWireColor(int id, const char* color_str)
  255. {
  256. int color;
  257. G_debug(1, "Nviz::SetWireColor(): id=%d, color=%s",
  258. id, color_str);
  259. color = Nviz_color_from_str(color_str);
  260. if (id > 0) {
  261. if (!GS_surf_exists(id)) {
  262. return -1;
  263. }
  264. GS_set_wire_color(id, color);
  265. }
  266. else {
  267. int *surf_list, nsurfs, id;
  268. surf_list = GS_get_surf_list(&nsurfs);
  269. for (int i = 0; i < nsurfs; i++) {
  270. id = surf_list[i];
  271. GS_set_wire_color(id, color);
  272. }
  273. G_free(surf_list);
  274. }
  275. return 1;
  276. }
  277. /*!
  278. \brief Get surface position
  279. \param id surface id
  280. \return x,y,z
  281. \return zero-length vector on error
  282. */
  283. std::vector<double> Nviz::GetSurfacePosition(int id)
  284. {
  285. std::vector<double> vals;
  286. float x, y, z;
  287. if (!GS_surf_exists(id)) {
  288. return vals;
  289. }
  290. GS_get_trans(id, &x, &y, &z);
  291. G_debug(1, "Nviz::GetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  292. id, x, y, z);
  293. vals.push_back(double (x));
  294. vals.push_back(double (y));
  295. vals.push_back(double (z));
  296. return vals;
  297. }
  298. /*!
  299. \brief Set surface position
  300. \param id surface id
  301. \param x,y,z translation values
  302. \return 1 on success
  303. \return -1 surface not found
  304. \return -2 setting position failed
  305. */
  306. int Nviz::SetSurfacePosition(int id, float x, float y, float z)
  307. {
  308. if (!GS_surf_exists(id)) {
  309. return -1;
  310. }
  311. G_debug(1, "Nviz::SetSurfacePosition(): id=%d, x=%f, y=%f, z=%f",
  312. id, x, y, z);
  313. GS_set_trans(id, x, y, z);
  314. return 1;
  315. }