surface.cpp 7.6 KB

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